build_topics.py 2.6 KB

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