mkhtml.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: mkhtml.py
  5. # AUTHOR(S): Markus Neteler, Glynn Clements
  6. # PURPOSE: create HTML manual page snippets
  7. # COPYRIGHT: (C) 2007,2009 Glynn Clements and the GRASS Development Team
  8. #
  9. # This program is free software under the GNU General Public
  10. # License (>=v2). Read the file COPYING that comes with GRASS
  11. # for details.
  12. #
  13. #############################################################################
  14. import sys
  15. import os
  16. import string
  17. import re
  18. pgm = sys.argv[1]
  19. src_file = "%s.html" % pgm
  20. tmp_file = "%s.tmp.html" % pgm
  21. header_tmpl = string.Template(\
  22. """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  23. <html>
  24. <head>
  25. <title>GRASS GIS Manual: ${PGM}</title>
  26. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  27. <link rel="stylesheet" href="grassdocs.css" type="text/css">
  28. </head>
  29. <body bgcolor="white">
  30. <img src="grass_logo.png" alt="GRASS logo"><hr align=center size=6 noshade>
  31. <h2>NAME</h2>
  32. <em><b>${PGM}</b></em>
  33. """)
  34. footer_tmpl = string.Template(\
  35. """<hr>
  36. <p><a href="index.html">Main index</a> - <a href="$INDEXNAME.html">$INDEXNAME index</a> - <a href="full_index.html">Full index</a></p>
  37. <p>&copy; 2003-2009 <a href="http://grass.osgeo.org">GRASS Development Team</a></p>
  38. </body>
  39. </html>
  40. """)
  41. def read_file(name):
  42. try:
  43. f = open(name, 'rb')
  44. s = f.read()
  45. f.close()
  46. return s
  47. except IOError:
  48. return ""
  49. src_data = read_file(src_file)
  50. if not re.search('<html>', src_data, re.IGNORECASE):
  51. tmp_data = read_file(tmp_file)
  52. if not re.search('<html>', tmp_data, re.IGNORECASE):
  53. sys.stdout.write(header_tmpl.substitute(PGM = pgm))
  54. if tmp_data:
  55. for line in tmp_data.splitlines(True):
  56. if not re.search('</body>|</html>', line, re.IGNORECASE):
  57. sys.stdout.write(line)
  58. sys.stdout.write(src_data)
  59. # if </html> is found, suppose a complete html is provided.
  60. # otherwise, generate module class reference:
  61. if re.search('</html>', src_data, re.IGNORECASE):
  62. sys.exit()
  63. index_names = {
  64. 'd': 'display',
  65. 'db': 'database',
  66. 'g': 'general',
  67. 'i': 'imagery',
  68. 'm': 'misc',
  69. 'pg': 'postGRASS',
  70. 'ps': 'postscript',
  71. 'p': 'paint',
  72. 'r': 'raster',
  73. 'r3': 'raster3D',
  74. 's': 'sites',
  75. 'v': 'vector'
  76. }
  77. mod_class = pgm.split('.', 1)[0]
  78. index_name = index_names.get(mod_class, mod_class)
  79. sys.stdout.write(footer_tmpl.substitute(INDEXNAME = index_name))