Inline header functions from sx_components into pages/__init__.py

Move _blog_header_sx, _settings_header_sx, _settings_nav_sx, and
_sub_settings_header_sx into the layout module as local helpers.
Eliminates 14 imports from sx_components.py for the layout system.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-04 09:27:31 +00:00
parent d7f9afff8e
commit f0fbcef3f6
2 changed files with 83 additions and 84 deletions

View File

@@ -158,11 +158,16 @@ def register(url_prefix, title):
"""Blog listing — moved from / to /index.""" """Blog listing — moved from / to /index."""
from shared.services.registry import services from shared.services.registry import services
from shared.sx.helpers import ( from shared.sx.helpers import (
render_to_sx, root_header_sx, full_page_sx, oob_page_sx, render_to_sx, root_header_sx, full_page_sx, oob_page_sx, oob_header_sx,
) )
from sx.sx_components import _blog_header_sx, _oob_header_sx
from shared.sx.parser import SxExpr from shared.sx.parser import SxExpr
async def _blog_hdr(ctx, oob=False):
return await render_to_sx("menu-row-sx",
id="blog-row", level=1,
link_label_content=SxExpr("(div)"),
child_id="blog-header-child", oob=oob)
data = await services.get("blog_page").index_data(g.s) data = await services.get("blog_page").index_data(g.s)
# Render content, aside, and filter via .sx defcomps # Render content, aside, and filter via .sx defcomps
@@ -175,7 +180,7 @@ def register(url_prefix, title):
if not is_htmx_request(): if not is_htmx_request():
root_hdr = await root_header_sx(tctx) root_hdr = await root_header_sx(tctx)
blog_hdr = await _blog_header_sx(tctx) blog_hdr = await _blog_hdr(tctx)
header_rows = "(<> " + root_hdr + " " + blog_hdr + ")" header_rows = "(<> " + root_hdr + " " + blog_hdr + ")"
html = await full_page_sx(tctx, header_rows=header_rows, html = await full_page_sx(tctx, header_rows=header_rows,
content=content, aside=aside, filter=filter_sx) content=content, aside=aside, filter=filter_sx)
@@ -185,9 +190,9 @@ def register(url_prefix, title):
return sx_response(content) return sx_response(content)
else: else:
root_hdr = await root_header_sx(tctx) root_hdr = await root_header_sx(tctx)
blog_hdr = await _blog_header_sx(tctx) blog_hdr = await _blog_hdr(tctx)
rows = "(<> " + root_hdr + " " + blog_hdr + ")" rows = "(<> " + root_hdr + " " + blog_hdr + ")"
header_oob = await _oob_header_sx("root-header-child", "blog-header-child", rows) header_oob = await oob_header_sx("root-header-child", "blog-header-child", rows)
sx_src = await oob_page_sx(oobs=header_oob, content=content, sx_src = await oob_page_sx(oobs=header_oob, content=content,
aside=aside, filter=filter_sx) aside=aside, filter=filter_sx)
return sx_response(sx_src) return sx_response(sx_src)

View File

