3 # This script transforms markdown files into html and (optionally) nroff. The
4 # output files are written into the current directory named for the input file
5 # without the .md suffix and either the .html suffix or no suffix.
7 # If the input .md file has a section number at the end of the name (e.g.,
8 # rsync.1.md) a nroff file is also output (PROJ.NUM.md -> PROJ.NUM).
10 # The markdown input format has one extra extension: if a numbered list starts
11 # at 0, it is turned into a description list. The dl's dt tag is taken from the
12 # contents of the first tag inside the li, which is usually a p, code, or
15 # The cmarkgfm or commonmark lib is used to transforms the input file into
16 # html. Then, the html.parser is used as a state machine that lets us tweak
17 # the html and (optionally) output nroff data based on the html tags.
19 # If the string @USE_GFM_PARSER@ exists in the file, the string is removed and
20 # a github-flavored-markup parser is used to parse the file.
22 # The man-page .md files also get the vars @VERSION@, @BINDIR@, and @LIBDIR@
23 # substituted. Some of these values depend on the Makefile $(prefix) (see the
24 # generated Makefile). If the maintainer wants to build files for /usr/local
25 # while creating release-ready man-page files for /usr, use the environment to
26 # set RSYNC_OVERRIDE_PREFIX=/usr.
28 # Copyright (C) 2020 - 2021 Wayne Davison
30 # This program is freely redistributable.
32 import os, sys, re, argparse, subprocess, time
33 from html.parser import HTMLParser
35 CONSUMES_TXT = set('h1 h2 h3 p li pre'.split())
39 <title>%TITLE%</title>
40 <meta charset="UTF-8"/>
41 <link href="https://fonts.googleapis.com/css2?family=Roboto&family=Roboto+Mono&display=swap" rel="stylesheet">
48 font-family: 'Roboto', sans-serif;
50 a.tgt { font-face: symbol; font-weight: 400; font-size: 70%; visibility: hidden; text-decoration: none; color: #ddd; padding: 0 4px; border: 0; vertical-align: top; }
51 a.tgt:after { content: '🔗'; }
52 a.tgt:hover { color: #444; background-color: #eaeaea; }
53 h1:hover > a.tgt, h2:hover > a.tgt, h3:hover > a.tgt, dt:hover > a.tgt { visibility: visible; }
55 font-family: 'Roboto Mono', monospace;
67 margin-block-start: 0em;
79 border-top: 1px solid grey;
82 background-color: #f6f8fa;
85 border: 1px solid #dfe2e5;
93 <div style="float: right"><p><i>%s</i></p></div>
101 .TH "%s" "%s" "%s" "%s" "User Commands"
108 NORM_FONT = ('\1', r"\fP")
109 BOLD_FONT = ('\2', r"\fB")
110 UNDR_FONT = ('\3', r"\fI")
111 NBR_DASH = ('\4', r"\-")
112 NBR_SPACE = ('\xa0', r"\ ")
118 for mdfn in args.mdfiles:
122 print("The test was successful.")
125 def parse_md_file(mdfn):
126 fi = re.match(r'^(?P<fn>(?P<srcdir>.+/)?(?P<name>(?P<prog>[^/]+?)(\.(?P<sect>\d+))?)\.md)$', mdfn)
128 die('Failed to parse a md input file name:', mdfn)
129 fi = argparse.Namespace(**fi.groupdict())
130 fi.want_manpage = not not fi.sect
132 fi.title = fi.prog + '(' + fi.sect + ') man page'
138 find_man_substitutions()
139 prog_ver = 'rsync ' + env_subs['VERSION']
140 if fi.prog != 'rsync':
141 prog_ver = fi.prog + ' from ' + prog_ver
142 fi.man_headings = (fi.prog, fi.sect, env_subs['date'], prog_ver, env_subs['prefix'])
144 with open(mdfn, 'r', encoding='utf-8') as fh:
147 use_gfm_parser = '@USE_GFM_PARSER@' in txt
149 txt = txt.replace('@USE_GFM_PARSER@', '')
152 txt = (txt.replace('@VERSION@', env_subs['VERSION'])
153 .replace('@BINDIR@', env_subs['bindir'])
154 .replace('@LIBDIR@', env_subs['libdir']))
158 die('Input file requires cmarkgfm parser:', mdfn)
159 fi.html_in = gfm_parser(txt)
161 fi.html_in = md_parser(txt)
169 output_list = [ (fi.name + '.html', fi.html_out) ]
171 output_list += [ (fi.name, fi.man_out) ]
172 for fn, txt in output_list:
173 if os.path.lexists(fn):
176 with open(fn, 'w', encoding='utf-8') as fh:
180 def find_man_substitutions():
181 srcdir = os.path.dirname(sys.argv[0]) + '/'
184 git_dir = srcdir + '.git'
185 if os.path.lexists(git_dir):
186 mtime = int(subprocess.check_output(['git', '--git-dir', git_dir, 'log', '-1', '--format=%at']))
188 # Allow "prefix" to be overridden via the environment:
189 env_subs['prefix'] = os.environ.get('RSYNC_OVERRIDE_PREFIX', None)
192 env_subs['VERSION'] = '1.0.0'
193 env_subs['bindir'] = '/usr/bin'
194 env_subs['libdir'] = '/usr/lib/rsync'
196 for fn in (srcdir + 'version.h', 'Makefile'):
200 die('Failed to find', srcdir + fn)
204 with open(srcdir + 'version.h', 'r', encoding='utf-8') as fh:
206 m = re.search(r'"(.+?)"', txt)
207 env_subs['VERSION'] = m.group(1)
209 with open('Makefile', 'r', encoding='utf-8') as fh:
211 m = re.match(r'^(\w+)=(.+)', line)
214 var, val = (m.group(1), m.group(2))
215 if var == 'prefix' and env_subs[var] is not None:
217 while re.search(r'\$\{', val):
218 val = re.sub(r'\$\{(\w+)\}', lambda m: env_subs[m.group(1)], val)
223 env_subs['date'] = time.strftime('%d %b %Y', time.localtime(mtime))
226 def html_via_commonmark(txt):
227 return commonmark.HtmlRenderer().render(commonmark.Parser().parse(txt))
230 class TransformHtml(HTMLParser):
231 def __init__(self, fi):
232 HTMLParser.__init__(self, convert_charrefs=True)
234 st = self.state = argparse.Namespace(
237 at_first_tag_in_li = False,
238 at_first_tag_in_dd = False,
242 html_out = [ HTML_START.replace('%TITLE%', fi.title) ],
245 want_manpage = fi.want_manpage,
249 st.man_out.append(MAN_START % fi.man_headings)
251 if '</table>' in fi.html_in:
252 st.html_out[0] = st.html_out[0].replace('</style>', TABLE_STYLE + '</style>')
254 self.feed(fi.html_in)
258 st.html_out.append(MAN_HTML_END % env_subs['date'])
259 st.html_out.append(HTML_END)
260 st.man_out.append(MAN_END)
262 fi.html_out = ''.join(st.html_out)
265 fi.man_out = ''.join(st.man_out)
269 def handle_starttag(self, tag, attrs_list):
272 self.output_debug('START', (tag, attrs_list))
273 if st.at_first_tag_in_li:
274 if st.list_state[-1] == 'dl':
279 st.html_out.append('<dt>')
281 st.at_first_tag_in_dd = True # Kluge to suppress a .P at the start of an li.
282 st.at_first_tag_in_li = False
284 if not st.at_first_tag_in_dd:
285 st.man_out.append(st.p_macro)
287 st.at_first_tag_in_li = True
288 lstate = st.list_state[-1]
292 st.man_out.append(".IP o\n")
294 st.man_out.append(".IP " + str(lstate) + ".\n")
295 st.list_state[-1] += 1
296 elif tag == 'blockquote':
297 st.man_out.append(".RS 4\n")
300 st.man_out.append(st.p_macro + ".nf\n")
301 elif tag == 'code' and not st.in_pre:
303 st.txt += BOLD_FONT[0]
304 elif tag == 'strong' or tag == 'b':
305 st.txt += BOLD_FONT[0]
306 elif tag == 'em' or tag == 'i':
308 tag = 'u' # Change it into underline to be more like the man page
309 st.txt += UNDR_FONT[0]
312 for var, val in attrs_list:
314 start = int(val) # We only support integers.
317 st.man_out.append(".RS\n")
321 st.list_state.append('dl')
323 st.list_state.append(start)
324 st.man_out.append(st.p_macro)
327 st.man_out.append(st.p_macro)
329 st.man_out.append(".RS\n")
331 st.list_state.append('o')
333 st.man_out.append(".l\n")
334 st.html_out.append("<hr />")
336 st.html_out.append('<' + tag + ''.join(' ' + var + '="' + htmlify(val) + '"' for var, val in attrs_list) + '>')
337 st.at_first_tag_in_dd = False
340 def add_target(self, txt):
342 txt = re.sub(r'[%s](.+?)[=%s].*' % (BOLD_FONT[0], NORM_FONT[0]), r'\1', txt.strip())
343 txt = re.sub(r'[%s]' % NBR_DASH[0], '-', txt)
344 txt = re.sub(r'[\1-\7]+', '', txt)
345 txt = re.sub(r'[^-A-Za-z0-9._]', '_', txt)
346 if txt.startswith('-'):
349 txt = re.sub(r'^([^A-Za-z])', r't\1', txt)
351 st.html_out.append('<a id="' + txt + '" href="#' + txt + '" class="tgt"></a>')
354 def handle_endtag(self, tag):
357 self.output_debug('END', (tag,))
358 if tag in CONSUMES_TXT or st.dt_from == tag:
364 if tag == 'h1' or tag == 'h2':
365 st.man_out.append(st.p_macro + '.SH "' + manify(txt) + '"\n')
368 st.man_out.append(st.p_macro + '.SS "' + manify(txt) + '"\n')
371 if st.dt_from == 'p':
373 st.man_out.append('.IP "' + manify(txt) + '"\n')
374 if txt.startswith(BOLD_FONT[0]):
378 st.man_out.append(manify(txt) + "\n")
380 if st.list_state[-1] == 'dl':
381 if st.at_first_tag_in_li:
382 die("Invalid 0. -> td translation")
385 st.man_out.append(manify(txt) + "\n")
386 st.at_first_tag_in_li = False
387 elif tag == 'blockquote':
388 st.man_out.append(".RE\n")
391 st.man_out.append(manify(txt) + "\n.fi\n")
392 elif (tag == 'code' and not st.in_pre):
394 add_to_txt = NORM_FONT[0]
395 elif tag == 'strong' or tag == 'b':
396 add_to_txt = NORM_FONT[0]
397 elif tag == 'em' or tag == 'i':
399 tag = 'u' # Change it into underline to be more like the man page
400 add_to_txt = NORM_FONT[0]
401 elif tag == 'ol' or tag == 'ul':
402 if st.list_state.pop() == 'dl':
405 st.man_out.append(".RE\n")
408 st.at_first_tag_in_dd = False
411 st.html_out.append('</' + tag + '>')
417 if st.dt_from == tag:
418 st.man_out.append('.IP "' + manify(txt) + '"\n')
419 st.html_out.append('</dt><dd>')
420 st.at_first_tag_in_dd = True
423 st.html_out.append('<dd>')
424 st.at_first_tag_in_dd = True
427 def handle_data(self, txt):
430 self.output_debug('DATA', (txt,))
434 txt = re.sub(r'\s--(\s)', NBR_SPACE[0] + r'--\1', txt).replace('--', NBR_DASH[0]*2)
435 txt = re.sub(r'(^|\W)-', r'\1' + NBR_DASH[0], txt)
438 txt = re.sub(r'\s', NBR_SPACE[0], txt)
439 html = html.replace(NBR_DASH[0], '-').replace(NBR_SPACE[0], ' ') # <code> is non-breaking in CSS
440 st.html_out.append(html.replace(NBR_SPACE[0], ' ').replace(NBR_DASH[0], '-⁠'))
444 def output_debug(self, event, extra):
448 st = argparse.Namespace(**vars(st))
449 if len(st.html_out) > 2:
450 st.html_out = ['...'] + st.html_out[-2:]
451 if len(st.man_out) > 2:
452 st.man_out = ['...'] + st.man_out[-2:]
454 pprint.PrettyPrinter(indent=2).pprint(vars(st))
458 return re.sub(r"^(['.])", r'\&\1', txt.replace('\\', '\\\\')
459 .replace(NBR_SPACE[0], NBR_SPACE[1])
460 .replace(NBR_DASH[0], NBR_DASH[1])
461 .replace(NORM_FONT[0], NORM_FONT[1])
462 .replace(BOLD_FONT[0], BOLD_FONT[1])
463 .replace(UNDR_FONT[0], UNDR_FONT[1]), flags=re.M)
467 return txt.replace('&', '&').replace('<', '<').replace('>', '>').replace('"', '"')
471 print(*msg, file=sys.stderr)
479 if __name__ == '__main__':
480 parser = argparse.ArgumentParser(description="Output html and (optionally) nroff for markdown pages.", add_help=False)
481 parser.add_argument('--test', action='store_true', help="Just test the parsing without outputting any files.")
482 parser.add_argument('--debug', '-D', action='count', default=0, help='Output copious info on the html parsing. Repeat for even more.')
483 parser.add_argument("--help", "-h", action="help", help="Output this help message and exit.")
484 parser.add_argument("mdfiles", nargs='+', help="The source .md files to convert.")
485 args = parser.parse_args()
489 md_parser = cmarkgfm.markdown_to_html
490 gfm_parser = cmarkgfm.github_flavored_markdown_to_html
494 md_parser = html_via_commonmark
496 die("Failed to find cmarkgfm or commonmark for python3.")