Skip to content

Commit 68c865c

Browse files
committed
A few more man page script improvements.
1 parent 6dc94e3 commit 68c865c

File tree

3 files changed

+60
-48
lines changed

3 files changed

+60
-48
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ aclocal.m4
1818
/rsync*.1
1919
/rsync*.5
2020
/rsync*.html
21+
/.md2man-works
2122
/autom4te*.cache
2223
/confdefs.h
2324
/conftest*

maybe-make-man

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@ fi
77

88
srcdir="$1"
99
inname="$2"
10+
flagfile="$srcdir/.md2man-works"
1011

1112
if [ ! -d "$srcdir" ]; then
1213
echo "The specified SRC_DIR is not a directory: $srcdir" 1>&2
1314
exit 1
1415
fi
1516

16-
if [ ! x"$RSYNC_ALWAYS_BUILD" ]; then
17+
if [ ! -f "$flagfile" ]; then
1718
# We test our smallest manpage just to see if the python setup works.
18-
if ! "$srcdir/md2man" --test "$srcdir/rsync-ssl.1.md" >/dev/null 2>&1; then
19+
if "$srcdir/md2man" --test "$srcdir/rsync-ssl.1.md" >/dev/null 2>&1; then
20+
touch $flagfile
21+
else
1922
outname=`echo "$inname" | sed 's/\.md$//'`
2023
if [ -f "$outname" ]; then
2124
exit 0

md2man

+54-46
Original file line numberDiff line numberDiff line change
@@ -104,77 +104,71 @@ def main():
104104
print("The test was successful.")
105105
return
106106

107-
fn = fi.name + '.html'
108-
print("Outputing HTML page:", fn)
109-
with open(fn, 'w', encoding='utf-8') as fh:
110-
fh.write(fi.html_out)
111-
112-
fn = fi.name
113-
print("Outputing man page:", fn)
114-
with open(fn, 'w', encoding='utf-8') as fh:
115-
fh.write(fi.man_out)
107+
for fn, txt in ((fi.name + '.html', fi.html_out), (fi.name, fi.man_out)):
108+
print("Wrote:", fn)
109+
with open(fn, 'w', encoding='utf-8') as fh:
110+
fh.write(txt)
116111

117112

118113
class HtmlToManPage(HTMLParser):
119114
def __init__(self, fi):
120115
HTMLParser.__init__(self, convert_charrefs=True)
121116

