mkhtml.py 4.1 KB

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