mkhtml.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: mkhtml.py
  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, 2009, 2011 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. pgm = sys.argv[1]
  22. if len(sys.argv) > 1:
  23. year = sys.argv[2]
  24. else:
  25. year = "2011"
  26. src_file = "%s.html" % pgm
  27. tmp_file = "%s.tmp.html" % pgm
  28. header_base = """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  29. <html>
  30. <head>
  31. <title>GRASS GIS Manual: ${PGM}</title>
  32. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  33. <link rel="stylesheet" href="grassdocs.css" type="text/css">
  34. </head>
  35. <body bgcolor="white">
  36. <img src="grass_logo.png" alt="GRASS logo"><hr align=center size=6 noshade>
  37. """
  38. header_nopgm = """<h2>${PGM}</h2>
  39. """
  40. header_pgm = """<h2>NAME</h2>
  41. <em><b>${PGM}</b></em>
  42. """
  43. footer_index = string.Template(\
  44. """<hr>
  45. <p><a href="index.html">Main index</a> - <a href="${INDEXNAME}.html">${INDEXNAME} index</a> - <a href="full_index.html">Full index</a></p>
  46. <p>&copy; 2003-${YEAR} <a href="http://grass.osgeo.org">GRASS Development Team</a></p>
  47. </body>
  48. </html>
  49. """)
  50. footer_noindex = string.Template(\
  51. """<hr>
  52. <p><a href="index.html">Main index</a> - <a href="full_index.html">Full index</a></p>
  53. <p>&copy; 2003-${YEAR} <a href="http://grass.osgeo.org">GRASS Development Team</a></p>
  54. </body>
  55. </html>
  56. """)
  57. def read_file(name):
  58. try:
  59. f = open(name, 'rb')
  60. s = f.read()
  61. f.close()
  62. return s
  63. except IOError:
  64. return ""
  65. src_data = read_file(src_file)
  66. name = re.search('(<!-- meta page name:)(.*)(-->)', src_data, re.IGNORECASE)
  67. if name:
  68. pgm = name.group(2).strip().split('-', 1)[0].strip()
  69. desc = re.search('(<!-- meta page description:)(.*)(-->)', src_data, re.IGNORECASE)
  70. if desc:
  71. pgm = desc.group(2).strip()
  72. header_tmpl = string.Template(header_base + header_nopgm)
  73. else:
  74. header_tmpl = string.Template(header_base + header_pgm)
  75. if not re.search('<html>', src_data, re.IGNORECASE):
  76. tmp_data = read_file(tmp_file)
  77. if not re.search('<html>', tmp_data, re.IGNORECASE):
  78. sys.stdout.write(header_tmpl.substitute(PGM = pgm))
  79. if tmp_data:
  80. for line in tmp_data.splitlines(True):
  81. if not re.search('</body>|</html>', line, re.IGNORECASE):
  82. sys.stdout.write(line)
  83. sys.stdout.write(src_data)
  84. # if </html> is found, suppose a complete html is provided.
  85. # otherwise, generate module class reference:
  86. if re.search('</html>', src_data, re.IGNORECASE):
  87. sys.exit()
  88. index_names = {
  89. 'd': 'display',
  90. 'db': 'database',
  91. 'g': 'general',
  92. 'i': 'imagery',
  93. 'm': 'misc',
  94. 'ps': 'postscript',
  95. 'p': 'paint',
  96. 'r': 'raster',
  97. 'r3': 'raster3D',
  98. 's': 'sites',
  99. 'v': 'vector'
  100. }
  101. index = re.search('(<!-- meta page index:)(.*)(-->)', src_data, re.IGNORECASE)
  102. if index:
  103. index_name = index.group(2).strip()
  104. else:
  105. mod_class = pgm.split('.', 1)[0]
  106. index_name = index_names.get(mod_class, '')
  107. if index_name:
  108. sys.stdout.write(footer_index.substitute(INDEXNAME = index_name, YEAR = year))
  109. else:
  110. sys.stdout.write(footer_noindex.substitute(YEAR = year))