Annotation of embedaddon/rsync/packaging/md2html, revision 1.1.1.1
1.1 misho 1: #!/usr/bin/env python3
2:
3: # Copyright (C) 2020 Wayne Davison
4: #
5: # This program is freely redistributable.
6:
7: import os, re, argparse
8:
9: HTML_START = """\
10: <html><head>
11: <title>%s</title>
12: <link href="https://fonts.googleapis.com/css2?family=Roboto&family=Roboto+Mono&display=swap" rel="stylesheet">
13: <style>
14: body {
15: max-width: 50em;
16: margin: auto;
17: }
18: body, b, strong, u {
19: font-family: 'Roboto', sans-serif;
20: }
21: code {
22: font-family: 'Roboto Mono', monospace;
23: font-weight: bold;
24: }
25: pre code {
26: display: block;
27: font-weight: normal;
28: }
29: blockquote pre code {
30: background: #f1f1f1;
31: }
32: dd p:first-of-type {
33: margin-block-start: 0em;
34: }
35: table {
36: border-color: grey;
37: border-spacing: 0;
38: }
39: tr {
40: border-top: 1px solid grey;
41: }
42: tr:nth-child(2n) {
43: background-color: #f6f8fa;
44: }
45: th, td {
46: border: 1px solid #dfe2e5;
47: text-align: center;
48: padding-left: 1em;
49: padding-right: 1em;
50: }
51: </style>
52: </head><body>
53: """
54:
55: HTML_END = """\
56: </body></html>
57: """
58:
59: md_parser = None
60:
61: def main():
62: for mdfn in args.mdfiles:
63: if not mdfn.endswith('.md'):
64: print('Ignoring non-md input file:', mdfn)
65: continue
66: title = re.sub(r'.*/', '', mdfn).replace('.md', '')
67: htfn = mdfn.replace('.md', '.html')
68:
69: print("Parsing", mdfn, '->', htfn)
70:
71: with open(mdfn, 'r', encoding='utf-8') as fh:
72: txt = fh.read()
73:
74: txt = re.sub(r'\s--\s', '\xa0-- ', txt)
75:
76: html = md_parser(txt)
77:
78: html = re.sub(r'(?<!<pre>)(<code>)([\s\S]*?)(</code>)', lambda m: m[1] + re.sub(r'\s', '\xa0', m[2]) + m[3], html)
79: html = html.replace('--', '‑‑').replace("\xa0-", ' ‑').replace("\xa0", ' ')
80: html = re.sub(r'(\W)-', r'\1‑', html)
81:
82: if os.path.lexists(htfn):
83: os.unlink(htfn)
84:
85: with open(htfn, 'w', encoding='utf-8') as fh:
86: fh.write(HTML_START % title)
87: fh.write(html)
88: fh.write(HTML_END)
89:
90:
91: if __name__ == '__main__':
92: parser = argparse.ArgumentParser(description='Output html for md pages.', add_help=False)
93: parser.add_argument("--help", "-h", action="help", help="Output this help message and exit.")
94: parser.add_argument("mdfiles", nargs='+', help="The .md files to turn into .html files.")
95: args = parser.parse_args()
96:
97: try:
98: import cmarkgfm
99: # Our NEWS.md file has a gfm table in it.
100: md_parser = cmarkgfm.github_flavored_markdown_to_html
101: except:
102: die("Failed to find cmarkgfm for python3.")
103:
104: main()
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>