mkhtml.py 7.5 KB

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