mkhtml.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. source_url = "https://trac.osgeo.org/grass/browser/grass/trunk"
  27. header_base = """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  28. <html>
  29. <head>
  30. <title>GRASS GIS Manual: ${PGM}</title>
  31. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  32. <link rel="stylesheet" href="grassdocs.css" type="text/css">
  33. </head>
  34. <body bgcolor="white">
  35. <div id="container">
  36. <a href="index.html"><img src="grass_logo.png" alt="GRASS logo"></a>
  37. <hr class="header">
  38. """
  39. header_nopgm = """<h2>${PGM}</h2>
  40. """
  41. header_pgm = """<h2>NAME</h2>
  42. <em><b>${PGM}</b></em>
  43. """
  44. header_pgm_desc = """<h2>NAME</h2>
  45. <em><b>${PGM}</b></em> - ${PGM_DESC}
  46. """
  47. footer_index = string.Template(
  48. """<hr class="header">
  49. <p>
  50. <a href="index.html">Main index</a> |
  51. <a href="${INDEXNAME}.html">${INDEXNAMECAP} index</a> |
  52. <a href="${URL}/${FOLDER}/${PGM}">Source code</a> |
  53. <a href="topics.html">Topics index</a> |
  54. <a href="keywords.html">Keywords index</a> |
  55. <a href="graphical_index.html">Graphical index</a> |
  56. <a href="full_index.html">Full index</a>
  57. </p>
  58. <p>
  59. &copy; 2003-${YEAR}
  60. <a href="http://grass.osgeo.org">GRASS Development Team</a>,
  61. GRASS GIS ${GRASS_VERSION} Reference Manual
  62. </p>
  63. </div>
  64. </body>
  65. </html>
  66. """)
  67. footer_noindex = string.Template(
  68. """<hr class="header">
  69. <p>
  70. <a href="index.html">Main index</a> |
  71. <a href="topics.html">Topics index</a> |
  72. <a href="keywords.html">Keywords index</a> |
  73. <a href="graphical_index.html">Graphical index</a> |
  74. <a href="full_index.html">Full index</a>
  75. </p>
  76. <p>
  77. &copy; 2003-${YEAR}
  78. <a href="http://grass.osgeo.org">GRASS Development Team</a>,
  79. GRASS GIS ${GRASS_VERSION} Reference Manual
  80. </p>
  81. </div>
  82. </body>
  83. </html>
  84. """)
  85. def read_file(name):
  86. try:
  87. f = open(name, 'rb')
  88. s = f.read()
  89. f.close()
  90. return s
  91. except IOError:
  92. return ""
  93. def create_toc(src_data):
  94. class MyHTMLParser(HTMLParser):
  95. def __init__(self):
  96. self.reset()
  97. self.idx = 1
  98. self.tag_curr = ''
  99. self.tag_last = ''
  100. self.process_text = False
  101. self.data = []
  102. self.tags_allowed = ('h1', 'h2', 'h3')
  103. self.tags_ignored = ('img')
  104. self.text = ''
  105. def handle_starttag(self, tag, attrs):
  106. if tag in self.tags_allowed:
  107. self.process_text = True
  108. self.tag_last = self.tag_curr
  109. self.tag_curr = tag
  110. def handle_endtag(self, tag):
  111. if tag in self.tags_allowed:
  112. self.data.append((tag, '%s_%d' % (tag, self.idx),
  113. self.text))
  114. self.idx += 1
  115. self.process_text = False
  116. self.text = ''
  117. self.tag_curr = self.tag_last
  118. def handle_data(self, data):
  119. if not self.process_text:
  120. return
  121. if self.tag_curr in self.tags_allowed or self.tag_curr in self.tags_ignored:
  122. self.text += data
  123. else:
  124. self.text += '<%s>%s</%s>' % (self.tag_curr, data, self.tag_curr)
  125. # instantiate the parser and fed it some HTML
  126. parser = MyHTMLParser()
  127. parser.feed(src_data)
  128. return parser.data
  129. def escape_href(label):
  130. # remove html tags
  131. label = re.sub('<[^<]+?>', '', label)
  132. # fix &nbsp;
  133. label = label.replace('&nbsp;', '')
  134. # fix "
  135. label = label.replace('"', '')
  136. # replace space with underscore + lower
  137. return label.replace(' ', '-').lower()
  138. def write_toc(data):
  139. if not data:
  140. return
  141. fd = sys.stdout
  142. fd.write('<div class="toc">\n')
  143. fd.write('<h4 class="toc">Table of contents</h4>\n')
  144. fd.write('<ul class="toc">\n')
  145. first = True
  146. has_h2 = False
  147. in_h3 = False
  148. indent = 4
  149. for tag, href, text in data:
  150. if tag == 'h3' and not in_h3 and has_h2:
  151. fd.write('\n%s<ul class="toc">\n' % (' ' * indent))
  152. indent += 4
  153. in_h3 = True
  154. elif not first:
  155. fd.write('</li>\n')
  156. if tag == 'h2':
  157. has_h2 = True
  158. if in_h3:
  159. indent -= 4
  160. fd.write('%s</ul></li>\n' % (' ' * indent))
  161. in_h3 = False
  162. fd.write('%s<li class="toc"><a href="#%s" class="toc">%s</a>' % \
  163. (' ' * indent, escape_href(text), text))
  164. first = False
  165. fd.write('</li>\n</ul>\n')
  166. fd.write('</div>\n')
  167. def update_toc(data):
  168. ret_data = []
  169. pat = re.compile(r'(<(h[2|3])>)(.+)(</h[2|3]>)')
  170. idx = 1
  171. for line in data.splitlines():
  172. if pat.search(line):
  173. xline = pat.split(line)
  174. line = xline[1] + '<a name="%s">' % escape_href(xline[3]) + xline[3] + '</a>' + xline[4]
  175. idx += 1
  176. ret_data.append(line)
  177. return '\n'.join(ret_data)
  178. # process header
  179. src_data = read_file(src_file)
  180. name = re.search('(<!-- meta page name:)(.*)(-->)', src_data, re.IGNORECASE)
  181. pgm_desc = None
  182. if name:
  183. pgm = name.group(2).strip().split('-', 1)[0].strip()
  184. name_desc = re.search('(<!-- meta page name description:)(.*)(-->)', src_data, re.IGNORECASE)
  185. if name_desc:
  186. pgm_desc = name_desc.group(2).strip()
  187. desc = re.search('(<!-- meta page description:)(.*)(-->)', src_data,
  188. re.IGNORECASE)
  189. if desc:
  190. pgm = desc.group(2).strip()
  191. header_tmpl = string.Template(header_base + header_nopgm)
  192. else:
  193. if not pgm_desc:
  194. header_tmpl = string.Template(header_base + header_pgm)
  195. else:
  196. header_tmpl = string.Template(header_base + header_pgm_desc)
  197. if not re.search('<html>', src_data, re.IGNORECASE):
  198. tmp_data = read_file(tmp_file)
  199. if not re.search('<html>', tmp_data, re.IGNORECASE):
  200. sys.stdout.write(header_tmpl.substitute(PGM=pgm, PGM_DESC=pgm_desc))
  201. if tmp_data:
  202. for line in tmp_data.splitlines(True):
  203. if not re.search('</body>|</html>', line, re.IGNORECASE):
  204. sys.stdout.write(line)
  205. # create TOC
  206. write_toc(create_toc(src_data))
  207. # process body
  208. sys.stdout.write(update_toc(src_data))
  209. # if </html> is found, suppose a complete html is provided.
  210. # otherwise, generate module class reference:
  211. if re.search('</html>', src_data, re.IGNORECASE):
  212. sys.exit()
  213. index_names = {
  214. 'd' : 'display',
  215. 'db': 'database',
  216. 'g' : 'general',
  217. 'i' : 'imagery',
  218. 'm' : 'misc',
  219. 'ps': 'postscript',
  220. 'p' : 'paint',
  221. 'r' : 'raster',
  222. 'r3': 'raster3d',
  223. 's' : 'sites',
  224. 't' : 'temporal',
  225. 'v' : 'vector'
  226. }
  227. def to_title(name):
  228. """Convert name of command class/family to form suitable for title"""
  229. return name.capitalize()
  230. index_titles = {}
  231. for key, name in index_names.iteritems():
  232. if key == 'r3':
  233. index_titles[key] = '3D raster'
  234. else:
  235. index_titles[key] = to_title(name)
  236. # process footer
  237. index = re.search('(<!-- meta page index:)(.*)(-->)', src_data, re.IGNORECASE)
  238. if index:
  239. index_name = index.group(2).strip()
  240. if '|' in index_name:
  241. index_name, index_name_cap = index_name.split('|', 1)
  242. else:
  243. index_name_cap = index_name
  244. else:
  245. mod_class = pgm.split('.', 1)[0]
  246. index_name = index_names.get(mod_class, '')
  247. index_name_cap = index_titles.get(mod_class, '')
  248. grass_version = os.getenv("VERSION_NUMBER", "unknown")
  249. year = os.getenv("VERSION_DATE")
  250. if not year:
  251. year = str(datetime.now().year)
  252. # check the names of scripts to assign the right folder
  253. topdir = os.getenv("MODULE_TOPDIR")
  254. scripts = os.listdir(os.path.join(topdir, 'scripts'))
  255. if pgm in scripts:
  256. folder = 'scripts'
  257. else:
  258. folder = index_name
  259. if index_name:
  260. sys.stdout.write(footer_index.substitute(INDEXNAME=index_name, PGM=pgm,
  261. INDEXNAMECAP=index_name_cap,
  262. YEAR=year, URL=source_url,
  263. FOLDER=folder,
  264. GRASS_VERSION=grass_version))
  265. else:
  266. sys.stdout.write(footer_noindex.substitute(YEAR=year,
  267. GRASS_VERSION=grass_version))