diff --git a/apps/board/tests/test_templates.py b/apps/board/tests/test_templates.py index 7134455..3c1fa49 100644 --- a/apps/board/tests/test_templates.py +++ b/apps/board/tests/test_templates.py @@ -388,3 +388,48 @@ def test_the_public_agenda_draws_every_day_on_a_phone_too(): assert weekday and weekday[2] != "none", ( "the phone block never shows .agenda__weekday, so every row of the " "month is a bare number — there is no column header at that width") + + +# --- the share row, which is theme and not members area -------------------- + +def test_the_share_buttons_that_need_a_script_say_so_in_the_markup(): + """Three of the seven cannot be links: Signal has no web intent, Instagram + accepts no URL at all, and copying needs the clipboard. They carry `hidden` + and share.js takes it off — so a reader without scripting sees four buttons + that work rather than seven of which three do nothing. + """ + share = (THEME / "layouts" / "partials" / "share.html").read_text(encoding="utf-8") + for hook in ("data-share-native", "data-share-copy"): + for element in re.findall(r"<(?:a|button)[^>]*" + hook + r"[^>]*>", share): + assert "hidden" in element, f"{hook} without `hidden`: {element[:80]}" + + +def test_a_hidden_share_button_is_actually_hidden(): + """`[hidden] { display: none }` lives in the browser's own stylesheet, and + an author rule beats a user-agent rule whatever the specificity — so + `.share__btn { display: inline-flex }` quietly un-hid all three. With + scripting off, seven buttons appeared and three of them did nothing, which + is the exact failure the attribute was there to prevent. + """ + css = (THEME / "assets" / "css" / "main.css").read_text(encoding="utf-8") + assert re.search(r"\.share__btn\[hidden\]\s*\{[^}]*display:\s*none", css), ( + "main.css gives .share__btn a display without taking it back for " + "[hidden], so the buttons share.js is meant to reveal are visible " + "before it runs — and useless until it does") + + +def test_every_share_link_is_marked_safe_so_it_is_not_encoded_twice(): + """Go's template escaper treats an href as a URL and escapes it on the way + out. A value already encoded with `urlquery` therefore went out as %253A + and %2b — a WhatsApp link with the percent signs spelled out, which opens + the composer with gibberish in it. `safeURL` is what stops the second pass. + """ + share = (THEME / "layouts" / "partials" / "share.html").read_text(encoding="utf-8") + # By line rather than by matching the attribute: the value contains its own + # quotes — `href="{{ printf "https://…" $enc | safeURL }}"` — and a regex + # for href="..." stops at the first of them and reads nothing. + lines = [line.strip() for line in share.splitlines() + if "href=" in line and "$enc" in line] + assert lines, "share.html builds no encoded links at all any more" + for line in lines: + assert "safeURL" in line, f"encoded twice: {line}" diff --git a/config.yaml b/config.yaml index 9e869ff..33ac3a0 100644 --- a/config.yaml +++ b/config.yaml @@ -135,3 +135,35 @@ markup: unsafe: false enableRobotsTXT: false # robots.txt is a static file in static/ + +# The uploads are mounted twice on purpose. +# +# Hugo copies `static/` and processes `assets/`, and a picture that is only +# copied cannot be resized — which is why a share card used to point at the +# original: a photograph off a phone, two to five megabytes. WhatsApp fetches a +# preview image on a tight budget and gives up on a file that size, so the card +# arrived with a blank where the picture should be. +# +# Mounting `static/uploads` into `assets/uploads` as well lets seo-head.html +# build a 1200×630 card from the same file, while the static copy stays exactly +# where it is: every existing /uploads/… URL, every image in every post, and +# everything the editor commits are untouched. +# +# Declaring any mount means declaring them all — these five are Hugo's own +# defaults, written out so nothing silently stops being read. +module: + mounts: + - source: content + target: content + - source: layouts + target: layouts + - source: data + target: data + - source: assets + target: assets + - source: static + target: static + - source: static/uploads + target: assets/uploads + - source: static/brand + target: assets/brand diff --git a/docs/server-setup.md b/docs/server-setup.md index 8f34dc9..e561330 100644 --- a/docs/server-setup.md +++ b/docs/server-setup.md @@ -1080,9 +1080,8 @@ card and `BlogPosting` for articles. What it now also does: - **Static pages get all of it** — they had none, so *Acerca de* shared as a bare link. `WebPage` schema, Open Graph, the card -- **`og:image` with an alt**, from the post's own picture or `params.defaultImage` - as a fallback. That param is empty: fill it in when there is a logo, and every - link shared on WhatsApp, Signal, Mastodon or Bluesky starts carrying a picture +- **`og:image` with an alt**, built rather than pointed at — see §11.16b, which + is the difference between a card with a picture and a card without one - **`Organization`** on the home, with `areaServed: Wien` and a `sameAs` list ready for social profiles — this is how a search or answer engine knows the Instagram account and this site are one body @@ -1098,6 +1097,63 @@ The translation pipeline carries all of it: `image_alt` joins the translated keys, and `faq` is translated line by line with each half handled separately so the `|` survives. +### 11.16b The share row, and the card that arrives + +**Seven buttons under the title of every post and again after the text**, from +`partials/share.html`. Four are ordinary links and work with scripting off: +WhatsApp, Facebook, Bluesky and Mastodon all accept a URL that opens their own +composer. + +The other three cannot be links, and it is worth knowing why rather than +rediscovering it: + +| | | +|---|---| +| **Signal** | has no web intent at all. It is a share *target* in the operating system's own sheet and nowhere else | +| **Instagram** | accepts no link from anybody, by design. Every site with an Instagram share button is really offering a copied link | +| **Copiar enlace** | the fallback for everything the two above do not cover, including a desktop with no share sheet | + +All three call `navigator.share()` where it exists and copy the address where it +does not. They carry `hidden` in the markup and `share.js` removes it, so a +reader without scripting sees four buttons that work rather than seven of which +three do nothing. `main.css` has to say `.share__btn[hidden] { display: none }` +explicitly: `[hidden]` lives in the browser's own stylesheet and an author rule +beats it whatever the specificity, so `display: inline-flex` silently un-hid all +three the first time. + +Mastodon has no central address — the composer lives on whichever server the +*reader* has an account on. The button asks once, keeps the answer in +`localStorage`, and without scripting falls back to the association's own server +from *Gestión → Identidad* when one is set. + +The links are built with `urlquery` **and** marked `safeURL`. Go's template +escaper treats an href as a URL and escapes it again on the way out, so without +that the WhatsApp link went out as `%253A` and `%2b` — the composer opening with +the percent signs spelled out. + +**The card image is generated, not linked.** This is the half that was actually +broken: `og:image` pointed at the original upload, a photograph off a phone of +two to five megabytes, and WhatsApp fetches preview images on a tight budget and +gives up on a file that size — so the card arrived as a title and a link with a +blank where the picture should be. + +Hugo copies `static/` and processes `assets/`, and only the second can be +resized, so `config.yaml` mounts `static/uploads` and `static/brand` into +`assets/` **as well**. The static copies stay exactly where they are: every +`/uploads/…` URL, every image in every post and everything the editor commits +are untouched. `seo-head.html` then fills a post's photograph to **1200×630** +(80–150 KB in practice, measured at 93 KB against a 22 MB source) and fits the +logo fallback without cropping — a wordmark filled to a landscape card loses its +ends, and converting a transparent PNG to JPEG puts it on a black square. + +`partials/og-image.html` emits the five tags together, including +`og:image:width` and `og:image:height`, which are not decoration: several +clients draw the small card rather than the large one when they would have to +fetch the file to find out how big it is. + +A picture the pipeline cannot find falls back to the absolute URL with no +dimensions — exactly what happened before, so nothing is ever worse than it was. + ### 11.17 Writing boxes Every box where somebody writes prose — the wall, a comment, a private message, a diff --git a/themes/vienalatina/assets/css/main.css b/themes/vienalatina/assets/css/main.css index 66663ef..196e7d5 100644 --- a/themes/vienalatina/assets/css/main.css +++ b/themes/vienalatina/assets/css/main.css @@ -4150,3 +4150,100 @@ a.notif-card__message:hover { letter-spacing: 0.02em; vertical-align: middle; } + +/* --- sharing ------------------------------------------------------------ + * + * A tinted strip rather than six bare icons: the row has to be noticed to be + * used, and a label saying what it is costs one word. Drawn twice on a post — + * under the title and after the text — so it meets both the reader who decides + * on sight and the one who decides at the end. + * + * Each button takes its own brand colour on hover and keeps the page's muted + * grey at rest, so the row reads as one object instead of six logos competing + * with the article. + */ +.share { + display: flex; + flex-wrap: wrap; + align-items: center; + gap: 8px; + margin: 18px 0 24px; + padding: 10px 14px; + border: 1px solid var(--border-light, #e0cec8); + border-radius: var(--radius-lg, 14px); + background: var(--surface-soft, #faf2ef); +} + +.share__title { + font-size: 0.85rem; + font-weight: 700; + color: var(--muted, #5c3d37); + margin-right: 4px; +} + +.share__btn { + display: inline-flex; + align-items: center; + justify-content: center; + width: 38px; + height: 38px; + padding: 0; + border: 1px solid var(--border-light, #e0cec8); + border-radius: 50%; + background: var(--surface, #fff); + color: var(--muted, #5c3d37); + cursor: pointer; + transition: color 120ms ease, border-color 120ms ease, transform 120ms ease; +} +.share__btn svg { width: 20px; height: 20px; fill: currentColor; } +.share__btn:hover { transform: translateY(-1px); } + +/* The `hidden` attribute, given back its meaning. + * + * `[hidden] { display: none }` lives in the browser's own stylesheet, and an + * author rule beats a user-agent rule whatever the specificity — so the + * `display: inline-flex` above quietly un-hid the three buttons that are + * supposed to appear only once share.js has run. With scripting off, all + * seven showed and three of them did nothing at all, which is the exact + * failure the attribute was there to prevent. */ +.share__btn[hidden] { display: none; } + +/* Their own colours, and only on hover: at rest the row belongs to the page. */ +.share__btn--whatsapp:hover { color: #25d366; border-color: #25d366; } +.share__btn--facebook:hover { color: #1877f2; border-color: #1877f2; } +.share__btn--bluesky:hover { color: #0085ff; border-color: #0085ff; } +.share__btn--mastodon:hover { color: #6364ff; border-color: #6364ff; } +.share__btn--signal:hover { color: #3a76f0; border-color: #3a76f0; } +.share__btn--instagram:hover { color: #e1306c; border-color: #e1306c; } +.share__btn--copy:hover { color: var(--brand, #c0391c); border-color: var(--brand, #c0391c); } + +.share__btn:focus-visible { + outline: 2px solid var(--brand, #c0391c); + outline-offset: 2px; +} + +/* The name is for a screen reader and for `title` on a cursor; the icon is for + everybody else. Hidden this way rather than with display:none, which would + take it out of the accessibility tree along with the button's only label. */ +.share__name { + position: absolute; + width: 1px; + height: 1px; + margin: -1px; + padding: 0; + overflow: hidden; + clip-path: inset(50%); + white-space: nowrap; +} + +/* What the copy button says back. Empty until it has something to say, so it + takes no room on a row that is already seven items wide. */ +.share__said { + font-size: 0.8rem; + color: var(--brand-dark, #9a2a0f); + font-weight: 600; +} + +@media (pointer: coarse) { + .share__btn { width: 44px; height: 44px; } +} diff --git a/themes/vienalatina/assets/js/share.js b/themes/vienalatina/assets/js/share.js new file mode 100644 index 0000000..8445647 --- /dev/null +++ b/themes/vienalatina/assets/js/share.js @@ -0,0 +1,116 @@ +// The three share buttons that cannot be plain links. +// +// WhatsApp, Facebook, Bluesky and Mastodon all accept a URL that opens their +// own composer, so those are anchors in share.html and work with this file +// absent. These three cannot be: +// +// Signal has no web intent at all. It is a share *target* in the +// operating system's own sheet and nowhere else. +// Instagram accepts no link from anybody, by design. Every site with an +// Instagram share button is really offering a copied link. +// Copiar is the fallback for every case the two above do not cover, +// including a desktop with no share sheet. +// +// All three are `hidden` in the markup and shown here, so a reader without +// scripting sees four buttons that work rather than seven of which three do +// nothing. + +(function () { + "use strict"; + + function copy(text) { + if (navigator.clipboard && navigator.clipboard.writeText) { + return navigator.clipboard.writeText(text); + } + // Older engines, and any page not served over https: a textarea, selected + // and copied the way it was done before the clipboard API existed. + return new Promise(function (resolve, reject) { + var box = document.createElement("textarea"); + box.value = text; + box.setAttribute("readonly", ""); + box.style.position = "fixed"; + box.style.opacity = "0"; + document.body.appendChild(box); + box.select(); + try { + document.execCommand("copy") ? resolve() : reject(); + } catch (error) { + reject(error); + } + document.body.removeChild(box); + }); + } + + function wire(row) { + var url = row.getAttribute("data-url"); + var title = row.getAttribute("data-title"); + var text = row.getAttribute("data-text"); + var said = row.querySelector(".share__said"); + + function announce(message) { + if (!said) { return; } + said.textContent = message; + window.setTimeout(function () { said.textContent = ""; }, 2500); + } + + function copyAndSay() { + copy(url).then(function () { + announce(row.getAttribute("data-copied")); + }).catch(function () { + // Copying can be refused outright — a permission, an engine without + // either route. Saying nothing would look like a button that does + // nothing, so the address is put on screen to be copied by hand. + announce(url); + }); + } + + // Signal and Instagram: the operating system's sheet, which lists both + // when they are installed. There is no way to open one app in particular, + // and pretending otherwise is what a broken button is made of. + Array.prototype.forEach.call(row.querySelectorAll("[data-share-native]"), function (button) { + button.hidden = false; + button.addEventListener("click", function () { + if (navigator.share) { + navigator.share({ title: title, text: text, url: url }).catch(function () { + // Dismissed, which is not a failure and needs no message. + }); + } else { + copyAndSay(); + } + }); + }); + + Array.prototype.forEach.call(row.querySelectorAll("[data-share-copy]"), function (button) { + button.hidden = false; + button.addEventListener("click", copyAndSay); + }); + + // Mastodon has no central address: the composer lives on whichever server + // the *reader* has an account on. Asked once and kept, because asking on + // every post would be worse than not offering it. + var masto = row.querySelector("[data-share-mastodon]"); + if (masto) { + masto.hidden = false; + masto.addEventListener("click", function (event) { + var saved = null; + try { saved = window.localStorage.getItem("vl-mastodon"); } catch (error) { saved = null; } + var host = saved || window.prompt(row.getAttribute("data-ask"), "mastodon.social"); + if (!host) { + // Nothing typed: fall through to the association's own server when + // the markup had one, and otherwise do nothing at all. + if (masto.getAttribute("href") === "#") { event.preventDefault(); } + return; + } + host = host.trim().replace(/^https?:\/\//, "").replace(/\/.*$/, ""); + if (!host) { event.preventDefault(); return; } + try { window.localStorage.setItem("vl-mastodon", host); } catch (error) { /* private window */ } + masto.setAttribute("href", + "https://" + host + "/share?text=" + encodeURIComponent(text)); + }); + } + } + + document.addEventListener("DOMContentLoaded", function () { + Array.prototype.forEach.call(document.querySelectorAll("[data-share]"), wire); + }); +})(); diff --git a/themes/vienalatina/layouts/_default/single.html b/themes/vienalatina/layouts/_default/single.html index 741a284..8f84b19 100644 --- a/themes/vienalatina/layouts/_default/single.html +++ b/themes/vienalatina/layouts/_default/single.html @@ -41,6 +41,11 @@

