"""Emit render/index.html — loads vendored mermaid, renders every diagram,
and exposes the resulting SVG strings on window.__SVGS for harvesting."""
from diagrams import DIAGRAMS
HERE = Path(__file__).parent
RENDER.mkdir(exist_ok=True)
[{"id": d["id"], "keyword": d["keyword"], "code": d["code"]} for d in DIAGRAMS]
html = """<!doctype html>
<!-- FontAwesome Free so inline `fas:`/`fab:` tokens paint as real icons in the
harvested SVGs (mermaid emits <i class="fas fa-…">, FA's font paints it).
Vendor fontawesome/ next to this file for an offline build; the CDN link is
<link rel="stylesheet" href="fontawesome/css/all.min.css"
onerror="this.onerror=null;this.href='https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@7/css/all.min.css'">
body { font: 14px system-ui, sans-serif; margin: 24px; background:#fff; }
.item { margin: 0 0 40px; padding: 16px; border: 1px solid #e2e8f0; border-radius: 10px; }
.item h2 { font-size: 15px; margin: 0 0 12px; color:#0f172a; }
.err { color:#b91c1c; font-family: monospace; white-space: pre-wrap; }
<script src="mermaid.min.js"></script>
const DIAGRAMS = __PAYLOAD__;
const root = document.getElementById('root');
// Monotone-first house theme: neutral node fills, dark text, grey edges. Colour
// never appears as a default — only where a diagram opts in with `style … fill:`.
flowchart: { htmlLabels: true, useMaxWidth: false },
background: '#ffffff', primaryColor: '#ffffff', primaryTextColor: '#1b2431',
primaryBorderColor: '#c6cbd3', secondaryColor: '#f1f5f9', tertiaryColor: '#ffffff',
lineColor: '#586372', textColor: '#1b2431',
clusterBkg: '#f8fafc', clusterBorder: '#c6cbd3', titleColor: '#1b2431',
noteBkgColor: '#f1f5f9', noteTextColor: '#1b2431', noteBorderColor: '#c6cbd3',
actorBkg: '#ffffff', actorBorder: '#c6cbd3', actorTextColor: '#1b2431',
for (const d of DIAGRAMS) {
const item = document.createElement('div');
const h = document.createElement('h2');
h.textContent = d.id + ' (' + d.keyword + ')';
const holder = document.createElement('div');
item.appendChild(holder);
const { svg } = await mermaid.render('m_' + d.id, d.code);
window.__SVGS[d.id] = svg;
const msg = (e && e.message) ? e.message : String(e);
window.__ERRORS[d.id] = msg;
const pre = document.createElement('div');
pre.textContent = 'RENDER ERROR: ' + msg;
document.title = 'done:' + Object.keys(window.__SVGS).length + ' err:' + Object.keys(window.__ERRORS).length;
await fetch('/save', { method: 'POST', body: JSON.stringify(window.__SVGS) });
} catch (e) { window.__saved = 'error: ' + e; }
html = html.replace("__PAYLOAD__", payload)
(RENDER / "index.html").write_text(html, encoding="utf-8")
print(f"Wrote {RENDER/'index.html'} with {len(DIAGRAMS)} diagrams")