#!/usr/bin/python3 # Copyright (C) 2020 Wayne Davison # # This program is freely redistributable. import re, argparse HTML_START = """\ %s """ HTML_END = """\ """ 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'()([\s\S]*?)()', lambda m: m[1] + re.sub(r'\s', '\xa0', m[2]) + m[3], html) html = html.replace('--', '‑‑').replace("\xa0-", ' ‑').replace("\xa0", ' ') html = re.sub(r'(\W)-', r'\1‑', html) with open(htfn, 'w', encoding='utf-8') as fh: fh.write(HTML_START % title) fh.write(html) fh.write(HTML_END) def html_via_cmarkgfm(txt): return cmarkgfm.markdown_to_html(txt) def html_via_commonmark(txt): return commonmark.HtmlRenderer().render(commonmark.Parser().parse(txt)) 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 md_parser = html_via_cmarkgfm except: try: import commonmark md_parser = html_via_commonmark except: die("Failed to find cmarkgfm or commonmark for python3.") main()