File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / rsync / packaging / md2html
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Wed Mar 17 00:32:36 2021 UTC (3 years, 7 months ago) by misho
Branches: rsync, MAIN
CVS tags: v3_2_3, HEAD
rsync 3.2.3

#!/usr/bin/env python3

# Copyright (C) 2020 Wayne Davison
#
# This program is freely redistributable.

import os, re, argparse

HTML_START = """\
<html><head>
<title>%s</title>
<link href="https://fonts.googleapis.com/css2?family=Roboto&family=Roboto+Mono&display=swap" rel="stylesheet">
<style>
body {
  max-width: 50em;
  margin: auto;
}
body, b, strong, u {
  font-family: 'Roboto', sans-serif;
}
code {
  font-family: 'Roboto Mono', monospace;
  font-weight: bold;
}
pre code {
  display: block;
  font-weight: normal;
}
blockquote pre code {
  background: #f1f1f1;
}
dd p:first-of-type {
  margin-block-start: 0em;
}
table {
  border-color: grey;
  border-spacing: 0;
}
tr {
  border-top: 1px solid grey;
}
tr:nth-child(2n) {
  background-color: #f6f8fa;
}
th, td {
  border: 1px solid #dfe2e5;
  text-align: center;
  padding-left: 1em;
  padding-right: 1em;
}
</style>
</head><body>
"""

HTML_END = """\
</body></html>
"""

md_parser = None

def main():
    for mdfn in args.mdfiles:
        if not mdfn.endswith('.md'):
            print('Ignoring non-md input file:', mdfn)
            continue
        title = re.sub(r'.*/', '', mdfn).replace('.md', '')
        htfn = mdfn.replace('.md', '.html')

        print("Parsing", mdfn, '->', htfn)

        with open(mdfn, 'r', encoding='utf-8') as fh:
            txt = fh.read()

        txt = re.sub(r'\s--\s', '\xa0-- ', txt)

        html = md_parser(txt)

        html = re.sub(r'(?<!<pre>)(<code>)([\s\S]*?)(</code>)', lambda m: m[1] + re.sub(r'\s', '\xa0', m[2]) + m[3], html)
        html = html.replace('--', '&#8209;&#8209;').replace("\xa0-", '&nbsp;&#8209;').replace("\xa0", '&nbsp;')
        html = re.sub(r'(\W)-', r'\1&#8209;', html)

        if os.path.lexists(htfn):
            os.unlink(htfn)

        with open(htfn, 'w', encoding='utf-8') as fh:
            fh.write(HTML_START % title)
            fh.write(html)
            fh.write(HTML_END)


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Output html for md pages.', add_help=False)
    parser.add_argument("--help", "-h", action="help", help="Output this help message and exit.")
    parser.add_argument("mdfiles", nargs='+', help="The .md files to turn into .html files.")
    args = parser.parse_args()

    try:
        import cmarkgfm
        # Our NEWS.md file has a gfm table in it.
        md_parser = cmarkgfm.github_flavored_markdown_to_html
    except:
        die("Failed to find cmarkgfm for python3.")

    main()

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>