mkhtml.py 3.7 KB

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