build_topics.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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(header1_tmpl.substitute(title = "GRASS GIS " \
  39. "%s Reference Manual: Topics index" % grass_version))
  40. topicsfile.write(headertopics_tmpl)
  41. for key, values in sorted(keywords.items(), key=lambda s: s[0].lower()):
  42. keyfile = open(os.path.join(path, 'topic_%s.html' % key), 'w')
  43. keyfile.write(header1_tmpl.substitute(title = "GRASS GIS " \
  44. "%s Reference Manual: Topic %s" % (grass_version,
  45. key.replace('_', ' '))))
  46. keyfile.write(headerkey_tmpl.substitute(keyword=key.replace('_', ' ')))
  47. num_modules = 0
  48. for mod, desc in sorted(values.items()):
  49. num_modules += 1
  50. keyfile.write(desc1_tmpl.substitute(cmd=mod, desc=desc,
  51. basename=mod.replace('.html', '')))
  52. if num_modules >= min_num_modules_for_topic:
  53. topicsfile.writelines([moduletopics_tmpl.substitute(
  54. key=key, name=key.replace('_', ' '))])
  55. keyfile.write("</table>\n")
  56. # link to the keywords index
  57. # TODO: the labels in keywords index are with spaces and capitals
  58. # this should be probably changed to lowercase with underscores
  59. keyfile.write(
  60. '<p><em>See also the corresponding keyword'
  61. ' <a href="keywords.html#{key}">{key}</a>'
  62. ' for additional references.</em>'.format(key=key.replace('_', ' ')))
  63. write_html_footer(keyfile, "index.html", year)
  64. topicsfile.write("</ul>\n")
  65. write_html_footer(topicsfile, "index.html", year)
  66. topicsfile.close()