mkhtml.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: Builds manual pages
  5. # AUTHOR(S): Markus Neteler
  6. # Glynn Clements
  7. # Martin Landa <landa.martin gmail.com>
  8. # PURPOSE: Create HTML manual page snippets
  9. # COPYRIGHT: (C) 2007-2014 by Glynn Clements
  10. # and the GRASS Development Team
  11. #
  12. # This program is free software under the GNU General
  13. # Public License (>=v2). Read the file COPYING that
  14. # comes with GRASS for details.
  15. #
  16. #############################################################################
  17. import sys
  18. import os
  19. import string
  20. import re
  21. from datetime import datetime
  22. from HTMLParser import HTMLParser
  23. pgm = sys.argv[1]
  24. src_file = "%s.html" % pgm
  25. tmp_file = "%s.tmp.html" % pgm
  26. header_base = """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  27. <html>
  28. <head>
  29. <title>GRASS GIS Manual: ${PGM}</title>
  30. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  31. <link rel="stylesheet" href="grassdocs.css" type="text/css">
  32. </head>
  33. <body bgcolor="white">
  34. <img src="grass_logo.png" alt="GRASS logo"><hr align=center size=6 noshade>
  35. """
  36. header_nopgm = """<h2>${PGM}</h2>
  37. """
  38. header_pgm = """<h2>NAME</h2>
  39. <em><b>${PGM}</b></em>
  40. """
  41. footer_index = string.Template(\
  42. """<hr>
  43. <p><a href="index.html">Main index</a> | <a href="${INDEXNAME}.html">${INDEXNAMECAP} index</a> | <a href="topics.html">Topics index</a> | <a href="keywords.html">Keywords Index</a> | <a href="full_index.html">Full index</a></p>
  44. <p>&copy; 2003-${YEAR} <a href="http://grass.osgeo.org">GRASS Development Team</a>, GRASS GIS ${GRASS_VERSION} Reference Manual</p>
  45. </body>
  46. </html>
  47. """)
  48. footer_noindex = string.Template(\
  49. """<hr>
  50. <p><a href="index.html">Main index</a> | <a href="topics.html">Topics index</a> | <a href="keywords.html">Keywords Index</a> | <a href="full_index.html">Full index</a></p>
  51. <p>&copy; 2003-${YEAR} <a href="http://grass.osgeo.org">GRASS Development Team</a>, GRASS GIS ${GRASS_VERSION} Reference Manual</p>
  52. </body>
  53. </html>
  54. """)
  55. def read_file(name):
  56. try:
  57. f = open(name, 'rb')
  58. s = f.read()
  59. f.close()
  60. return s
  61. except IOError:
  62. return ""
  63. def create_toc(src_data):
  64. class MyHTMLParser(HTMLParser):
  65. def __init__(self):
  66. self.reset()
  67. self.idx = 1
  68. self.tag_curr = ''
  69. self.tag_last = ''
  70. self.process_text = False
  71. self.data = []
  72. self.tags_allowed = ('h1', 'h2', 'h3')
  73. self.tags_ignored = ('img')
  74. self.text = ''
  75. def handle_starttag(self, tag, attrs):
  76. if tag in self.tags_allowed:
  77. self.process_text = True
  78. self.tag_last = self.tag_curr
  79. self.tag_curr = tag
  80. def handle_endtag(self, tag):
  81. if tag in self.tags_allowed:
  82. self.data.append((tag, '%s_%d' % (tag, self.idx),
  83. self.text))
  84. self.idx += 1
  85. self.process_text = False
  86. self.text = ''
  87. self.tag_curr = self.tag_last
  88. def handle_data(self, data):
  89. if not self.process_text:
  90. return
  91. if self.tag_curr in self.tags_allowed or self.tag_curr in self.tags_ignored:
  92. self.text += data
  93. else:
  94. self.text += '<%s>%s</%s>' % (self.tag_curr, data, self.tag_curr)
  95. # instantiate the parser and fed it some HTML
  96. parser = MyHTMLParser()
  97. parser.feed(src_data)
  98. return parser.data
  99. def write_toc(data):
  100. if not data:
  101. return
  102. fd = sys.stdout
  103. fd.write('<div class="toc">\n')
  104. fd.write('<ul class="toc">\n')
  105. first = True
  106. has_h2 = False
  107. in_h3 = False
  108. indent = 4
  109. for tag, href, text in data:
  110. if tag == 'h3' and not in_h3 and has_h2:
  111. fd.write('\n%s<ul class="toc">\n' % (' ' * indent))
  112. indent += 4
  113. in_h3 = True
  114. elif not first:
  115. fd.write('</li>\n')
  116. if tag == 'h2':
  117. has_h2 = True
  118. if in_h3:
  119. indent -= 4
  120. fd.write('%s</ul></li>\n' % (' ' * indent))
  121. in_h3 = False
  122. fd.write('%s<li class="toc"><a href="#%s" class="toc">%s</a>' % \
  123. (' ' * indent, href, text))
  124. first = False
  125. fd.write('</li>\n</ul>\n')
  126. fd.write('</div>\n')
  127. def update_toc(data):
  128. ret_data = []
  129. pat = re.compile(r'(<(h\d)>)(.+)(</h\d>)')
  130. idx = 1
  131. for line in data.splitlines():
  132. if pat.search(line):
  133. xline = pat.split(line)
  134. line = xline[1] + '<a name="%s_%d">' % (xline[2], idx) + xline[3] + '</a>' + xline[4]
  135. idx += 1
  136. ret_data.append(line)
  137. return '\n'.join(ret_data)
  138. # process header
  139. src_data = read_file(src_file)
  140. name = re.search('(<!-- meta page name:)(.*)(-->)', src_data, re.IGNORECASE)
  141. if name:
  142. pgm = name.group(2).strip().split('-', 1)[0].strip()
  143. desc = re.search('(<!-- meta page description:)(.*)(-->)', src_data,
  144. re.IGNORECASE)
  145. if desc:
  146. pgm = desc.group(2).strip()
  147. header_tmpl = string.Template(header_base + header_nopgm)
  148. else:
  149. header_tmpl = string.Template(header_base + header_pgm)
  150. if not re.search('<html>', src_data, re.IGNORECASE):
  151. tmp_data = read_file(tmp_file)
  152. if not re.search('<html>', tmp_data, re.IGNORECASE):
  153. sys.stdout.write(header_tmpl.substitute(PGM=pgm))
  154. if tmp_data:
  155. for line in tmp_data.splitlines(True):
  156. if not re.search('</body>|</html>', line, re.IGNORECASE):
  157. sys.stdout.write(line)
  158. # create TOC
  159. write_toc(create_toc(src_data))
  160. # process body
  161. sys.stdout.write(update_toc(src_data))
  162. # if </html> is found, suppose a complete html is provided.
  163. # otherwise, generate module class reference:
  164. if re.search('</html>', src_data, re.IGNORECASE):
  165. sys.exit()
  166. index_names = {
  167. 'd' : 'display',
  168. 'db': 'database',
  169. 'g' : 'general',
  170. 'i' : 'imagery',
  171. 'm' : 'misc',
  172. 'ps': 'postscript',
  173. 'p' : 'paint',
  174. 'r' : 'raster',
  175. 'r3': 'raster3D',
  176. 's' : 'sites',
  177. 't' : 'temporal',
  178. 'v' : 'vector'
  179. }
  180. # process footer
  181. index = re.search('(<!-- meta page index:)(.*)(-->)', src_data, re.IGNORECASE)
  182. if index:
  183. index_name_cap = index_name = index.group(2).strip()
  184. else:
  185. mod_class = pgm.split('.', 1)[0]
  186. index_name = index_names.get(mod_class, '')
  187. index_name_cap = index_name.title()
  188. grass_version = os.getenv("VERSION_NUMBER", "unknown")
  189. year = os.getenv("VERSION_DATE")
  190. if not year:
  191. year = str(datetime.now().year)
  192. if index_name:
  193. sys.stdout.write(footer_index.substitute(INDEXNAME=index_name,
  194. INDEXNAMECAP=index_name_cap,
  195. YEAR=year,
  196. GRASS_VERSION=grass_version))
  197. else:
  198. sys.stdout.write(footer_noindex.substitute(YEAR=year,
  199. GRASS_VERSION=grass_version))