"""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, explanation: dict | None = None) -> str: aid = article["id"] title = (article.get("title") or "upbeatBytes").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() source_id = article.get("source_id") # Link the source name into the app's publication feed for that source. source_html = ( f'{escape(source)}' if source_id else f'
{escape(source)}
' ) 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", "upbeatBytes"), _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 "" ) raw_tags = (article.get("tags") or "") chips = "".join( f'{escape(t.replace("-", " "))}' for t in raw_tags.split(",") if t ) groupings = f'
{chips}
' if chips else "" if summary: summary_block = f'

{escape(summary)}

' 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}

' # The structured "Why it belongs" editorial section — only when the LLM gave a # clean three-part read; otherwise the single reason line is the calm fallback. def _why_row(lbl: str, text: str) -> str: return f'
{lbl}

{escape(text)}

' if explanation: why_block = ( '
' + _why_row("What happened", explanation["what_happened"]) + _why_row("Why it matters", explanation["why_matters"]) + _why_row("Why it belongs here", explanation["why_belongs"]) + '
' ) else: why_block = f'
Why it’s here

{escape(why)}

' # Quietly poll until BOTH the summary and the structured read are cached, then # swap each in (older summaries get topped up with the section on first view). poll = "" if not summary or not explanation: poll = f"""""" # Fire a privacy-respecting 'source_click' when the reader heads to the source # (reuses the SPA's anonymous visitor token from same-origin localStorage). src_click = f"""""" return f""" {escape(title)} · upbeatBytes {meta}
upbeatBytes
{src_click} {poll} """ def render_digest(items: list[dict], base_url: str, brief_date: str | None) -> str: """A shareable, indexable 'today's good news, summarized' page.""" lead_img = next((i.get("image_url") for i in items if i.get("image_url")), None) intro = "The day's calm, constructive news — summarized in plain English, so you can get the gist and go deeper only if you want." page_url = f"{base_url}/today" cards = "" for it in items: aid = it["id"] title = escape((it.get("title") or "").strip()) source = escape((it.get("source_name") or "the source").strip()) src_url = escape(it.get("canonical_url") or base_url) gist = escape((it.get("summary") or it.get("reason_text") or it.get("description") or "").strip()) cards += ( '
' f'

{title}

' f'
{source}
' f'

{gist}

' f'' "
" ) meta = "\n".join(filter(None, [ _tag("og:site_name", "upbeatBytes"), _tag("og:type", "website"), _tag("og:title", "Today's good news, summarized"), _tag("og:description", intro), _tag("og:url", page_url), _tag("og:image", lead_img), _tag("twitter:card", "summary_large_image" if lead_img else "summary", attr="name"), _tag("twitter:title", "Today's good news, summarized", attr="name"), _tag("twitter:description", intro, attr="name"), _tag("twitter:image", lead_img, attr="name"), ])) return f""" Today's good news, summarized · upbeatBytes {meta}
upbeatBytes

Today's good news

{escape(intro)}{f' · {escape(brief_date)}' if brief_date else ''}

{cards}

Browse more on upbeatBytes →

""" def render_not_found(base_url: str) -> str: return f""" Story not found · upbeatBytes upbeatBytes

That story isn't here

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

← Back to upbeatBytes """