build_keywords.py 3.4 KB

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