Skip to content

Commit 6dc94e3

Browse files
committed
Output the files at the end; fix a missing double-quote.
1 parent 8146b04 commit 6dc94e3

File tree

1 file changed

+78
-79
lines changed

1 file changed

+78
-79
lines changed

md2man

+78-79
Original file line numberDiff line numberDiff line change
@@ -57,26 +57,28 @@ NORM_FONT = ('\1', r"\fP")
5757
BOLD_FONT = ('\2', r"\fB")
5858
ULIN_FONT = ('\3', r"\fI")
5959

60-
env_subs = { }
61-
6260
def main():
63-
mtime = None
64-
6561
fi = re.match(r'^(?P<fn>(?P<srcdir>.+/)?(?P<name>(?P<prog>[^/]+)\.(?P<sect>\d+))\.md)$', args.mdfile)
6662
if not fi:
6763
die('Failed to parse NAME.NUM.md out of input file:', args.mdfile)
6864
fi = argparse.Namespace(**fi.groupdict())
65+
6966
if not fi.srcdir:
7067
fi.srcdir = './'
7168

69+
fi.title = fi.prog + '(' + fi.sect + ') man page'
70+
fi.date = None
71+
7272
chk_files = 'latest-year.h Makefile'.split()
7373
for fn in chk_files:
7474
try:
7575
st = os.lstat(fi.srcdir + fn)
7676
except:
7777
die('Failed to find', fi.srcdir + fn)
78-
if not mtime:
79-
mtime = st.st_mtime
78+
if not fi.date:
79+
fi.date = time.strftime('%d %b %Y', time.localtime(st.st_mtime))
80+
81+
env_subs = { }
8082

8183
with open(fi.srcdir + 'Makefile', 'r', encoding='utf-8') as fh:
8284
for line in fh:
@@ -90,91 +92,89 @@ def main():
9092
if var == 'VERSION':
9193
break
9294

93-
MarkdownToManPage(fi, mtime)
95+
with open(fi.fn, 'r', encoding='utf-8') as fh:
96+
txt = re.sub(r'@VERSION@', env_subs['VERSION'], fh.read())
97+
txt = re.sub(r'@LIBDIR@', env_subs['libdir'], txt)
98+
fi.html_in = cmarkgfm.github_flavored_markdown_to_html(txt)
99+
txt = None
100+
101+
HtmlToManPage(fi)
102+
103+
if args.test:
104+
print("The test was successful.")
105+
return
106+
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)
94111

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)
95116

96-
class MarkdownToManPage(HTMLParser):
97-
def __init__(self, fi, mtime):
117+
118+
class HtmlToManPage(HTMLParser):
119+
def __init__(self, fi):
98120
HTMLParser.__init__(self, convert_charrefs=True)
99121

100-
self.man_fh = self.html_fh = None
101122
self.state = argparse.Namespace(
102123
list_state = [ ],
103124
p_macro = ".P\n",
104-
first_li_tag = False,
105-
first_dd_tag = False,
125+
at_first_tag_in_li = False,
126+
at_first_tag_in_dd = False,
106127
dt_from = None,
107128
in_pre = False,
108129
txt = '',
109130
)
110131

111-
self.date = time.strftime('%d %b %Y', time.localtime(mtime))
112-
113-
with open(fi.fn, 'r', encoding='utf-8') as fh:
114-
txt = re.sub(r'@VERSION@', env_subs['VERSION'], fh.read())
115-
txt = re.sub(r'@LIBDIR@', env_subs['libdir'], txt)
116-
html = cmarkgfm.github_flavored_markdown_to_html(txt)
117-
txt = None
132+
self.html_out = [ HTML_START % fi.title ]
133+
self.man_out = [ MAN_START % (fi.prog, fi.sect, fi.date) ]
118134

