#!/usr/bin/env python3 # Copyright (C) 2020 Wayne Davison # # This program is freely redistributable. import os, 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) 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()