import re import tarfile import io import sys from pathlib import Path import argparse import jinja2 import requests MDTOREVEAL_CSS = """ """ MDTOREVEAL_JS = """ Reveal.initialize({ controls: true, progress: true, center: true, hash: true, disableLayout: false, plugins: [ RevealZoom, RevealNotes, RevealSearch, RevealMarkdown, RevealHighlight ] }); """ TPL = """ Prez
{% for section in slides %}
{% for slide in section %}
{% endfor%}
{% endfor %}
""" def parse_args(): parser = argparse.ArgumentParser() parser.add_argument("md", help="Markdown input file") parser.add_argument("-o", "--output", dest="html", help="HTML output file") return parser.parse_args() REVEAL_JS_VERSION = "4.1.0" REVEAL_ARCHIVE = ( f"https://github.com/hakimel/reveal.js/archive/{REVEAL_JS_VERSION}.tar.gz" ) def main(): args = parse_args() if not args.html: args.html = args.md.replace(".md", ".html") if args.md == args.html: print( "You did not specified an output file, and I failed to guess it, " "please use the --output option" ) sys.exit(1) tpl = jinja2.Template(TPL) root = Path(args.html).resolve().parent (root / "mdtoreveal.css").write_text(MDTOREVEAL_CSS) (root / "mdtoreveal.js").write_text(MDTOREVEAL_JS) reveal_dir = f"reveal.js-{REVEAL_JS_VERSION}" reveal_path = root / reveal_dir if not reveal_path.exists(): tarball = io.BytesIO(requests.get(REVEAL_ARCHIVE).content) tarfile.open(fileobj=tarball, mode="r:gz").extractall(root) with open(args.md) as f: md = f.read() sections = [] for section in re.split("^# ", md, flags=re.M): if not section: continue slides = [] for slide in re.split("^## ", section, flags=re.M): slide = re.sub("^::: notes$", '", slide, flags=re.M) slides.append("## " + slide) sections.append(slides) with open(args.html, "w") as f: f.write(tpl.render(slides=sections, revealjs_url=reveal_dir)) if __name__ == "__main__": main()