122-
self.state = argparse.Namespace(
117+
st = self.state = argparse.Namespace(
123118
list_state = [ ],
124119
p_macro = ".P\n",
125120
at_first_tag_in_li = False,
126121
at_first_tag_in_dd = False,
127122
dt_from = None,
128123
in_pre = False,
124+
html_out = [ HTML_START % fi.title ],
125+
man_out = [ MAN_START % (fi.prog, fi.sect, fi.date) ],
129126
txt = '',
130127
)
131128

132-
self.html_out = [ HTML_START % fi.title ]
133-
self.man_out = [ MAN_START % (fi.prog, fi.sect, fi.date) ]
134-
135129
self.feed(fi.html_in)
136130
fi.html_in = None
137131

138-
self.html_out.append(HTML_END % fi.date)
139-
self.man_out.append(MAN_END)
132+
st.html_out.append(HTML_END % fi.date)
133+
st.man_out.append(MAN_END)
140134

141-
fi.html_out = ''.join(self.html_out)
142-
self.html_out = None
135+
fi.html_out = ''.join(st.html_out)
136+
st.html_out = None
143137

144-
fi.man_out = ''.join(self.man_out)
145-
self.man_out = None
138+
fi.man_out = ''.join(st.man_out)
139+
st.man_out = None
146140

147141

148142
def handle_starttag(self, tag, attrs_list):
149143
st = self.state
150144
if args.debug:
151-
print('START', tag, attrs_list, st)
145+
self.output_debug('START', (tag, attrs_list))
152146
if st.at_first_tag_in_li:
153147
if st.list_state[-1] == 'dl':
154148
st.dt_from = tag
155149
if tag == 'p':
156150
tag = 'dt'
157151
else:
158-
self.html_out.append('<dt>')
152+
st.html_out.append('<dt>')
159153
st.at_first_tag_in_li = False
160154
if tag == 'p':
161155
if not st.at_first_tag_in_dd:
162-
self.man_out.append(st.p_macro)
156+
st.man_out.append(st.p_macro)
163157
elif tag == 'li':
164158
st.at_first_tag_in_li = True
165159
lstate = st.list_state[-1]
166160
if lstate == 'dl':
167161
return
168162
if lstate == 'o':
169-
self.man_out.append(".IP o\n")
163+
st.man_out.append(".IP o\n")
170164
else:
171-
self.man_out.append(".IP " + str(lstate) + ".\n")
165+
st.man_out.append(".IP " + str(lstate) + ".\n")
172166
st.list_state[-1] += 1
173167
elif tag == 'blockquote':
174-
self.man_out.append(".RS 4\n")
168+
st.man_out.append(".RS 4\n")
175169
elif tag == 'pre':
176170
st.in_pre = True
177-
self.man_out.append(st.p_macro + ".nf\n")
171+
st.man_out.append(st.p_macro + ".nf\n")
178172
elif tag == 'code' and not st.in_pre:
179173
st.txt += BOLD_FONT[0]
180174
elif tag == 'strong' or tag == 'bold':
@@ -188,57 +182,59 @@ class HtmlToManPage(HTMLParser):
188182
start = int(val) # We only support integers.
189183
break
190184
if st.list_state:
191-
self.man_out.append(".RS\n")
185+
st.man_out.append(".RS\n")
192186
if start == 0:
193187
tag = 'dl'
194188
attrs_list = [ ]
195189
st.list_state.append('dl')
196190
else:
197191
st.list_state.append(start)
198-
self.man_out.append(st.p_macro)
192+
st.man_out.append(st.p_macro)
199193
st.p_macro = ".IP\n"
200194
elif tag == 'ul':
201-
self.man_out.append(st.p_macro)
195+
st.man_out.append(st.p_macro)
202196
if st.list_state:
203-
self.man_out.append(".RS\n")
197+
st.man_out.append(".RS\n")
204198
st.p_macro = ".IP\n"
205199
st.list_state.append('o')
206-
self.html_out.append('<' + tag + ' '.join( ' ' + var + '="' + safeText(val) + '"' for var, val in attrs_list) + '>')
200+
st.html_out.append('<' + tag + ' '.join( ' ' + var + '="' + safeText(val) + '"' for var, val in attrs_list) + '>')
207201
st.at_first_tag_in_dd = False
208202

209203

210204
def handle_endtag(self, tag):
211205
st = self.state
212206
if args.debug:
213-
print(' END', tag, st)
207+
self.output_debug('END', (tag,))
214208
if tag in CONSUMES_TXT or st.dt_from == tag:
215209
txt = st.txt.strip()
216210
st.txt = ''
217211
else:
218212
txt = None
219213
add_to_txt = None
220214
if tag == 'h1':
221-
self.man_out.append(st.p_macro + '.SH "' + manify(txt) + '"\n')
215+
st.man_out.append(st.p_macro + '.SH "' + manify(txt) + '"\n')
216+
elif tag == 'h2':
217+
st.man_out.append(st.p_macro + '.SS "' + manify(txt) + '"\n')
222218
elif tag == 'p':
223219
if st.dt_from == 'p':
224220
tag = 'dt'
225-
self.man_out.append('.IP "' + manify(txt) + '"\n')
221+
st.man_out.append('.IP "' + manify(txt) + '"\n')
226222
st.dt_from = None
227-
else:
228-
self.man_out.append(manify(txt) + "\n")
223+
elif txt != '':
224+
st.man_out.append(manify(txt) + "\n")
229225
elif tag == 'li':
230226
if st.list_state[-1] == 'dl':
231227
if st.at_first_tag_in_li:
232228
die("Invalid 0. -> td translation")
233229
tag = 'dd'
234230
if txt != '':
235-
self.man_out.append(manify(txt) + "\n")
231+
st.man_out.append(manify(txt) + "\n")
236232
st.at_first_tag_in_li = False
237233
elif tag == 'blockquote':
238-
self.man_out.append(".RE\n")
234+
st.man_out.append(".RE\n")
239235
elif tag == 'pre':
240236
st.in_pre = False
241-
self.man_out.append(manify(txt) + "\n.fi\n")
237+
st.man_out.append(manify(txt) + "\n.fi\n")
242238
elif tag == 'code' and not st.in_pre:
243239
add_to_txt = NORM_FONT[0]
244240
elif tag == 'strong' or tag == 'bold':
@@ -249,34 +245,46 @@ class HtmlToManPage(HTMLParser):
249245
if st.list_state.pop() == 'dl':
250246
tag = 'dl'
251247
if st.list_state:
252-
self.man_out.append(".RE\n")
248+
st.man_out.append(".RE\n")
253249
else:
254250
st.p_macro = ".P\n"
255251
st.at_first_tag_in_dd = False
256-
self.html_out.append('</' + tag + '>')
252+
st.html_out.append('</' + tag + '>')
257253
if add_to_txt:
258254
if txt is None:
259255
st.txt += add_to_txt
260256
else:
261257
txt += add_to_txt
262258
if st.dt_from == tag:
263-
self.man_out.append('.IP "' + manify(txt) + '"\n')
264-
self.html_out.append('</dt><dd>')
259+
st.man_out.append('.IP "' + manify(txt) + '"\n')
260+
st.html_out.append('</dt><dd>')
265261
st.at_first_tag_in_dd = True
266262
st.dt_from = None
267263
elif tag == 'dt':
268-
self.html_out.append('<dd>')
264+
st.html_out.append('<dd>')
269265
st.at_first_tag_in_dd = True
270266

271267

272268
def handle_data(self, data):
273269
st = self.state
274270
if args.debug:
275-
print(' DATA', [data], st)
276-
self.html_out.append(safeText(data))
271+
self.output_debug('DATA', (data,))
272+
st.html_out.append(safeText(data))
277273
st.txt += data
278274

279275

276+
def output_debug(self, event, extra):
277+
import pprint
278+
st = self.state
279+
if args.debug < 2:
280+
if len(st.html_out) > 2:
281+
st.html_out = ['...'] + st.html_out[-2:]
282+
if len(st.man_out) > 2:
283+
st.man_out = ['...'] + st.man_out[-2:]
284+
print(event, extra)
285+
pprint.PrettyPrinter(indent=2).pprint(vars(st))
286+
287+
280288
def manify(txt):
281289
return re.sub(r"^(['.])", r'\&\1', txt.replace('\\', '\\\\')
282290
.replace(NORM_FONT[0], NORM_FONT[1])

0 commit comments

Comments
 (0)