Something went wrong. Try again.
unified development activity
Something went wrong. Try again.
1.5 kB · 40 lines
Python
at main
1234567891011121314151617181920212223242526272829303132333435363738394041#!/usr/bin/env python3"""Build docs/mockup.html: web/index.html with its stylesheet, a site.jsonand app.js inlined, so it opens without a server.
python3 scripts/build_mockup.py dist/data/site.json"""
import base64import jsonimport reimport sysfrom pathlib import Path
root = Path(__file__).resolve().parent.parentweb = 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 = '<link rel="stylesheet" href="style.css">'script = '<script src="app.js"></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"<style>{css}</style>")html = html.replace(script, f"<script>window.__TALLY_DATA__={payload};</script>\n <script>{js}</script>")(root / "docs" / "mockup.html").write_text(html, encoding="utf-8")