"""Build the HTML and PDF versions of the report from ``report/paper.md``. Run on a machine that has WeasyPrint and a CJK font installed:: python scripts/build_report.py The HTML is self-contained apart from the figure files (``report/figures/``), so it can be opened directly in a browser or printed to PDF by hand. """ from __future__ import annotations import shutil import sys from pathlib import Path import markdown from weasyprint import CSS, HTML ROOT = Path(__file__).resolve().parents[1] REPORT = ROOT / "report" SRC = REPORT / "paper.md" OUT_HTML = REPORT / "paper.html" OUT_PDF = REPORT / "paper.pdf" FIG_SRC = ROOT / "results" / "figures" FIG_DST = REPORT / "figures" # The text uses LaTeX only inside the first sections; converting those few # expressions to Unicode keeps the PDF readable without a JS math renderer. MATH = { r"$\mathrm{m^{-2}}$": "m⁻²", r"$10^{16}\,\mathrm{m^{-2}}$": "10¹⁶ m⁻²", r"$5\times10^{-4}$": "5×10⁻⁴", r"$33\times401=13233$": "33 × 401 = 13233", r"$T=401$": "T = 401", r"$R^2=1$": "R² = 1", r"$R^2=0$": "R² = 0", r"$R^2<0$": "R² < 0", r"$\sigma$": "σ", r"$c$": "c", r"$\delta$": "δ", r"$\chi$": "χ", r"$\Delta H_{\mathrm{mix}}$": "ΔH_mix", r"$\Delta S_{\mathrm{mix}}$": "ΔS_mix", r"$\rho_{\mathrm{total}}$": "ρ_total", r"$\rho_{\mathrm{twin}}$": "ρ_twin", r"$\varepsilon_{1:T}$": "ε(1..T)", r"$\xi_{\mathrm{BCC}}, \xi_{\mathrm{FCC}}, \xi_{\mathrm{HCP}}, \xi_{\mathrm{other}}$": "ξ_BCC, ξ_FCC, ξ_HCP, ξ_other", r"$\rho_{1/2},\rho_{a/2},\rho_{a/3},\rho_{\mathrm{edge}},\rho_{\mathrm{screw}},\rho_{\mathrm{perfect}},\rho_{\mathrm{other}}$": "ρ_1/2, ρ_a/2, ρ_a/3, ρ_edge, ρ_screw, ρ_perfect, ρ_other", } CSS_TEXT = r""" @page { size: A4; margin: 20mm 18mm 18mm 18mm; @bottom-center { content: counter(page); font-family: "Noto Serif CJK SC", serif; font-size: 9pt; color: #555; } @top-center { content: string(doctitle); font-family: "Noto Serif CJK SC", serif; font-size: 8.5pt; color: #777; } } @page :first { @top-center { content: none; } } html { font-size: 10.5pt; } body { font-family: "Noto Serif CJK SC", "Songti SC", serif; line-height: 1.78; color: #111; text-align: left; max-width: 820px; margin: 0 auto; padding: 0 8px; } h1 { font-size: 19pt; line-height: 1.45; text-align: center; margin: 0 0 6px 0; font-weight: 700; string-set: doctitle content(); } h2 { font-size: 13.5pt; margin: 22px 0 8px 0; font-weight: 700; border-bottom: 1.2px solid #333; padding-bottom: 3px; page-break-after: avoid; } h3 { font-size: 11.5pt; margin: 16px 0 6px 0; font-weight: 700; page-break-after: avoid; } p { margin: 0 0 8px 0; text-indent: 2em; } p.eq { text-align: center; text-indent: 0; margin: 9px 0 11px 0; } p.eq sub, td sub { font-size: 68%; } p:has(> img) { text-indent: 0; text-align: center; margin: 12px 0 4px 0; } img { max-width: 96%; height: auto; } table { border-collapse: collapse; width: 100%; margin: 10px 0 6px 0; font-size: 8.6pt; line-height: 1.45; page-break-inside: avoid; break-before: avoid; /* keep the caption above the table on the same page */ } th, td { border: 0.5pt solid #999; padding: 3px 5px; text-align: center; vertical-align: middle; } th { background: #f0f0f0; font-weight: 700; } td:first-child, th:first-child { text-align: left; } tr:nth-child(even) td { background: #fafafa; } code { font-family: "DejaVu Sans Mono", monospace; font-size: 8.6pt; background: #f4f4f4; padding: 0 2px; } pre { background: #f6f6f6; border: 0.5pt solid #ddd; padding: 6px 8px; font-size: 8.2pt; line-height: 1.4; white-space: pre-wrap; page-break-inside: avoid; } pre code { background: none; } blockquote { margin: 8px 0; padding-left: 10px; border-left: 3px solid #ccc; color: #444; } hr { border: none; border-top: 0.6pt solid #ccc; margin: 16px 0; } strong { font-weight: 700; } #TOC { background: #f7f7f7; border: 0.5pt solid #ddd; padding: 8px 14px; margin: 14px 0 18px 0; } #TOC ul { margin: 2px 0; padding-left: 18px; } #TOC li { margin: 1px 0; font-size: 9.2pt; } #TOC a { color: #111; text-decoration: none; } a { color: #1a4f8b; text-decoration: none; } """ TEMPLATE = """ {title} {body} """ def main() -> int: if not SRC.exists(): print(f"missing {SRC}") return 1 text = SRC.read_text(encoding="utf-8") for k, v in MATH.items(): text = text.replace(k, v) left = text.count("$") if left: print(f"warning: {left} '$' characters remain (unconverted LaTeX?)") body = markdown.markdown( text, extensions=["tables", "fenced_code", "toc", "sane_lists"], extension_configs={"toc": {"title": "目录", "toc_depth": "2-2"}}) doc = TEMPLATE.format(title="多合金 MD 轨迹的应力–应变预测", css=CSS_TEXT, body=body) OUT_HTML.write_text(doc, encoding="utf-8") if FIG_SRC.exists(): FIG_DST.mkdir(exist_ok=True) for f in FIG_SRC.glob("*.png"): shutil.copy2(f, FIG_DST / f.name) HTML(string=doc, base_url=str(REPORT)).write_pdf( str(OUT_PDF), stylesheets=[CSS(string=CSS_TEXT)]) print(f"html {OUT_HTML.stat().st_size/1024:.0f} KB, " f"pdf {OUT_PDF.stat().st_size/1024:.0f} KB") return 0 if __name__ == "__main__": sys.exit(main())