mkrest.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/usr/bin/env python
  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 <http://grass.osgeo.org>`_
  32. """)
  33. footer_noindex = string.Template(\
  34. """
  35. :doc:`Main Page <index>` - :doc:`Full index <full_index>`
  36. 2003-${YEAR} `GRASS Development Team <http://grass.osgeo.org>`_
  37. """)
  38. def read_file(name):
  39. try:
  40. f = open(name, 'rb')
  41. s = f.read()
  42. f.close()
  43. return s
  44. except IOError:
  45. return ""
  46. replacement = {
  47. '*`' : '`',
  48. '`* `' : '`',
  49. '>`_*' : '>`_',
  50. '>`_,*' : '>`_,',
  51. '``*\ "' : '``"',
  52. '***' : '**'
  53. }
  54. src_data = read_file(src_file)
  55. title = re.search('(<!-- meta page description:)(.*)(-->)', src_data, re.IGNORECASE)
  56. if title:
  57. title_name = title.group(2).strip()
  58. sys.stdout.write("%s\n" % title_name)
  59. title_style = "=" * (len(title_name)+2)
  60. sys.stdout.write("%s\n\n" % title_style)
  61. tmp_data = read_file(tmp_file)
  62. if tmp_data:
  63. sys.stdout.write(tmp_data)
  64. process = subprocess.Popen('pandoc -s -r html %s -w rst' % src_file,
  65. shell=True, stdout=subprocess.PIPE)
  66. html_text = process.communicate()[0]
  67. if html_text:
  68. for k, v in replacement.iteritems():
  69. html_text = html_text.replace(k, v)
  70. #TODO remove with space if string start with it, " vector..." -> "vector..."
  71. # not if it is a tab: " vector...." -> " vector..."
  72. # for line in html_text.splitlines(True):
  73. # sys.stdout.write("%s" % line.lstrip())
  74. sys.stdout.write(html_text)
  75. index_names = {
  76. 'd': 'display',
  77. 'db': 'database',
  78. 'g': 'general',
  79. 'i': 'imagery',
  80. 'm': 'miscellaneous',
  81. 'ps': 'postscript',
  82. 'p': 'paint',
  83. 'r': 'raster',
  84. 'r3': 'raster3D',
  85. 's': 'sites',
  86. 't': 'temporal',
  87. 'v': 'vector'
  88. }
  89. index = re.search('(<!-- meta page index:)(.*)(-->)', src_data, re.IGNORECASE)
  90. if index:
  91. index_name = index.group(2).strip()
  92. else:
  93. mod_class = pgm.split('.', 1)[0]
  94. index_name = index_names.get(mod_class, '')
  95. if index_name:
  96. sys.stdout.write(footer_index.substitute(INDEXNAME = index_name,
  97. INDEXNAMECAP = index_name.title(),
  98. YEAR = year))
  99. else:
  100. sys.stdout.write(footer_noindex.substitute(YEAR = year))
  101. sys.exit()