Migrate all apps to defpage declarative page routes
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 3m41s

Replace Python GET page handlers with declarative defpage definitions in .sx
files across all 8 apps (sx docs, orders, account, market, cart, federation,
events, blog). Each app now has sxc/pages/ with setup functions, layout
registrations, page helpers, and .sx defpage declarations.

Core infrastructure: add g I/O primitive, PageDef support for auth/layout/
data/content/filter/aside/menu slots, post_author auth level, and custom
layout registration. Remove ~1400 lines of render_*_page/render_*_oob
boilerplate. Update all endpoint references in routes, sx_components, and
templates to defpage_* naming.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-03 14:52:34 +00:00
parent 5b4cacaf19
commit c243d17eeb
108 changed files with 3598 additions and 2851 deletions

View File

@@ -50,7 +50,15 @@ def create_app() -> "Quart":
import sxc.sx_components # noqa: F401
app.register_blueprint(register_pages(url_prefix="/"))
from sxc.pages import setup_sx_pages
setup_sx_pages()
bp = register_pages(url_prefix="/")
from shared.sx.pages import mount_pages
mount_pages(bp, "sx")
app.register_blueprint(bp)
return app

View File

@@ -1,4 +1,8 @@
"""SX docs page routes."""
"""SX docs page routes.
Page GET routes are defined declaratively in sxc/pages/docs.sx via defpage.
This file contains only redirect routes and example API endpoints.
"""
from __future__ import annotations
import asyncio
@@ -14,122 +18,6 @@ from shared.browser.app.csrf import csrf_exempt
def register(url_prefix: str = "/") -> Blueprint:
bp = Blueprint("pages", __name__, url_prefix=url_prefix)
def _is_sx_request() -> bool:
return bool(request.headers.get("SX-Request") or request.headers.get("HX-Request"))
# ------------------------------------------------------------------
# Home
# ------------------------------------------------------------------
@bp.get("/")
async def index():
if _is_sx_request():
from shared.sx.helpers import sx_response
from sxc.sx_components import home_oob_sx
return sx_response(await home_oob_sx())
from shared.sx.page import get_template_context
from sxc.sx_components import render_home_page_sx
ctx = await get_template_context()
html = await render_home_page_sx(ctx)
return await make_response(html, 200)
# ------------------------------------------------------------------
# Docs
# ------------------------------------------------------------------
@bp.get("/docs/")
async def docs_index():
from quart import redirect
return redirect("/docs/introduction")
@bp.get("/docs/<slug>")
async def docs_page(slug: str):
if _is_sx_request():
from shared.sx.helpers import sx_response
from sxc.sx_components import docs_oob_sx
return sx_response(await docs_oob_sx(slug))
from shared.sx.page import get_template_context
from sxc.sx_components import render_docs_page_sx
ctx = await get_template_context()
html = await render_docs_page_sx(ctx, slug)
return await make_response(html, 200)
# ------------------------------------------------------------------
# Reference
# ------------------------------------------------------------------
@bp.get("/reference/")
async def reference_index():
if _is_sx_request():
from shared.sx.helpers import sx_response
from sxc.sx_components import reference_oob_sx
return sx_response(await reference_oob_sx(""))
from shared.sx.page import get_template_context
from sxc.sx_components import render_reference_page_sx
ctx = await get_template_context()
html = await render_reference_page_sx(ctx, "")
return await make_response(html, 200)
@bp.get("/reference/<slug>")
async def reference_page(slug: str):
if _is_sx_request():
from shared.sx.helpers import sx_response
from sxc.sx_components import reference_oob_sx
return sx_response(await reference_oob_sx(slug))
from shared.sx.page import get_template_context
from sxc.sx_components import render_reference_page_sx
ctx = await get_template_context()
html = await render_reference_page_sx(ctx, slug)
return await make_response(html, 200)
# ------------------------------------------------------------------
# Protocols
# ------------------------------------------------------------------
@bp.get("/protocols/")
async def protocols_index():
from quart import redirect
return redirect("/protocols/wire-format")
@bp.get("/protocols/<slug>")
async def protocol_page(slug: str):
if _is_sx_request():
from shared.sx.helpers import sx_response
from sxc.sx_components import protocol_oob_sx
return sx_response(await protocol_oob_sx(slug))
from shared.sx.page import get_template_context
from sxc.sx_components import render_protocol_page_sx
ctx = await get_template_context()
html = await render_protocol_page_sx(ctx, slug)
return await make_response(html, 200)
# ------------------------------------------------------------------
# Examples
# ------------------------------------------------------------------
@bp.get("/examples/")
async def examples_index():
from quart import redirect
return redirect("/examples/click-to-load")
@bp.get("/examples/<slug>")
async def examples_page(slug: str):
if _is_sx_request():
from shared.sx.helpers import sx_response
from sxc.sx_components import examples_oob_sx
return sx_response(await examples_oob_sx(slug))
from shared.sx.page import get_template_context
from sxc.sx_components import render_examples_page_sx
ctx = await get_template_context()
html = await render_examples_page_sx(ctx, slug)
return await make_response(html, 200)
# ------------------------------------------------------------------
# Example API endpoints (for live demos)
# ------------------------------------------------------------------
@@ -821,26 +709,4 @@ def register(url_prefix: str = "/") -> Blueprint:
oob_comp = _oob_code("retry-comp", comp_text)
return sx_response(f'(<> {sx_src} {oob_wire} {oob_comp})')
# ------------------------------------------------------------------
# Essays
# ------------------------------------------------------------------
@bp.get("/essays/")
async def essays_index():
from quart import redirect
return redirect("/essays/sx-sucks")
@bp.get("/essays/<slug>")
async def essay_page(slug: str):
if _is_sx_request():
from shared.sx.helpers import sx_response
from sxc.sx_components import essay_oob_sx
return sx_response(await essay_oob_sx(slug))
from shared.sx.page import get_template_context
from sxc.sx_components import render_essay_page_sx
ctx = await get_template_context()
html = await render_essay_page_sx(ctx, slug)
return await make_response(html, 200)
return bp

