forked from OSGeo/grass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkrest.py
executable file
·132 lines (106 loc) · 3.05 KB
/
mkrest.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
132
#!/usr/bin/env python3
############################################################################
#
# MODULE: mkrest.py
# AUTHOR(S): Luca Delucchi
# PURPOSE: Create HTML manual page snippets
# COPYRIGHT: (C) 2012 by Luca Delucchi
# 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 string
import re
import subprocess
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.txt" % pgm
# TODO add copyright
footer_index = string.Template(
"""
:doc:`Main Page <index>` - :doc:`${INDEXNAMECAP} index <${INDEXNAME}>` - :doc:`Full index <full_index>`
2003-${YEAR} `GRASS Development Team <https://grass.osgeo.org>`_
"""
)
footer_noindex = string.Template(
"""
:doc:`Main Page <index>` - :doc:`Full index <full_index>`
2003-${YEAR} `GRASS Development Team <https://grass.osgeo.org>`_
"""
)
def read_file(name):
try:
f = open(name, "rb")
s = f.read()
f.close()
return s
except IOError:
return ""
replacement = {
"*`": "`",
"`* `": "`",
">`_*": ">`_",
">`_,*": ">`_,",
'``*\ "': '``"',
"***": "**",
}
src_data = read_file(src_file)
title = re.search("(<!-- meta page description:)(.*)(-->)", src_data, re.IGNORECASE)
if title:
title_name = title.group(2).strip()
sys.stdout.write("%s\n" % title_name)
title_style = "=" * (len(title_name) + 2)
sys.stdout.write("%s\n\n" % title_style)
tmp_data = read_file(tmp_file)
if tmp_data:
sys.stdout.write(tmp_data)
process = subprocess.Popen(
"pandoc -s -r html %s -w rst" % src_file, shell=True, stdout=subprocess.PIPE
)
html_text = process.communicate()[0]
if html_text:
for k, v in replacement.iteritems():
html_text = html_text.replace(k, v)
# TODO remove with space if string start with it, " vector..." -> "vector..."
# not if it is a tab: " vector...." -> " vector..."
# for line in html_text.splitlines(True):
# sys.stdout.write("%s" % line.lstrip())
sys.stdout.write(html_text)
index_names = {
"d": "display",
"db": "database",
"g": "general",
"i": "imagery",
"m": "miscellaneous",
"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))
sys.exit()