119-
if args.test:
120-
self.html_fh = open(os.devnull, 'w', encoding='utf-8')
121-
self.man_fh = self.html_fh
122-
else:
123-
self.html_fn = fi.name + '.html'
124-
self.html_fh = open(self.html_fn, 'w', encoding='utf-8')
125-
self.html_fh.write(HTML_START % (fi.prog + '(' + fi.sect + ') man page'))
135+
self.feed(fi.html_in)
136+
fi.html_in = None
126137

127-
self.man_fn = fi.name
128-
self.man_fh = open(self.man_fn, 'w', encoding='utf-8')
129-
self.man_fh.write(MAN_START % (fi.prog, fi.sect, self.date))
138+
self.html_out.append(HTML_END % fi.date)
139+
self.man_out.append(MAN_END)
130140

131-
self.feed(html)
141+
fi.html_out = ''.join(self.html_out)
142+
self.html_out = None
132143

133-
def __del__(self):
134-
if args.test:
135-
print("The test was successful.")
136-
return
144+
fi.man_out = ''.join(self.man_out)
145+
self.man_out = None
137146

138-
if self.html_fh:
139-
self.html_fh.write(HTML_END % self.date)
140-
self.html_fh.close()
141-
print("Output HTML page: ", self.html_fn)
142-
143-
if self.man_fh:
144-
self.man_fh.write(MAN_END)
145-
self.man_fh.close()
146-
print("Output man page: ", self.man_fn)
147147

148148
def handle_starttag(self, tag, attrs_list):
149149
st = self.state
150150
if args.debug:
151151
print('START', tag, attrs_list, st)
152-
if st.first_li_tag:
152+
if st.at_first_tag_in_li:
153153
if st.list_state[-1] == 'dl':
154154
st.dt_from = tag
155155
if tag == 'p':
156156
tag = 'dt'
157157
else:
158-
self.html_fh.write('<dt>')
159-
st.first_li_tag = False
158+
self.html_out.append('<dt>')
159+
st.at_first_tag_in_li = False
160160
if tag == 'p':
161-
if not st.first_dd_tag:
162-
self.man_fh.write(st.p_macro)
161+
if not st.at_first_tag_in_dd:
162+
self.man_out.append(st.p_macro)
163163
elif tag == 'li':
164-
st.first_li_tag = True
164+
st.at_first_tag_in_li = True
165165
lstate = st.list_state[-1]
166166
if lstate == 'dl':
167167
return
168168
if lstate == 'o':
169-
self.man_fh.write(".IP o\n")
169+
self.man_out.append(".IP o\n")
170170
else:
171-
self.man_fh.write(".IP " + str(lstate) + ".\n")
171+
self.man_out.append(".IP " + str(lstate) + ".\n")
172172
st.list_state[-1] += 1
173173
elif tag == 'blockquote':
174-
self.man_fh.write(".RS 4\n")
174+
self.man_out.append(".RS 4\n")
175175
elif tag == 'pre':
176176
st.in_pre = True
177-
self.man_fh.write(st.p_macro + ".nf\n")
177+
self.man_out.append(st.p_macro + ".nf\n")
178178
elif tag == 'code' and not st.in_pre:
179179
st.txt += BOLD_FONT[0]
180180
elif tag == 'strong' or tag == 'bold':
@@ -188,26 +188,24 @@ class MarkdownToManPage(HTMLParser):
188188
start = int(val) # We only support integers.
189189
break
190190
if st.list_state:
191-
self.man_fh.write(".RS\n")
191+
self.man_out.append(".RS\n")
192192
if start == 0:
193193
tag = 'dl'
194194
attrs_list = [ ]
195195
st.list_state.append('dl')
196196
else:
197197
st.list_state.append(start)
198-
self.man_fh.write(st.p_macro)
198+
self.man_out.append(st.p_macro)
199199
st.p_macro = ".IP\n"
200200
elif tag == 'ul':
201-
self.man_fh.write(st.p_macro)
201+
self.man_out.append(st.p_macro)
202202
if st.list_state:
203-
self.man_fh.write(".RS\n")
203+
self.man_out.append(".RS\n")
204204
st.p_macro = ".IP\n"
205205
st.list_state.append('o')
206-
outer_tag = '<' + tag
207-
for var, val in attrs_list:
208-
outer_tag += ' ' + var + '=' + safeText(val) + '"'
209-
self.html_fh.write(outer_tag + '>')
210-
st.first_dd_tag = False
206+
self.html_out.append('<' + tag + ' '.join( ' ' + var + '="' + safeText(val) + '"' for var, val in attrs_list) + '>')
207+
st.at_first_tag_in_dd = False
208+
211209