View File

@@ -30,8 +30,8 @@ def _register_sx_layouts() -> None:
"""Register the sx docs layout presets."""
from shared.sx.layouts import register_custom_layout
register_custom_layout("sx", _sx_full_headers, _sx_oob_headers)
register_custom_layout("sx-section", _sx_section_full_headers, _sx_section_oob_headers)
register_custom_layout("sx", _sx_full_headers, _sx_oob_headers, _sx_mobile)
register_custom_layout("sx-section", _sx_section_full_headers, _sx_section_oob_headers, _sx_section_mobile)
def _sx_full_headers(ctx: dict, **kw: Any) -> str:
@@ -98,6 +98,47 @@ def _sx_section_oob_headers(ctx: dict, **kw: Any) -> str:
return oob_header_sx("root-header-child", "sx-header-child", rows)
def _sx_mobile(ctx: dict, **kw: Any) -> str:
"""Mobile menu for sx home page: main nav + root."""
from shared.sx.helpers import (
mobile_menu_sx, mobile_root_nav_sx, sx_call, SxExpr,
)
from sxc.sx_components import _main_nav_sx
main_nav = _main_nav_sx(kw.get("section"))
return mobile_menu_sx(
sx_call("mobile-menu-section",
label="sx", href="/", level=1, colour="violet",
items=SxExpr(main_nav)),
mobile_root_nav_sx(ctx),
)
def _sx_section_mobile(ctx: dict, **kw: Any) -> str:
"""Mobile menu for sx section pages: sub nav + main nav + root."""
from shared.sx.helpers import (
mobile_menu_sx, mobile_root_nav_sx, sx_call, SxExpr,
)
from sxc.sx_components import _main_nav_sx
section = kw.get("section", "")
sub_label = kw.get("sub_label", section)
sub_href = kw.get("sub_href", "/")
sub_nav = kw.get("sub_nav", "")
main_nav = _main_nav_sx(section)
parts = []
if sub_nav:
parts.append(sx_call("mobile-menu-section",
label=sub_label, href=sub_href, level=2, colour="violet",
items=SxExpr(sub_nav)))
parts.append(sx_call("mobile-menu-section",
label="sx", href="/", level=1, colour="violet",
items=SxExpr(main_nav)))
parts.append(mobile_root_nav_sx(ctx))
return mobile_menu_sx(*parts)
# ---------------------------------------------------------------------------
# Page helpers — Python functions callable from defpage content expressions
# ---------------------------------------------------------------------------

