build_keywords.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # generates keywords.html
  4. # (c) 2013 by the GRASS Development Team, Luca Delucchi
  5. import os
  6. import sys
  7. import glob
  8. from build_html import *
  9. blacklist = ['Display', 'Database', 'General', 'Imagery', 'Misc', 'Postscript',
  10. 'Raster', 'Raster3D', 'Temporal', 'Vector']
  11. path = sys.argv[1]
  12. year = os.getenv("VERSION_DATE")
  13. keywords = {}
  14. htmlfiles = glob.glob1(path, '*.html')
  15. char_list = {}
  16. for fname in htmlfiles:
  17. fil = open(os.path.join(path, fname))
  18. # TODO maybe move to Python re (regex)
  19. lines = fil.readlines()
  20. # remove empty lines
  21. lines = [x for x in lines if x != '\n']
  22. try:
  23. index_keys = lines.index('<h2>KEYWORDS</h2>\n') + 1
  24. index_desc = lines.index('<h2>NAME</h2>\n') + 1
  25. except:
  26. continue
  27. try:
  28. keys = lines[index_keys].split(',')
  29. except:
  30. continue
  31. for key in keys:
  32. key = key.strip()
  33. try:
  34. key = key.split('>')[1].split('<')[0]
  35. except:
  36. pass
  37. if key not in keywords.keys():
  38. keywords[key] = []
  39. keywords[key].append(fname)
  40. elif fname not in keywords[key]:
  41. keywords[key].append(fname)
  42. for black in blacklist:
  43. try:
  44. del keywords[black]
  45. except:
  46. try:
  47. del keywords[black.lower()]
  48. except:
  49. continue
  50. for key in sorted(keywords.keys()):
  51. # this list it is useful to create the TOC using only the first
  52. # character for keyword
  53. firstchar = key[0].lower()
  54. if firstchar not in char_list.keys():
  55. char_list[str(firstchar)] = key
  56. elif firstchar in char_list.keys():
  57. if key.lower() < char_list[str(firstchar)].lower():
  58. char_list[str(firstchar.lower())] = key
  59. keywordsfile = open(os.path.join(path, 'keywords.html'), 'w')
  60. keywordsfile.write(header1_tmpl.substitute(title="GRASS GIS %s Reference "
  61. "Manual: Keywords index" % grass_version))
  62. keywordsfile.write(headerkeywords_tmpl)
  63. keywordsfile.write('<dl>')
  64. sortedKeys = keywords.keys()
  65. sortedKeys.sort(key=lambda s: s.lower())
  66. for key in sortedKeys:
  67. keyword_line = '<dt><b><a name="%s" class="urlblack">%s</a></b></dt>' \
  68. '<dd>' % (key, key)
  69. for value in sorted(keywords[key]):
  70. keyword_line += ' <a href="%s">%s</a>,' % (value,
  71. value.replace('.html', ''))
  72. keyword_line = keyword_line.rstrip(',')
  73. keyword_line += '</dd>\n'
  74. keywordsfile.write(keyword_line)
  75. keywordsfile.write("</dl>\n")
  76. # create toc
  77. toc = '<div class="toc">\n<h4 class="toc">Table of contents</h4><p class="toc">'
  78. test_lenght = 0
  79. all_keys = len(char_list.keys())
  80. for k in sorted(char_list.keys()):
  81. test_lenght += 1
  82. # toc += '<li><a href="#%s" class="toc">%s</a></li>' % (char_list[k], k)
  83. if test_lenght % 4 == 0 and not test_lenght == all_keys:
  84. toc += '\n<a href="#%s" class="toc">%s</a>, ' % (char_list[k], k)
  85. elif test_lenght % 4 == 0 and test_lenght == all_keys:
  86. toc += '\n<a href="#%s" class="toc">%s</a>' % (char_list[k], k)
  87. elif test_lenght == all_keys:
  88. toc += '<a href="#%s" class="toc">%s</a>' % (char_list[k], k)
  89. else:
  90. toc += '<a href="#%s" class="toc">%s</a>, ' % (char_list[k], k)
  91. toc += '</p></div>\n'
  92. keywordsfile.write(toc)
  93. write_html_footer(keywordsfile, "index.html", year)
  94. keywordsfile.close()