Wire sexp.js into page template with auto-init and HTMX integration
- Load sexp.js in ~app-layout before body.js - Auto-process <script type="text/sexp"> tags on DOMContentLoaded - Re-process after htmx:afterSwap for dynamic content - Sexp.mount(target, expr, env) for rendering into DOM elements - Sexp.processScripts() picks up data-components and data-mount tags - client_components_tag() Python helper serializes Component objects back to sexp source for client-side consumption - 37 parity tests all passing Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -201,6 +201,40 @@ def _get_request_context():
|
||||
# Quart integration
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def client_components_tag(*names: str) -> str:
|
||||
"""Emit a <script type="text/sexp"> tag with component definitions.
|
||||
|
||||
Reads the source definitions from loaded .sexpr files and sends them
|
||||
to the client so sexp.js can render them identically.
|
||||
|
||||
Usage in Python::
|
||||
|
||||
body_end_html = client_components_tag("test-filter-card", "test-row")
|
||||
|
||||
Or send all loaded components::
|
||||
|
||||
body_end_html = client_components_tag()
|
||||
"""
|
||||
from .parser import serialize
|
||||
parts = []
|
||||
for key, val in _COMPONENT_ENV.items():
|
||||
if not isinstance(val, Component):
|
||||
continue
|
||||
if names and val.name not in names and key.lstrip("~") not in names:
|
||||
continue
|
||||
# Reconstruct defcomp source from the Component object
|
||||
param_strs = ["&key"] + list(val.params)
|
||||
if val.has_children:
|
||||
param_strs.extend(["&rest", "children"])
|
||||
params_sexp = "(" + " ".join(param_strs) + ")"
|
||||
body_sexp = serialize(val.body, pretty=True)
|
||||
parts.append(f"(defcomp ~{val.name} {params_sexp} {body_sexp})")
|
||||
if not parts:
|
||||
return ""
|
||||
source = "\n".join(parts)
|
||||
return f'<script type="text/sexp" data-components>{source}</script>'
|
||||
|
||||
|
||||
def setup_sexp_bridge(app: Any) -> None:
|
||||
"""Register s-expression helpers with a Quart app's Jinja environment.
|
||||
|
||||
|
||||
@@ -70,6 +70,7 @@
|
||||
(when content-html (raw! content-html))
|
||||
(div :class "pb-8"))))))
|
||||
(when body-end-html (raw! body-end-html))
|
||||
(script :src (str asset-url "/scripts/sexp.js"))
|
||||
(script :src (str asset-url "/scripts/body.js")))))))
|
||||
|
||||
(defcomp ~oob-response (&key oobs-html filter-html aside-html menu-html content-html)
|
||||
|
||||
@@ -133,6 +133,36 @@ class TestComponents:
|
||||
assert html == '<div><span>hi</span></div>'
|
||||
|
||||
|
||||
class TestClientComponentsTag:
|
||||
"""client_components_tag() generates valid sexp for JS consumption."""
|
||||
|
||||
def test_emits_script_tag(self):
|
||||
from shared.sexp.jinja_bridge import client_components_tag, register_components, _COMPONENT_ENV
|
||||
# Register a test component
|
||||
register_components('(defcomp ~test-cct (&key label) (span label))')
|
||||
try:
|
||||
tag = client_components_tag("test-cct")
|
||||
assert tag.startswith('<script type="text/sexp" data-components>')
|
||||
assert tag.endswith('</script>')
|
||||
assert "defcomp ~test-cct" in tag
|
||||
finally:
|
||||
_COMPONENT_ENV.pop("~test-cct", None)
|
||||
|
||||
def test_roundtrip_through_js(self):
|
||||
"""Component emitted by client_components_tag renders identically in JS."""
|
||||
from shared.sexp.jinja_bridge import client_components_tag, register_components, _COMPONENT_ENV
|
||||
register_components('(defcomp ~test-rt (&key title) (div :class "rt" title))')
|
||||
try:
|
||||
tag = client_components_tag("test-rt")
|
||||
# Extract the sexp source from the script tag
|
||||
sexp_source = tag.replace('<script type="text/sexp" data-components>', '').replace('</script>', '')
|
||||
js_html = _js_render('(~test-rt :title "hello")', sexp_source)
|
||||
py_html = py_render(parse('(~test-rt :title "hello")'), _COMPONENT_ENV)
|
||||
assert js_html == py_html
|
||||
finally:
|
||||
_COMPONENT_ENV.pop("~test-rt", None)
|
||||
|
||||
|
||||
class TestPythonParity:
|
||||
"""JS string renderer matches Python renderer output."""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user