All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 1m15s
Migrate ~52 GET route handlers across all 7 services from Jinja render_template() to s-expression component rendering. Each service gets a sexp_components.py with page/oob/cards render functions. - Add per-service sexp_components.py (account, blog, cart, events, federation, market, orders) with full page, OOB, and pagination card rendering - Add shared/sexp/helpers.py with call_url, root_header_html, full_page, oob_page utilities - Update all GET routes to use get_template_context() + render fns - Fix get_template_context() to inject Jinja globals (URL helpers) - Add qs_filter to base_context for sexp filter URL building - Mount sexp_components.py in docker-compose.dev.yml for all services - Import sexp_components in app.py for Hypercorn --reload watching - Fix route_prefix import (shared.utils not shared.infrastructure.urls) - Fix federation choose-username missing actor in context - Fix market page_markets missing post in context Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1806 lines
79 KiB
Python
1806 lines
79 KiB
Python
"""
|
|
Blog service s-expression page components.
|
|
|
|
Renders home, blog index (posts/pages), new post/page, post detail,
|
|
post admin, post data, post entries, post edit, post settings,
|
|
settings home, cache, snippets, menu items, and tag groups pages.
|
|
Called from route handlers in place of ``render_template()``.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
from markupsafe import escape
|
|
|
|
from shared.sexp.jinja_bridge import sexp
|
|
from shared.sexp.helpers import (
|
|
call_url, get_asset_url, root_header_html,
|
|
search_mobile_html, search_desktop_html,
|
|
full_page, oob_page,
|
|
)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# OOB header helper
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def _oob_header_html(parent_id: str, child_id: str, row_html: str) -> str:
|
|
"""Wrap a header row in OOB div with child placeholder."""
|
|
return (
|
|
f'<div id="{parent_id}" hx-swap-oob="outerHTML" class="w-full">'
|
|
f'<div class="w-full">{row_html}'
|
|
f'<div id="{child_id}"></div></div></div>'
|
|
)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Blog header (root-header-child -> blog-header-child)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def _blog_header_html(ctx: dict, *, oob: bool = False) -> str:
|
|
"""Blog header row — empty child of root."""
|
|
return sexp(
|
|
'(~menu-row :id "blog-row" :level 1'
|
|
' :link-label-html llh'
|
|
' :child-id "blog-header-child" :oob oob)',
|
|
llh="<div></div>",
|
|
oob=oob,
|
|
)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Post header helpers
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def _post_header_html(ctx: dict, *, oob: bool = False) -> str:
|
|
"""Build the post-level header row."""
|
|
post = ctx.get("post") or {}
|
|
slug = post.get("slug", "")
|
|
title = (post.get("title") or "")[:160]
|
|
feature_image = post.get("feature_image")
|
|
|
|
label_parts = []
|
|
if feature_image:
|
|
label_parts.append(
|
|
f'<img src="{feature_image}" class="h-8 w-8 rounded-full object-cover border border-stone-300 flex-shrink-0">'
|
|
)
|
|
label_parts.append(f"<span>{escape(title)}</span>")
|
|
label_html = "".join(label_parts)
|
|
|
|
nav_parts = []
|
|
page_cart_count = ctx.get("page_cart_count", 0)
|
|
if page_cart_count and page_cart_count > 0:
|
|
cart_href = call_url(ctx, "cart_url", f"/{slug}/")
|
|
nav_parts.append(
|
|
f'<a href="{cart_href}" class="relative inline-flex items-center gap-1.5 px-3 py-1.5 text-sm rounded-full'
|
|
f' border border-emerald-300 bg-emerald-50 text-emerald-800 hover:bg-emerald-100 transition">'
|
|
f'<i class="fa fa-shopping-cart" aria-hidden="true"></i>'
|
|
f'<span>{page_cart_count}</span></a>'
|
|
)
|
|
|
|
# Container nav fragments (calendars, markets)
|
|
container_nav = ctx.get("container_nav_html", "")
|
|
if container_nav:
|
|
nav_parts.append(
|
|
f'<div class="flex flex-col sm:flex-row sm:items-center gap-2 border-r border-stone-200 mr-2 sm:max-w-2xl"'
|
|
f' id="entries-calendars-nav-wrapper">{container_nav}</div>'
|
|
)
|
|
|
|
# Admin link
|
|
from quart import url_for as qurl, g
|
|
rights = ctx.get("rights") or {}
|
|
has_admin = rights.get("admin") if isinstance(rights, dict) else getattr(rights, "admin", False)
|
|
if has_admin:
|
|
hx_select = ctx.get("hx_select_search", "#main-panel")
|
|
select_colours = ctx.get("select_colours", "")
|
|
styles = ctx.get("styles") or {}
|
|
nav_btn = styles.get("nav_button", "") if isinstance(styles, dict) else getattr(styles, "nav_button", "")
|
|
admin_href = qurl("blog.post.admin.admin", slug=slug)
|
|
nav_parts.append(
|
|
f'<div class="relative nav-group"><a href="{admin_href}"'
|
|
f' hx-get="{admin_href}" hx-target="#main-panel" hx-select="{hx_select}"'
|
|
f' hx-swap="outerHTML" hx-push-url="true"'
|
|
f' class="{nav_btn} {select_colours}">'
|
|
f'<i class="fa fa-cog" aria-hidden="true"></i></a></div>'
|
|
)
|
|
|
|
nav_html = "".join(nav_parts)
|
|
link_href = call_url(ctx, "blog_url", f"/{slug}/")
|
|
|
|
return sexp(
|
|
'(~menu-row :id "post-row" :level 1'
|
|
' :link-href lh :link-label-html llh'
|
|
' :nav-html nh :child-id "post-header-child" :oob oob)',
|
|
lh=link_href,
|
|
llh=label_html,
|
|
nh=nav_html,
|
|
oob=oob,
|
|
)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Post admin header
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def _post_admin_header_html(ctx: dict, *, oob: bool = False) -> str:
|
|
"""Post admin header row with admin icon and nav links."""
|
|
from quart import url_for as qurl
|
|
|
|
post = ctx.get("post") or {}
|
|
slug = post.get("slug", "")
|
|
hx_select = ctx.get("hx_select_search", "#main-panel")
|
|
select_colours = ctx.get("select_colours", "")
|
|
styles = ctx.get("styles") or {}
|
|
nav_btn = styles.get("nav_button", "") if isinstance(styles, dict) else getattr(styles, "nav_button", "")
|
|
|
|
admin_href = qurl("blog.post.admin.admin", slug=slug)
|
|
label_html = '<i class="fa fa-shield-halved" aria-hidden="true"></i> admin'
|
|
|
|
nav_html = _post_admin_nav_html(ctx)
|
|
|
|
return sexp(
|
|
'(~menu-row :id "post-admin-row" :level 2'
|
|
' :link-href lh :link-label-html llh'
|
|
' :nav-html nh :child-id "post-admin-header-child" :oob oob)',
|
|
lh=admin_href,
|
|
llh=label_html,
|
|
nh=nav_html,
|
|
oob=oob,
|
|
)
|
|
|
|
|
|
def _post_admin_nav_html(ctx: dict) -> str:
|
|
"""Post admin desktop nav: calendars, markets, payments, entries, data, edit, settings."""
|
|
from quart import url_for as qurl
|
|
|
|
post = ctx.get("post") or {}
|
|
slug = post.get("slug", "")
|
|
hx_select = ctx.get("hx_select_search", "#main-panel")
|
|
select_colours = ctx.get("select_colours", "")
|
|
styles = ctx.get("styles") or {}
|
|
nav_btn = styles.get("nav_button", "") if isinstance(styles, dict) else getattr(styles, "nav_button", "")
|
|
|
|
parts = []
|
|
|
|
# External links to events service
|
|
events_url_fn = ctx.get("events_url")
|
|
if callable(events_url_fn):
|
|
for path, label in [
|
|
(f"/{slug}/calendars/", "calendars"),
|
|
(f"/{slug}/markets/", "markets"),
|
|
(f"/{slug}/payments/", "payments"),
|
|
]:
|
|
href = events_url_fn(path)
|
|
parts.append(
|
|
f'<div class="relative nav-group">'
|
|
f'<a href="{href}" class="{nav_btn}">{label}</a></div>'
|
|
)
|
|
|
|
# HTMX links
|
|
for endpoint, label in [
|
|
("blog.post.admin.entries", "entries"),
|
|
("blog.post.admin.data", "data"),
|
|
("blog.post.admin.edit", "edit"),
|
|
("blog.post.admin.settings", "settings"),
|
|
]:
|
|
href = qurl(endpoint, slug=slug)
|
|
parts.append(sexp(
|
|
'(~nav-link :href h :label l :select-colours sc)',
|
|
h=href, l=label, sc=select_colours,
|
|
))
|
|
|
|
return "".join(parts)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Settings header (root-header-child -> root-settings-header-child)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def _settings_header_html(ctx: dict, *, oob: bool = False) -> str:
|
|
"""Settings header row with admin icon and nav links."""
|
|
from quart import url_for as qurl
|
|
|
|
hx_select = ctx.get("hx_select_search", "#main-panel")
|
|
settings_href = qurl("settings.home")
|
|
label_html = '<i class="fa fa-shield-halved" aria-hidden="true"></i> admin'
|
|
|
|
nav_html = _settings_nav_html(ctx)
|
|
|
|
return sexp(
|
|
'(~menu-row :id "root-settings-row" :level 1'
|
|
' :link-href lh :link-label-html llh'
|
|
' :nav-html nh :child-id "root-settings-header-child" :oob oob)',
|
|
lh=settings_href,
|
|
llh=label_html,
|
|
nh=nav_html,
|
|
oob=oob,
|
|
)
|
|
|
|
|
|
def _settings_nav_html(ctx: dict) -> str:
|
|
"""Settings desktop nav: menu items, snippets, tag groups, cache."""
|
|
from quart import url_for as qurl
|
|
|
|
select_colours = ctx.get("select_colours", "")
|
|
parts = []
|
|
|
|
for endpoint, icon, label in [
|
|
("menu_items.list_menu_items", "bars", "Menu Items"),
|
|
("snippets.list_snippets", "puzzle-piece", "Snippets"),
|
|
("blog.tag_groups_admin.index", "tags", "Tag Groups"),
|
|
("settings.cache", "refresh", "Cache"),
|
|
]:
|
|
href = qurl(endpoint)
|
|
parts.append(sexp(
|
|
'(~nav-link :href h :icon ic :label l :select-colours sc)',
|
|
h=href, ic=f"fa fa-{icon}", l=label, sc=select_colours,
|
|
))
|
|
|
|
return "".join(parts)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Sub-settings headers (root-settings-header-child -> X-header-child)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def _sub_settings_header_html(row_id: str, child_id: str, href: str,
|
|
icon: str, label: str, ctx: dict,
|
|
*, oob: bool = False, nav_html: str = "") -> str:
|
|
"""Generic sub-settings header row (menu_items, snippets, tag_groups, cache)."""
|
|
select_colours = ctx.get("select_colours", "")
|
|
label_html = f'<i class="fa fa-{icon}" aria-hidden="true"></i> {escape(label)}'
|
|
|
|
return sexp(
|
|
'(~menu-row :id rid :level 2'
|
|
' :link-href lh :link-label-html llh'
|
|
' :nav-html nh :child-id cid :oob oob)',
|
|
rid=row_id,
|
|
lh=href,
|
|
llh=label_html,
|
|
nh=nav_html,
|
|
cid=child_id,
|
|
oob=oob,
|
|
)
|
|
|
|
|
|
def _post_sub_admin_header_html(row_id: str, child_id: str, href: str,
|
|
icon: str, label: str, ctx: dict,
|
|
*, oob: bool = False, nav_html: str = "") -> str:
|
|
"""Generic post sub-admin header row (data, edit, entries, settings)."""
|
|
label_html = f'<i class="fa fa-{icon}" aria-hidden="true"></i><div>{escape(label)}</div>'
|
|
|
|
return sexp(
|
|
'(~menu-row :id rid :level 3'
|
|
' :link-href lh :link-label-html llh'
|
|
' :nav-html nh :child-id cid :oob oob)',
|
|
rid=row_id,
|
|
lh=href,
|
|
llh=label_html,
|
|
nh=nav_html,
|
|
cid=child_id,
|
|
oob=oob,
|
|
)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Blog index main panel helpers
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def _blog_cards_html(ctx: dict) -> str:
|
|
"""Render blog post cards (list or tile)."""
|
|
posts = ctx.get("posts") or []
|
|
view = ctx.get("view")
|
|
parts = []
|
|
for p in posts:
|
|
if view == "tile":
|
|
parts.append(_blog_card_tile_html(p, ctx))
|
|
else:
|
|
parts.append(_blog_card_html(p, ctx))
|
|
parts.append(_blog_sentinel_html(ctx))
|
|
return "".join(parts)
|
|
|
|
|
|
def _blog_card_html(post: dict, ctx: dict) -> str:
|
|
"""Single blog post card (list view)."""
|
|
from quart import url_for as qurl, g
|
|
|
|
slug = post.get("slug", "")
|
|
href = call_url(ctx, "blog_url", f"/{slug}/")
|
|
hx_select = ctx.get("hx_select_search", "#main-panel")
|
|
user = getattr(g, "user", None)
|
|
|
|
parts = ['<article class="border-b pb-6 last:border-b-0 relative">']
|
|
|
|
# Like button
|
|
if user:
|
|
liked = post.get("is_liked", False)
|
|
like_url = call_url(ctx, "blog_url", f"/{slug}/like/toggle/")
|
|
parts.append(
|
|
f'<div class="absolute top-20 right-2 z-10 text-6xl md:text-4xl">'
|
|
f'<button hx-post="{like_url}" hx-swap="outerHTML"'
|
|
f' hx-headers=\'{{\"X-CSRFToken\": \"{ctx.get("csrf_token", "")}\"}}\''
|
|
f' class="cursor-pointer">'
|
|
f'{"❤️" if liked else "🤍"}</button></div>'
|
|
)
|
|
|
|
parts.append(
|
|
f'<a href="{href}" hx-get="{href}" hx-target="#main-panel"'
|
|
f' hx-select="{hx_select}" hx-swap="outerHTML" hx-push-url="true"'
|
|
f' class="block rounded-xl bg-white shadow hover:shadow-md transition overflow-hidden">'
|
|
)
|
|
|
|
# Header
|
|
parts.append(f'<header class="mb-2 text-center"><h2 class="text-4xl font-bold text-stone-900">{escape(post.get("title", ""))}</h2>')
|
|
|
|
status = post.get("status", "published")
|
|
if status == "draft":
|
|
parts.append('<div class="flex justify-center gap-2 mt-1">')
|
|
parts.append('<span class="inline-block px-2 py-0.5 rounded-full text-xs font-semibold bg-amber-100 text-amber-800">Draft</span>')
|
|
if post.get("publish_requested"):
|
|
parts.append('<span class="inline-block px-2 py-0.5 rounded-full text-xs font-semibold bg-blue-100 text-blue-800">Publish requested</span>')
|
|
parts.append('</div>')
|
|
updated = post.get("updated_at")
|
|
if updated:
|
|
ts = updated.strftime("%-d %b %Y at %H:%M") if hasattr(updated, "strftime") else str(updated)
|
|
parts.append(f'<p class="text-sm text-stone-500">Updated: {ts}</p>')
|
|
else:
|
|
pub = post.get("published_at")
|
|
if pub:
|
|
ts = pub.strftime("%-d %b %Y at %H:%M") if hasattr(pub, "strftime") else str(pub)
|
|
parts.append(f'<p class="text-sm text-stone-500">Published: {ts}</p>')
|
|
|
|
parts.append('</header>')
|
|
|
|
# Feature image
|
|
fi = post.get("feature_image")
|
|
if fi:
|
|
parts.append(f'<div class="mb-4"><img src="{fi}" alt="" class="rounded-lg w-full object-cover"></div>')
|
|
|
|
# Excerpt
|
|
excerpt = post.get("custom_excerpt") or post.get("excerpt", "")
|
|
if excerpt:
|
|
parts.append(f'<p class="text-stone-700 text-lg leading-relaxed text-center">{escape(excerpt)}</p>')
|
|
|
|
parts.append('</a>')
|
|
|
|
# Card widgets (fragments)
|
|
card_widgets = ctx.get("card_widgets_html") or {}
|
|
widget = card_widgets.get(str(post.get("id", "")), "")
|
|
if widget:
|
|
parts.append(widget)
|
|
|
|
# Tags + authors bar
|
|
parts.append(_at_bar_html(post, ctx))
|
|
parts.append('</article>')
|
|
return "".join(parts)
|
|
|
|
|
|
def _blog_card_tile_html(post: dict, ctx: dict) -> str:
|
|
"""Single blog post card (tile view)."""
|
|
slug = post.get("slug", "")
|
|
href = call_url(ctx, "blog_url", f"/{slug}/")
|
|
hx_select = ctx.get("hx_select_search", "#main-panel")
|
|
|
|
parts = ['<article class="relative">']
|
|
parts.append(
|
|
f'<a href="{href}" hx-get="{href}" hx-target="#main-panel"'
|
|
f' hx-select="{hx_select}" hx-swap="outerHTML" hx-push-url="true"'
|
|
f' class="block rounded-xl bg-white shadow hover:shadow-md transition overflow-hidden">'
|
|
)
|
|
|
|
fi = post.get("feature_image")
|
|
if fi:
|
|
parts.append(f'<div><img src="{fi}" alt="" class="w-full aspect-video object-cover"></div>')
|
|
|
|
parts.append('<div class="p-3 text-center">')
|
|
parts.append(f'<h2 class="text-lg font-bold text-stone-900">{escape(post.get("title", ""))}</h2>')
|
|
|
|
status = post.get("status", "published")
|
|
if status == "draft":
|
|
parts.append('<div class="flex justify-center gap-1 mt-1">')
|
|
parts.append('<span class="inline-block px-2 py-0.5 rounded-full text-xs font-semibold bg-amber-100 text-amber-800">Draft</span>')
|
|
if post.get("publish_requested"):
|
|
parts.append('<span class="inline-block px-2 py-0.5 rounded-full text-xs font-semibold bg-blue-100 text-blue-800">Publish requested</span>')
|
|
parts.append('</div>')
|
|
updated = post.get("updated_at")
|
|
if updated:
|
|
ts = updated.strftime("%-d %b %Y at %H:%M") if hasattr(updated, "strftime") else str(updated)
|
|
parts.append(f'<p class="text-sm text-stone-500">Updated: {ts}</p>')
|
|
else:
|
|
pub = post.get("published_at")
|
|
if pub:
|
|
ts = pub.strftime("%-d %b %Y at %H:%M") if hasattr(pub, "strftime") else str(pub)
|
|
parts.append(f'<p class="text-sm text-stone-500">Published: {ts}</p>')
|
|
|
|
excerpt = post.get("custom_excerpt") or post.get("excerpt", "")
|
|
if excerpt:
|
|
parts.append(f'<p class="text-stone-700 text-sm leading-relaxed line-clamp-3 mt-1">{escape(excerpt)}</p>')
|
|
|
|
parts.append('</div></a>')
|
|
parts.append(_at_bar_html(post, ctx))
|
|
parts.append('</article>')
|
|
return "".join(parts)
|
|
|
|
|
|
def _at_bar_html(post: dict, ctx: dict) -> str:
|
|
"""Tags + authors bar below a card."""
|
|
tags = post.get("tags") or []
|
|
authors = post.get("authors") or []
|
|
if not tags and not authors:
|
|
return ""
|
|
|
|
all_tags = ctx.get("tags") or []
|
|
tag_slugs = {t.get("slug") or getattr(t, "slug", "") for t in all_tags} if all_tags else set()
|
|
|
|
parts = ['<div class="flex flex-row justify-center gap-3">']
|
|
|
|
if tags:
|
|
parts.append('<div class="mt-4 flex items-center gap-2"><div>in</div><ul class="flex flex-wrap gap-2 text-sm">')
|
|
for t in tags:
|
|
t_slug = t.get("slug") or getattr(t, "slug", "")
|
|
t_name = t.get("name") or getattr(t, "name", "")
|
|
t_fi = t.get("feature_image") or getattr(t, "feature_image", None)
|
|
if t_fi:
|
|
icon = f'<img src="{t_fi}" alt="{escape(t_name)}" class="h-4 w-4 rounded-full object-cover border border-stone-300 flex-shrink-0">'
|
|
else:
|
|
init = escape(t_name[:1]) if t_name else ""
|
|
icon = (
|
|
f'<div class="h-4 w-4 rounded-full text-[8px] font-semibold flex items-center justify-center'
|
|
f' border border-stone-300 flex-shrink-0 bg-stone-200 text-stone-600">{init}</div>'
|
|
)
|
|
parts.append(
|
|
f'<li><a class="flex items-center gap-1">{icon}'
|
|
f'<span class="inline-block rounded-full bg-stone-100 text-stone-600 px-2 py-1 text-sm font-medium'
|
|
f' border border-stone-200">{escape(t_name)}</span></a></li>'
|
|
)
|
|
parts.append('</ul></div>')
|
|
|
|
parts.append('<div></div>')
|
|
|
|
if authors:
|
|
parts.append('<div class="mt-4 flex items-center gap-2"><div>by</div><ul class="flex flex-wrap gap-2 text-sm">')
|
|
for a in authors:
|
|
a_name = a.get("name") or getattr(a, "name", "")
|
|
a_img = a.get("profile_image") or getattr(a, "profile_image", None)
|
|
if a_img:
|
|
parts.append(
|
|
f'<li class="flex items-center gap-1">'
|
|
f'<img src="{a_img}" alt="{escape(a_name)}" class="h-5 w-5 rounded-full object-cover">'
|
|
f'<span class="text-stone-700">{escape(a_name)}</span></li>'
|
|
)
|
|
else:
|
|
parts.append(f'<li class="text-stone-700">{escape(a_name)}</li>')
|
|
parts.append('</ul></div>')
|
|
|
|
parts.append('</div>')
|
|
return "".join(parts)
|
|
|
|
|
|
def _blog_sentinel_html(ctx: dict) -> str:
|
|
"""Infinite scroll sentinels for blog post list."""
|
|
page = ctx.get("page", 1)
|
|
total_pages = ctx.get("total_pages", 1)
|
|
if isinstance(total_pages, str):
|
|
total_pages = int(total_pages)
|
|
|
|
if page >= total_pages:
|
|
return '<div class="col-span-full mt-4 text-center text-xs text-stone-400">End of results</div>'
|
|
|
|
current_local_href = ctx.get("current_local_href", "/index")
|
|
qs_fn = ctx.get("qs")
|
|
# Build next page URL
|
|
next_url = f"{current_local_href}?page={page + 1}"
|
|
|
|
parts = []
|
|
# Mobile sentinel
|
|
parts.append(
|
|
f'<div id="sentinel-{page}-m" class="block md:hidden h-[60vh] opacity-0 pointer-events-none js-mobile-sentinel"'
|
|
f' hx-get="{next_url}" hx-trigger="intersect once delay:250ms, sentinelmobile:retry"'
|
|
f' hx-swap="outerHTML"'
|
|
f' _="init if not me.dataset.retryMs then set me.dataset.retryMs to 1000 end'
|
|
f' if window.matchMedia(\'(min-width: 768px)\').matches then set @hx-disabled to \'\' end'
|
|
f' on resize from window if window.matchMedia(\'(min-width: 768px)\').matches then set @hx-disabled to \'\' else remove @hx-disabled end'
|
|
f' on htmx:beforeRequest if window.matchMedia(\'(min-width: 768px)\').matches then halt end'
|
|
f' add .hidden to .js-neterr in me remove .hidden from .js-loading in me remove .opacity-100 from me add .opacity-0 to me'
|
|
f' def backoff() set ms to me.dataset.retryMs if ms > 30000 then set ms to 30000 end'
|
|
f' add .hidden to .js-loading in me remove .hidden from .js-neterr in me remove .opacity-0 from me add .opacity-100 to me'
|
|
f' wait ms ms trigger sentinelmobile:retry set ms to ms * 2 if ms > 30000 then set ms to 30000 end set me.dataset.retryMs to ms end'
|
|
f' on htmx:sendError call backoff() on htmx:responseError call backoff() on htmx:timeout call backoff()"'
|
|
f' role="status" aria-live="polite" aria-hidden="true">'
|
|
f'<div class="js-loading hidden flex justify-center py-8"><div class="animate-spin h-8 w-8 border-4 border-stone-300 border-t-stone-600 rounded-full"></div></div>'
|
|
f'<div class="js-neterr hidden text-center py-8 text-stone-400"><i class="fa fa-exclamation-triangle text-2xl"></i><p class="mt-2">Loading failed — retrying…</p></div>'
|
|
f'</div>'
|
|
)
|
|
# Desktop sentinel
|
|
parts.append(
|
|
f'<div id="sentinel-{page}-d" class="hidden md:block h-4 opacity-0 pointer-events-none"'
|
|
f' hx-get="{next_url}" hx-trigger="intersect once delay:250ms, sentinel:retry"'
|
|
f' hx-swap="outerHTML"'
|
|
f' _="init if not me.dataset.retryMs then set me.dataset.retryMs to 1000 end'
|
|
f' on htmx:beforeRequest(event) add .hidden to .js-neterr in me remove .hidden from .js-loading in me'
|
|
f' remove .opacity-100 from me add .opacity-0 to me'
|
|
f' set trig to null if event.detail and event.detail.triggeringEvent then set trig to event.detail.triggeringEvent end'
|
|
f' if trig and trig.type is \'intersect\' set scroller to the closest .js-grid-viewport'
|
|
f' if scroller is null then halt end if scroller.scrollTop < 20 then halt end end'
|
|
f' def backoff() set ms to me.dataset.retryMs if ms > 30000 then set ms to 30000 end'
|
|
f' add .hidden to .js-loading in me remove .hidden from .js-neterr in me remove .opacity-0 from me add .opacity-100 to me'
|
|
f' wait ms ms trigger sentinel:retry set ms to ms * 2 if ms > 30000 then set ms to 30000 end set me.dataset.retryMs to ms end'
|
|
f' on htmx:sendError call backoff() on htmx:responseError call backoff() on htmx:timeout call backoff()"'
|
|
f' role="status" aria-live="polite" aria-hidden="true">'
|
|
f'<div class="js-loading hidden flex justify-center py-2"><div class="animate-spin h-6 w-6 border-2 border-stone-300 border-t-stone-600 rounded-full"></div></div>'
|
|
f'<div class="js-neterr hidden text-center py-2 text-stone-400 text-sm">Retry…</div>'
|
|
f'</div>'
|
|
)
|
|
return "".join(parts)
|
|
|
|
|
|
def _page_cards_html(ctx: dict) -> str:
|
|
"""Render page cards with sentinel."""
|
|
pages = ctx.get("pages") or ctx.get("posts") or []
|
|
page_num = ctx.get("page", 1)
|
|
total_pages = ctx.get("total_pages", 1)
|
|
if isinstance(total_pages, str):
|
|
total_pages = int(total_pages)
|
|
hx_select = ctx.get("hx_select_search", "#main-panel")
|
|
|
|
parts = []
|
|
for pg in pages:
|
|
parts.append(_page_card_html(pg, ctx))
|
|
|
|
if page_num < total_pages:
|
|
current_local_href = ctx.get("current_local_href", "/index?type=pages")
|
|
next_url = f"{current_local_href}&page={page_num + 1}" if "?" in current_local_href else f"{current_local_href}?page={page_num + 1}"
|
|
parts.append(
|
|
f'<div id="sentinel-{page_num}-d" class="h-4 opacity-0 pointer-events-none"'
|
|
f' hx-get="{next_url}" hx-trigger="intersect once delay:250ms" hx-swap="outerHTML"></div>'
|
|
)
|
|
elif pages:
|
|
parts.append('<div class="col-span-full mt-4 text-center text-xs text-stone-400">End of results</div>')
|
|
else:
|
|
parts.append('<div class="col-span-full mt-8 text-center text-stone-500">No pages found.</div>')
|
|
|
|
return "".join(parts)
|
|
|
|
|
|
def _page_card_html(page: dict, ctx: dict) -> str:
|
|
"""Single page card."""
|
|
slug = page.get("slug", "")
|
|
href = call_url(ctx, "blog_url", f"/{slug}/")
|
|
hx_select = ctx.get("hx_select_search", "#main-panel")
|
|
|
|
parts = ['<article class="border-b pb-6 last:border-b-0 relative">']
|
|
parts.append(
|
|
f'<a href="{href}" hx-get="{href}" hx-target="#main-panel"'
|
|
f' hx-select="{hx_select}" hx-swap="outerHTML" hx-push-url="true"'
|
|
f' class="block rounded-xl bg-white shadow hover:shadow-md transition overflow-hidden">'
|
|
)
|
|
parts.append(f'<header class="mb-2 text-center"><h2 class="text-4xl font-bold text-stone-900">{escape(page.get("title", ""))}</h2>')
|
|
|
|
# Feature badges
|
|
features = page.get("features") or {}
|
|
if features:
|
|
parts.append('<div class="flex justify-center gap-2 mt-2">')
|
|
if features.get("calendar"):
|
|
parts.append('<span class="inline-block px-2 py-0.5 rounded-full text-xs font-semibold bg-blue-100 text-blue-800"><i class="fa fa-calendar mr-1"></i>Calendar</span>')
|
|
if features.get("market"):
|
|
parts.append('<span class="inline-block px-2 py-0.5 rounded-full text-xs font-semibold bg-green-100 text-green-800"><i class="fa fa-shopping-bag mr-1"></i>Market</span>')
|
|
parts.append('</div>')
|
|
|
|
pub = page.get("published_at")
|
|
if pub:
|
|
ts = pub.strftime("%-d %b %Y at %H:%M") if hasattr(pub, "strftime") else str(pub)
|
|
parts.append(f'<p class="text-sm text-stone-500">Published: {ts}</p>')
|
|
|
|
parts.append('</header>')
|
|
|
|
fi = page.get("feature_image")
|
|
if fi:
|
|
parts.append(f'<div class="mb-4"><img src="{fi}" alt="" class="rounded-lg w-full object-cover"></div>')
|
|
|
|
excerpt = page.get("custom_excerpt") or page.get("excerpt", "")
|
|
if excerpt:
|
|
parts.append(f'<p class="text-stone-700 text-lg leading-relaxed text-center">{escape(excerpt)}</p>')
|
|
|
|
parts.append('</a></article>')
|
|
return "".join(parts)
|
|
|
|
|
|
def _view_toggle_html(ctx: dict) -> str:
|
|
"""View toggle bar (list/tile) for desktop."""
|
|
view = ctx.get("view")
|
|
current_local_href = ctx.get("current_local_href", "/index")
|
|
hx_select = ctx.get("hx_select_search", "#main-panel")
|
|
|
|
list_cls = "bg-stone-200 text-stone-800" if view != "tile" else "text-stone-400 hover:text-stone-600"
|
|
tile_cls = "bg-stone-200 text-stone-800" if view == "tile" else "text-stone-400 hover:text-stone-600"
|
|
|
|
list_href = f"{current_local_href}"
|
|
tile_href = f"{current_local_href}{'&' if '?' in current_local_href else '?'}view=tile"
|
|
|
|
list_svg = '<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path stroke-linecap="round" stroke-linejoin="round" d="M4 6h16M4 12h16M4 18h16" /></svg>'
|
|
tile_svg = '<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path stroke-linecap="round" stroke-linejoin="round" d="M4 5a1 1 0 011-1h4a1 1 0 011 1v4a1 1 0 01-1 1H5a1 1 0 01-1-1V5zM14 5a1 1 0 011-1h4a1 1 0 011 1v4a1 1 0 01-1 1h-4a1 1 0 01-1-1V5zM4 15a1 1 0 011-1h4a1 1 0 011 1v4a1 1 0 01-1 1H5a1 1 0 01-1-1v-4zM14 15a1 1 0 011-1h4a1 1 0 011 1v4a1 1 0 01-1 1h-4a1 1 0 01-1-1v-4z" /></svg>'
|
|
|
|
return (
|
|
f'<div class="hidden md:flex justify-end px-3 pt-3 gap-1">'
|
|
f'<a href="{list_href}" hx-get="{list_href}" hx-target="#main-panel" hx-select="{hx_select}"'
|
|
f' hx-swap="outerHTML" hx-push-url="true" class="p-1.5 rounded {list_cls}" title="List view"'
|
|
f' _="on click js localStorage.removeItem(\'blog_view\') end">{list_svg}</a>'
|
|
f'<a href="{tile_href}" hx-get="{tile_href}" hx-target="#main-panel" hx-select="{hx_select}"'
|
|
f' hx-swap="outerHTML" hx-push-url="true" class="p-1.5 rounded {tile_cls}" title="Tile view"'
|
|
f' _="on click js localStorage.setItem(\'blog_view\',\'tile\') end">{tile_svg}</a>'
|
|
f'</div>'
|
|
)
|
|
|
|
|
|
def _content_type_tabs_html(ctx: dict) -> str:
|
|
"""Posts/Pages tabs."""
|
|
from quart import url_for as qurl
|
|
|
|
content_type = ctx.get("content_type", "posts")
|
|
hx_select = ctx.get("hx_select_search", "#main-panel")
|
|
|
|
posts_href = call_url(ctx, "blog_url", "/index")
|
|
pages_href = f"{posts_href}?type=pages"
|
|
|
|
posts_cls = "bg-stone-700 text-white" if content_type != "pages" else "bg-stone-100 text-stone-600 hover:bg-stone-200"
|
|
pages_cls = "bg-stone-700 text-white" if content_type == "pages" else "bg-stone-100 text-stone-600 hover:bg-stone-200"
|
|
|
|
return (
|
|
f'<div class="flex justify-center gap-1 px-3 pt-3">'
|
|
f'<a href="{posts_href}" hx-get="{posts_href}" hx-target="#main-panel"'
|
|
f' hx-select="{hx_select}" hx-swap="outerHTML" hx-push-url="true"'
|
|
f' class="px-4 py-1.5 rounded-t text-sm font-medium transition-colors {posts_cls}">Posts</a>'
|
|
f'<a href="{pages_href}" hx-get="{pages_href}" hx-target="#main-panel"'
|
|
f' hx-select="{hx_select}" hx-swap="outerHTML" hx-push-url="true"'
|
|
f' class="px-4 py-1.5 rounded-t text-sm font-medium transition-colors {pages_cls}">Pages</a>'
|
|
f'</div>'
|
|
)
|
|
|
|
|
|
def _blog_main_panel_html(ctx: dict) -> str:
|
|
"""Blog index main panel with tabs, toggle, and cards."""
|
|
content_type = ctx.get("content_type", "posts")
|
|
view = ctx.get("view")
|
|
|
|
parts = [_content_type_tabs_html(ctx)]
|
|
|
|
if content_type == "pages":
|
|
parts.append('<div class="max-w-full px-3 py-3 space-y-3">')
|
|
parts.append(_page_cards_html(ctx))
|
|
parts.append('</div><div class="pb-8"></div>')
|
|
else:
|
|
parts.append(_view_toggle_html(ctx))
|
|
if view == "tile":
|
|
parts.append('<div class="max-w-full px-3 py-3 grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-4">')
|
|
else:
|
|
parts.append('<div class="max-w-full px-3 py-3 space-y-3">')
|
|
parts.append(_blog_cards_html(ctx))
|
|
parts.append('</div><div class="pb-8"></div>')
|
|
|
|
return "".join(parts)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Desktop aside (filter sidebar)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def _blog_aside_html(ctx: dict) -> str:
|
|
"""Desktop aside with search, action buttons, and filters."""
|
|
parts = []
|
|
parts.append(search_desktop_html(ctx))
|
|
parts.append(_action_buttons_html(ctx))
|
|
parts.append(f'<div id="category-summary-desktop" hxx-swap-oob="outerHTML">')
|
|
parts.append(_tag_groups_filter_html(ctx))
|
|
parts.append(_authors_filter_html(ctx))
|
|
parts.append('</div>')
|
|
parts.append('<div id="filter-summary-desktop" hxx-swap-oob="outerHTML"></div>')
|
|
return "".join(parts)
|
|
|
|
|
|
def _blog_filter_html(ctx: dict) -> str:
|
|
"""Mobile filter (details/summary)."""
|
|
current_local_href = ctx.get("current_local_href", "/index")
|
|
search = ctx.get("search", "")
|
|
search_count = ctx.get("search_count", "")
|
|
hx_select = ctx.get("hx_select", "#main-panel")
|
|
|
|
# Mobile filter summary tags
|
|
summary_parts = []
|
|
summary_parts.append(_tag_groups_filter_summary_html(ctx))
|
|
summary_parts.append(_authors_filter_summary_html(ctx))
|
|
summary_html = "".join(summary_parts)
|
|
|
|
filter_content = search_mobile_html(ctx) + summary_html
|
|
action_buttons = _action_buttons_html(ctx)
|
|
filter_details = _tag_groups_filter_html(ctx) + _authors_filter_html(ctx)
|
|
|
|
return sexp(
|
|
'(~mobile-filter :filter-summary-html fsh :action-buttons-html abh'
|
|
' :filter-details-html fdh)',
|
|
fsh=filter_content,
|
|
abh=action_buttons,
|
|
fdh=filter_details,
|
|
)
|
|
|
|
|
|
def _action_buttons_html(ctx: dict) -> str:
|
|
"""New Post/Page + Drafts toggle buttons."""
|
|
from quart import g
|
|
|
|
rights = ctx.get("rights") or {}
|
|
has_admin = rights.get("admin") if isinstance(rights, dict) else getattr(rights, "admin", False)
|
|
user = getattr(g, "user", None)
|
|
hx_select = ctx.get("hx_select_search", "#main-panel")
|
|
drafts = ctx.get("drafts")
|
|
draft_count = ctx.get("draft_count", 0)
|
|
current_local_href = ctx.get("current_local_href", "/index")
|
|
|
|
parts = ['<div class="flex flex-wrap gap-2 px-4 py-3">']
|
|
|
|
if has_admin:
|
|
new_href = call_url(ctx, "blog_url", "/new/")
|
|
parts.append(
|
|
f'<a href="{new_href}" hx-get="{new_href}" hx-target="#main-panel"'
|
|
f' hx-select="{hx_select}" hx-swap="outerHTML" hx-push-url="true"'
|
|
f' class="px-3 py-1 rounded bg-stone-700 text-white text-sm hover:bg-stone-800 transition-colors"'
|
|
f' title="New Post"><i class="fa fa-plus mr-1"></i> New Post</a>'
|
|
)
|
|
new_page_href = call_url(ctx, "blog_url", "/new-page/")
|
|
parts.append(
|
|
f'<a href="{new_page_href}" hx-get="{new_page_href}" hx-target="#main-panel"'
|
|
f' hx-select="{hx_select}" hx-swap="outerHTML" hx-push-url="true"'
|
|
f' class="px-3 py-1 rounded bg-blue-600 text-white text-sm hover:bg-blue-700 transition-colors"'
|
|
f' title="New Page"><i class="fa fa-plus mr-1"></i> New Page</a>'
|
|
)
|
|
|
|
if user and (draft_count or drafts):
|
|
if drafts:
|
|
off_href = f"{current_local_href}"
|
|
parts.append(
|
|
f'<a href="{off_href}" hx-get="{off_href}" hx-target="#main-panel"'
|
|
f' hx-select="{hx_select}" hx-swap="outerHTML" hx-push-url="true"'
|
|
f' class="px-3 py-1 rounded bg-stone-700 text-white text-sm hover:bg-stone-800 transition-colors"'
|
|
f' title="Hide Drafts"><i class="fa fa-file-text-o mr-1"></i> Drafts'
|
|
f' <span class="inline-block bg-stone-500 text-white px-1.5 py-0.5 text-xs font-medium rounded ml-1">{draft_count}</span></a>'
|
|
)
|
|
else:
|
|
on_href = f"{current_local_href}{'&' if '?' in current_local_href else '?'}drafts=1"
|
|
parts.append(
|
|
f'<a href="{on_href}" hx-get="{on_href}" hx-target="#main-panel"'
|
|
f' hx-select="{hx_select}" hx-swap="outerHTML" hx-push-url="true"'
|
|
f' class="px-3 py-1 rounded bg-amber-600 text-white text-sm hover:bg-amber-700 transition-colors"'
|
|
f' title="Show Drafts"><i class="fa fa-file-text-o mr-1"></i> Drafts'
|
|
f' <span class="inline-block bg-amber-500 text-white px-1.5 py-0.5 text-xs font-medium rounded ml-1">{draft_count}</span></a>'
|
|
)
|
|
|
|
parts.append('</div>')
|
|
return "".join(parts)
|
|
|
|
|
|
def _tag_groups_filter_html(ctx: dict) -> str:
|
|
"""Tag group filter bar for desktop/mobile."""
|
|
tag_groups = ctx.get("tag_groups") or []
|
|
selected_groups = ctx.get("selected_groups") or ()
|
|
selected_tags = ctx.get("selected_tags") or ()
|
|
hx_select = ctx.get("hx_select_search", "#main-panel")
|
|
|
|
parts = ['<nav class="max-w-3xl mx-auto px-4 pb-4 flex flex-wrap gap-2 text-sm">'
|
|
'<ul class="divide-y flex flex-col gap-3">']
|
|
|
|
# "Any Topic" link
|
|
is_any = len(selected_groups) == 0 and len(selected_tags) == 0
|
|
any_cls = "bg-stone-900 text-white border-stone-900" if is_any else "bg-white text-stone-600 border-stone-300 hover:bg-stone-50"
|
|
parts.append(
|
|
f'<li><a class="px-3 py-1 rounded border {any_cls}"'
|
|
f' hx-get="?page=1" hx-target="#main-panel" hx-select="{hx_select}"'
|
|
f' hx-swap="outerHTML" hx-push-url="true">Any Topic</a></li>'
|
|
)
|
|
|
|
for group in tag_groups:
|
|
g_slug = getattr(group, "slug", "") if hasattr(group, "slug") else group.get("slug", "")
|
|
g_name = getattr(group, "name", "") if hasattr(group, "name") else group.get("name", "")
|
|
g_fi = getattr(group, "feature_image", None) if hasattr(group, "feature_image") else group.get("feature_image")
|
|
g_colour = getattr(group, "colour", None) if hasattr(group, "colour") else group.get("colour")
|
|
g_count = getattr(group, "post_count", 0) if hasattr(group, "post_count") else group.get("post_count", 0)
|
|
|
|
if g_count <= 0 and g_slug not in selected_groups:
|
|
continue
|
|
|
|
is_on = g_slug in selected_groups
|
|
cls = "bg-stone-900 text-white border-stone-900" if is_on else "bg-white text-stone-600 border-stone-300 hover:bg-stone-50"
|
|
|
|
if g_fi:
|
|
icon = f'<img src="{g_fi}" alt="{escape(g_name)}" class="h-6 w-6 rounded-full object-cover border border-stone-300 flex-shrink-0">'
|
|
else:
|
|
style = f"background-color: {g_colour}; color: white;" if g_colour else "background-color: #e7e5e4; color: #57534e;"
|
|
icon = (
|
|
f'<div class="h-6 w-6 rounded-full text-[10px] font-semibold flex items-center justify-center'
|
|
f' border border-stone-300 flex-shrink-0" style="{style}">{escape(g_name[:1])}</div>'
|
|
)
|
|
|
|
parts.append(
|
|
f'<li><a class="flex items-center gap-2 px-3 py-1 rounded border {cls}"'
|
|
f' hx-get="?group={g_slug}&page=1" hx-target="#main-panel" hx-select="{hx_select}"'
|
|
f' hx-swap="outerHTML" hx-push-url="true">{icon}'
|
|
f'<span class="inline-block rounded-full bg-stone-100 text-stone-600 px-2 py-1 text-sm font-medium border border-stone-200">{escape(g_name)}</span>'
|
|
f'<span class="flex-1"></span>'
|
|
f'<span class="inline-block bg-stone-100 text-stone-600 px-2 py-1 text-xs font-medium border border-stone-200">{g_count}</span>'
|
|
f'</a></li>'
|
|
)
|
|
|
|
parts.append('</ul></nav>')
|
|
return "".join(parts)
|
|
|
|
|
|
def _authors_filter_html(ctx: dict) -> str:
|
|
"""Author filter bar for desktop/mobile."""
|
|
authors = ctx.get("authors") or []
|
|
selected_authors = ctx.get("selected_authors") or ()
|
|
hx_select = ctx.get("hx_select_search", "#main-panel")
|
|
|
|
parts = ['<nav class="max-w-3xl mx-auto px-4 pb-4 flex flex-wrap gap-2 text-sm">'
|
|
'<ul class="divide-y flex flex-col gap-3">']
|
|
|
|
is_any = len(selected_authors) == 0
|
|
any_cls = "bg-stone-900 text-white border-stone-900" if is_any else "bg-white text-stone-600 border-stone-300 hover:bg-stone-50"
|
|
parts.append(
|
|
f'<li><a class="px-3 py-1 rounded {any_cls}"'
|
|
f' hx-get="?page=1" hx-target="#main-panel" hx-select="{hx_select}"'
|
|
f' hx-swap="outerHTML" hx-push-url="true">Any author</a></li>'
|
|
)
|
|
|
|
for author in authors:
|
|
a_slug = getattr(author, "slug", "") if hasattr(author, "slug") else author.get("slug", "")
|
|
a_name = getattr(author, "name", "") if hasattr(author, "name") else author.get("name", "")
|
|
a_img = getattr(author, "profile_image", None) if hasattr(author, "profile_image") else author.get("profile_image")
|
|
a_count = getattr(author, "published_post_count", 0) if hasattr(author, "published_post_count") else author.get("published_post_count", 0)
|
|
|
|
is_on = a_slug in selected_authors
|
|
cls = "bg-stone-900 text-white border-stone-900" if is_on else "bg-white text-stone-600 border-stone-300 hover:bg-stone-50"
|
|
|
|
icon = ""
|
|
if a_img:
|
|
icon = f'<img src="{a_img}" alt="{escape(a_name)}" class="h-5 w-5 rounded-full object-cover">'
|
|
|
|
parts.append(
|
|
f'<li><a class="flex items-center gap-2 px-3 py-1 rounded {cls}"'
|
|
f' hx-get="?author={a_slug}&page=1" hx-target="#main-panel" hx-select="{hx_select}"'
|
|
f' hx-swap="outerHTML" hx-push-url="true">{icon}'
|
|
f'<span class="text-stone-700">{escape(a_name)}</span>'
|
|
f'<span class="flex-1"></span>'
|
|
f'<span class="inline-block bg-stone-100 text-stone-600 px-2 py-1 text-xs font-medium border border-stone-200">{a_count}</span>'
|
|
f'</a></li>'
|
|
)
|
|
|
|
parts.append('</ul></nav>')
|
|
return "".join(parts)
|
|
|
|
|
|
def _tag_groups_filter_summary_html(ctx: dict) -> str:
|
|
"""Mobile filter summary for tag groups."""
|
|
selected_groups = ctx.get("selected_groups") or ()
|
|
tag_groups = ctx.get("tag_groups") or []
|
|
if not selected_groups:
|
|
return ""
|
|
names = []
|
|
for g in tag_groups:
|
|
g_slug = getattr(g, "slug", "") if hasattr(g, "slug") else g.get("slug", "")
|
|
g_name = getattr(g, "name", "") if hasattr(g, "name") else g.get("name", "")
|
|
if g_slug in selected_groups:
|
|
names.append(g_name)
|
|
if not names:
|
|
return ""
|
|
return f'<span class="text-sm text-stone-600">{escape(", ".join(names))}</span>'
|
|
|
|
|
|
def _authors_filter_summary_html(ctx: dict) -> str:
|
|
"""Mobile filter summary for authors."""
|
|
selected_authors = ctx.get("selected_authors") or ()
|
|
authors = ctx.get("authors") or []
|
|
if not selected_authors:
|
|
return ""
|
|
names = []
|
|
for a in authors:
|
|
a_slug = getattr(a, "slug", "") if hasattr(a, "slug") else a.get("slug", "")
|
|
a_name = getattr(a, "name", "") if hasattr(a, "name") else a.get("name", "")
|
|
if a_slug in selected_authors:
|
|
names.append(a_name)
|
|
if not names:
|
|
return ""
|
|
return f'<span class="text-sm text-stone-600">{escape(", ".join(names))}</span>'
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Post detail main panel
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def _post_main_panel_html(ctx: dict) -> str:
|
|
"""Post/page article content."""
|
|
from quart import g, url_for as qurl
|
|
|
|
post = ctx.get("post") or {}
|
|
slug = post.get("slug", "")
|
|
user = getattr(g, "user", None)
|
|
rights = ctx.get("rights") or {}
|
|
is_admin = rights.get("admin") if isinstance(rights, dict) else getattr(rights, "admin", False)
|
|
hx_select = ctx.get("hx_select_search", "#main-panel")
|
|
|
|
parts = ['<article class="relative">']
|
|
|
|
# Draft indicator
|
|
if post.get("status") == "draft":
|
|
parts.append('<div class="flex items-center justify-center gap-2 mb-3">')
|
|
parts.append('<span class="inline-block px-3 py-1 rounded-full text-sm font-semibold bg-amber-100 text-amber-800">Draft</span>')
|
|
if post.get("publish_requested"):
|
|
parts.append('<span class="inline-block px-3 py-1 rounded-full text-sm font-semibold bg-blue-100 text-blue-800">Publish requested</span>')
|
|
if is_admin or (user and post.get("user_id") == getattr(user, "id", None)):
|
|
edit_href = qurl("blog.post.admin.edit", slug=slug)
|
|
parts.append(
|
|
f'<a href="{edit_href}" hx-get="{edit_href}" hx-target="#main-panel"'
|
|
f' hx-select="{hx_select}" hx-swap="outerHTML" hx-push-url="true"'
|
|
f' class="inline-block px-3 py-1 rounded-full text-sm font-semibold bg-stone-700 text-white hover:bg-stone-800 transition-colors">'
|
|
f'<i class="fa fa-pencil mr-1"></i> Edit</a>'
|
|
)
|
|
parts.append('</div>')
|
|
|
|
# Blog post chrome (not for pages)
|
|
if not post.get("is_page"):
|
|
if user:
|
|
liked = post.get("is_liked", False)
|
|
like_url = call_url(ctx, "blog_url", f"/{slug}/like/toggle/")
|
|
parts.append(
|
|
f'<div class="absolute top-2 right-2 z-10 text-8xl md:text-6xl">'
|
|
f'<button hx-post="{like_url}" hx-swap="outerHTML"'
|
|
f' hx-headers=\'{{\"X-CSRFToken\": \"{ctx.get("csrf_token", "")}\"}}\''
|
|
f' class="cursor-pointer">{"❤️" if liked else "🤍"}</button></div>'
|
|
)
|
|
|
|
if post.get("custom_excerpt"):
|
|
parts.append(f'<div class="w-full text-center italic text-3xl p-2">{post["custom_excerpt"]}</div>')
|
|
|
|
# Desktop at_bar
|
|
parts.append(f'<div class="hidden md:block">{_at_bar_html(post, ctx)}</div>')
|
|
|
|
# Feature image
|
|
fi = post.get("feature_image")
|
|
if fi:
|
|
parts.append(f'<div class="mb-3 flex justify-center"><img src="{fi}" alt="" class="rounded-lg w-full md:w-3/4 object-cover"></div>')
|
|
|
|
# Post HTML content
|
|
html_content = post.get("html", "")
|
|
if html_content:
|
|
parts.append(f'<div class="blog-content p-2">{html_content}</div>')
|
|
|
|
parts.append('</article><div class="pb-8"></div>')
|
|
return "".join(parts)
|
|
|
|
|
|
def _post_meta_html(ctx: dict) -> str:
|
|
"""Post SEO meta tags (Open Graph, Twitter, JSON-LD)."""
|
|
post = ctx.get("post") or {}
|
|
base_title = ctx.get("base_title", "")
|
|
|
|
is_public = post.get("visibility") == "public"
|
|
is_published = post.get("status") == "published"
|
|
email_only = post.get("email_only", False)
|
|
robots = "index,follow" if (is_public and is_published and not email_only) else "noindex,nofollow"
|
|
|
|
# Description
|
|
desc = (post.get("meta_description") or post.get("og_description") or
|
|
post.get("twitter_description") or post.get("custom_excerpt") or
|
|
post.get("excerpt") or "")
|
|
if not desc and post.get("html"):
|
|
import re
|
|
desc = re.sub(r'<[^>]+>', '', post["html"])
|
|
desc = desc.replace("\n", " ").replace("\r", " ").strip()[:160]
|
|
|
|
# Image
|
|
image = (post.get("og_image") or post.get("twitter_image") or post.get("feature_image") or "")
|
|
|
|
# Canonical
|
|
from quart import request as req
|
|
canonical = post.get("canonical_url") or (req.url if req else "")
|
|
|
|
og_title = post.get("og_title") or base_title
|
|
tw_title = post.get("twitter_title") or base_title
|
|
is_article = not post.get("is_page")
|
|
|
|
parts = [f'<meta name="robots" content="{robots}">']
|
|
parts.append(f'<title>{escape(base_title)}</title>')
|
|
parts.append(f'<meta name="description" content="{escape(desc)}">')
|
|
if canonical:
|
|
parts.append(f'<link rel="canonical" href="{escape(canonical)}">')
|
|
|
|
parts.append(f'<meta property="og:type" content="{"article" if is_article else "website"}">')
|
|
parts.append(f'<meta property="og:title" content="{escape(og_title)}">')
|
|
parts.append(f'<meta property="og:description" content="{escape(desc)}">')
|
|
if canonical:
|
|
parts.append(f'<meta property="og:url" content="{escape(canonical)}">')
|
|
if image:
|
|
parts.append(f'<meta property="og:image" content="{escape(image)}">')
|
|
|
|
parts.append(f'<meta name="twitter:card" content="{"summary_large_image" if image else "summary"}">')
|
|
parts.append(f'<meta name="twitter:title" content="{escape(tw_title)}">')
|
|
parts.append(f'<meta name="twitter:description" content="{escape(desc)}">')
|
|
if image:
|
|
parts.append(f'<meta name="twitter:image" content="{escape(image)}">')
|
|
|
|
return "".join(parts)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Home page (Ghost "home" page)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def _home_main_panel_html(ctx: dict) -> str:
|
|
"""Home page content — renders the Ghost page HTML."""
|
|
post = ctx.get("post") or {}
|
|
html = post.get("html", "")
|
|
return f'<article class="relative"><div class="blog-content p-2">{html}</div></article>'
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Post admin - empty main panel
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def _post_admin_main_panel_html(ctx: dict) -> str:
|
|
return '<div class="pb-8"></div>'
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Settings main panels
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def _settings_main_panel_html(ctx: dict) -> str:
|
|
return '<div class="max-w-2xl mx-auto px-4 py-6"></div>'
|
|
|
|
|
|
def _cache_main_panel_html(ctx: dict) -> str:
|
|
from quart import url_for as qurl
|
|
|
|
csrf = ctx.get("csrf_token", "")
|
|
clear_url = qurl("settings.cache_clear")
|
|
return (
|
|
f'<div class="max-w-2xl mx-auto px-4 py-6 space-y-6">'
|
|
f'<div class="flex flex-col md:flex-row gap-3 items-start">'
|
|
f'<form hx-post="{clear_url}" hx-trigger="submit" hx-target="#cache-status" hx-swap="innerHTML">'
|
|
f'<input type="hidden" name="csrf_token" value="{csrf}">'
|
|
f'<button class="border rounded px-4 py-2 bg-stone-800 text-white text-sm" type="submit">Clear cache</button>'
|
|
f'</form>'
|
|
f'<div id="cache-status" class="py-2"></div>'
|
|
f'</div></div>'
|
|
)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Snippets main panel
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def _snippets_main_panel_html(ctx: dict) -> str:
|
|
return (
|
|
f'<div class="max-w-4xl mx-auto p-6">'
|
|
f'<div class="mb-6 flex justify-between items-center">'
|
|
f'<h1 class="text-3xl font-bold">Snippets</h1></div>'
|
|
f'<div id="snippets-list">{_snippets_list_html(ctx)}</div></div>'
|
|
)
|
|
|
|
|
|
def _snippets_list_html(ctx: dict) -> str:
|
|
"""Snippets list with visibility badges and delete buttons."""
|
|
from quart import url_for as qurl, g
|
|
|
|
snippets = ctx.get("snippets") or []
|
|
is_admin = ctx.get("is_admin", False)
|
|
csrf = ctx.get("csrf_token", "")
|
|
user = getattr(g, "user", None)
|
|
user_id = getattr(user, "id", None)
|
|
|
|
if not snippets:
|
|
return (
|
|
'<div class="bg-white rounded-lg shadow">'
|
|
'<div class="p-8 text-center text-stone-400">'
|
|
'<i class="fa fa-puzzle-piece text-4xl mb-2"></i>'
|
|
'<p>No snippets yet. Create one from the blog editor.</p></div></div>'
|
|
)
|
|
|
|
badge_colours = {
|
|
"private": "bg-stone-200 text-stone-700",
|
|
"shared": "bg-blue-100 text-blue-700",
|
|
"admin": "bg-amber-100 text-amber-700",
|
|
}
|
|
|
|
parts = ['<div class="bg-white rounded-lg shadow"><div class="divide-y">']
|
|
for s in snippets:
|
|
s_id = getattr(s, "id", None) or s.get("id")
|
|
s_name = getattr(s, "name", "") if hasattr(s, "name") else s.get("name", "")
|
|
s_uid = getattr(s, "user_id", None) if hasattr(s, "user_id") else s.get("user_id")
|
|
s_vis = getattr(s, "visibility", "private") if hasattr(s, "visibility") else s.get("visibility", "private")
|
|
|
|
owner = "You" if s_uid == user_id else f"User #{s_uid}"
|
|
badge_cls = badge_colours.get(s_vis, "bg-stone-200 text-stone-700")
|
|
|
|
parts.append(
|
|
f'<div class="flex items-center gap-4 p-4 hover:bg-stone-50 transition">'
|
|
f'<div class="flex-1 min-w-0"><div class="font-medium truncate">{escape(s_name)}</div>'
|
|
f'<div class="text-xs text-stone-500">{owner}</div></div>'
|
|
f'<span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium {badge_cls}">{s_vis}</span>'
|
|
)
|
|
|
|
if is_admin:
|
|
patch_url = qurl("snippets.patch_visibility", snippet_id=s_id)
|
|
parts.append(
|
|
f'<select name="visibility" hx-patch="{patch_url}" hx-target="#snippets-list" hx-swap="innerHTML"'
|
|
f' hx-headers=\'{{\"X-CSRFToken\": \"{csrf}\"}}\' class="text-sm border border-stone-300 rounded px-2 py-1">'
|
|
)
|
|
for v in ["private", "shared", "admin"]:
|
|
sel = " selected" if s_vis == v else ""
|
|
parts.append(f'<option value="{v}"{sel}>{v}</option>')
|
|
parts.append('</select>')
|
|
|
|
if s_uid == user_id or is_admin:
|
|
del_url = qurl("snippets.delete_snippet", snippet_id=s_id)
|
|
parts.append(
|
|
f'<button type="button" data-confirm data-confirm-title="Delete snippet?"'
|
|
f' data-confirm-text="Delete “{escape(s_name)}”?"'
|
|
f' data-confirm-icon="warning" data-confirm-confirm-text="Yes, delete"'
|
|
f' data-confirm-cancel-text="Cancel" data-confirm-event="confirmed"'
|
|
f' hx-delete="{del_url}" hx-trigger="confirmed" hx-target="#snippets-list" hx-swap="innerHTML"'
|
|
f' hx-headers=\'{{\"X-CSRFToken\": \"{csrf}\"}}\''
|
|
f' class="px-3 py-1 text-sm bg-red-200 hover:bg-red-300 rounded text-red-800 flex-shrink-0">'
|
|
f'<i class="fa fa-trash"></i> Delete</button>'
|
|
)
|
|
|
|
parts.append('</div>')
|
|
|
|
parts.append('</div></div>')
|
|
return "".join(parts)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Menu items main panel
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def _menu_items_main_panel_html(ctx: dict) -> str:
|
|
from quart import url_for as qurl
|
|
|
|
new_url = qurl("menu_items.new_menu_item")
|
|
return (
|
|
f'<div class="max-w-4xl mx-auto p-6">'
|
|
f'<div class="mb-6 flex justify-end items-center">'
|
|
f'<button type="button" hx-get="{new_url}" hx-target="#menu-item-form" hx-swap="innerHTML"'
|
|
f' class="px-4 py-2 bg-blue-600 text-white rounded hover:bg-blue-700">'
|
|
f'<i class="fa fa-plus"></i> Add Menu Item</button></div>'
|
|
f'<div id="menu-item-form" class="mb-6"></div>'
|
|
f'<div id="menu-items-list">{_menu_items_list_html(ctx)}</div></div>'
|
|
)
|
|
|
|
|
|
def _menu_items_list_html(ctx: dict) -> str:
|
|
from quart import url_for as qurl
|
|
|
|
menu_items = ctx.get("menu_items") or []
|
|
csrf = ctx.get("csrf_token", "")
|
|
|
|
if not menu_items:
|
|
return (
|
|
'<div class="bg-white rounded-lg shadow">'
|
|
'<div class="p-8 text-center text-stone-400">'
|
|
'<i class="fa fa-inbox text-4xl mb-2"></i>'
|
|
'<p>No menu items yet. Add one to get started!</p></div></div>'
|
|
)
|
|
|
|
parts = ['<div class="bg-white rounded-lg shadow"><div class="divide-y">']
|
|
for item in menu_items:
|
|
i_id = getattr(item, "id", None) or item.get("id")
|
|
label = getattr(item, "label", "") if hasattr(item, "label") else item.get("label", "")
|
|
slug = getattr(item, "slug", "") if hasattr(item, "slug") else item.get("slug", "")
|
|
fi = getattr(item, "feature_image", None) if hasattr(item, "feature_image") else item.get("feature_image")
|
|
sort = getattr(item, "sort_order", 0) if hasattr(item, "sort_order") else item.get("sort_order", 0)
|
|
|
|
edit_url = qurl("menu_items.edit_menu_item", item_id=i_id)
|
|
del_url = qurl("menu_items.delete_menu_item_route", item_id=i_id)
|
|
|
|
img = (f'<img src="{fi}" alt="{escape(label)}" class="w-12 h-12 rounded-full object-cover flex-shrink-0" />'
|
|
if fi else '<div class="w-12 h-12 rounded-full bg-stone-200 flex-shrink-0"></div>')
|
|
|
|
parts.append(
|
|
f'<div class="flex items-center gap-4 p-4 hover:bg-stone-50 transition">'
|
|
f'<div class="text-stone-400 cursor-move"><i class="fa fa-grip-vertical"></i></div>'
|
|
f'{img}'
|
|
f'<div class="flex-1 min-w-0"><div class="font-medium truncate">{escape(label)}</div>'
|
|
f'<div class="text-xs text-stone-500 truncate">{escape(slug)}</div></div>'
|
|
f'<div class="text-sm text-stone-500">Order: {sort}</div>'
|
|
f'<div class="flex gap-2 flex-shrink-0">'
|
|
f'<button type="button" hx-get="{edit_url}" hx-target="#menu-item-form" hx-swap="innerHTML"'
|
|
f' class="px-3 py-1 text-sm bg-stone-200 hover:bg-stone-300 rounded"><i class="fa fa-edit"></i> Edit</button>'
|
|
f'<button type="button" data-confirm data-confirm-title="Delete menu item?"'
|
|
f' data-confirm-text="Remove {escape(label)} from the menu?"'
|
|
f' data-confirm-icon="warning" data-confirm-confirm-text="Yes, delete"'
|
|
f' data-confirm-cancel-text="Cancel" data-confirm-event="confirmed"'
|
|
f' hx-delete="{del_url}" hx-trigger="confirmed" hx-target="#menu-items-list" hx-swap="innerHTML"'
|
|
f' hx-headers=\'{{\"X-CSRFToken\": \"{csrf}\"}}\''
|
|
f' class="px-3 py-1 text-sm bg-red-200 hover:bg-red-300 rounded text-red-800">'
|
|
f'<i class="fa fa-trash"></i> Delete</button></div></div>'
|
|
)
|
|
|
|
parts.append('</div></div>')
|
|
return "".join(parts)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Tag groups main panel
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def _tag_groups_main_panel_html(ctx: dict) -> str:
|
|
from quart import url_for as qurl
|
|
|
|
groups = ctx.get("groups") or []
|
|
unassigned_tags = ctx.get("unassigned_tags") or []
|
|
csrf = ctx.get("csrf_token", "")
|
|
|
|
parts = ['<div class="max-w-2xl mx-auto px-4 py-6 space-y-8">']
|
|
|
|
# Create form
|
|
create_url = qurl("blog.tag_groups_admin.create")
|
|
parts.append(
|
|
f'<form method="post" action="{create_url}" class="border rounded p-4 bg-white space-y-3">'
|
|
f'<input type="hidden" name="csrf_token" value="{csrf}">'
|
|
f'<h3 class="text-sm font-semibold text-stone-700">New Group</h3>'
|
|
f'<div class="flex flex-col sm:flex-row gap-3">'
|
|
f'<input type="text" name="name" placeholder="Group name" required class="flex-1 border rounded px-3 py-2 text-sm">'
|
|
f'<input type="text" name="colour" placeholder="#colour" class="w-28 border rounded px-3 py-2 text-sm">'
|
|
f'<input type="number" name="sort_order" placeholder="Order" value="0" class="w-20 border rounded px-3 py-2 text-sm">'
|
|
f'</div>'
|
|
f'<input type="text" name="feature_image" placeholder="Image URL (optional)" class="w-full border rounded px-3 py-2 text-sm">'
|
|
f'<button type="submit" class="border rounded px-4 py-2 bg-stone-800 text-white text-sm">Create</button>'
|
|
f'</form>'
|
|
)
|
|
|
|
# Groups list
|
|
if groups:
|
|
parts.append('<ul class="space-y-2">')
|
|
for group in groups:
|
|
g_id = getattr(group, "id", None) or group.get("id")
|
|
g_name = getattr(group, "name", "") if hasattr(group, "name") else group.get("name", "")
|
|
g_slug = getattr(group, "slug", "") if hasattr(group, "slug") else group.get("slug", "")
|
|
g_fi = getattr(group, "feature_image", None) if hasattr(group, "feature_image") else group.get("feature_image")
|
|
g_colour = getattr(group, "colour", None) if hasattr(group, "colour") else group.get("colour")
|
|
g_sort = getattr(group, "sort_order", 0) if hasattr(group, "sort_order") else group.get("sort_order", 0)
|
|
|
|
edit_href = qurl("blog.tag_groups_admin.edit", id=g_id)
|
|
|
|
if g_fi:
|
|
icon = f'<img src="{g_fi}" alt="{escape(g_name)}" class="h-8 w-8 rounded-full object-cover border border-stone-300 flex-shrink-0">'
|
|
else:
|
|
style = f"background-color: {g_colour}; color: white;" if g_colour else "background-color: #e7e5e4; color: #57534e;"
|
|
icon = (
|
|
f'<div class="h-8 w-8 rounded-full text-xs font-semibold flex items-center justify-center border border-stone-300 flex-shrink-0"'
|
|
f' style="{style}">{escape(g_name[:1])}</div>'
|
|
)
|
|
|
|
parts.append(
|
|
f'<li class="border rounded p-3 bg-white flex items-center gap-3">{icon}'
|
|
f'<div class="flex-1"><a href="{edit_href}" class="font-medium text-stone-800 hover:underline">{escape(g_name)}</a>'
|
|
f'<span class="text-xs text-stone-500 ml-2">{escape(g_slug)}</span></div>'
|
|
f'<span class="text-xs text-stone-500">order: {g_sort}</span></li>'
|
|
)
|
|
parts.append('</ul>')
|
|
else:
|
|
parts.append('<p class="text-stone-500 text-sm">No tag groups yet.</p>')
|
|
|
|
# Unassigned tags
|
|
if unassigned_tags:
|
|
parts.append(f'<div class="border-t pt-4"><h3 class="text-sm font-semibold text-stone-700 mb-2">Unassigned Tags ({len(unassigned_tags)})</h3>')
|
|
parts.append('<div class="flex flex-wrap gap-2">')
|
|
for tag in unassigned_tags:
|
|
t_name = getattr(tag, "name", "") if hasattr(tag, "name") else tag.get("name", "")
|
|
parts.append(
|
|
f'<span class="inline-block bg-stone-100 text-stone-600 px-2 py-1 text-xs font-medium border border-stone-200 rounded">'
|
|
f'{escape(t_name)}</span>'
|
|
)
|
|
parts.append('</div></div>')
|
|
|
|
parts.append('</div>')
|
|
return "".join(parts)
|
|
|
|
|
|
def _tag_groups_edit_main_panel_html(ctx: dict) -> str:
|
|
from quart import url_for as qurl
|
|
|
|
group = ctx.get("group")
|
|
all_tags = ctx.get("all_tags") or []
|
|
assigned_tag_ids = ctx.get("assigned_tag_ids") or set()
|
|
csrf = ctx.get("csrf_token", "")
|
|
|
|
g_id = getattr(group, "id", None) or group.get("id") if group else None
|
|
g_name = getattr(group, "name", "") if hasattr(group, "name") else (group.get("name", "") if group else "")
|
|
g_colour = getattr(group, "colour", "") if hasattr(group, "colour") else (group.get("colour", "") if group else "")
|
|
g_sort = getattr(group, "sort_order", 0) if hasattr(group, "sort_order") else (group.get("sort_order", 0) if group else 0)
|
|
g_fi = getattr(group, "feature_image", "") if hasattr(group, "feature_image") else (group.get("feature_image", "") if group else "")
|
|
|
|
save_url = qurl("blog.tag_groups_admin.save", id=g_id)
|
|
del_url = qurl("blog.tag_groups_admin.delete_group", id=g_id)
|
|
|
|
parts = [f'<div class="max-w-2xl mx-auto px-4 py-6 space-y-6">']
|
|
|
|
# Edit form
|
|
parts.append(
|
|
f'<form method="post" action="{save_url}" class="border rounded p-4 bg-white space-y-4">'
|
|
f'<input type="hidden" name="csrf_token" value="{csrf}">'
|
|
f'<div class="space-y-3">'
|
|
f'<div><label class="block text-xs font-medium text-stone-600 mb-1">Name</label>'
|
|
f'<input type="text" name="name" value="{escape(g_name)}" required class="w-full border rounded px-3 py-2 text-sm"></div>'
|
|
f'<div class="flex gap-3"><div class="flex-1"><label class="block text-xs font-medium text-stone-600 mb-1">Colour</label>'
|
|
f'<input type="text" name="colour" value="{escape(g_colour or "")}" placeholder="#hex" class="w-full border rounded px-3 py-2 text-sm"></div>'
|
|
f'<div class="w-24"><label class="block text-xs font-medium text-stone-600 mb-1">Order</label>'
|
|
f'<input type="number" name="sort_order" value="{g_sort}" class="w-full border rounded px-3 py-2 text-sm"></div></div>'
|
|
f'<div><label class="block text-xs font-medium text-stone-600 mb-1">Feature Image URL</label>'
|
|
f'<input type="text" name="feature_image" value="{escape(g_fi or "")}" placeholder="https://..." class="w-full border rounded px-3 py-2 text-sm"></div>'
|
|
f'</div>'
|
|
)
|
|
|
|
# Tag checkboxes
|
|
parts.append(
|
|
'<div><label class="block text-xs font-medium text-stone-600 mb-2">Assign Tags</label>'
|
|
'<div class="grid grid-cols-1 sm:grid-cols-2 gap-1 max-h-64 overflow-y-auto border rounded p-2">'
|
|
)
|
|
for tag in all_tags:
|
|
t_id = getattr(tag, "id", None) or tag.get("id")
|
|
t_name = getattr(tag, "name", "") if hasattr(tag, "name") else tag.get("name", "")
|
|
t_fi = getattr(tag, "feature_image", None) if hasattr(tag, "feature_image") else tag.get("feature_image")
|
|
checked = " checked" if t_id in assigned_tag_ids else ""
|
|
img = f'<img src="{t_fi}" alt="" class="h-4 w-4 rounded-full object-cover">' if t_fi else ""
|
|
parts.append(
|
|
f'<label class="flex items-center gap-2 px-2 py-1 hover:bg-stone-50 rounded text-sm cursor-pointer">'
|
|
f'<input type="checkbox" name="tag_ids" value="{t_id}"{checked} class="rounded border-stone-300">'
|
|
f'{img}<span>{escape(t_name)}</span></label>'
|
|
)
|
|
parts.append('</div></div>')
|
|
|
|
parts.append(
|
|
'<div class="flex gap-3"><button type="submit" class="border rounded px-4 py-2 bg-stone-800 text-white text-sm">Save</button></div>'
|
|
'</form>'
|
|
)
|
|
|
|
# Delete form
|
|
parts.append(
|
|
f'<form method="post" action="{del_url}" class="border-t pt-4"'
|
|
f' onsubmit="return confirm(\'Delete this tag group? Tags will not be deleted.\')">'
|
|
f'<input type="hidden" name="csrf_token" value="{csrf}">'
|
|
f'<button type="submit" class="border rounded px-4 py-2 bg-red-600 text-white text-sm">Delete Group</button></form>'
|
|
)
|
|
|
|
parts.append('</div>')
|
|
return "".join(parts)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# New post/page main panel — left as render_template (uses Koenig editor JS)
|
|
# Post edit main panel — left as render_template (uses Koenig editor JS)
|
|
# Post settings main panel — left as render_template (complex form macros)
|
|
# Post entries main panel — left as render_template (calendar browser lazy-loads)
|
|
# Post data main panel — left as render_template (uses ORM introspection macros)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
# ===========================================================================
|
|
# PUBLIC API — called from route handlers
|
|
# ===========================================================================
|
|
|
|
# ---- Home page ----
|
|
|
|
async def render_home_page(ctx: dict) -> str:
|
|
root_hdr = root_header_html(ctx)
|
|
post_hdr = _post_header_html(ctx)
|
|
header_rows = root_hdr + post_hdr
|
|
content = _home_main_panel_html(ctx)
|
|
meta = _post_meta_html(ctx)
|
|
menu_html = ctx.get("nav_html", "") or ""
|
|
return full_page(ctx, header_rows_html=header_rows, content_html=content,
|
|
meta_html=meta, menu_html=menu_html)
|
|
|
|
|
|
async def render_home_oob(ctx: dict) -> str:
|
|
root_hdr = root_header_html(ctx, oob=True)
|
|
post_oob = _oob_header_html("root-header-child", "post-header-child",
|
|
_post_header_html(ctx))
|
|
content = _home_main_panel_html(ctx)
|
|
return oob_page(ctx, oobs_html=root_hdr + post_oob, content_html=content)
|
|
|
|
|
|
# ---- Blog index ----
|
|
|
|
async def render_blog_page(ctx: dict) -> str:
|
|
root_hdr = root_header_html(ctx)
|
|
blog_hdr = _blog_header_html(ctx)
|
|
header_rows = root_hdr + blog_hdr
|
|
content = _blog_main_panel_html(ctx)
|
|
aside = _blog_aside_html(ctx)
|
|
filter_html = _blog_filter_html(ctx)
|
|
return full_page(ctx, header_rows_html=header_rows, content_html=content,
|
|
aside_html=aside, filter_html=filter_html)
|
|
|
|
|
|
async def render_blog_oob(ctx: dict) -> str:
|
|
root_hdr = root_header_html(ctx, oob=True)
|
|
blog_oob = _oob_header_html("root-header-child", "blog-header-child",
|
|
_blog_header_html(ctx))
|
|
content = _blog_main_panel_html(ctx)
|
|
aside = _blog_aside_html(ctx)
|
|
filter_html = _blog_filter_html(ctx)
|
|
nav_html = ctx.get("nav_html", "") or ""
|
|
return oob_page(ctx, oobs_html=root_hdr + blog_oob,
|
|
content_html=content, aside_html=aside,
|
|
filter_html=filter_html, menu_html=nav_html)
|
|
|
|
|
|
async def render_blog_cards(ctx: dict) -> str:
|
|
"""Pagination-only response (page > 1)."""
|
|
return _blog_cards_html(ctx)
|
|
|
|
|
|
async def render_blog_page_cards(ctx: dict) -> str:
|
|
"""Page cards pagination response."""
|
|
return _page_cards_html(ctx)
|
|
|
|
|
|
# ---- New post/page ----
|
|
|
|
async def render_new_post_page(ctx: dict) -> str:
|
|
root_hdr = root_header_html(ctx)
|
|
blog_hdr = _blog_header_html(ctx)
|
|
header_rows = root_hdr + blog_hdr
|
|
# Content comes from Jinja (editor template)
|
|
content = ctx.get("editor_html", "")
|
|
return full_page(ctx, header_rows_html=header_rows, content_html=content)
|
|
|
|
|
|
async def render_new_post_oob(ctx: dict) -> str:
|
|
root_hdr = root_header_html(ctx, oob=True)
|
|
blog_oob = _blog_header_html(ctx, oob=True)
|
|
content = ctx.get("editor_html", "")
|
|
return oob_page(ctx, oobs_html=root_hdr + blog_oob, content_html=content)
|
|
|
|
|
|
# ---- Post detail ----
|
|
|
|
async def render_post_page(ctx: dict) -> str:
|
|
root_hdr = root_header_html(ctx)
|
|
post_hdr = _post_header_html(ctx)
|
|
header_rows = root_hdr + post_hdr
|
|
content = _post_main_panel_html(ctx)
|
|
meta = _post_meta_html(ctx)
|
|
menu_html = ctx.get("nav_html", "") or ""
|
|
return full_page(ctx, header_rows_html=header_rows, content_html=content,
|
|
meta_html=meta, menu_html=menu_html)
|
|
|
|
|
|
async def render_post_oob(ctx: dict) -> str:
|
|
root_hdr = root_header_html(ctx, oob=True)
|
|
post_oob = _oob_header_html("root-header-child", "post-header-child",
|
|
_post_header_html(ctx))
|
|
content = _post_main_panel_html(ctx)
|
|
menu_html = ctx.get("nav_html", "") or ""
|
|
return oob_page(ctx, oobs_html=root_hdr + post_oob,
|
|
content_html=content, menu_html=menu_html)
|
|
|
|
|
|
# ---- Post admin ----
|
|
|
|
async def render_post_admin_page(ctx: dict) -> str:
|
|
root_hdr = root_header_html(ctx)
|
|
post_hdr = _post_header_html(ctx)
|
|
admin_hdr = _post_admin_header_html(ctx)
|
|
header_rows = root_hdr + post_hdr + admin_hdr
|
|
content = _post_admin_main_panel_html(ctx)
|
|
menu_html = ctx.get("nav_html", "") or ""
|
|
return full_page(ctx, header_rows_html=header_rows, content_html=content,
|
|
menu_html=menu_html)
|
|
|
|
|
|
async def render_post_admin_oob(ctx: dict) -> str:
|
|
post_hdr_oob = _post_header_html(ctx, oob=True)
|
|
admin_oob = _oob_header_html("post-header-child", "post-admin-header-child",
|
|
_post_admin_header_html(ctx))
|
|
content = _post_admin_main_panel_html(ctx)
|
|
menu_html = ctx.get("nav_html", "") or ""
|
|
return oob_page(ctx, oobs_html=post_hdr_oob + admin_oob,
|
|
content_html=content, menu_html=menu_html)
|
|
|
|
|
|
# ---- Post data ----
|
|
|
|
async def render_post_data_page(ctx: dict) -> str:
|
|
root_hdr = root_header_html(ctx)
|
|
post_hdr = _post_header_html(ctx)
|
|
admin_hdr = _post_admin_header_html(ctx)
|
|
from quart import url_for as qurl
|
|
slug = (ctx.get("post") or {}).get("slug", "")
|
|
data_hdr = _post_sub_admin_header_html(
|
|
"post_data-row", "post_data-header-child",
|
|
qurl("blog.post.admin.data", slug=slug),
|
|
"database", "data", ctx,
|
|
)
|
|
header_rows = root_hdr + post_hdr + admin_hdr + data_hdr
|
|
content = ctx.get("data_html", "")
|
|
return full_page(ctx, header_rows_html=header_rows, content_html=content)
|
|
|
|
|
|
async def render_post_data_oob(ctx: dict) -> str:
|
|
admin_hdr_oob = _post_admin_header_html(ctx, oob=True)
|
|
from quart import url_for as qurl
|
|
slug = (ctx.get("post") or {}).get("slug", "")
|
|
data_hdr = _post_sub_admin_header_html(
|
|
"post_data-row", "post_data-header-child",
|
|
qurl("blog.post.admin.data", slug=slug),
|
|
"database", "data", ctx,
|
|
)
|
|
data_oob = _oob_header_html("post-admin-header-child", "post_data-header-child",
|
|
data_hdr)
|
|
content = ctx.get("data_html", "")
|
|
return oob_page(ctx, oobs_html=admin_hdr_oob + data_oob, content_html=content)
|
|
|
|
|
|
# ---- Post entries ----
|
|
|
|
async def render_post_entries_page(ctx: dict) -> str:
|
|
root_hdr = root_header_html(ctx)
|
|
post_hdr = _post_header_html(ctx)
|
|
admin_hdr = _post_admin_header_html(ctx)
|
|
from quart import url_for as qurl
|
|
slug = (ctx.get("post") or {}).get("slug", "")
|
|
entries_hdr = _post_sub_admin_header_html(
|
|
"post_entries-row", "post_entries-header-child",
|
|
qurl("blog.post.admin.entries", slug=slug),
|
|
"clock", "entries", ctx,
|
|
)
|
|
header_rows = root_hdr + post_hdr + admin_hdr + entries_hdr
|
|
content = ctx.get("entries_html", "")
|
|
return full_page(ctx, header_rows_html=header_rows, content_html=content)
|
|
|
|
|
|
async def render_post_entries_oob(ctx: dict) -> str:
|
|
admin_hdr_oob = _post_admin_header_html(ctx, oob=True)
|
|
from quart import url_for as qurl
|
|
slug = (ctx.get("post") or {}).get("slug", "")
|
|
entries_hdr = _post_sub_admin_header_html(
|
|
"post_entries-row", "post_entries-header-child",
|
|
qurl("blog.post.admin.entries", slug=slug),
|
|
"clock", "entries", ctx,
|
|
)
|
|
entries_oob = _oob_header_html("post-admin-header-child", "post_entries-header-child",
|
|
entries_hdr)
|
|
content = ctx.get("entries_html", "")
|
|
return oob_page(ctx, oobs_html=admin_hdr_oob + entries_oob, content_html=content)
|
|
|
|
|
|
# ---- Post edit ----
|
|
|
|
async def render_post_edit_page(ctx: dict) -> str:
|
|
root_hdr = root_header_html(ctx)
|
|
post_hdr = _post_header_html(ctx)
|
|
admin_hdr = _post_admin_header_html(ctx)
|
|
from quart import url_for as qurl
|
|
slug = (ctx.get("post") or {}).get("slug", "")
|
|
edit_hdr = _post_sub_admin_header_html(
|
|
"post_edit-row", "post_edit-header-child",
|
|
qurl("blog.post.admin.edit", slug=slug),
|
|
"pen-to-square", "edit", ctx,
|
|
)
|
|
header_rows = root_hdr + post_hdr + admin_hdr + edit_hdr
|
|
content = ctx.get("edit_html", "")
|
|
body_end = ctx.get("body_end_html", "")
|
|
return full_page(ctx, header_rows_html=header_rows, content_html=content,
|
|
body_end_html=body_end)
|
|
|
|
|
|
async def render_post_edit_oob(ctx: dict) -> str:
|
|
admin_hdr_oob = _post_admin_header_html(ctx, oob=True)
|
|
from quart import url_for as qurl
|
|
slug = (ctx.get("post") or {}).get("slug", "")
|
|
edit_hdr = _post_sub_admin_header_html(
|
|
"post_edit-row", "post_edit-header-child",
|
|
qurl("blog.post.admin.edit", slug=slug),
|
|
"pen-to-square", "edit", ctx,
|
|
)
|
|
edit_oob = _oob_header_html("post-admin-header-child", "post_edit-header-child",
|
|
edit_hdr)
|
|
content = ctx.get("edit_html", "")
|
|
return oob_page(ctx, oobs_html=admin_hdr_oob + edit_oob, content_html=content)
|
|
|
|
|
|
# ---- Post settings ----
|
|
|
|
async def render_post_settings_page(ctx: dict) -> str:
|
|
root_hdr = root_header_html(ctx)
|
|
post_hdr = _post_header_html(ctx)
|
|
admin_hdr = _post_admin_header_html(ctx)
|
|
from quart import url_for as qurl
|
|
slug = (ctx.get("post") or {}).get("slug", "")
|
|
settings_hdr = _post_sub_admin_header_html(
|
|
"post_settings-row", "post_settings-header-child",
|
|
qurl("blog.post.admin.settings", slug=slug),
|
|
"cog", "settings", ctx,
|
|
)
|
|
header_rows = root_hdr + post_hdr + admin_hdr + settings_hdr
|
|
content = ctx.get("settings_html", "")
|
|
return full_page(ctx, header_rows_html=header_rows, content_html=content)
|
|
|
|
|
|
async def render_post_settings_oob(ctx: dict) -> str:
|
|
admin_hdr_oob = _post_admin_header_html(ctx, oob=True)
|
|
from quart import url_for as qurl
|
|
slug = (ctx.get("post") or {}).get("slug", "")
|
|
settings_hdr = _post_sub_admin_header_html(
|
|
"post_settings-row", "post_settings-header-child",
|
|
qurl("blog.post.admin.settings", slug=slug),
|
|
"cog", "settings", ctx,
|
|
)
|
|
settings_oob = _oob_header_html("post-admin-header-child", "post_settings-header-child",
|
|
settings_hdr)
|
|
content = ctx.get("settings_html", "")
|
|
return oob_page(ctx, oobs_html=admin_hdr_oob + settings_oob, content_html=content)
|
|
|
|
|
|
# ---- Settings home ----
|
|
|
|
async def render_settings_page(ctx: dict) -> str:
|
|
root_hdr = root_header_html(ctx)
|
|
settings_hdr = _settings_header_html(ctx)
|
|
header_rows = root_hdr + settings_hdr
|
|
content = _settings_main_panel_html(ctx)
|
|
menu_html = _settings_nav_html(ctx)
|
|
return full_page(ctx, header_rows_html=header_rows, content_html=content,
|
|
menu_html=menu_html)
|
|
|
|
|
|
async def render_settings_oob(ctx: dict) -> str:
|
|
root_hdr = root_header_html(ctx, oob=True)
|
|
settings_oob = _oob_header_html("root-header-child", "root-settings-header-child",
|
|
_settings_header_html(ctx))
|
|
content = _settings_main_panel_html(ctx)
|
|
menu_html = _settings_nav_html(ctx)
|
|
return oob_page(ctx, oobs_html=root_hdr + settings_oob,
|
|
content_html=content, menu_html=menu_html)
|
|
|
|
|
|
# ---- Cache ----
|
|
|
|
async def render_cache_page(ctx: dict) -> str:
|
|
root_hdr = root_header_html(ctx)
|
|
settings_hdr = _settings_header_html(ctx)
|
|
from quart import url_for as qurl
|
|
cache_hdr = _sub_settings_header_html(
|
|
"cache-row", "cache-header-child",
|
|
qurl("settings.cache"), "refresh", "Cache", ctx,
|
|
)
|
|
header_rows = root_hdr + settings_hdr + cache_hdr
|
|
content = _cache_main_panel_html(ctx)
|
|
return full_page(ctx, header_rows_html=header_rows, content_html=content)
|
|
|
|
|
|
async def render_cache_oob(ctx: dict) -> str:
|
|
settings_hdr_oob = _settings_header_html(ctx, oob=True)
|
|
from quart import url_for as qurl
|
|
cache_hdr = _sub_settings_header_html(
|
|
"cache-row", "cache-header-child",
|
|
qurl("settings.cache"), "refresh", "Cache", ctx,
|
|
)
|
|
cache_oob = _oob_header_html("root-settings-header-child", "cache-header-child",
|
|
cache_hdr)
|
|
content = _cache_main_panel_html(ctx)
|
|
return oob_page(ctx, oobs_html=settings_hdr_oob + cache_oob, content_html=content)
|
|
|
|
|
|
# ---- Snippets ----
|
|
|
|
async def render_snippets_page(ctx: dict) -> str:
|
|
root_hdr = root_header_html(ctx)
|
|
settings_hdr = _settings_header_html(ctx)
|
|
from quart import url_for as qurl
|
|
snippets_hdr = _sub_settings_header_html(
|
|
"snippets-row", "snippets-header-child",
|
|
qurl("snippets.list_snippets"), "puzzle-piece", "Snippets", ctx,
|
|
)
|
|
header_rows = root_hdr + settings_hdr + snippets_hdr
|
|
content = _snippets_main_panel_html(ctx)
|
|
return full_page(ctx, header_rows_html=header_rows, content_html=content)
|
|
|
|
|
|
async def render_snippets_oob(ctx: dict) -> str:
|
|
settings_hdr_oob = _settings_header_html(ctx, oob=True)
|
|
from quart import url_for as qurl
|
|
snippets_hdr = _sub_settings_header_html(
|
|
"snippets-row", "snippets-header-child",
|
|
qurl("snippets.list_snippets"), "puzzle-piece", "Snippets", ctx,
|
|
)
|
|
snippets_oob = _oob_header_html("root-settings-header-child", "snippets-header-child",
|
|
snippets_hdr)
|
|
content = _snippets_main_panel_html(ctx)
|
|
return oob_page(ctx, oobs_html=settings_hdr_oob + snippets_oob, content_html=content)
|
|
|
|
|
|
# ---- Menu items ----
|
|
|
|
async def render_menu_items_page(ctx: dict) -> str:
|
|
root_hdr = root_header_html(ctx)
|
|
settings_hdr = _settings_header_html(ctx)
|
|
from quart import url_for as qurl
|
|
mi_hdr = _sub_settings_header_html(
|
|
"menu_items-row", "menu_items-header-child",
|
|
qurl("menu_items.list_menu_items"), "bars", "Menu Items", ctx,
|
|
)
|
|
header_rows = root_hdr + settings_hdr + mi_hdr
|
|
content = _menu_items_main_panel_html(ctx)
|
|
return full_page(ctx, header_rows_html=header_rows, content_html=content)
|
|
|
|
|
|
async def render_menu_items_oob(ctx: dict) -> str:
|
|
settings_hdr_oob = _settings_header_html(ctx, oob=True)
|
|
from quart import url_for as qurl
|
|
mi_hdr = _sub_settings_header_html(
|
|
"menu_items-row", "menu_items-header-child",
|
|
qurl("menu_items.list_menu_items"), "bars", "Menu Items", ctx,
|
|
)
|
|
mi_oob = _oob_header_html("root-settings-header-child", "menu_items-header-child",
|
|
mi_hdr)
|
|
content = _menu_items_main_panel_html(ctx)
|
|
return oob_page(ctx, oobs_html=settings_hdr_oob + mi_oob, content_html=content)
|
|
|
|
|
|
# ---- Tag groups ----
|
|
|
|
async def render_tag_groups_page(ctx: dict) -> str:
|
|
root_hdr = root_header_html(ctx)
|
|
settings_hdr = _settings_header_html(ctx)
|
|
from quart import url_for as qurl
|
|
tg_hdr = _sub_settings_header_html(
|
|
"tag-groups-row", "tag-groups-header-child",
|
|
qurl("blog.tag_groups_admin.index"), "tags", "Tag Groups", ctx,
|
|
)
|
|
header_rows = root_hdr + settings_hdr + tg_hdr
|
|
content = _tag_groups_main_panel_html(ctx)
|
|
return full_page(ctx, header_rows_html=header_rows, content_html=content)
|
|
|
|
|
|
async def render_tag_groups_oob(ctx: dict) -> str:
|
|
settings_hdr_oob = _settings_header_html(ctx, oob=True)
|
|
from quart import url_for as qurl
|
|
tg_hdr = _sub_settings_header_html(
|
|
"tag-groups-row", "tag-groups-header-child",
|
|
qurl("blog.tag_groups_admin.index"), "tags", "Tag Groups", ctx,
|
|
)
|
|
tg_oob = _oob_header_html("root-settings-header-child", "tag-groups-header-child",
|
|
tg_hdr)
|
|
content = _tag_groups_main_panel_html(ctx)
|
|
return oob_page(ctx, oobs_html=settings_hdr_oob + tg_oob, content_html=content)
|
|
|
|
|
|
# ---- Tag group edit ----
|
|
|
|
async def render_tag_group_edit_page(ctx: dict) -> str:
|
|
root_hdr = root_header_html(ctx)
|
|
settings_hdr = _settings_header_html(ctx)
|
|
from quart import url_for as qurl
|
|
g_id = (ctx.get("group") or {}).get("id") or getattr(ctx.get("group"), "id", None)
|
|
tg_hdr = _sub_settings_header_html(
|
|
"tag-groups-row", "tag-groups-header-child",
|
|
qurl("blog.tag_groups_admin.edit", id=g_id), "tags", "Tag Groups", ctx,
|
|
)
|
|
header_rows = root_hdr + settings_hdr + tg_hdr
|
|
content = _tag_groups_edit_main_panel_html(ctx)
|
|
return full_page(ctx, header_rows_html=header_rows, content_html=content)
|
|
|
|
|
|
async def render_tag_group_edit_oob(ctx: dict) -> str:
|
|
settings_hdr_oob = _settings_header_html(ctx, oob=True)
|
|
from quart import url_for as qurl
|
|
g_id = (ctx.get("group") or {}).get("id") or getattr(ctx.get("group"), "id", None)
|
|
tg_hdr = _sub_settings_header_html(
|
|
"tag-groups-row", "tag-groups-header-child",
|
|
qurl("blog.tag_groups_admin.edit", id=g_id), "tags", "Tag Groups", ctx,
|
|
)
|
|
tg_oob = _oob_header_html("root-settings-header-child", "tag-groups-header-child",
|
|
tg_hdr)
|
|
content = _tag_groups_edit_main_panel_html(ctx)
|
|
return oob_page(ctx, oobs_html=settings_hdr_oob + tg_oob, content_html=content)
|