forked from OSGeo/grass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkhtml.py
131 lines (113 loc) · 3.72 KB
/
mkhtml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python
############################################################################
#
# MODULE: mkhtml.py
# AUTHOR(S): Markus Neteler
# Glynn Clements
# Martin Landa <landa.martin gmail.com>
# PURPOSE: Create HTML manual page snippets
# COPYRIGHT: (C) 2007, 2009, 2011-2012 by Glynn Clements
# and the GRASS Development Team
#
# This program is free software under the GNU General
# Public License (>=v2). Read the file COPYING that
# comes with GRASS for details.
#
#############################################################################
import sys
import os
import string
import re
from datetime import datetime
pgm = sys.argv[1]
if len(sys.argv) > 1:
year = sys.argv[2]
else:
year = str(datetime.now().year)
src_file = "%s.html" % pgm
tmp_file = "%s.tmp.html" % pgm
header_base = """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>GRASS GIS Manual: ${PGM}</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="grassdocs.css" type="text/css">
</head>
<body bgcolor="white">
<img src="grass_logo.png" alt="GRASS logo"><hr align=center size=6 noshade>
"""
header_nopgm = """<h2>${PGM}</h2>
"""
header_pgm = """<h2>NAME</h2>
<em><b>${PGM}</b></em>
"""
footer_index = string.Template(\
"""<hr>
<p><a href="index.html">Main index</a> - <a href="${INDEXNAME}.html">${INDEXNAMECAP} index</a> - <a href="full_index.html">Full index</a></p>
<p>© 2003-${YEAR} <a href="http://grass.osgeo.org">GRASS Development Team</a></p>
</body>
</html>
""")
footer_noindex = string.Template(\
"""<hr>
<p><a href="index.html">Main index</a> - <a href="full_index.html">Full index</a></p>
<p>© 2003-${YEAR} <a href="http://grass.osgeo.org">GRASS Development Team</a></p>
</body>
</html>
""")
def read_file(name):
try:
f = open(name, 'rb')
s = f.read()
f.close()
return s
except IOError:
return ""
src_data = read_file(src_file)
name = re.search('(<!-- meta page name:)(.*)(-->)', src_data, re.IGNORECASE)
if name:
pgm = name.group(2).strip().split('-', 1)[0].strip()
desc = re.search('(<!-- meta page description:)(.*)(-->)', src_data, re.IGNORECASE)
if desc:
pgm = desc.group(2).strip()
header_tmpl = string.Template(header_base + header_nopgm)
else:
header_tmpl = string.Template(header_base + header_pgm)
if not re.search('<html>', src_data, re.IGNORECASE):
tmp_data = read_file(tmp_file)
if not re.search('<html>', tmp_data, re.IGNORECASE):
sys.stdout.write(header_tmpl.substitute(PGM = pgm))
if tmp_data:
for line in tmp_data.splitlines(True):
if not re.search('</body>|</html>', line, re.IGNORECASE):
sys.stdout.write(line)
sys.stdout.write(src_data)
# if </html> is found, suppose a complete html is provided.
# otherwise, generate module class reference:
if re.search('</html>', src_data, re.IGNORECASE):
sys.exit()
index_names = {
'd' : 'display',
'db': 'database',
'g' : 'general',
'i' : 'imagery',
'm' : 'misc',
'ps': 'postscript',
'p' : 'paint',
'r' : 'raster',
'r3': 'raster3D',
's' : 'sites',
't' : 'temporal',
'v' : 'vector'
}
index = re.search('(<!-- meta page index:)(.*)(-->)', src_data, re.IGNORECASE)
if index:
index_name = index.group(2).strip()
else:
mod_class = pgm.split('.', 1)[0]
index_name = index_names.get(mod_class, '')
if index_name:
sys.stdout.write(footer_index.substitute(INDEXNAME = index_name, INDEXNAMECAP = index_name.title(),
YEAR = year))
else:
sys.stdout.write(footer_noindex.substitute(YEAR = year))