"""Render the installation handbooks from their Markdown sources using Chrome."""
from pathlib import Path
from html import escape
import subprocess
import tempfile
import markdown
ROOT = Path(__file__).resolve().parent
CHROME = Path(r"C:\Program Files\Google\Chrome\Application\chrome.exe")
if not CHROME.exists():
CHROME = Path(r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe")
def render(source: str, destination: str) -> None:
body = (ROOT / source).read_text(encoding="utf-8")
content = markdown.markdown(body, extensions=["fenced_code", "tables"])
html = f"""
{escape(destination)}{content}"""
with tempfile.TemporaryDirectory() as temp:
page = Path(temp) / "guide.html"
page.write_text(html, encoding="utf-8")
output = ROOT / destination
command = [str(CHROME), "--headless", "--disable-gpu", "--no-first-run",
"--no-default-browser-check", "--disable-extensions", "--no-pdf-header-footer",
f"--user-data-dir={Path(temp) / 'profile'}", f"--print-to-pdf={output}", page.as_uri()]
subprocess.run(command, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
if not output.is_file() or output.stat().st_size < 1000:
raise RuntimeError(f"PDF was not created: {output}")
print(output)
if __name__ == "__main__":
render("INSTALL.md", "INSTALL.pdf")
render("INSTALL-INTERNAL.md", "INSTALL-INTERNAL.pdf")