"""Server-rendered share/landing page for /a/. A *pointer* page (never a republished article): the story's own title, "why it's uplifting" note, and image, wrapped with rich OpenGraph/Twitter meta and a prominent link to the original source. Social scrapers don't run JS, so this is plain server-rendered HTML. All dynamic values are HTML-escaped. """ from __future__ import annotations from html import escape def _tag(name: str, content: str | None, attr: str = "property") -> str: if not content: return "" return f'' def render_share_page(article: dict, base_url: str, summary: str | None = None) -> str: aid = article["id"] title = (article.get("title") or "Upbeat Bytes").strip() why = (article.get("reason_text") or article.get("description") or "A calm, constructive story worth your attention.").strip() source = (article.get("source_name") or "the source").strip() src_url = article.get("canonical_url") or base_url image = article.get("image_url") page_url = f"{base_url}/a/{aid}" # With an image: a large-image card. Without: a clean text unfurl (title + # why + brand) — tidy, not a broken/muddy preview. (A branded fallback PNG # can replace this later.) twitter_card = "summary_large_image" if image else "summary" meta = "\n".join(filter(None, [ _tag("og:site_name", "Upbeat Bytes"), _tag("og:type", "article"), _tag("og:title", title), _tag("og:description", why), _tag("og:url", page_url), _tag("og:image", image), _tag("twitter:card", twitter_card, attr="name"), _tag("twitter:title", title, attr="name"), _tag("twitter:description", why, attr="name"), _tag("twitter:image", image, attr="name"), ])) media = ( f'' if image else "" ) source_short = source.split(" ")[0] if source else "the source" if summary: summary_block = f'

{escape(summary)}

' poll = "" else: pending = ( f"✦ We’re putting together a quick summary — in the meantime, " f"read the full story at {escape(source)} below." ) summary_block = f'

{pending}

' # Quietly poll; swap the real summary in the moment it's cached. poll = f"""""" return f""" {escape(title)} · Upbeat Bytes {meta}
Upbeat Bytes
{poll} """ def render_not_found(base_url: str) -> str: return f""" Story not found · Upbeat Bytes Upbeat Bytes

That story isn't here

It may have moved on — the good news refreshes often.

← Back to Upbeat Bytes """