mkrest.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #!/usr/bin/env python3
  2. ############################################################################
  3. #
  4. # MODULE: mkrest.py
  5. # AUTHOR(S): Luca Delucchi
  6. # PURPOSE: Create HTML manual page snippets
  7. # COPYRIGHT: (C) 2012 by Luca Delucchi
  8. # and the GRASS Development Team
  9. #
  10. # This program is free software under the GNU General
  11. # Public License (>=v2). Read the file COPYING that
  12. # comes with GRASS for details.
  13. #
  14. #############################################################################
  15. import sys
  16. import string
  17. import re
  18. import subprocess
  19. from datetime import datetime
  20. pgm = sys.argv[1]
  21. if len(sys.argv) > 1:
  22. year = sys.argv[2]
  23. else:
  24. year = str(datetime.now().year)
  25. src_file = "%s.html" % pgm
  26. tmp_file = "%s.tmp.txt" % pgm
  27. # TODO add copyright
  28. footer_index = string.Template(
  29. """
  30. :doc:`Main Page <index>` - :doc:`${INDEXNAMECAP} index <${INDEXNAME}>` - :doc:`Full index <full_index>`
  31. 2003-${YEAR} `GRASS Development Team <https://grass.osgeo.org>`_
  32. """
  33. )
  34. footer_noindex = string.Template(
  35. """
  36. :doc:`Main Page <index>` - :doc:`Full index <full_index>`
  37. 2003-${YEAR} `GRASS Development Team <https://grass.osgeo.org>`_
  38. """
  39. )
  40. def read_file(name):
  41. try:
  42. f = open(name, "rb")
  43. s = f.read()
  44. f.close()
  45. return s
  46. except IOError:
  47. return ""
  48. replacement = {
  49. "*`": "`",
  50. "`* `": "`",
  51. ">`_*": ">`_",
  52. ">`_,*": ">`_,",
  53. '``*\ "': '``"',
  54. "***": "**",
  55. }
  56. src_data = read_file(src_file)
  57. title = re.search("(<!-- meta page description:)(.*)(-->)", src_data, re.IGNORECASE)
  58. if title:
  59. title_name = title.group(2).strip()
  60. sys.stdout.write("%s\n" % title_name)
  61. title_style = "=" * (len(title_name) + 2)
  62. sys.stdout.write("%s\n\n" % title_style)
  63. tmp_data = read_file(tmp_file)
  64. if tmp_data:
  65. sys.stdout.write(tmp_data)
  66. process = subprocess.Popen(
  67. "pandoc -s -r html %s -w rst" % src_file, shell=True, stdout=subprocess.PIPE
  68. )
  69. html_text = process.communicate()[0]
  70. if html_text:
  71. for k, v in replacement.iteritems():
  72. html_text = html_text.replace(k, v)
  73. # TODO remove with space if string start with it, " vector..." -> "vector..."
  74. # not if it is a tab: " vector...." -> " vector..."
  75. # for line in html_text.splitlines(True):
  76. # sys.stdout.write("%s" % line.lstrip())
  77. sys.stdout.write(html_text)
  78. index_names = {
  79. "d": "display",
  80. "db": "database",
  81. "g": "general",
  82. "i": "imagery",
  83. "m": "miscellaneous",
  84. "ps": "postscript",
  85. "p": "paint",
  86. "r": "raster",
  87. "r3": "raster3D",
  88. "s": "sites",
  89. "t": "temporal",
  90. "v": "vector",
  91. }
  92. index = re.search("(<!-- meta page index:)(.*)(-->)", src_data, re.IGNORECASE)
  93. if index:
  94. index_name = index.group(2).strip()
  95. else:
  96. mod_class = pgm.split(".", 1)[0]
  97. index_name = index_names.get(mod_class, "")
  98. if index_name:
  99. sys.stdout.write(
  100. footer_index.substitute(
  101. INDEXNAME=index_name, INDEXNAMECAP=index_name.title(), YEAR=year
  102. )
  103. )
  104. else:
  105. sys.stdout.write(footer_noindex.substitute(YEAR=year))
  106. sys.exit()