diff options
| author | Craig Jennings <c@cjennings.net> | 2026-06-08 22:08:47 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2026-06-08 22:08:47 -0500 |
| commit | 634bebcd5e8e7ade1a54f1d2aa1596487bf472ad (patch) | |
| tree | 06e0625e19788cc88eca22c0a5d9390effb5c2ab /scripts/theme-studio/generate.py | |
| parent | 78269dae7dd445425e5ec0863b65ca768a4f76a3 (diff) | |
| download | dotemacs-634bebcd5e8e7ade1a54f1d2aa1596487bf472ad.tar.gz dotemacs-634bebcd5e8e7ade1a54f1d2aa1596487bf472ad.zip | |
test(theme-studio): make generate.py importable and test the templating
The page generator's risky logic, the export-strip that inlines colormath.js and the placeholder substitution that fills in the sample and palette data, had no tests. A bug there ships a broken theme-studio.html the JS tests can't see, since they import colormath.js directly and never look at the assembled page.
I pulled the strip into a strip_exports function and guarded the file write behind __main__, so importing generate.py builds the page in memory without writing it. test_generate.py asserts the strip removes export lines, preserves the body, and rstrips. It asserts the assembled page has every placeholder filled and carries the colormath body verbatim. And it guards that colormath.js keeps its export on a single line, since the line-based strip would leave a multi-line export's continuation behind. That was the exact failure that bit during the first integration.
Diffstat (limited to 'scripts/theme-studio/generate.py')
| -rw-r--r-- | scripts/theme-studio/generate.py | 27 |
1 files changed, 18 insertions, 9 deletions
diff --git a/scripts/theme-studio/generate.py b/scripts/theme-studio/generate.py index e2d72acd..e6926e04 100644 --- a/scripts/theme-studio/generate.py +++ b/scripts/theme-studio/generate.py @@ -1,13 +1,20 @@ import json, os HERE=os.path.dirname(os.path.abspath(__file__)) + +def strip_exports(src): + """Drop ES-module `export` lines so the body loads as a classic <script>. + + A top-level `export` is a syntax error outside a module, so it must go before + the body is spliced into the page. test-colormath.mjs applies the identical + strip and asserts the page carries the result verbatim (inline-integrity), so + the two copies cannot drift. NOTE: this is line-based — the export statement in + colormath.js must stay on a single line or the continuation lines survive. + """ + return '\n'.join(l for l in src.splitlines() if not l.startswith('export')).rstrip() + # Pure color-math core, inlined verbatim into the page so the browser runs the -# same code the Node tests import (one source of truth). Strip the ES-module -# `export` line(s) — a top-level export is a syntax error in a classic <script>. -# test-colormath.mjs applies the identical strip and asserts the page carries this -# body verbatim (inline-integrity), so the two copies cannot drift. -COLORMATH_BODY='\n'.join( - l for l in open(os.path.join(HERE,'colormath.js')).read().splitlines() - if not l.startswith('export')).rstrip() +# same code the Node tests import (one source of truth). +COLORMATH_BODY=strip_exports(open(os.path.join(HERE,'colormath.js')).read()) ns={} src=open(os.path.join(HERE,'samples.py')).read() exec(src[:src.index('cols=')], ns) @@ -1269,5 +1276,7 @@ HTML=(HTML.replace("COLORMATH_J",COLORMATH_BODY) .replace("UIFACES_J",json.dumps(UI_FACES)).replace("UIMAP_J",json.dumps(UIMAP)).replace("APPS_J",json.dumps(APPS)) .replace("BOLD_J",json.dumps(BOLD)).replace("MAP_J",json.dumps(MAP))) OUT=os.path.join(HERE,'theme-studio.html') -open(OUT,"w").write(HTML) -print("wrote",OUT) + +if __name__=='__main__': + open(OUT,"w").write(HTML) + print("wrote",OUT) |