@@ -107,18 +107,80 @@ async def _inject_post_context(p_data: dict) -> None:
_add_to_defpage_ctx(**ctx) _add_to_defpage_ctx(**ctx)
# ---------------------------------------------------------------------------
# Header helpers (moved from sx_components — thin render_to_sx wrappers)
# ---------------------------------------------------------------------------
async def _blog_header_sx(ctx: dict, *, oob: bool = False) -> str:
from shared.sx.helpers import render_to_sx
from shared.sx.parser import SxExpr
return await render_to_sx("menu-row-sx",
id="blog-row", level=1,
link_label_content=SxExpr("(div)"),
child_id="blog-header-child", oob=oob)
async def _settings_header_sx(ctx: dict, *, oob: bool = False) -> str:
from shared.sx.helpers import render_to_sx
from shared.sx.parser import SxExpr
from quart import url_for as qurl
settings_href = qurl("settings.defpage_settings_home")
label_sx = await render_to_sx("blog-admin-label")
nav_sx = await _settings_nav_sx(ctx)
return await render_to_sx("menu-row-sx",
id="root-settings-row", level=1,
link_href=settings_href,
link_label_content=SxExpr(label_sx),
nav=SxExpr(nav_sx) if nav_sx else None,
child_id="root-settings-header-child", oob=oob)
async def _settings_nav_sx(ctx: dict) -> str:
from shared.sx.helpers import render_to_sx
from quart import url_for as qurl
select_colours = ctx.get("select_colours", "")
parts = []
for endpoint, icon, label in [
("menu_items.defpage_menu_items_page", "bars", "Menu Items"),
("snippets.defpage_snippets_page", "puzzle-piece", "Snippets"),
("blog.tag_groups_admin.defpage_tag_groups_page", "tags", "Tag Groups"),
("settings.defpage_cache_page", "refresh", "Cache"),
]:
href = qurl(endpoint)
parts.append(await render_to_sx("nav-link",
href=href, icon=f"fa fa-{icon}", label=label,
select_colours=select_colours))
return "(<> " + " ".join(parts) + ")" if parts else ""
async def _sub_settings_header_sx(row_id: str, child_id: str, href: str,
icon: str, label: str, ctx: dict,
*, oob: bool = False, nav_sx: str = "") -> str:
from shared.sx.helpers import render_to_sx
from shared.sx.parser import SxExpr
label_sx = await render_to_sx("blog-sub-settings-label",
icon=f"fa fa-{icon}", label=label)
return await render_to_sx("menu-row-sx",
id=row_id, level=2,
link_href=href,
link_label_content=SxExpr(label_sx),
nav=SxExpr(nav_sx) if nav_sx else None,
child_id=child_id, oob=oob)
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Layouts # Layouts
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
def _register_blog_layouts() -> None: def _register_blog_layouts() -> None:
from shared.sx.layouts import register_custom_layout from shared.sx.layouts import register_custom_layout
# :blog — root + blog header (for new-post, new-page)
register_custom_layout("blog", _blog_full, _blog_oob) register_custom_layout("blog", _blog_full, _blog_oob)
# :blog-settings — root + settings header (with settings nav menu)
register_custom_layout("blog-settings", _settings_full, _settings_oob, register_custom_layout("blog-settings", _settings_full, _settings_oob,
mobile_fn=_settings_mobile) mobile_fn=_settings_mobile)
# Sub-settings layouts (root + settings + sub header)
register_custom_layout("blog-cache", _cache_full, _cache_oob) register_custom_layout("blog-cache", _cache_full, _cache_oob)
register_custom_layout("blog-snippets", _snippets_full, _snippets_oob) register_custom_layout("blog-snippets", _snippets_full, _snippets_oob)
register_custom_layout("blog-menu-items", _menu_items_full, _menu_items_oob) register_custom_layout("blog-menu-items", _menu_items_full, _menu_items_oob)
@@ -131,7 +193,6 @@ def _register_blog_layouts() -> None:
async def _blog_full(ctx: dict, **kw: Any) -> str: async def _blog_full(ctx: dict, **kw: Any) -> str:
from shared.sx.helpers import root_header_sx from shared.sx.helpers import root_header_sx
from sx.sx_components import _blog_header_sx
root_hdr = await root_header_sx(ctx) root_hdr = await root_header_sx(ctx)
blog_hdr = await _blog_header_sx(ctx) blog_hdr = await _blog_header_sx(ctx)
return "(<> " + root_hdr + " " + blog_hdr + ")" return "(<> " + root_hdr + " " + blog_hdr + ")"
@@ -139,7 +200,6 @@ async def _blog_full(ctx: dict, **kw: Any) -> str:
async def _blog_oob(ctx: dict, **kw: Any) -> str: async def _blog_oob(ctx: dict, **kw: Any) -> str:
from shared.sx.helpers import root_header_sx, oob_header_sx from shared.sx.helpers import root_header_sx, oob_header_sx
from sx.sx_components import _blog_header_sx
root_hdr = await root_header_sx(ctx) root_hdr = await root_header_sx(ctx)
blog_hdr = await _blog_header_sx(ctx) blog_hdr = await _blog_header_sx(ctx)
rows = "(<> " + root_hdr + " " + blog_hdr + ")" rows = "(<> " + root_hdr + " " + blog_hdr + ")"
@@ -150,7 +210,6 @@ async def _blog_oob(ctx: dict, **kw: Any) -> str:
async def _settings_full(ctx: dict, **kw: Any) -> str: async def _settings_full(ctx: dict, **kw: Any) -> str:
from shared.sx.helpers import root_header_sx from shared.sx.helpers import root_header_sx
from sx.sx_components import _settings_header_sx
root_hdr = await root_header_sx(ctx) root_hdr = await root_header_sx(ctx)
settings_hdr = await _settings_header_sx(ctx) settings_hdr = await _settings_header_sx(ctx)
return "(<> " + root_hdr + " " + settings_hdr + ")" return "(<> " + root_hdr + " " + settings_hdr + ")"
@@ -158,7 +217,6 @@ async def _settings_full(ctx: dict, **kw: Any) -> str:
async def _settings_oob(ctx: dict, **kw: Any) -> str: async def _settings_oob(ctx: dict, **kw: Any) -> str:
from shared.sx.helpers import root_header_sx, oob_header_sx from shared.sx.helpers import root_header_sx, oob_header_sx
from sx.sx_components import _settings_header_sx
root_hdr = await root_header_sx(ctx) root_hdr = await root_header_sx(ctx)
settings_hdr = await _settings_header_sx(ctx) settings_hdr = await _settings_header_sx(ctx)
rows = "(<> " + root_hdr + " " + settings_hdr + ")" rows = "(<> " + root_hdr + " " + settings_hdr + ")"
@@ -166,7 +224,6 @@ async def _settings_oob(ctx: dict, **kw: Any) -> str:
async def _settings_mobile(ctx: dict, **kw: Any) -> str: async def _settings_mobile(ctx: dict, **kw: Any) -> str:
from sx.sx_components import _settings_nav_sx
return await _settings_nav_sx(ctx) return await _settings_nav_sx(ctx)
@@ -175,7 +232,6 @@ async def _settings_mobile(ctx: dict, **kw: Any) -> str:
async def _sub_settings_full(ctx: dict, row_id: str, child_id: str, async def _sub_settings_full(ctx: dict, row_id: str, child_id: str,
endpoint: str, icon: str, label: str) -> str: endpoint: str, icon: str, label: str) -> str:
from shared.sx.helpers import root_header_sx from shared.sx.helpers import root_header_sx
from sx.sx_components import _settings_header_sx, _sub_settings_header_sx
from quart import url_for as qurl from quart import url_for as qurl
root_hdr = await root_header_sx(ctx) root_hdr = await root_header_sx(ctx)
settings_hdr = await _settings_header_sx(ctx) settings_hdr = await _settings_header_sx(ctx)
@@ -187,7 +243,6 @@ async def _sub_settings_full(ctx: dict, row_id: str, child_id: str,
async def _sub_settings_oob(ctx: dict, row_id: str, child_id: str, async def _sub_settings_oob(ctx: dict, row_id: str, child_id: str,
endpoint: str, icon: str, label: str) -> str: endpoint: str, icon: str, label: str) -> str:
from shared.sx.helpers import oob_header_sx from shared.sx.helpers import oob_header_sx
from sx.sx_components import _settings_header_sx, _sub_settings_header_sx
from quart import url_for as qurl from quart import url_for as qurl
settings_hdr_oob = await _settings_header_sx(ctx, oob=True) settings_hdr_oob = await _settings_header_sx(ctx, oob=True)
sub_hdr = await _sub_settings_header_sx(row_id, child_id, sub_hdr = await _sub_settings_header_sx(row_id, child_id,
@@ -247,11 +302,9 @@ async def _tag_groups_oob(ctx: dict, **kw: Any) -> str:
# --- Tag Group Edit --- # --- Tag Group Edit ---
async def _tag_group_edit_full(ctx: dict, **kw: Any) -> str: async def _tag_group_edit_full(ctx: dict, **kw: Any) -> str:
from quart import request from quart import request, url_for as qurl
g_id = (request.view_args or {}).get("id")
from quart import url_for as qurl
from shared.sx.helpers import root_header_sx from shared.sx.helpers import root_header_sx
from sx.sx_components import _settings_header_sx, _sub_settings_header_sx g_id = (request.view_args or {}).get("id")
root_hdr = await root_header_sx(ctx) root_hdr = await root_header_sx(ctx)
settings_hdr = await _settings_header_sx(ctx) settings_hdr = await _settings_header_sx(ctx)
sub_hdr = await _sub_settings_header_sx("tag-groups-row", "tag-groups-header-child", sub_hdr = await _sub_settings_header_sx("tag-groups-row", "tag-groups-header-child",
@@ -261,11 +314,9 @@ async def _tag_group_edit_full(ctx: dict, **kw: Any) -> str:
async def _tag_group_edit_oob(ctx: dict, **kw: Any) -> str: async def _tag_group_edit_oob(ctx: dict, **kw: Any) -> str:
from quart import request from quart import request, url_for as qurl
g_id = (request.view_args or {}).get("id")
from quart import url_for as qurl
from shared.sx.helpers import oob_header_sx from shared.sx.helpers import oob_header_sx
from sx.sx_components import _settings_header_sx, _sub_settings_header_sx g_id = (request.view_args or {}).get("id")
settings_hdr_oob = await _settings_header_sx(ctx, oob=True) settings_hdr_oob = await _settings_header_sx(ctx, oob=True)
sub_hdr = await _sub_settings_header_sx("tag-groups-row", "tag-groups-header-child", sub_hdr = await _sub_settings_header_sx("tag-groups-row", "tag-groups-header-child",
qurl("defpage_tag_group_edit", id=g_id), qurl("defpage_tag_group_edit", id=g_id),
@@ -308,36 +359,7 @@ async def _h_editor_page_content(**kw):
async def _h_post_admin_content(slug=None, **kw): async def _h_post_admin_content(slug=None, **kw):
await _ensure_post_data(slug) await _ensure_post_data(slug)
from quart import g return '(div :class "pb-8")'
from sqlalchemy import select
from shared.models.page_config import PageConfig
post = (g.post_data or {}).get("post", {})
features = {}
sumup_configured = False
sumup_merchant_code = ""
sumup_checkout_prefix = ""
if post.get("is_page"):
pc = (await g.s.execute(
select(PageConfig).where(
PageConfig.container_type == "page",
PageConfig.container_id == post["id"],
)
)).scalar_one_or_none()
if pc:
features = pc.features or {}
sumup_configured = bool(pc.sumup_api_key)
sumup_merchant_code = pc.sumup_merchant_code or ""
sumup_checkout_prefix = pc.sumup_checkout_prefix or ""
from shared.sx.page import get_template_context
from sx.sx_components import _post_admin_main_panel_sx
tctx = await get_template_context()
tctx.update({
"features": features,
"sumup_configured": sumup_configured,
"sumup_merchant_code": sumup_merchant_code,
"sumup_checkout_prefix": sumup_checkout_prefix,
})
return _post_admin_main_panel_sx(tctx)
async def _h_post_data_content(slug=None, **kw): async def _h_post_data_content(slug=None, **kw):
@@ -351,40 +373,12 @@ async def _h_post_data_content(slug=None, **kw):
async def _h_post_preview_content(slug=None, **kw): async def _h_post_preview_content(slug=None, **kw):
await _ensure_post_data(slug) await _ensure_post_data(slug)
from quart import g from quart import g
from models.ghost_content import Post from shared.services.registry import services
from sqlalchemy import select as sa_select
post_id = g.post_data["post"]["id"]
post = (await g.s.execute(
sa_select(Post).where(Post.id == post_id)
)).scalar_one_or_none()
preview_ctx: dict = {}
sx_content = getattr(post, "sx_content", None) or ""
if sx_content:
from shared.sx.prettify import sx_to_pretty_sx
preview_ctx["sx_pretty"] = sx_to_pretty_sx(sx_content)
lexical_raw = getattr(post, "lexical", None) or ""
if lexical_raw:
from shared.sx.prettify import json_to_pretty_sx
preview_ctx["json_pretty"] = json_to_pretty_sx(lexical_raw)
if sx_content:
from shared.sx.parser import parse as sx_parse
from shared.sx.html import render as sx_html_render
from shared.sx.jinja_bridge import _COMPONENT_ENV
try:
parsed = sx_parse(sx_content)
preview_ctx["sx_rendered"] = sx_html_render(parsed, dict(_COMPONENT_ENV))
except Exception:
preview_ctx["sx_rendered"] = "<em>Error rendering sx</em>"
if lexical_raw:
from bp.blog.ghost.lexical_renderer import render_lexical
try:
preview_ctx["lex_rendered"] = render_lexical(lexical_raw)
except Exception:
preview_ctx["lex_rendered"] = "<em>Error rendering lexical</em>"
from shared.sx.page import get_template_context from shared.sx.page import get_template_context
from sx.sx_components import _preview_main_panel_sx from sx.sx_components import _preview_main_panel_sx
preview_data = await services.get("blog_page").preview_data(g.s)
tctx = await get_template_context() tctx = await get_template_context()
tctx.update(preview_ctx) tctx.update(preview_data)
return await _preview_main_panel_sx(tctx) return await _preview_main_panel_sx(tctx)