3 # This script takes a manpage written in markdown and turns it into an html web
4 # page and a nroff man page. The input file must have the name of the program
5 # and the section in this format: NAME.NUM.md. The output files are written
6 # into the current directory named NAME.NUM.html and NAME.NUM. The input
7 # format has one extra extension: if a numbered list starts at 0, it is turned
8 # into a description list. The dl's dt tag is taken from the contents of the
9 # first tag inside the li, which is usually a p, code, or strong tag. The
10 # cmarkgfm or commonmark lib is used to transforms the input file into html.
11 # The html.parser is used as a state machine that both tweaks the html and
12 # outputs the nroff data based on the html tags.
14 # Copyright (C) 2020 Wayne Davison
16 # This program is freely redistributable.
18 import sys, os, re, argparse, subprocess, time
19 from html.parser import HTMLParser
21 CONSUMES_TXT = set('h1 h2 p li pre'.split())
26 <link href="https://fonts.googleapis.com/css2?family=Roboto&family=Roboto+Mono&display=swap" rel="stylesheet">
33 font-family: 'Roboto', sans-serif;
36 font-family: 'Roboto Mono', monospace;
47 margin-block-start: 0em;
54 <div style="float: right"><p><i>%s</i></p></div>
59 .TH "%s" "%s" "%s" "%s" "User Commands"
65 NORM_FONT = ('\1', r"\fP")
66 BOLD_FONT = ('\2', r"\fB")
67 ULIN_FONT = ('\3', r"\fI")
72 fi = re.match(r'^(?P<fn>(?P<srcdir>.+/)?(?P<name>(?P<prog>[^/]+)\.(?P<sect>\d+))\.md)$', args.mdfile)
74 die('Failed to parse NAME.NUM.md out of input file:', args.mdfile)
75 fi = argparse.Namespace(**fi.groupdict())
80 fi.title = fi.prog + '(' + fi.sect + ') man page'
83 if os.path.lexists(fi.srcdir + '.git'):
84 fi.mtime = int(subprocess.check_output('git log -1 --format=%at'.split()))
86 chk_files = 'NEWS.md Makefile'.split()
89 st = os.lstat(fi.srcdir + fn)
91 die('Failed to find', fi.srcdir + fn)
93 fi.mtime = st.st_mtime
95 fi.date = time.strftime('%d %b %Y', time.localtime(fi.mtime))
99 with open(fi.srcdir + 'Makefile', 'r', encoding='utf-8') as fh:
101 m = re.match(r'^(\w+)=(.+)', line)
104 var, val = (m[1], m[2])
105 while re.search(r'\$\{', val):
106 val = re.sub(r'\$\{(\w+)\}', lambda m: env_subs[m[1]], val)
111 with open(fi.fn, 'r', encoding='utf-8') as fh:
114 txt = re.sub(r'@VERSION@', env_subs['VERSION'], txt)
115 txt = re.sub(r'@LIBDIR@', env_subs['libdir'], txt)
116 fi.html_in = md_parser(txt)
119 fi.man_headings = (fi.prog, fi.sect, fi.date, fi.prog + ' ' + env_subs['VERSION'])
124 print("The test was successful.")
127 for fn, txt in ((fi.name + '.html', fi.html_out), (fi.name, fi.man_out)):
129 with open(fn, 'w', encoding='utf-8') as fh:
133 def html_via_cmarkgfm(txt):
134 return cmarkgfm.markdown_to_html(txt)
137 def html_via_commonmark(txt):
138 return commonmark.HtmlRenderer().render(commonmark.Parser().parse(txt))
141 class HtmlToManPage(HTMLParser):
142 def __init__(self, fi):
143 HTMLParser.__init__(self, convert_charrefs=True)
145 st = self.state = argparse.Namespace(
148 at_first_tag_in_li = False,
149 at_first_tag_in_dd = False,
152 html_out = [ HTML_START % fi.title ],
153 man_out = [ MAN_START % fi.man_headings ],
157 self.feed(fi.html_in)
160 st.html_out.append(HTML_END % fi.date)
161 st.man_out.append(MAN_END)
163 fi.html_out = ''.join(st.html_out)
166 fi.man_out = ''.join(st.man_out)
170 def handle_starttag(self, tag, attrs_list):
173 self.output_debug('START', (tag, attrs_list))
174 if st.at_first_tag_in_li:
175 if st.list_state[-1] == 'dl':
180 st.html_out.append('<dt>')
181 st.at_first_tag_in_li = False
183 if not st.at_first_tag_in_dd:
184 st.man_out.append(st.p_macro)
186 st.at_first_tag_in_li = True
187 lstate = st.list_state[-1]
191 st.man_out.append(".IP o\n")
193 st.man_out.append(".IP " + str(lstate) + ".\n")
194 st.list_state[-1] += 1
195 elif tag == 'blockquote':
196 st.man_out.append(".RS 4\n")
199 st.man_out.append(st.p_macro + ".nf\n")
200 elif tag == 'code' and not st.in_pre:
201 st.txt += BOLD_FONT[0]
202 elif tag == 'strong' or tag == 'b':
203 st.txt += BOLD_FONT[0]
204 elif tag == 'em' or tag == 'i':
205 tag = 'u' # Change it into underline to be more like the man page
206 st.txt += ULIN_FONT[0]
209 for var, val in attrs_list:
211 start = int(val) # We only support integers.
214 st.man_out.append(".RS\n")
218 st.list_state.append('dl')
220 st.list_state.append(start)
221 st.man_out.append(st.p_macro)
224 st.man_out.append(st.p_macro)
226 st.man_out.append(".RS\n")
228 st.list_state.append('o')
229 st.html_out.append('<' + tag + ''.join(' ' + var + '="' + htmlify(val) + '"' for var, val in attrs_list) + '>')
230 st.at_first_tag_in_dd = False
233 def handle_endtag(self, tag):
236 self.output_debug('END', (tag,))
237 if tag in CONSUMES_TXT or st.dt_from == tag:
244 st.man_out.append(st.p_macro + '.SH "' + manify(txt) + '"\n')
246 st.man_out.append(st.p_macro + '.SS "' + manify(txt) + '"\n')
248 if st.dt_from == 'p':
250 st.man_out.append('.IP "' + manify(txt) + '"\n')
253 st.man_out.append(manify(txt) + "\n")
255 if st.list_state[-1] == 'dl':
256 if st.at_first_tag_in_li:
257 die("Invalid 0. -> td translation")
260 st.man_out.append(manify(txt) + "\n")
261 st.at_first_tag_in_li = False
262 elif tag == 'blockquote':
263 st.man_out.append(".RE\n")
266 st.man_out.append(manify(txt) + "\n.fi\n")
267 elif (tag == 'code' and not st.in_pre) or tag == 'strong' or tag == 'b':
268 add_to_txt = NORM_FONT[0]
269 elif tag == 'em' or tag == 'i':
270 tag = 'u' # Change it into underline to be more like the man page
271 add_to_txt = NORM_FONT[0]
272 elif tag == 'ol' or tag == 'ul':
273 if st.list_state.pop() == 'dl':
276 st.man_out.append(".RE\n")
279 st.at_first_tag_in_dd = False
280 st.html_out.append('</' + tag + '>')
286 if st.dt_from == tag:
287 st.man_out.append('.IP "' + manify(txt) + '"\n')
288 st.html_out.append('</dt><dd>')
289 st.at_first_tag_in_dd = True
292 st.html_out.append('<dd>')
293 st.at_first_tag_in_dd = True
296 def handle_data(self, data):
299 self.output_debug('DATA', (data,))
300 st.html_out.append(htmlify(data))
304 def output_debug(self, event, extra):
308 st = argparse.Namespace(**vars(st))
309 if len(st.html_out) > 2:
310 st.html_out = ['...'] + st.html_out[-2:]
311 if len(st.man_out) > 2:
312 st.man_out = ['...'] + st.man_out[-2:]
314 pprint.PrettyPrinter(indent=2).pprint(vars(st))
318 return re.sub(r"^(['.])", r'\&\1', txt.replace('\\', '\\\\')
319 .replace(NORM_FONT[0], NORM_FONT[1])
320 .replace(BOLD_FONT[0], BOLD_FONT[1])
321 .replace(ULIN_FONT[0], ULIN_FONT[1]), flags=re.M)
325 return txt.replace('&', '&').replace('<', '<').replace('>', '>').replace('"', '"')
329 print(*msg, file=sys.stderr)
337 if __name__ == '__main__':
338 parser = argparse.ArgumentParser(description='Transform a NAME.NUM.md markdown file into a NAME.NUM.html web page & a NAME.NUM man page.', add_help=False)
339 parser.add_argument('--test', action='store_true', help='Test if we can parse the input w/o updating any files.')
340 parser.add_argument('--debug', '-D', action='count', default=0, help='Output copious info on the html parsing. Repeat for even more.')
341 parser.add_argument("--help", "-h", action="help", help="Output this help message and exit.")
342 parser.add_argument('mdfile', help="The NAME.NUM.md file to parse.")
343 args = parser.parse_args()
347 md_parser = html_via_cmarkgfm
351 md_parser = html_via_commonmark
353 die("Failed to find cmarkgfm or commonmark for python3.")