#!/usr/bin/env python3
"""Build docs/mockup.html: web/index.html with its stylesheet, a site.json
and app.js inlined, so it opens without a server.
python3 scripts/build_mockup.py dist/data/site.json
"""
import base64
import json
import re
import sys
from pathlib import Path
root = Path(__file__).resolve().parent.parent
web = root / "web"
data = json.loads(Path(sys.argv[1]).read_text(encoding="utf-8"))
html = (web / "index.html").read_text(encoding="utf-8")
css = (web / "style.css").read_text(encoding="utf-8")
js = (web / "app.js").read_text(encoding="utf-8")
# "" can't appear inside an inline script; JSON allows "<\/".
payload = json.dumps(data, separators=(",", ":")).replace("", "<\\/")
link = ''
script = ''
assert link in html and script in html, "index.html changed shape"
# The worker fills index.html's meta placeholders; the mockup only needs them
# gone.
html = re.sub(r"\{\{\w+\}\}", "", html)
# The link icons the stylesheet loads, inlined so the mockup needs no files.
css = re.sub(
r'url\("icons/(\w+)\.svg"\)',
lambda m: 'url("data:image/svg+xml;base64,'
+ base64.b64encode((web / "icons" / f"{m.group(1)}.svg").read_bytes()).decode()
+ '")',
css,
)
html = html.replace(link, f"")
html = html.replace(script, f"\n ")
(root / "docs" / "mockup.html").write_text(html, encoding="utf-8")