forked from OSGeo/grass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ggroff.py
269 lines (232 loc) · 7.03 KB
/
ggroff.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import sys
import os
import re
__all__ = ["Formatter"]
try:
version = os.environ["VERSION_NUMBER"]
except KeyError:
version = ""
styles = {
"b": "\\fB@\\fR",
"i": "\\fI@\\fR",
"em": "\\fI@\\fR",
"code": "\\fC@\\fR",
"span": "\\fC@\\fR",
"sup": "\\u@\\d",
"hr": "",
}
formats = {
"br": "\n.br\n",
"h2": "\n.SH @",
"h3": "\n.SS @",
"h4": "\n.SS @",
"dt": ('\n.IP "@" 4m', "no_nl"),
"dd": "\n.br\n@",
"ul": ("\n.RS 4n\n@\n.RE\n", "in_ul"),
"menu": ("\n.RS 4n\n@\n.RE\n", "in_ul"),
"dir": ("\n.RS 4n\n@\n.RE\n", "in_ul"),
"ol": ("\n.IP\n@\n.PP\n", "index"),
"p": "\n.PP\n@",
"pre": ("\n.br\n.nf\n\\fC\n@\n\\fR\n.fi\n", "preformat"),
}
formats.update(styles)
def is_string(x):
return isinstance(x, str)
def is_tuple(x):
return isinstance(x, tuple)
def is_list(x):
return isinstance(x, list)
def is_blank(s):
return is_string(s) and s.strip() == ""
def clean(content):
return [item for item in content if not is_blank(item)]
class Formatter:
def __init__(self, filename, stream=sys.stdout):
self.stream = stream
self.style = {
"preformat": False,
"in_ul": False,
"no_nl": False,
"in_table": False,
"in_tr": False,
"index": [],
}
self.stack = []
self.strip_re = re.compile("^[ \t]+")
self.filename = filename
self.at_bol = True
def warning(self, msg):
sys.stderr.write(msg + "\n")
def set(self, var, val):
self.style[var] = val
def get(self, var):
return self.style[var]
def push(self, **kwargs):
self.stack.append(self.style.copy())
self.style.update(**kwargs)
def pop(self):
self.style = self.stack.pop()
def show(self, s):
self.stream.write(s)
if s != "":
self.at_bol = s.endswith("\n")
def pp_with(self, content, var, val):
self.push()
self.set(var, val)
self.pp(content)
self.pop()
def fmt(self, format, content, var=None):
# String.partition is only in 2.5+
# (pre,sep,post) = format.partition("@")
if self.get("no_nl") and "\n" in format:
self.warning("can't handle line breaks in <dt>...</dt>")
format = "@"
f = format.split("@", 1)
pre = f[0]
if len(f) > 1:
sep = "@"
post = f[1]
else:
sep = ""
post = ""
if pre != "":
self.show(pre)
if sep != "":
if var:
val = self.get("index") + [0] if var == "index" else True
self.pp_with(content, var, val)
else:
self.pp(content)
if post != "":
self.show(post)
def pp_li(self, content):
if self.get("in_ul"):
self.fmt("\n.IP \\(bu 4n\n@", content)
else:
idx = self.get("index")
idx[-1] += 1
sec = ".".join(map(str, idx))
self.show("\n.IP \\fB%s\\fR\n" % sec)
self.set("index", idx)
self.pp(content)
def pp_title(self):
self.show(
"\n.TH "
+ os.path.basename(self.filename).replace(".html", "")
+ ' 1 "" "GRASS '
+ version
+ '" "GRASS GIS User\'s Manual"'
)
def pp_tr(self, content):
content = clean(content)
self.push(in_tr=True)
col = 0
for item in content:
if not is_tuple(item):
self.warning("invalid item in table row: %s" % str(item))
continue
(tag, attrs, body) = item
if tag not in {"td", "th"}:
self.warning("invalid tag in table row: %s" % tag)
continue
if col > 0:
self.show("\t \t")
self.show("T{\n")
self.pp(body)
self.show("\nT}")
col += 1
self.show("\n")
self.pop()
def pp_tbody(self, content):
for item in content:
if is_tuple(item):
(tag, attrs, body) = item
if tag in {"thead", "tbody", "tfoot"}:
self.pp_tbody(body)
elif tag == "tr":
self.pp_tr(body)
self.show(".sp 1\n")
def count_cols(self, content):
cols = 0
for item in content:
n = 0
if is_blank(item):
pass
elif is_tuple(item):
(tag, attrs, body) = item
if tag in {"thead", "tbody", "tfoot"}:
n = self.count_cols(body)
elif tag == "tr":
n = len(clean(body))
cols = max(cols, n)
else:
self.warning("invalid item in table: %s" % str(item))
return cols
def pp_table(self, content):
cols = self.count_cols(content)
if cols == 0:
return
self.show("\n.TS\nexpand;\n")
self.show(" lw1 ".join(["lw60" for i in range(cols)]) + ".\n")
self.pp_tbody(content)
self.show("\n.TE\n")
def pp_tag(self, tag, content):
if self.get("in_tr") and tag not in styles:
self.pp(content)
elif tag in formats:
spec = formats[tag]
if is_string(spec):
self.fmt(spec, content)
else:
(fmt, var) = spec
self.fmt(fmt, content, var)
elif tag == "table":
if self.get("in_table"):
self.warning("cannot handle nested tables")
return
self.push(in_table=True)
self.pp_table(content)
self.pop()
elif tag == "li":
self.pp_li(content)
elif tag == "title":
self.pp_title()
else:
self.pp(content)
def pp_string(self, content):
if content == "":
return
s = content
if self.get("no_nl"):
s = s.replace("\n", " ")
s = s.replace("\\", "\\(rs")
s = s.replace("'", "\\(cq")
s = s.replace('"', "\\(dq")
s = s.replace("`", "\\(ga")
s = s.replace("-", "\\-")
if self.at_bol and s[0] in {".", "'"}:
s = "\\&" + s
self.show(s)
def pp_text(self, content):
if content == "":
return
lines = content.splitlines(True)
if len(lines) != 1:
for line in lines:
self.pp_text(line)
return
content = lines[0]
if self.at_bol and not self.get("preformat"):
content = self.strip_re.sub("", content)
self.pp_string(content)
def pp_list(self, content):
for item in content:
self.pp(item)
def pp(self, content):
if is_list(content):
self.pp_list(content)
elif is_tuple(content):
(tag, attrs, body) = content
self.pp_tag(tag, body)
elif is_string(content):
self.pp_text(content)