{{ .Title }}

+ {{/* Twice on purpose: here for the reader who decides to send it to + somebody the moment they see what it is, and again after the text + for the one who decides having read it. */}} + {{ partial "share.html" . }} + {{/* An event leads with the three things somebody needs before they read a word: when, what time, and where. Everything else on the page is the same article it would otherwise be. */}} @@ -101,10 +106,18 @@ {{ end }} + {{ partial "share.html" . }} + ← Volver al inicio + {{/* Only on this template: the home page and the lists carry no share + row, so they should not carry its script either. Fingerprinted and + deferred like the others. */}} + {{ $share := resources.Get "js/share.js" | minify | fingerprint }} + + {{ end }} diff --git a/themes/vienalatina/layouts/partials/og-image.html b/themes/vienalatina/layouts/partials/og-image.html new file mode 100644 index 0000000..373cf86 --- /dev/null +++ b/themes/vienalatina/layouts/partials/og-image.html @@ -0,0 +1,23 @@ +{{/* The tags a share card needs to arrive with its picture. + + One partial rather than the same five lines in three branches: the article, + the home page and a static page all share cards, and the set drifted once + already — the home page had no `twitter:image` for a while because it was + added to one branch and not the others. + + Width and height are not decoration. Several clients draw the small card + instead of the large one when they would have to fetch the file to find + out how big it is, and WhatsApp in particular gives up on a slow or heavy + image rather than waiting for it. Both numbers come from the generated + card, so they cannot disagree with the file. + + Takes: url, alt, width, height, type. */}} +{{ with .url }} + + +{{ with $.alt }}{{ end }} +{{ with $.width }}{{ end }} +{{ with $.height }}{{ end }} +{{ with $.type }}{{ end }} + +{{ end }} diff --git a/themes/vienalatina/layouts/partials/seo-head.html b/themes/vienalatina/layouts/partials/seo-head.html index bd4718f..b546d78 100644 --- a/themes/vienalatina/layouts/partials/seo-head.html +++ b/themes/vienalatina/layouts/partials/seo-head.html @@ -27,9 +27,46 @@ one in config.yaml: most sites want their logo and the field exists for the ones that want something else. */}} {{ $fallback := or $chosen (or $brandLogo .Site.Params.defaultImage) }} -{{ $image := "" }} -{{ with .Params.image }}{{ $image = . | absURL }} -{{ else }}{{ with $fallback }}{{ $image = . | absURL }}{{ end }}{{ end }} + +{{/* The card is *generated*, not pointed at. + + What used to go out was the original upload — a photograph off a phone, + two to five megabytes. WhatsApp fetches a preview image on a tight budget + and gives up on a file that size, so the card arrived as a title and a + link with a blank where the picture should be. That is the whole reason + `static/uploads` is mounted into `assets/` in config.yaml: Hugo copies + static files and processes asset files, and only one of those can be + resized. + + A post's own photograph is filled to 1200×630 — the size every client + draws its large card at — and comes out at 80–150 KB. The fallback is + fitted rather than filled, because a wordmark cropped to a landscape card + is a wordmark with its ends cut off. + + An image the pipeline cannot find — a path typed by hand, a file added + after the build — falls back to exactly what happened before: the absolute + URL, no dimensions. Nothing is worse than it was. */}} +{{ $source := or .Params.image $fallback }} +{{ $image := "" }}{{ $cardW := "" }}{{ $cardH := "" }}{{ $cardType := "" }} +{{ with $source }} +{{ $image = . | absURL }} +{{ with resources.Get (strings.TrimPrefix "/" .) }} +{{ $card := "" }} +{{ if $.Params.image }} +{{ $card = .Fill "1200x630 Center q82 jpg" }} +{{ else }} +{{/* Fitted, and in its own format: the fallback is usually the logo, and a + logo cropped to a landscape card loses its ends — while converting a + transparent PNG to JPEG puts it on a black square, because JPEG has no + alpha to put it on anything else. */}} +{{ $card = .Fit "1200x630" }} +{{ end }} +{{ $image = $card.Permalink }} +{{ $cardW = $card.Width }}{{ $cardH = $card.Height }}{{ $cardType = $card.MediaType.Type }} +{{ end }} +{{ end }} +{{ $cardAlt := cond (ne .Params.image_alt nil) .Params.image_alt .Title }} +{{ $card := dict "url" $image "alt" $cardAlt "width" $cardW "height" $cardH "type" $cardType }} @@ -54,10 +91,7 @@ {{ range .Translations }} {{ with index $localeMap .Language.Lang }}{{ end }} {{ end }} -{{ with $image }} - - -{{ end }} +{{ partial "og-image.html" $card }} {{ with .Params.author }}{{ end }} @@ -65,7 +99,6 @@ -{{ with $image }}{{ end }} {{ $schema := dict "@context" "https://schema.org" @@ -136,11 +169,7 @@ {{/* The home page shared without a picture until there was one to use. Now that an admin can upload a logo, the front page of the site is the last place that should share as a bare link. */}} -{{ with $image }} - - - -{{ end }} +{{ partial "og-image.html" (merge $card (dict "alt" (partial "site-name.html" .))) }} {{/* The organisation, as a search or answer engine reads it. Everything overridable lives in Gestión → Identidad; `areaServed` stays here because a @@ -215,15 +244,11 @@ {{ range .Translations }} {{ with index $localeMap .Language.Lang }}{{ end }} {{ end }} -{{ with $image }} - - -{{ end }} +{{ partial "og-image.html" $card }} -{{ with $image }}{{ end }} {{ $schema := dict "@context" "https://schema.org" diff --git a/themes/vienalatina/layouts/partials/share.html b/themes/vienalatina/layouts/partials/share.html new file mode 100644 index 0000000..a03d7bb --- /dev/null +++ b/themes/vienalatina/layouts/partials/share.html @@ -0,0 +1,116 @@ +{{/* The row of share buttons, drawn under the title and again after the text. + + Four of these are ordinary links and work with scripting switched off: + WhatsApp, Facebook, Bluesky and Mastodon all accept a URL that opens their + own composer with the text already in it. + + Signal and Instagram do not, and no amount of wanting changes it. Signal + has no web intent at all — it appears in the operating system's own share + sheet and nowhere else — and Instagram cannot be handed a link by anybody, + which is why every site that claims an Instagram share button is really + offering a copied link. So both call `navigator.share()`, which opens that + sheet with every installed app in it, and fall back to copying. They carry + `hidden` until share.js removes it: a button that does nothing at all is + worse than no button. + + The icons are simplified marks in one visual language rather than the + official logo files. Reproducing those from memory gives something subtly + wrong, and subtly wrong looks worse than plainly simple; each carries its + name for a screen reader and in `title` for a cursor. Swapping in the real + SVGs later is one file and no other change. */}} + +{{ $lang := .Site.Language.Lang }} +{{ $url := .Permalink }} +{{ $text := printf "%s — %s" .Title $url }} +{{/* Encoded once, and marked safe so it is not encoded again. Go's template + escaper treats an href as a URL and escapes it on the way out, so a value + that arrived already encoded came through as %253A and %2b — a link that + opens WhatsApp with the percent signs spelled out. */}} +{{ $encText := $text | urlquery }} +{{ $encUrl := $url | urlquery }} + +{{/* Without scripting the Mastodon button needs an instance to aim at, and the + association's own is the only one this page knows. With scripting it asks + the reader for theirs instead and remembers the answer. */}} +{{ $masto := "" }} +{{ with site.Data.site }}{{ with .social }}{{ with .mastodon }} +{{ $masto = (urls.Parse .).Host }} +{{ end }}{{ end }}{{ end }} + + diff --git a/themes/vienalatina/layouts/partials/t.html b/themes/vienalatina/layouts/partials/t.html index 79e27a8..00cb4d4 100644 --- a/themes/vienalatina/layouts/partials/t.html +++ b/themes/vienalatina/layouts/partials/t.html @@ -19,6 +19,12 @@ "noevents" (dict "es" "No hay nada en este mes." "de" "In diesem Monat ist nichts geplant." "pt-br" "Nada marcado neste mês.") "allevents" (dict "es" "Todos los eventos" "de" "Alle Termine" "pt-br" "Todos os eventos") "ai" (dict "es" "Generado con IA" "de" "Mit KI erstellt" "pt-br" "Gerado com IA") + "share" (dict "es" "Compartir" "de" "Teilen" "pt-br" "Compartilhar") + "copylink" (dict "es" "Copiar enlace" "de" "Link kopieren" "pt-br" "Copiar link") + "copied" (dict "es" "Enlace copiado" "de" "Link kopiert" "pt-br" "Link copiado") + "askmasto" (dict "es" "¿En qué servidor de Mastodon tienes tu cuenta? Por ejemplo: mastodon.social" + "de" "Auf welchem Mastodon-Server hast du dein Konto? Zum Beispiel: mastodon.social" + "pt-br" "Em qual servidor do Mastodon está a sua conta? Por exemplo: mastodon.social") "aifull" (dict "es" "Quien lo publicó marcó este texto como generado con inteligencia artificial." "de" "Wer diesen Beitrag veröffentlicht hat, hat ihn als mit künstlicher Intelligenz erstellt gekennzeichnet." "pt-br" "Quem publicou marcou este texto como gerado com inteligência artificial.")