Move sx docs page helpers from Python to pure SX composition (Phase 6)
Nav data, section nav, example content, reference table builders, and all slug dispatch now live in .sx files. Python helpers reduced to data-only returns (highlight, primitives-data, reference-data, attr-detail-data). Deleted essays.py and utils.py entirely. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,110 +1,135 @@
|
||||
"""Public partials and page helper registration for sx docs."""
|
||||
"""Page helper registration for sx docs.
|
||||
|
||||
All helpers return data values (dicts, lists) — no sx_call(), no SxExpr.
|
||||
Markup composition lives entirely in .sx files.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from .essays import (
|
||||
_docs_content_sx, _reference_content_sx, _protocol_content_sx,
|
||||
_examples_content_sx, _essay_content_sx,
|
||||
_reference_index_sx, _reference_attr_detail_sx,
|
||||
)
|
||||
from .utils import _docs_nav_sx, _reference_nav_sx, _protocols_nav_sx, _examples_nav_sx, _essays_nav_sx
|
||||
|
||||
|
||||
def home_content_sx() -> str:
|
||||
"""Home page content as sx wire format."""
|
||||
return (
|
||||
'(section :id "main-panel"'
|
||||
' :class "flex-1 md:h-full md:min-h-0 overflow-y-auto overscroll-contain js-grid-viewport"'
|
||||
' (~sx-home-content))'
|
||||
)
|
||||
|
||||
|
||||
async def docs_content_partial_sx(slug: str) -> str:
|
||||
"""Docs content as sx wire format."""
|
||||
inner = await _docs_content_sx(slug)
|
||||
return (
|
||||
f'(section :id "main-panel"'
|
||||
f' :class "flex-1 md:h-full md:min-h-0 overflow-y-auto overscroll-contain js-grid-viewport"'
|
||||
f' {inner})'
|
||||
)
|
||||
|
||||
|
||||
async def reference_content_partial_sx(slug: str) -> str:
|
||||
inner = await _reference_content_sx(slug)
|
||||
return (
|
||||
f'(section :id "main-panel"'
|
||||
f' :class "flex-1 md:h-full md:min-h-0 overflow-y-auto overscroll-contain js-grid-viewport"'
|
||||
f' {inner})'
|
||||
)
|
||||
|
||||
|
||||
async def protocol_content_partial_sx(slug: str) -> str:
|
||||
inner = await _protocol_content_sx(slug)
|
||||
return (
|
||||
f'(section :id "main-panel"'
|
||||
f' :class "flex-1 md:h-full md:min-h-0 overflow-y-auto overscroll-contain js-grid-viewport"'
|
||||
f' {inner})'
|
||||
)
|
||||
|
||||
|
||||
async def examples_content_partial_sx(slug: str) -> str:
|
||||
inner = await _examples_content_sx(slug)
|
||||
return (
|
||||
f'(section :id "main-panel"'
|
||||
f' :class "flex-1 md:h-full md:min-h-0 overflow-y-auto overscroll-contain js-grid-viewport"'
|
||||
f' {inner})'
|
||||
)
|
||||
|
||||
|
||||
async def essay_content_partial_sx(slug: str) -> str:
|
||||
inner = await _essay_content_sx(slug)
|
||||
return (
|
||||
f'(section :id "main-panel"'
|
||||
f' :class "flex-1 md:h-full md:min-h-0 overflow-y-auto overscroll-contain js-grid-viewport"'
|
||||
f' {inner})'
|
||||
)
|
||||
|
||||
|
||||
def _register_sx_helpers() -> None:
|
||||
"""Register Python content builder functions as page helpers."""
|
||||
"""Register Python data helpers as page helpers."""
|
||||
from shared.sx.pages import register_page_helpers
|
||||
from content.highlight import highlight as _highlight
|
||||
from content.pages import (
|
||||
DOCS_NAV, REFERENCE_NAV, PROTOCOLS_NAV,
|
||||
EXAMPLES_NAV, ESSAYS_NAV,
|
||||
)
|
||||
|
||||
def _find_current(nav_list, slug, match_fn=None):
|
||||
"""Find the current nav label for a slug."""
|
||||
if match_fn:
|
||||
return match_fn(nav_list, slug)
|
||||
for label, href in nav_list:
|
||||
if href.endswith(slug):
|
||||
return label
|
||||
return None
|
||||
|
||||
register_page_helpers("sx", {
|
||||
# Content builders
|
||||
"home-content": lambda: "(~sx-home-content)",
|
||||
"docs-content": _docs_content_sx,
|
||||
"reference-content": _reference_content_sx,
|
||||
"reference-index-content": _reference_index_sx,
|
||||
"reference-attr-detail": _reference_attr_detail_sx,
|
||||
"protocol-content": _protocol_content_sx,
|
||||
"examples-content": _examples_content_sx,
|
||||
"essay-content": _essay_content_sx,
|
||||
"highlight": _highlight,
|
||||
# Nav builders
|
||||
"docs-nav": _docs_nav_sx,
|
||||
"reference-nav": _reference_nav_sx,
|
||||
"protocols-nav": _protocols_nav_sx,
|
||||
"examples-nav": _examples_nav_sx,
|
||||
"essays-nav": _essays_nav_sx,
|
||||
# Nav data (for current label lookup)
|
||||
"DOCS_NAV": DOCS_NAV,
|
||||
"REFERENCE_NAV": REFERENCE_NAV,
|
||||
"PROTOCOLS_NAV": PROTOCOLS_NAV,
|
||||
"EXAMPLES_NAV": EXAMPLES_NAV,
|
||||
"ESSAYS_NAV": ESSAYS_NAV,
|
||||
# Utility
|
||||
"find-current": _find_current,
|
||||
"primitives-data": _primitives_data,
|
||||
"reference-data": _reference_data,
|
||||
"attr-detail-data": _attr_detail_data,
|
||||
})
|
||||
|
||||
|
||||
def _primitives_data() -> dict:
|
||||
"""Return the PRIMITIVES dict for the primitives docs page."""
|
||||
from content.pages import PRIMITIVES
|
||||
return PRIMITIVES
|
||||
|
||||
|
||||
def _reference_data(slug: str) -> dict:
|
||||
"""Return reference table data for a given slug.
|
||||
|
||||
Returns a dict whose keys become SX env bindings:
|
||||
- attributes: req-attrs, beh-attrs, uniq-attrs
|
||||
- headers: req-headers, resp-headers
|
||||
- events: events-list
|
||||
- js-api: js-api-list
|
||||
"""
|
||||
from content.pages import (
|
||||
REQUEST_ATTRS, BEHAVIOR_ATTRS, SX_UNIQUE_ATTRS,
|
||||
REQUEST_HEADERS, RESPONSE_HEADERS,
|
||||
EVENTS, JS_API, ATTR_DETAILS,
|
||||
)
|
||||
|
||||
if slug == "attributes":
|
||||
return {
|
||||
"req-attrs": [
|
||||
{"name": a, "desc": d, "exists": e,
|
||||
"href": f"/reference/attributes/{a}" if e and a in ATTR_DETAILS else None}
|
||||
for a, d, e in REQUEST_ATTRS
|
||||
],
|
||||
"beh-attrs": [
|
||||
{"name": a, "desc": d, "exists": e,
|
||||
"href": f"/reference/attributes/{a}" if e and a in ATTR_DETAILS else None}
|
||||
for a, d, e in BEHAVIOR_ATTRS
|
||||
],
|
||||
"uniq-attrs": [
|
||||
{"name": a, "desc": d, "exists": e,
|
||||
"href": f"/reference/attributes/{a}" if e and a in ATTR_DETAILS else None}
|
||||
for a, d, e in SX_UNIQUE_ATTRS
|
||||
],
|
||||
}
|
||||
elif slug == "headers":
|
||||
return {
|
||||
"req-headers": [
|
||||
{"name": n, "value": v, "desc": d}
|
||||
for n, v, d in REQUEST_HEADERS
|
||||
],
|
||||
"resp-headers": [
|
||||
{"name": n, "value": v, "desc": d}
|
||||
for n, v, d in RESPONSE_HEADERS
|
||||
],
|
||||
}
|
||||
elif slug == "events":
|
||||
return {
|
||||
"events-list": [
|
||||
{"name": n, "desc": d}
|
||||
for n, d in EVENTS
|
||||
],
|
||||
}
|
||||
elif slug == "js-api":
|
||||
return {
|
||||
"js-api-list": [
|
||||
{"name": n, "desc": d}
|
||||
for n, d in JS_API
|
||||
],
|
||||
}
|
||||
# Default — return attrs data for fallback
|
||||
return {
|
||||
"req-attrs": [
|
||||
{"name": a, "desc": d, "exists": e,
|
||||
"href": f"/reference/attributes/{a}" if e and a in ATTR_DETAILS else None}
|
||||
for a, d, e in REQUEST_ATTRS
|
||||
],
|
||||
"beh-attrs": [
|
||||
{"name": a, "desc": d, "exists": e,
|
||||
"href": f"/reference/attributes/{a}" if e and a in ATTR_DETAILS else None}
|
||||
for a, d, e in BEHAVIOR_ATTRS
|
||||
],
|
||||
"uniq-attrs": [
|
||||
{"name": a, "desc": d, "exists": e,
|
||||
"href": f"/reference/attributes/{a}" if e and a in ATTR_DETAILS else None}
|
||||
for a, d, e in SX_UNIQUE_ATTRS
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
def _attr_detail_data(slug: str) -> dict:
|
||||
"""Return attribute detail data for a specific attribute slug.
|
||||
|
||||
Returns a dict whose keys become SX env bindings:
|
||||
- attr-title, attr-description, attr-example, attr-handler
|
||||
- attr-demo (component call or None)
|
||||
- attr-wire-id (wire placeholder id or None)
|
||||
- attr-not-found (truthy if not found)
|
||||
"""
|
||||
from content.pages import ATTR_DETAILS
|
||||
from shared.sx.helpers import SxExpr
|
||||
|
||||
detail = ATTR_DETAILS.get(slug)
|
||||
if not detail:
|
||||
return {"attr-not-found": True}
|
||||
|
||||
demo_name = detail.get("demo")
|
||||
wire_id = None
|
||||
if "handler" in detail:
|
||||
wire_id = f"ref-wire-{slug.replace(':', '-').replace('*', 'star')}"
|
||||
|
||||
return {
|
||||
"attr-not-found": None,
|
||||
"attr-title": slug,
|
||||
"attr-description": detail["description"],
|
||||
"attr-example": detail["example"],
|
||||
"attr-handler": detail.get("handler"),
|
||||
"attr-demo": SxExpr(f"(~{demo_name})") if demo_name else None,
|
||||
"attr-wire-id": wire_id,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user