build_topics.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env python3
  2. # generates topics.html and topic_*.html
  3. # (c) 2012 by the GRASS Development Team, Markus Neteler, Luca Delucchi
  4. import os
  5. import sys
  6. import glob
  7. from build_html import *
  8. path = sys.argv[1]
  9. year = os.getenv("VERSION_DATE")
  10. min_num_modules_for_topic = 3
  11. keywords = {}
  12. htmlfiles = glob.glob1(path, "*.html")
  13. for fname in htmlfiles:
  14. fil = open(os.path.join(path, fname))
  15. # TODO maybe move to Python re (regex)
  16. lines = fil.readlines()
  17. try:
  18. index_keys = lines.index("<h2>KEYWORDS</h2>\n") + 1
  19. index_desc = lines.index("<h2>NAME</h2>\n") + 1
  20. except:
  21. continue
  22. try:
  23. key = lines[index_keys].split(",")[1].strip().replace(" ", "_")
  24. key = key.split(">")[1].split("<")[0]
  25. except:
  26. continue
  27. try:
  28. desc = lines[index_desc].split("-", 1)[1].strip()
  29. except:
  30. desc.strip()
  31. if key not in keywords.keys():
  32. keywords[key] = {}
  33. keywords[key][fname] = desc
  34. elif fname not in keywords[key]:
  35. keywords[key][fname] = desc
  36. topicsfile = open(os.path.join(path, "topics.html"), "w")
  37. topicsfile.write(
  38. header1_tmpl.substitute(
  39. title="GRASS GIS " "%s Reference Manual: Topics index" % grass_version
  40. )
  41. )
  42. topicsfile.write(headertopics_tmpl)
  43. for key, values in sorted(keywords.items(), key=lambda s: s[0].lower()):
  44. keyfile = open(os.path.join(path, "topic_%s.html" % key), "w")
  45. keyfile.write(
  46. header1_tmpl.substitute(
  47. title="GRASS GIS "
  48. "%s Reference Manual: Topic %s" % (grass_version, key.replace("_", " "))
  49. )
  50. )
  51. keyfile.write(headerkey_tmpl.substitute(keyword=key.replace("_", " ")))
  52. num_modules = 0
  53. for mod, desc in sorted(values.items()):
  54. num_modules += 1
  55. keyfile.write(
  56. desc1_tmpl.substitute(cmd=mod, desc=desc, basename=mod.replace(".html", ""))
  57. )
  58. if num_modules >= min_num_modules_for_topic:
  59. topicsfile.writelines(
  60. [moduletopics_tmpl.substitute(key=key, name=key.replace("_", " "))]
  61. )
  62. keyfile.write("</table>\n")
  63. # link to the keywords index
  64. # TODO: the labels in keywords index are with spaces and capitals
  65. # this should be probably changed to lowercase with underscores
  66. keyfile.write(
  67. "<p><em>See also the corresponding keyword"
  68. ' <a href="keywords.html#{key}">{key}</a>'
  69. " for additional references.</em>".format(key=key.replace("_", " "))
  70. )
  71. write_html_footer(keyfile, "index.html", year)
  72. topicsfile.write("</ul>\n")
  73. write_html_footer(topicsfile, "index.html", year)
  74. topicsfile.close()