mkhtml.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. <div id="container">
  35. <a href="index.html"><img src="grass_logo.png" alt="GRASS logo"></a>
  36. <hr class="header">
  37. """
  38. header_nopgm = """<h2>${PGM}</h2>
  39. """
  40. header_pgm = """<h2>NAME</h2>
  41. <em><b>${PGM}</b></em>
  42. """
  43. header_pgm_desc = """<h2>NAME</h2>
  44. <em><b>${PGM}</b></em> - ${PGM_DESC}
  45. """
  46. footer_index = string.Template(\
  47. """<hr class="header">
  48. <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>
  49. <p>&copy; 2003-${YEAR} <a href="http://grass.osgeo.org">GRASS Development Team</a>, GRASS GIS ${GRASS_VERSION} Reference Manual</p>
  50. </div>
  51. </body>
  52. </html>
  53. """)
  54. footer_noindex = string.Template(\
  55. """<hr class="header">
  56. <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>
  57. <p>&copy; 2003-${YEAR} <a href="http://grass.osgeo.org">GRASS Development Team</a>, GRASS GIS ${GRASS_VERSION} Reference Manual</p>
  58. </div>
  59. </body>
  60. </html>
  61. """)
  62. def read_file(name):
  63. try:
  64. f = open(name, 'rb')
  65. s = f.read()
  66. f.close()
  67. return s
  68. except IOError:
  69. return ""
  70. def create_toc(src_data):
  71. class MyHTMLParser(HTMLParser):
  72. def __init__(self):
  73. self.reset()
  74. self.idx = 1
  75. self.tag_curr = ''
  76. self.tag_last = ''
  77. self.process_text = False
  78. self.data = []
  79. self.tags_allowed = ('h1', 'h2', 'h3')
  80. self.tags_ignored = ('img')
  81. self.text = ''
  82. def handle_starttag(self, tag, attrs):
  83. if tag in self.tags_allowed:
  84. self.process_text = True
  85. self.tag_last = self.tag_curr
  86. self.tag_curr = tag
  87. def handle_endtag(self, tag):
  88. if tag in self.tags_allowed:
  89. self.data.append((tag, '%s_%d' % (tag, self.idx),
  90. self.text))
  91. self.idx += 1
  92. self.process_text = False
  93. self.text = ''
  94. self.tag_curr = self.tag_last
  95. def handle_data(self, data):
  96. if not self.process_text:
  97. return
  98. if self.tag_curr in self.tags_allowed or self.tag_curr in self.tags_ignored:
  99. self.text += data
  100. else:
  101. self.text += '<%s>%s</%s>' % (self.tag_curr, data, self.tag_curr)
  102. # instantiate the parser and fed it some HTML
  103. parser = MyHTMLParser()
  104. parser.feed(src_data)
  105. return parser.data
  106. def escape_href(label):
  107. # remove html tags
  108. label = re.sub('<[^<]+?>', '', label)
  109. # fix &nbsp;
  110. label = label.replace('&nbsp;', '')
  111. # fix "
  112. label = label.replace('"', '')
  113. # replace space with underscore + lower
  114. return label.replace(' ', '-').lower()
  115. def write_toc(data):
  116. if not data:
  117. return
  118. fd = sys.stdout
  119. fd.write('<div class="toc">\n')
  120. fd.write('<h4 class="toc">Table of contents</h4>\n')
  121. fd.write('<ul class="toc">\n')
  122. first = True
  123. has_h2 = False
  124. in_h3 = False
  125. indent = 4
  126. for tag, href, text in data:
  127. if tag == 'h3' and not in_h3 and has_h2:
  128. fd.write('\n%s<ul class="toc">\n' % (' ' * indent))
  129. indent += 4
  130. in_h3 = True
  131. elif not first:
  132. fd.write('</li>\n')
  133. if tag == 'h2':
  134. has_h2 = True
  135. if in_h3:
  136. indent -= 4
  137. fd.write('%s</ul></li>\n' % (' ' * indent))
  138. in_h3 = False
  139. fd.write('%s<li class="toc"><a href="#%s" class="toc">%s</a>' % \
  140. (' ' * indent, escape_href(text), text))
  141. first = False
  142. fd.write('</li>\n</ul>\n')
  143. fd.write('</div>\n')
  144. def update_toc(data):
  145. ret_data = []
  146. pat = re.compile(r'(<(h[2|3])>)(.+)(</h[2|3]>)')
  147. idx = 1
  148. for line in data.splitlines():
  149. if pat.search(line):
  150. xline = pat.split(line)
  151. line = xline[1] + '<a name="%s">' % escape_href(xline[3]) + xline[3] + '</a>' + xline[4]
  152. idx += 1
  153. ret_data.append(line)
  154. return '\n'.join(ret_data)
  155. # process header
  156. src_data = read_file(src_file)
  157. name = re.search('(<!-- meta page name:)(.*)(-->)', src_data, re.IGNORECASE)
  158. pgm_desc = None
  159. if name:
  160. pgm = name.group(2).strip().split('-', 1)[0].strip()
  161. name_desc = re.search('(<!-- meta page name description:)(.*)(-->)', src_data, re.IGNORECASE)
  162. if name_desc:
  163. pgm_desc = name_desc.group(2).strip()
  164. desc = re.search('(<!-- meta page description:)(.*)(-->)', src_data,
  165. re.IGNORECASE)
  166. if desc:
  167. pgm = desc.group(2).strip()
  168. header_tmpl = string.Template(header_base + header_nopgm)
  169. else:
  170. if not pgm_desc:
  171. header_tmpl = string.Template(header_base + header_pgm)
  172. else:
  173. header_tmpl = string.Template(header_base + header_pgm_desc)
  174. if not re.search('<html>', src_data, re.IGNORECASE):
  175. tmp_data = read_file(tmp_file)
  176. if not re.search('<html>', tmp_data, re.IGNORECASE):
  177. sys.stdout.write(header_tmpl.substitute(PGM=pgm, PGM_DESC=pgm_desc))
  178. if tmp_data:
  179. for line in tmp_data.splitlines(True):
  180. if not re.search('</body>|</html>', line, re.IGNORECASE):
  181. sys.stdout.write(line)
  182. # create TOC
  183. write_toc(create_toc(src_data))
  184. # process body
  185. sys.stdout.write(update_toc(src_data))
  186. # if </html> is found, suppose a complete html is provided.
  187. # otherwise, generate module class reference:
  188. if re.search('</html>', src_data, re.IGNORECASE):
  189. sys.exit()
  190. index_names = {
  191. 'd' : 'display',
  192. 'db': 'database',
  193. 'g' : 'general',
  194. 'i' : 'imagery',
  195. 'm' : 'misc',
  196. 'ps': 'postscript',
  197. 'p' : 'paint',
  198. 'r' : 'raster',
  199. 'r3': 'raster3D',
  200. 's' : 'sites',
  201. 't' : 'temporal',
  202. 'v' : 'vector'
  203. }
  204. # process footer
  205. index = re.search('(<!-- meta page index:)(.*)(-->)', src_data, re.IGNORECASE)
  206. if index:
  207. index_name = index.group(2).strip()
  208. if '|' in index_name:
  209. index_name, index_name_cap = index_name.split('|', 1)
  210. else:
  211. index_name_cap = index_name
  212. else:
  213. mod_class = pgm.split('.', 1)[0]
  214. index_name = index_names.get(mod_class, '')
  215. index_name_cap = index_name.title()
  216. grass_version = os.getenv("VERSION_NUMBER", "unknown")
  217. year = os.getenv("VERSION_DATE")
  218. if not year:
  219. year = str(datetime.now().year)
  220. if index_name:
  221. sys.stdout.write(footer_index.substitute(INDEXNAME=index_name,
  222. INDEXNAMECAP=index_name_cap,
  223. YEAR=year,
  224. GRASS_VERSION=grass_version))
  225. else:
  226. sys.stdout.write(footer_noindex.substitute(YEAR=year,
  227. GRASS_VERSION=grass_version))