View File

@@ -15,6 +15,17 @@
;; Docs section
;; ---------------------------------------------------------------------------
(defpage docs-index
:path "/docs/"
:auth :public
:layout (:sx-section
:section "Docs"
:sub-label "Docs"
:sub-href "/docs/introduction"
:sub-nav (docs-nav "Introduction")
:selected "Introduction")
:content (docs-content "introduction"))
(defpage docs-page
:path "/docs/<slug>"
:auth :public
@@ -56,6 +67,17 @@
;; Protocols section
;; ---------------------------------------------------------------------------
(defpage protocols-index
:path "/protocols/"
:auth :public
:layout (:sx-section
:section "Protocols"
:sub-label "Protocols"
:sub-href "/protocols/wire-format"
:sub-nav (protocols-nav "Wire Format")
:selected "Wire Format")
:content (protocol-content "wire-format"))
(defpage protocol-page
:path "/protocols/<slug>"
:auth :public
@@ -71,6 +93,17 @@
;; Examples section
;; ---------------------------------------------------------------------------
(defpage examples-index
:path "/examples/"
:auth :public
:layout (:sx-section
:section "Examples"
:sub-label "Examples"
:sub-href "/examples/click-to-load"
:sub-nav (examples-nav "Click to Load")
:selected "Click to Load")
:content (examples-content "click-to-load"))
(defpage examples-page
:path "/examples/<slug>"
:auth :public
@@ -86,6 +119,17 @@
;; Essays section
;; ---------------------------------------------------------------------------
(defpage essays-index
:path "/essays/"
:auth :public
:layout (:sx-section
:section "Essays"
:sub-label "Essays"
:sub-href "/essays/sx-sucks"
:sub-nav (essays-nav "sx sucks")
:selected "sx sucks")
:content (essay-content "sx-sucks"))
(defpage essay-page
:path "/essays/<slug>"
:auth :public

View File

@@ -6,8 +6,6 @@ import os
from shared.sx.jinja_bridge import load_sx_dir, watch_sx_dir
from shared.sx.helpers import (
sx_call, SxExpr, get_asset_url,
root_header_sx, full_page_sx,
oob_header_sx, oob_page_sx,
)
from content.highlight import highlight
@@ -17,10 +15,6 @@ load_sx_dir(_sxc_dir)
watch_sx_dir(_sxc_dir)
def _full_page(ctx: dict, **kwargs) -> str:
"""full_page_sx wrapper."""
return full_page_sx(ctx, **kwargs)
def _code(code: str, language: str = "lisp") -> str:
"""Build a ~doc-code component with highlighted content."""
@@ -168,13 +162,6 @@ def _main_nav_sx(current_section: str | None = None) -> str:
return _nav_items_sx(MAIN_NAV, current_section)
def _header_stack_sx(ctx: dict, section_nav: str | None = None) -> str:
"""Full header stack: root header + sx menu row."""
hdr = root_header_sx(ctx)
sx_row = _sx_header_sx(section_nav)
return "(<> " + hdr + " " + sx_row + ")"
def _sub_row_sx(sub_label: str, sub_href: str, sub_nav: str,
selected: str = "") -> str:
"""Build the level-2 sub-section menu-row."""
@@ -186,104 +173,6 @@ def _sub_row_sx(sub_label: str, sub_href: str, sub_nav: str,
)
def _section_header_stack_sx(ctx: dict, main_nav: str, sub_nav: str,
sub_label: str, sub_href: str,
selected: str = "") -> str:
"""Header stack with main nav + sub-section nav row."""
hdr = root_header_sx(ctx)
sub_row = _sub_row_sx(sub_label, sub_href, sub_nav, selected)
sx_row = _sx_header_sx(main_nav, child=sub_row)
return "(<> " + hdr + " " + sx_row + ")"
# ---------------------------------------------------------------------------
# OOB helpers — rebuild header rows for AJAX navigation
# ---------------------------------------------------------------------------
async def _section_oob_sx(section: str, sub_label: str, sub_href: str,
sub_nav: str, content: str,
selected: str = "") -> str:
"""Generic OOB response: rebuild both header rows + content."""
from shared.sx.page import get_template_context
ctx = await get_template_context()
root_hdr = root_header_sx(ctx)
main_nav = _main_nav_sx(section)
sub_row = _sub_row_sx(sub_label, sub_href, sub_nav, selected)
sx_row = _sx_header_sx(main_nav, child=sub_row)
rows = "(<> " + root_hdr + " " + sx_row + ")"
header_oob = oob_header_sx("root-header-child", "sx-header-child", rows)
return oob_page_sx(oobs=header_oob, content=content)
async def home_oob_sx() -> str:
"""OOB response for home page navigation."""
from shared.sx.page import get_template_context
ctx = await get_template_context()
root_hdr = root_header_sx(ctx)
main_nav = _main_nav_sx()
sx_row = _sx_header_sx(main_nav)
rows = "(<> " + root_hdr + " " + sx_row + ")"
header_oob = oob_header_sx("root-header-child", "sx-header-child", rows)
hero_code = highlight('(div :class "p-4 bg-white rounded shadow"\n'
' (h1 :class "text-2xl font-bold" "Hello")\n'
' (button :sx-get "/api/data"\n'
' :sx-target "#result"\n'
' "Load data"))', "lisp")
content = (
f'(div :id "main-content"'
f' (~sx-hero {hero_code})'
f' (~sx-philosophy)'
f' (~sx-how-it-works)'
f' (~sx-credits))'
)
return oob_page_sx(oobs=header_oob, content=content)
async def docs_oob_sx(slug: str) -> str:
"""OOB response for docs section navigation."""
from content.pages import DOCS_NAV
current = next((label for label, href in DOCS_NAV if href.endswith(slug)), None)
sub_nav = _docs_nav_sx(current)
return await _section_oob_sx("Docs", "Docs", "/docs/introduction", sub_nav,
_docs_content_sx(slug), selected=current or "")
async def reference_oob_sx(slug: str) -> str:
"""OOB response for reference section navigation."""
from content.pages import REFERENCE_NAV
current = next((label for label, href in REFERENCE_NAV
if href.rstrip("/").endswith(slug or "reference")), "Attributes")
sub_nav = _reference_nav_sx(current)
return await _section_oob_sx("Reference", "Reference", "/reference/", sub_nav,
_reference_content_sx(slug), selected=current or "")
async def protocol_oob_sx(slug: str) -> str:
"""OOB response for protocols section navigation."""
from content.pages import PROTOCOLS_NAV
current = next((label for label, href in PROTOCOLS_NAV if href.endswith(slug)), None)
sub_nav = _protocols_nav_sx(current)
return await _section_oob_sx("Protocols", "Protocols", "/protocols/wire-format", sub_nav,
_protocol_content_sx(slug), selected=current or "")
async def examples_oob_sx(slug: str) -> str:
"""OOB response for examples section navigation."""
from content.pages import EXAMPLES_NAV
current = next((label for label, href in EXAMPLES_NAV if href.endswith(slug)), None)
sub_nav = _examples_nav_sx(current)
return await _section_oob_sx("Examples", "Examples", "/examples/click-to-load", sub_nav,
_examples_content_sx(slug), selected=current or "")
async def essay_oob_sx(slug: str) -> str:
"""OOB response for essays section navigation."""
from content.pages import ESSAYS_NAV
current = next((label for label, href in ESSAYS_NAV if href.endswith(slug)), None)
sub_nav = _essays_nav_sx(current)
return await _section_oob_sx("Essays", "Essays", "/essays/sx-sucks", sub_nav,
_essay_content_sx(slug), selected=current or "")
# ---------------------------------------------------------------------------
# Content builders — return sx source strings
@@ -352,40 +241,6 @@ def _headers_table_sx(title: str, headers: list[tuple[str, str, str]]) -> str:
)
# ---------------------------------------------------------------------------
# Page renderers — async functions returning full HTML
# ---------------------------------------------------------------------------
async def render_home_page_sx(ctx: dict) -> str:
"""Full page: home."""
main_nav = _main_nav_sx()
hdr = _header_stack_sx(ctx, main_nav)
hero_code = highlight('(div :class "p-4 bg-white rounded shadow"\n'
' (h1 :class "text-2xl font-bold" "Hello")\n'
' (button :sx-get "/api/data"\n'
' :sx-target "#result"\n'
' "Load data"))', "lisp")
content = (
f'(div :id "main-content"'
f' (~sx-hero {hero_code})'
f' (~sx-philosophy)'
f' (~sx-how-it-works)'
f' (~sx-credits))'
)
return _full_page(ctx, header_rows=hdr, content=content)
async def render_docs_page_sx(ctx: dict, slug: str) -> str:
"""Full page: docs section."""
from content.pages import DOCS_NAV
current = next((label for label, href in DOCS_NAV if href.endswith(slug)), None)
main_nav = _main_nav_sx("Docs")
sub_nav = _docs_nav_sx(current)
hdr = _section_header_stack_sx(ctx, main_nav, sub_nav, "Docs", "/docs/introduction",
selected=current or "")
content = _docs_content_sx(slug)
return _full_page(ctx, header_rows=hdr, content=content)
def _docs_content_sx(slug: str) -> str:
"""Route to the right docs content builder."""
@@ -613,19 +468,6 @@ def _docs_server_rendering_sx() -> str:
# Reference pages
# ---------------------------------------------------------------------------
async def render_reference_page_sx(ctx: dict, slug: str) -> str:
"""Full page: reference section."""
from content.pages import REFERENCE_NAV
current = next((label for label, href in REFERENCE_NAV
if href.rstrip("/").endswith(slug or "reference")), "Attributes")
main_nav = _main_nav_sx("Reference")
sub_nav = _reference_nav_sx(current)
hdr = _section_header_stack_sx(ctx, main_nav, sub_nav, "Reference", "/reference/",
selected=current or "")
content = _reference_content_sx(slug)
return _full_page(ctx, header_rows=hdr, content=content)
def _reference_content_sx(slug: str) -> str:
builders = {
"": _reference_attrs_sx,
@@ -712,18 +554,6 @@ def _reference_js_api_sx() -> str:
# Protocol pages
# ---------------------------------------------------------------------------
async def render_protocol_page_sx(ctx: dict, slug: str) -> str:
"""Full page: protocols section."""
from content.pages import PROTOCOLS_NAV
current = next((label for label, href in PROTOCOLS_NAV if href.endswith(slug)), None)
main_nav = _main_nav_sx("Protocols")
sub_nav = _protocols_nav_sx(current)
hdr = _section_header_stack_sx(ctx, main_nav, sub_nav, "Protocols", "/protocols/wire-format",
selected=current or "")
content = _protocol_content_sx(slug)
return _full_page(ctx, header_rows=hdr, content=content)
def _protocol_content_sx(slug: str) -> str:
builders = {
"wire-format": _protocol_wire_format_sx,
@@ -887,18 +717,6 @@ def _protocol_future_sx() -> str:
# Examples pages
# ---------------------------------------------------------------------------
async def render_examples_page_sx(ctx: dict, slug: str) -> str:
"""Full page: examples section."""
from content.pages import EXAMPLES_NAV
current = next((label for label, href in EXAMPLES_NAV if href.endswith(slug)), None)
main_nav = _main_nav_sx("Examples")
sub_nav = _examples_nav_sx(current)
hdr = _section_header_stack_sx(ctx, main_nav, sub_nav, "Examples", "/examples/click-to-load",
selected=current or "")
content = _examples_content_sx(slug)
return _full_page(ctx, header_rows=hdr, content=content)
def _examples_content_sx(slug: str) -> str:
builders = {
"click-to-load": _example_click_to_load_sx,
@@ -1948,18 +1766,6 @@ def _example_retry_sx() -> str:
# Essays
# ---------------------------------------------------------------------------
async def render_essay_page_sx(ctx: dict, slug: str) -> str:
"""Full page: essays section."""
from content.pages import ESSAYS_NAV
current = next((label for label, href in ESSAYS_NAV if href.endswith(slug)), None)
main_nav = _main_nav_sx("Essays")
sub_nav = _essays_nav_sx(current)
hdr = _section_header_stack_sx(ctx, main_nav, sub_nav, "Essays", "/essays/sx-sucks",
selected=current or "")
content = _essay_content_sx(slug)
return _full_page(ctx, header_rows=hdr, content=content)
def _essay_content_sx(slug: str) -> str:
builders = {
"sx-sucks": _essay_sx_sucks,