Skip to content

Instantly share code, notes, and snippets.

@Yepoleb
Created April 20, 2019 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yepoleb/53f8ead3b0f43cbd4cf93565df75b54b to your computer and use it in GitHub Desktop.
Save Yepoleb/53f8ead3b0f43cbd4cf93565df75b54b to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import mistune
import sys
import os.path
import tempfile
import subprocess
XSL_PATH = os.path.expanduser("~/Coden/xml/outline-tmpl.xsl")
TEMPLATE = \
"""<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>{title}</title>
<style>
body {{
font-family: Ubuntu;
}}
</style>
</head>
<body>
{content}
</body>
</html>
"""
filename_md = sys.argv[1]
filebase = os.path.splitext(filename_md)[0]
filename_pdf = filebase + ".pdf"
with open(filename_md, "r") as md_file:
md_cont = md_file.read()
html_cont = mistune.markdown(md_cont)
html_data = TEMPLATE.format(title=filebase, content=html_cont)
with tempfile.NamedTemporaryFile("w", suffix=".html") as html_file:
html_file.write(html_data)
html_file.flush()
subprocess.run([
"wkhtmltopdf",
"toc", "--xsl-style-sheet", XSL_PATH,
html_file.name, filename_pdf])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment