"""Pré-rend les diagrammes Mermaid du guide intégré en PNG (#105). Scanne les blocs ``
`` de frontend/index.html, extrait leur code, et appelle scripts/render_guide_diagram.mjs (Chromium + mermaid v11) pour produire ``backend/assets/guide_diagrams/.png``. Ces PNG sont commités : le PDF du guide les embarque comme vraies images (WeasyPrint ne sait pas exécuter Mermaid). Usage : python scripts/build_guide_diagrams.py """ import hashlib import html import json import re import subprocess import sys import tempfile from pathlib import Path ROOT = Path(__file__).resolve().parent.parent ASSETS = ROOT / "backend" / "assets" / "guide_diagrams" def extract_mermaid() -> dict[str, str]: page_html = (ROOT / "frontend" / "index.html").read_text(encoding="utf-8") out: dict[str, str] = {} for m in re.finditer( r' ', page_html, re.DOTALL, ): code = m.group(1) code = html.unescape(code).strip() sha = hashlib.sha1(code.encode("utf-8")).hexdigest()[:16] out[sha] = code return out def main() -> int: jobs = extract_mermaid() if not jobs: print("aucun bloc mermaid dans index.html") return 1 todo = {k: v for k, v in jobs.items() if not (ASSETS / f"{k}.png").exists()} print("diagrammes:", len(jobs), "| à rendre:", len(todo)) if not todo: return 0 with tempfile.NamedTemporaryFile("w", suffix=".json", delete=False, encoding="utf-8") as f: json.dump(todo, f) path = f.name r = subprocess.run( ["node", str(ROOT / "scripts" / "render_guide_diagram.mjs"), path], cwd=ROOT, capture_output=True, check=False, text=True, timeout=300, ) print(r.stdout[-2000:]) if r.returncode != 0: print(r.stderr[-2000:]) return r.returncode if __name__ == "__main__": sys.exit(main())(.*?)