#!/usr/bin/env python3
"""Build web/index.html: web/index.src.html + web/engine.js + templates/ inlined.
The result is one self-contained file: open it from disk or put it on any
static host. Run after changing templates/ or web/.
"""
import json
import pathlib
root = pathlib.Path(__file__).resolve().parent.parent
templates = {
f"{p.parent.name}/{p.name}": p.read_text(encoding="utf-8")
for p in sorted((root / "templates").glob("*/*.yaml"))
}
engine = (root / "web" / "engine.js").read_text(encoding="utf-8")
page = (root / "web" / "index.src.html").read_text(encoding="utf-8")
tpl_json = json.dumps(templates, ensure_ascii=False, indent=1).replace("", "<\\/")
page = page.replace("/*__ENGINE__*/", engine).replace("/*__TEMPLATES__*/", tpl_json)
(root / "web" / "index.html").write_text(page, encoding="utf-8")
print(f"web/index.html: {len(templates)} templates")