212210
def handle_endtag(self, tag):
213211
st = self.state
@@ -220,27 +218,27 @@ class MarkdownToManPage(HTMLParser):
220218
txt = None
221219
add_to_txt = None
222220
if tag == 'h1':
223-
self.man_fh.write(st.p_macro + '.SH "' + manify(txt) + '"\n')
221+
self.man_out.append(st.p_macro + '.SH "' + manify(txt) + '"\n')
224222
elif tag == 'p':
225223
if st.dt_from == 'p':
226224
tag = 'dt'
227-
self.man_fh.write('.IP "' + manify(txt) + '"\n')
225+
self.man_out.append('.IP "' + manify(txt) + '"\n')
228226
st.dt_from = None
229227
else:
230-
self.man_fh.write(manify(txt) + "\n")
228+
self.man_out.append(manify(txt) + "\n")
231229
elif tag == 'li':
232230
if st.list_state[-1] == 'dl':
233-
if st.first_li_tag:
231+
if st.at_first_tag_in_li:
234232
die("Invalid 0. -> td translation")
235233
tag = 'dd'
236234
if txt != '':
237-
self.man_fh.write(manify(txt) + "\n")
238-
st.first_li_tag = False
235+
self.man_out.append(manify(txt) + "\n")
236+
st.at_first_tag_in_li = False
239237
elif tag == 'blockquote':
240-
self.man_fh.write(".RE\n")
238+
self.man_out.append(".RE\n")
241239
elif tag == 'pre':
242240
st.in_pre = False
243-
self.man_fh.write(manify(txt) + "\n.fi\n")
241+
self.man_out.append(manify(txt) + "\n.fi\n")
244242
elif tag == 'code' and not st.in_pre:
245243
add_to_txt = NORM_FONT[0]
246244
elif tag == 'strong' or tag == 'bold':
@@ -251,30 +249,31 @@ class MarkdownToManPage(HTMLParser):
251249
if st.list_state.pop() == 'dl':
252250
tag = 'dl'
253251
if st.list_state:
254-
self.man_fh.write(".RE\n")
252+
self.man_out.append(".RE\n")
255253
else:
256254
st.p_macro = ".P\n"
257-
st.first_dd_tag = False
258-
self.html_fh.write('</' + tag + '>')
255+
st.at_first_tag_in_dd = False
256+
self.html_out.append('</' + tag + '>')
259257
if add_to_txt:
260258
if txt is None:
261259
st.txt += add_to_txt
262260
else:
263261
txt += add_to_txt
264262
if st.dt_from == tag:
265-
self.man_fh.write('.IP "' + manify(txt) + '"\n')
266-
self.html_fh.write('</dt><dd>')
267-
st.first_dd_tag = True
263+
self.man_out.append('.IP "' + manify(txt) + '"\n')
264+
self.html_out.append('</dt><dd>')
265+
st.at_first_tag_in_dd = True
268266
st.dt_from = None
269267
elif tag == 'dt':
270-
self.html_fh.write('<dd>')
271-
st.first_dd_tag = True
268+
self.html_out.append('<dd>')
269+
st.at_first_tag_in_dd = True
270+
272271

273272
def handle_data(self, data):
274273
st = self.state
275274
if args.debug:
276275
print(' DATA', [data], st)
277-
self.html_fh.write(safeText(data))
276+
self.html_out.append(safeText(data))
278277
st.txt += data
279278

280279

0 commit comments

Comments
 (0)