build_keywords.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/usr/bin/env python3
  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 not key:
  38. exit("Empty keyword from file %s line: %s"
  39. % (fname, lines[index_keys]))
  40. if key not in keywords.keys():
  41. keywords[key] = []
  42. keywords[key].append(fname)
  43. elif fname not in keywords[key]:
  44. keywords[key].append(fname)
  45. for black in blacklist:
  46. try:
  47. del keywords[black]
  48. except:
  49. try:
  50. del keywords[black.lower()]
  51. except:
  52. continue
  53. for key in sorted(keywords.keys()):
  54. # this list it is useful to create the TOC using only the first
  55. # character for keyword
  56. firstchar = key[0].lower()
  57. if firstchar not in char_list.keys():
  58. char_list[str(firstchar)] = key
  59. elif firstchar in char_list.keys():
  60. if key.lower() < char_list[str(firstchar)].lower():
  61. char_list[str(firstchar.lower())] = key
  62. keywordsfile = open(os.path.join(path, 'keywords.html'), 'w')
  63. keywordsfile.write(header1_tmpl.substitute(title="GRASS GIS %s Reference "
  64. "Manual: Keywords index" % grass_version))
  65. keywordsfile.write(headerkeywords_tmpl)
  66. keywordsfile.write('<dl>')
  67. sortedKeys = sorted(keywords.keys(), key=lambda s: s.lower())
  68. for key in sortedKeys:
  69. keyword_line = '<dt><b><a name="%s" class="urlblack">%s</a></b></dt>' \
  70. '<dd>' % (key, key)
  71. for value in sorted(keywords[key]):
  72. keyword_line += ' <a href="%s">%s</a>,' % (value,
  73. value.replace('.html', ''))
  74. keyword_line = keyword_line.rstrip(',')
  75. keyword_line += '</dd>\n'
  76. keywordsfile.write(keyword_line)
  77. keywordsfile.write("</dl>\n")
  78. # create toc
  79. toc = '<div class="toc">\n<h4 class="toc">Table of contents</h4><p class="toc">'
  80. test_length = 0
  81. all_keys = len(char_list.keys())
  82. for k in sorted(char_list.keys()):
  83. test_length += 1
  84. # toc += '<li><a href="#%s" class="toc">%s</a></li>' % (char_list[k], k)
  85. if test_length % 4 == 0 and not test_length == all_keys:
  86. toc += '\n<a href="#%s" class="toc">%s</a>, ' % (char_list[k], k)
  87. elif test_length % 4 == 0 and test_length == all_keys:
  88. toc += '\n<a href="#%s" class="toc">%s</a>' % (char_list[k], k)
  89. elif test_length == all_keys:
  90. toc += '<a href="#%s" class="toc">%s</a>' % (char_list[k], k)
  91. else:
  92. toc += '<a href="#%s" class="toc">%s</a>, ' % (char_list[k], k)
  93. toc += '</p></div>\n'
  94. keywordsfile.write(toc)
  95. write_html_footer(keywordsfile, "index.html", year)
  96. keywordsfile.close()