"""composing-html / composer.py
Page shell + primitive helpers shared by every template module.
Templates receive a structured spec (a dict) and return a single HTML string
that goes into
. The composer wraps it with , inlines base.css /
base.js. If a caller passes `colophon=`, adds a colophon footer. Templates never write , ,
\n'
'\n'
f'\n'
f''
f'{masthead_html}'
f'{body}'
f'{colo_html}'
f'\n'
'\n'
'\n'
'\n'
)
# --------------------------------------------------------------------------- #
# helpers #
# --------------------------------------------------------------------------- #
_CSS_COLOR_RE = re.compile(
r"^("
r"#[0-9a-fA-F]{3,8}" # hex
r"|rgba?\([^)]+\)" # rgb / rgba
r"|hsla?\([^)]+\)" # hsl / hsla
r"|var\(--[a-zA-Z0-9_-]+(?:\s*,\s*[#a-zA-Z0-9.,()% _-]+)?\)" # var(--token[, fallback])
r"|[a-zA-Z]{3,30}" # named: 'red', 'transparent', 'currentColor'
r")$"
)
def css_color(value: Any, default: str = "var(--g500)") -> str:
"""Whitelist a CSS color value. Anything that doesn't look like a color
falls back to `default`. Use this for any color taken from a spec field."""
if not isinstance(value, str):
return default
v = value.strip()
return v if _CSS_COLOR_RE.match(v) else default
def slug(text: str) -> str:
s = re.sub(r"[^a-zA-Z0-9]+", "-", str(text)).strip("-").lower()
return s or "x"
def json_dump(obj: Any) -> str:
return json.dumps(obj, ensure_ascii=False, indent=2)