51 lines
2.3 KiB
Python
51 lines
2.3 KiB
Python
"""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"""<!doctype html><html lang="fa" dir="rtl"><head><meta charset="utf-8">
|
|
<title>{escape(destination)}</title><style>
|
|
@page {{ size: A4; margin: 16mm 16mm 18mm; }}
|
|
body {{ font-family: Tahoma, Arial, sans-serif; font-size: 10.5pt; line-height: 1.8; color: #20242a; }}
|
|
h1 {{ font-size: 18pt; margin: 0 0 14pt; }}
|
|
h2 {{ font-size: 13pt; margin: 16pt 0 6pt; break-after: avoid; }}
|
|
p, li {{ text-align: right; }}
|
|
ul {{ padding-right: 22pt; }}
|
|
pre {{ direction: ltr; text-align: left; background: #f3f5f7; border: 1px solid #dce0e3;
|
|
padding: 8pt; white-space: pre-wrap; overflow-wrap: anywhere; font: 8.5pt Consolas, monospace; }}
|
|
code {{ direction: ltr; unicode-bidi: isolate; font-family: Consolas, monospace; font-size: 9pt; }}
|
|
pre code {{ font-size: inherit; }}
|
|
h1, h2, pre {{ break-inside: avoid; }}
|
|
</style></head><body>{content}</body></html>"""
|
|
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")
|
|
render("UPDATE.md", "UPDATE.pdf")
|