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"\ ")
120 for mdfn in args.mdfiles:
124 print("The test was successful.")
127 def parse_md_file(mdfn):
128 fi = re.match(r'^(?P<fn>(?P<srcdir>.+/)?(?P<name>(?P<prog>[^/]+?)(\.(?P<sect>\d+))?)\.md)$', mdfn)
130 die('Failed to parse a md input file name:', mdfn)
131 fi = argparse.Namespace(**fi.groupdict())
132 fi.want_manpage = not not fi.sect
134 fi.title = fi.prog + '(' + fi.sect + ') man page'
140 find_man_substitutions()
141 prog_ver = 'rsync ' + env_subs['VERSION']
142 if fi.prog != 'rsync':
143 prog_ver = fi.prog + ' from ' + prog_ver
144 fi.man_headings = (fi.prog, fi.sect, env_subs['date'], prog_ver, env_subs['prefix'])
146 with open(mdfn, 'r', encoding='utf-8') as fh:
149 use_gfm_parser = '@USE_GFM_PARSER@' in txt
151 txt = txt.replace('@USE_GFM_PARSER@', '')
154 txt = (txt.replace('@VERSION@', env_subs['VERSION'])
155 .replace('@BINDIR@', env_subs['bindir'])
156 .replace('@LIBDIR@', env_subs['libdir']))
160 die('Input file requires cmarkgfm parser:', mdfn)
161 fi.html_in = gfm_parser(txt)
163 fi.html_in = md_parser(txt)
171 output_list = [ (fi.name + '.html', fi.html_out) ]
173 output_list += [ (fi.name, fi.man_out) ]
174 for fn, txt in output_list:
175 if os.path.lexists(fn):
178 with open(fn, 'w', encoding='utf-8') as fh:
182 def find_man_substitutions():
183 srcdir = os.path.dirname(sys.argv[0]) + '/'
186 git_dir = srcdir + '.git'
187 if os.path.lexists(git_dir):
188 mtime = int(subprocess.check_output(['git', '--git-dir', git_dir, 'log', '-1', '--format=%at']))
190 # Allow "prefix" to be overridden via the environment:
191 env_subs['prefix'] = os.environ.get('RSYNC_OVERRIDE_PREFIX', None)
194 env_subs['VERSION'] = '1.0.0'
195 env_subs['bindir'] = '/usr/bin'
196 env_subs['libdir'] = '/usr/lib/rsync'
198 for fn in (srcdir + 'version.h', 'Makefile'):
202 die('Failed to find', srcdir + fn)
206 with open(srcdir + 'version.h', 'r', encoding='utf-8') as fh:
208 m = re.search(r'"(.+?)"', txt)
209 env_subs['VERSION'] = m.group(1)
211 with open('Makefile', 'r', encoding='utf-8') as fh:
213 m = re.match(r'^(\w+)=(.+)', line)
216 var, val = (m.group(1), m.group(2))
217 if var == 'prefix' and env_subs[var] is not None:
219 while re.search(r'\$\{', val):
220 val = re.sub(r'\$\{(\w+)\}', lambda m: env_subs[m.group(1)], val)
225 env_subs['date'] = time.strftime('%d %b %Y', time.localtime(mtime))
228 def html_via_commonmark(txt):
229 return commonmark.HtmlRenderer().render(commonmark.Parser().parse(txt))
232 class TransformHtml(HTMLParser):
233 def __init__(self, fi):
234 HTMLParser.__init__(self, convert_charrefs=True)
238 st = self.state = argparse.Namespace(
241 at_first_tag_in_li = False,
242 at_first_tag_in_dd = False,
246 html_out = [ HTML_START.replace('%TITLE%', fi.title) ],
249 want_manpage = fi.want_manpage,
250 created_hashtags = set(),
251 derived_hashtags = set(),
252 referenced_hashtags = set(),
253 bad_hashtags = set(),
260 st.man_out.append(MAN_START % fi.man_headings)
262 if '</table>' in fi.html_in:
263 st.html_out[0] = st.html_out[0].replace('</style>', TABLE_STYLE + '</style>')
265 self.feed(fi.html_in)
269 st.html_out.append(MAN_HTML_END % env_subs['date'])
270 st.html_out.append(HTML_END)
271 st.man_out.append(MAN_END)
273 fi.html_out = ''.join(st.html_out)
276 fi.man_out = ''.join(st.man_out)
279 for href, txt in st.derived_hashtags:
280 derived = txt2target(txt, href[1:])
281 if derived not in st.created_hashtags:
282 txt = re.sub(r'[\1-\7]+', '', txt.replace(NBR_DASH[0], '-').replace(NBR_SPACE[0], ' '))
283 warn('Unknown derived hashtag link in', self.fn, 'based on:', (href, txt))
285 for bad in st.bad_hashtags:
286 if bad in st.created_hashtags:
287 warn('Missing "#" in hashtag link in', self.fn + ':', bad)
289 warn('Unknown non-hashtag link in', self.fn + ':', bad)
291 for bad in st.referenced_hashtags - st.created_hashtags:
292 warn('Unknown hashtag link in', self.fn + ':', '#' + bad)
294 def handle_starttag(self, tag, attrs_list):
297 self.output_debug('START', (tag, attrs_list))
298 if st.at_first_tag_in_li:
299 if st.list_state[-1] == 'dl':
304 st.html_out.append('<dt>')
306 st.at_first_tag_in_dd = True # Kluge to suppress a .P at the start of an li.
307 st.at_first_tag_in_li = False
309 if not st.at_first_tag_in_dd:
310 st.man_out.append(st.p_macro)
312 st.at_first_tag_in_li = True
313 lstate = st.list_state[-1]
317 st.man_out.append(".IP o\n")
319 st.man_out.append(".IP " + str(lstate) + ".\n")
320 st.list_state[-1] += 1
321 elif tag == 'blockquote':
322 st.man_out.append(".RS 4\n")
325 st.man_out.append(st.p_macro + ".nf\n")
326 elif tag == 'code' and not st.in_pre:
328 st.txt += BOLD_FONT[0]
329 elif tag == 'strong' or tag == 'b':
330 st.txt += BOLD_FONT[0]
331 elif tag == 'em' or tag == 'i':
333 tag = 'u' # Change it into underline to be more like the man page
334 st.txt += UNDR_FONT[0]
337 for var, val in attrs_list:
339 start = int(val) # We only support integers.
342 st.man_out.append(".RS\n")
346 st.list_state.append('dl')
348 st.list_state.append(start)
349 st.man_out.append(st.p_macro)
352 st.man_out.append(st.p_macro)
354 st.man_out.append(".RS\n")
356 st.list_state.append('o')
358 st.man_out.append(".l\n")
359 st.html_out.append("<hr />")
363 for var, val in attrs_list:
365 if val in ('#', '#opt', '#daemon-opt'):
367 elif val.startswith('#'):
368 st.referenced_hashtags.add(val[1:])
369 if val[1:] == st.prior_target:
370 warn('Found link to the current section in', self.fn + ':', val)
371 elif not val.startswith(('https://', 'http://', 'mailto:', 'ftp:', './')):
372 st.bad_hashtags.add(val)
373 st.a_txt_start = len(st.txt)
374 st.html_out.append('<' + tag + ''.join(' ' + var + '="' + htmlify(val) + '"' for var, val in attrs_list) + '>')
375 st.at_first_tag_in_dd = False
378 def handle_endtag(self, tag):
381 self.output_debug('END', (tag,))
382 if tag in CONSUMES_TXT or st.dt_from == tag:
388 if tag == 'h1' or tag == 'h2':
389 st.man_out.append(st.p_macro + '.SH "' + manify(txt) + '"\n')
391 st.opt_prefix = 'daemon-opt' if txt == 'DAEMON OPTIONS' else 'opt'
393 st.man_out.append(st.p_macro + '.SS "' + manify(txt) + '"\n')
396 if st.dt_from == 'p':
398 st.man_out.append('.IP "' + manify(txt) + '"\n')
399 if txt.startswith(BOLD_FONT[0]):
403 st.man_out.append(manify(txt) + "\n")
405 if st.list_state[-1] == 'dl':
406 if st.at_first_tag_in_li:
407 die("Invalid 0. -> td translation")
410 st.man_out.append(manify(txt) + "\n")
411 st.at_first_tag_in_li = False
412 elif tag == 'blockquote':
413 st.man_out.append(".RE\n")
416 st.man_out.append(manify(txt) + "\n.fi\n")
417 elif (tag == 'code' and not st.in_pre):
419 add_to_txt = NORM_FONT[0]
420 elif tag == 'strong' or tag == 'b':
421 add_to_txt = NORM_FONT[0]
422 elif tag == 'em' or tag == 'i':
424 tag = 'u' # Change it into underline to be more like the man page
425 add_to_txt = NORM_FONT[0]
426 elif tag == 'ol' or tag == 'ul':
427 if st.list_state.pop() == 'dl':
430 st.man_out.append(".RE\n")
433 st.at_first_tag_in_dd = False
438 atxt = st.txt[st.a_txt_start:]
439 find = 'href="' + st.a_href + '"'
440 for j in range(len(st.html_out)-1, 0, -1):
441 if find in st.html_out[j]:
442 derived = txt2target(atxt, st.a_href[1:])
443 if derived == st.prior_target:
444 warn('Found link to the current section in', self.fn + ':', derived)
445 st.derived_hashtags.add((st.a_href, atxt))
446 st.html_out[j] = st.html_out[j].replace(find, 'href="#' + derived + '"')
449 die('INTERNAL ERROR: failed to find href in html data:', find)
450 st.html_out.append('</' + tag + '>')
456 if st.dt_from == tag:
457 st.man_out.append('.IP "' + manify(txt) + '"\n')
458 st.html_out.append('</dt><dd>')
459 st.at_first_tag_in_dd = True
462 st.html_out.append('<dd>')
463 st.at_first_tag_in_dd = True
466 def handle_data(self, txt):
469 warn('Malformed link in', self.fn + ':', txt)
471 self.output_debug('DATA', (txt,))
475 txt = re.sub(r'\s--(\s)', NBR_SPACE[0] + r'--\1', txt).replace('--', NBR_DASH[0]*2)
476 txt = re.sub(r'(^|\W)-', r'\1' + NBR_DASH[0], txt)
479 txt = re.sub(r'\s', NBR_SPACE[0], txt)
480 html = html.replace(NBR_DASH[0], '-').replace(NBR_SPACE[0], ' ') # <code> is non-breaking in CSS
481 st.html_out.append(html.replace(NBR_SPACE[0], ' ').replace(NBR_DASH[0], '-⁠'))
485 def add_target(self, txt):
487 txt = txt2target(txt, st.opt_prefix)
489 st.html_out.append('<a id="' + txt + '" href="#' + txt + '" class="tgt"></a>')
490 st.created_hashtags.add(txt)
491 st.prior_target = txt
494 def output_debug(self, event, extra):
498 st = argparse.Namespace(**vars(st))
499 if len(st.html_out) > 2:
500 st.html_out = ['...'] + st.html_out[-2:]
501 if len(st.man_out) > 2:
502 st.man_out = ['...'] + st.man_out[-2:]
504 pprint.PrettyPrinter(indent=2).pprint(vars(st))
507 def txt2target(txt, opt_prefix):
508 txt = re.sub(r'[%s](.+?)[=%s].*' % (BOLD_FONT[0], NORM_FONT[0]), r'\1', txt.strip())
509 txt = re.sub(r'[%s]' % NBR_DASH[0], '-', txt)
510 txt = re.sub(r'[\1-\7]+', '', txt)
511 txt = re.sub(r'[^-A-Za-z0-9._]', '_', txt)
512 if opt_prefix and txt.startswith('-'):
513 txt = opt_prefix + txt
515 txt = re.sub(r'^([^A-Za-z])', r't\1', txt)
520 return re.sub(r"^(['.])", r'\&\1', txt.replace('\\', '\\\\')
521 .replace(NBR_SPACE[0], NBR_SPACE[1])
522 .replace(NBR_DASH[0], NBR_DASH[1])
523 .replace(NORM_FONT[0], NORM_FONT[1])
524 .replace(BOLD_FONT[0], BOLD_FONT[1])
525 .replace(UNDR_FONT[0], UNDR_FONT[1]), flags=re.M)
529 return txt.replace('&', '&').replace('<', '<').replace('>', '>').replace('"', '"')
533 print(*msg, file=sys.stderr)
543 if __name__ == '__main__':
544 parser = argparse.ArgumentParser(description="Output html and (optionally) nroff for markdown pages.", add_help=False)
545 parser.add_argument('--test', action='store_true', help="Just test the parsing without outputting any files.")
546 parser.add_argument('--debug', '-D', action='count', default=0, help='Output copious info on the html parsing. Repeat for even more.')
547 parser.add_argument("--help", "-h", action="help", help="Output this help message and exit.")
548 parser.add_argument("mdfiles", nargs='+', help="The source .md files to convert.")
549 args = parser.parse_args()
553 md_parser = cmarkgfm.markdown_to_html
554 gfm_parser = cmarkgfm.github_flavored_markdown_to_html
558 md_parser = html_via_commonmark
560 die("Failed to find cmarkgfm or commonmark for python3.")