Send all responses as sexp wire format with client-side rendering
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 5m35s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 5m35s
- Server sends sexp source text, client (sexp.js) renders everything - SexpExpr marker class for nested sexp composition in serialize() - sexp_page() HTML shell with data-mount="body" for full page loads - sexp_response() returns text/sexp for OOB/partial responses - ~app-body layout component replaces ~app-layout (no raw!) - ~rich-text is the only component using raw! (for CMS HTML content) - Fragment endpoints return text/sexp, auto-wrapped in SexpExpr - All _*_html() helpers converted to _*_sexp() returning sexp source - Head auto-hoist: sexp.js moves meta/title/link/script[ld+json] from rendered body to document.head automatically - Unknown components render warning box instead of crashing page - Component kwargs preserve AST for lazy rendering (fixes <> in kwargs) - Fix unterminated paren in events/sexp/tickets.sexpr Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
"""Events app fragment endpoints.
|
||||
|
||||
Exposes HTML fragments at ``/internal/fragments/<type>`` for consumption
|
||||
Exposes sexp fragments at ``/internal/fragments/<type>`` for consumption
|
||||
by other coop apps via the fragment client.
|
||||
"""
|
||||
|
||||
@@ -19,6 +19,9 @@ def register():
|
||||
|
||||
_handlers: dict[str, object] = {}
|
||||
|
||||
# Fragment types that still return HTML (Jinja templates)
|
||||
_html_types = {"container-cards", "account-page"}
|
||||
|
||||
@bp.before_request
|
||||
async def _require_fragment_header():
|
||||
if not request.headers.get(FRAGMENT_HEADER):
|
||||
@@ -28,16 +31,17 @@ def register():
|
||||
async def get_fragment(fragment_type: str):
|
||||
handler = _handlers.get(fragment_type)
|
||||
if handler is None:
|
||||
return Response("", status=200, content_type="text/html")
|
||||
html = await handler()
|
||||
return Response(html, status=200, content_type="text/html")
|
||||
return Response("", status=200, content_type="text/sexp")
|
||||
result = await handler()
|
||||
ct = "text/html" if fragment_type in _html_types else "text/sexp"
|
||||
return Response(result, status=200, content_type=ct)
|
||||
|
||||
# --- container-nav fragment: calendar entries + calendar links -----------
|
||||
|
||||
async def _container_nav_handler():
|
||||
from quart import current_app
|
||||
from shared.infrastructure.urls import events_url
|
||||
from shared.sexp.jinja_bridge import render as render_comp
|
||||
from shared.sexp.helpers import sexp_call
|
||||
|
||||
container_type = request.args.get("container_type", "page")
|
||||
container_id = int(request.args.get("container_id", 0))
|
||||
@@ -49,7 +53,7 @@ def register():
|
||||
|
||||
styles = current_app.jinja_env.globals.get("styles", {})
|
||||
nav_class = styles.get("nav_button_less_pad", "")
|
||||
html_parts = []
|
||||
parts = []
|
||||
|
||||
# Calendar entries nav
|
||||
if not any(e.startswith("calendar") for e in excludes):
|
||||
@@ -65,21 +69,16 @@ def register():
|
||||
date_str = entry.start_at.strftime("%b %d, %Y at %H:%M")
|
||||
if entry.end_at:
|
||||
date_str += f" – {entry.end_at.strftime('%H:%M')}"
|
||||
html_parts.append(render_comp(
|
||||
"calendar-entry-nav",
|
||||
parts.append(sexp_call("calendar-entry-nav",
|
||||
href=events_url(entry_path), name=entry.name,
|
||||
date_str=date_str, nav_class=nav_class,
|
||||
))
|
||||
# Infinite scroll sentinel (kept as raw HTML — HTMX-specific)
|
||||
date_str=date_str, nav_class=nav_class))
|
||||
if has_more and paginate_url_base:
|
||||
html_parts.append(render_comp(
|
||||
"htmx-sentinel",
|
||||
parts.append(sexp_call("htmx-sentinel",
|
||||
id=f"entries-load-sentinel-{page}",
|
||||
hx_get=f"{paginate_url_base}?page={page + 1}",
|
||||
hx_trigger="intersect once",
|
||||
hx_swap="beforebegin",
|
||||
**{"class": "flex-shrink-0 w-1"},
|
||||
))
|
||||
**{"class": "flex-shrink-0 w-1"}))
|
||||
|
||||
# Calendar links nav
|
||||
if not any(e.startswith("calendar") for e in excludes):
|
||||
@@ -88,16 +87,16 @@ def register():
|
||||
)
|
||||
for cal in calendars:
|
||||
href = events_url(f"/{post_slug}/{cal.slug}/")
|
||||
html_parts.append(render_comp(
|
||||
"calendar-link-nav",
|
||||
href=href, name=cal.name, nav_class=nav_class,
|
||||
))
|
||||
parts.append(sexp_call("calendar-link-nav",
|
||||
href=href, name=cal.name, nav_class=nav_class))
|
||||
|
||||
return "\n".join(html_parts)
|
||||
if not parts:
|
||||
return ""
|
||||
return "(<> " + " ".join(parts) + ")"
|
||||
|
||||
_handlers["container-nav"] = _container_nav_handler
|
||||
|
||||
# --- container-cards fragment: entries for blog listing cards ------------
|
||||
# --- container-cards fragment: entries for blog listing cards (still Jinja) --
|
||||
|
||||
async def _container_cards_handler():
|
||||
post_ids_raw = request.args.get("post_ids", "")
|
||||
@@ -107,7 +106,6 @@ def register():
|
||||
if not post_ids:
|
||||
return ""
|
||||
|
||||
# Build post_id -> slug mapping
|
||||
slug_map = {}
|
||||
for i, pid in enumerate(post_ids):
|
||||
slug_map[pid] = post_slugs[i] if i < len(post_slugs) else ""
|
||||
@@ -120,12 +118,12 @@ def register():
|
||||
|
||||
_handlers["container-cards"] = _container_cards_handler
|
||||
|
||||
# --- account-nav-item fragment: tickets + bookings links for account nav -
|
||||
# --- account-nav-item fragment: tickets + bookings links -----------------
|
||||
|
||||
async def _account_nav_item_handler():
|
||||
from quart import current_app
|
||||
from shared.infrastructure.urls import account_url
|
||||
from shared.sexp.jinja_bridge import render as render_comp
|
||||
from shared.sexp.helpers import sexp_call
|
||||
|
||||
styles = current_app.jinja_env.globals.get("styles", {})
|
||||
nav_class = styles.get("nav_button", "")
|
||||
@@ -135,19 +133,15 @@ def register():
|
||||
)
|
||||
tickets_url = account_url("/tickets/")
|
||||
bookings_url = account_url("/bookings/")
|
||||
# These two links use HTMX navigation — kept as raw HTML for the
|
||||
# hx-* attributes that don't map neatly to a reusable component.
|
||||
parts = []
|
||||
for href, label in [(tickets_url, "tickets"), (bookings_url, "bookings")]:
|
||||
parts.append(render_comp(
|
||||
"nav-group-link",
|
||||
href=href, hx_select=hx_select, nav_class=nav_class, label=label,
|
||||
))
|
||||
return "\n".join(parts)
|
||||
parts.append(sexp_call("nav-group-link",
|
||||
href=href, hx_select=hx_select, nav_class=nav_class, label=label))
|
||||
return "(<> " + " ".join(parts) + ")"
|
||||
|
||||
_handlers["account-nav-item"] = _account_nav_item_handler
|
||||
|
||||
# --- account-page fragment: tickets or bookings panel --------------------
|
||||
# --- account-page fragment: tickets or bookings panel (still Jinja) ------
|
||||
|
||||
async def _account_page_handler():
|
||||
slug = request.args.get("slug", "")
|
||||
@@ -171,15 +165,21 @@ def register():
|
||||
|
||||
_handlers["account-page"] = _account_page_handler
|
||||
|
||||
# --- link-card fragment: event page preview card ----------------------------
|
||||
# --- link-card fragment: event page preview card -------------------------
|
||||
|
||||
async def _link_card_handler():
|
||||
from shared.infrastructure.urls import events_url
|
||||
from shared.sexp.jinja_bridge import render as render_comp
|
||||
from shared.sexp.helpers import sexp_call
|
||||
|
||||
slug = request.args.get("slug", "")
|
||||
keys_raw = request.args.get("keys", "")
|
||||
|
||||
def _event_link_card_sexp(post, cal_names: str) -> str:
|
||||
return sexp_call("link-card",
|
||||
title=post.title, image=post.feature_image,
|
||||
subtitle=cal_names,
|
||||
link=events_url(f"/{post.slug}"))
|
||||
|
||||
# Batch mode
|
||||
if keys_raw:
|
||||
slugs = [k.strip() for k in keys_raw.split(",") if k.strip()]
|
||||
@@ -193,11 +193,7 @@ def register():
|
||||
g.s, "page", post.id,
|
||||
)
|
||||
cal_names = ", ".join(c.name for c in calendars) if calendars else ""
|
||||
parts.append(render_comp(
|
||||
"link-card",
|
||||
title=post.title, image=post.feature_image,
|
||||
subtitle=cal_names, link=events_url(f"/{post.slug}"),
|
||||
))
|
||||
parts.append(_event_link_card_sexp(post, cal_names))
|
||||
return "\n".join(parts)
|
||||
|
||||
# Single mode
|
||||
@@ -211,11 +207,7 @@ def register():
|
||||
g.s, "page", post.id,
|
||||
)
|
||||
cal_names = ", ".join(c.name for c in calendars) if calendars else ""
|
||||
return render_comp(
|
||||
"link-card",
|
||||
title=post.title, image=post.feature_image,
|
||||
subtitle=cal_names, link=events_url(f"/{post.slug}"),
|
||||
)
|
||||
return _event_link_card_sexp(post, cal_names)
|
||||
|
||||
_handlers["link-card"] = _link_card_handler
|
||||
|
||||
|
||||
Reference in New Issue
Block a user