Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/copilot-instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ Pure CSS dropdown using hidden checkbox trick (`input.lang-menu-trigger`). JavaS

### Preview Images

OpenGraph images use `resource.extra.preview_image` first, then an automatic parent-folder news preview, then the first JPEG/PNG/WebP asset in sorted filename order, falling back to `images/screenshots/prague.jpg`. Explicit local previews must exist or the build stops.
OpenGraph images use `resource.extra.preview_image` first, then an automatic parent-folder news preview, then the first JPEG/PNG/WebP asset in sorted filename order, falling back to `images/screenshots/prague.jpg`. Explicit local previews must exist or the build stops. When a published JPEG/PNG preview has a lowercase `.webp` sibling with the same filename stem, keep the published preview URL for sharing. Galleries and Telegram use the WebP. Key `extra.media_alt` by the gallery filename.

For a bundled news post in `content/news/YYYY-MM-DD/NNN/`, put its sharing cover at `content/news/YYYY-MM-DD/NNN-preview.jpg` (this exact filename convention). The shared `news_parent_preview` component finds it using the source folder ID, independently of translated public slugs. No preview YAML is needed in any language. The cover stays out of article galleries and Telegram media discovery, and is included in the image sitemap. Keep the lookup shared between page metadata and sitemap generation.
If a bundled post in `content/news/YYYY-MM-DD/NNN/` needs a separate sharing cover, use `content/news/YYYY-MM-DD/NNN-preview.webp`. An existing `NNN-preview.jpg` takes priority. The shared `news_parent_preview` component finds it using the source folder ID, independently of translated public slugs. No preview YAML is needed in any language. The cover stays out of article galleries and Telegram media discovery, and is included in the image sitemap. Keep the lookup shared between page metadata and sitemap generation.

### Matching substrings in templates

Expand Down
40 changes: 33 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,32 @@ listing and archive images load lazily. Articles keep their first image eager.
Listing and archive videos preload metadata so their previews and dimensions
are available before playback.

For new posts, publish WebP images once and reuse them for both the website and
sharing previews. The first image is used automatically, or `extra.preview_image`
can select another published image. Keep a separate sharing cover only when it
needs different artwork or framing, using the parent-folder convention below.
Do not add JPEG/PNG duplicates for new posts or introduce redirects or aliases
when converting news images.

Already-published posts retain their original preview filenames, paths and
`extra.preview_image` values. An original preview may remain alongside a lowercase
`.webp` with the same filename stem. The gallery and Telegram use the WebP.
Key `extra.media_alt` by the filename shown in the gallery.

For image optimization, install `cwebp` from libwebp and prepare a temporary
candidate outside the article folder:

```bash
cwebp -lossless -q 100 -m 6 -exact -metadata icc source.png -o /tmp/news-candidate.webp
cwebp -q 90 -m 6 -sharp_yuv -metadata icc source.jpg -o /tmp/news-candidate.webp
```

These settings preserve image dimensions and ICC profiles. WebP is 8-bit, so
16-bit PNG sources are reduced to 8-bit before lossless encoding. Compare size
and visual quality, then publish the WebP in the article folder when it is
smaller. Update filename references in every translation when you replace an
original.

Add specific image descriptions in each translation's frontmatter:

```yaml
Expand All @@ -71,16 +97,16 @@ for a decorative image), or a description is still identical to English.
Social previews use this order:

1. An explicit `extra.preview_image` (a colocated filename or site-root path).
2. A parent-folder JPEG named `<article-folder>-preview.jpg` for a bundled news post.
2. A parent-folder cover named `<article-folder>-preview.jpg` or `<article-folder>-preview.webp` for a bundled news post. Existing JPEG covers take priority.
3. The first supported article image in filename order, detecting extensions case-insensitively.
4. The default site image.

For example, `content/news/2026-11-11/650-preview.jpg` is automatically used by
`content/news/2026-11-11/650/index.md` and its translations, without any preview
YAML. The name comes from the source folder, not the public slug. The image
stays out of the article gallery and Telegram album. Explicit and automatic
parent previews are included in the image sitemap. Missing explicit local
previews stop the build instead of publishing broken image URLs.
A new post in `content/news/YYYY-MM-DD/NNN/` can use
`content/news/YYYY-MM-DD/NNN-preview.webp` as its separate cover. Zola finds it
by source folder name and uses it for translations without preview YAML. The
cover stays out of the article gallery and Telegram album. The image sitemap
includes explicit and automatic parent previews. A missing explicit local
preview stops the build.

Telegram media discovery also recognizes `.webp` files. Standalone Telegram
folders can include `.mov` and `.m4a`; news folders must use the website's
Expand Down
Binary file added content/news/2021-05-25/9/1.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added content/news/2021-06-20/14/1.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added content/news/2022-02-14/48/1.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added content/news/2022-03-16/53/1.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added content/news/2022-06-29/83/1.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added content/news/2022-07-28/88/1.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added content/news/2022-08-24/92/1.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added content/news/2022-08-31/95/1.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added content/news/2022-09-22/98/1.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added content/news/2022-11-03/105/1.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added content/news/2022-11-09/109/1.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added content/news/2022-12-02/113/1.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added content/news/2022-12-23/122/1.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed content/news/2022-12-23/122/2.jpeg
Binary file not shown.
Binary file added content/news/2022-12-23/122/2.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed content/news/2022-12-23/122/3.jpeg
Binary file not shown.
Binary file added content/news/2022-12-23/122/3.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed content/news/2022-12-23/122/4.jpeg
Binary file not shown.
Binary file added content/news/2022-12-23/122/4.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed content/news/2022-12-23/122/5.jpeg
Binary file not shown.
Binary file added content/news/2022-12-23/122/5.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added content/news/2023-01-20/130/1.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed content/news/2023-01-27/132/2.jpg
Binary file not shown.
Binary file added content/news/2023-01-27/132/2.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added content/news/2023-04-06/150/1.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed content/news/2023-04-06/150/2.png
Diff not rendered.
Binary file added content/news/2023-04-06/150/2.webp
Binary file removed content/news/2023-04-06/150/3.png
Diff not rendered.
Binary file added content/news/2023-04-06/150/3.webp
Binary file removed content/news/2023-04-06/150/4.png
Diff not rendered.
Binary file added content/news/2023-04-06/150/4.webp
Binary file removed content/news/2023-04-06/150/5.png
Diff not rendered.
Binary file added content/news/2023-04-06/150/5.webp
Binary file added content/news/2023-08-21/185/1.webp
Binary file removed content/news/2023-08-21/185/2.jpg
Diff not rendered.
Binary file added content/news/2023-08-21/185/2.webp
Binary file removed content/news/2023-08-21/185/3.jpg
Diff not rendered.
Binary file added content/news/2023-08-21/185/3.webp
Binary file added content/news/2023-09-15/202/1.webp
Binary file added content/news/2023-09-21/213/1.webp
Binary file added content/news/2023-09-30/221/1.webp
Binary file removed content/news/2023-09-30/221/2.jpg
Diff not rendered.
Binary file added content/news/2023-09-30/221/2.webp
Binary file added content/news/2023-11-22/244/1.webp
Binary file removed content/news/2023-11-22/244/2.PNG
Diff not rendered.
Binary file added content/news/2023-11-22/244/2.webp
Binary file removed content/news/2023-11-22/244/3.PNG
Diff not rendered.
Binary file added content/news/2023-11-22/244/3.webp
Binary file removed content/news/2023-11-22/244/4.PNG
Diff not rendered.
Binary file added content/news/2023-11-22/244/4.webp
Binary file added content/news/2023-12-05/264/1.webp
Binary file added content/news/2023-12-19/280/1.webp
Binary file added content/news/2024-02-14/304/1.webp
Binary file removed content/news/2024-02-14/304/2.png
Diff not rendered.
Binary file added content/news/2024-02-14/304/2.webp
Binary file removed content/news/2024-02-14/304/3.png
Diff not rendered.
Binary file added content/news/2024-02-14/304/3.webp
Binary file added content/news/2024-03-11/317/1.webp
Binary file removed content/news/2024-03-11/317/2.jpg
Diff not rendered.
Binary file added content/news/2024-03-11/317/2.webp
Binary file added content/news/2024-03-18/323/1.webp
Binary file added content/news/2024-04-26/343/review.webp
Binary file removed content/news/2024-05-19/364/1.png
Diff not rendered.
Binary file added content/news/2024-05-19/364/1.webp
Binary file removed content/news/2024-05-19/364/2.png
Diff not rendered.
Binary file added content/news/2024-05-19/364/2.webp
Binary file removed content/news/2024-05-19/364/3.png
Diff not rendered.
Binary file added content/news/2024-05-19/364/3.webp
Binary file removed content/news/2024-05-19/364/4.png
Diff not rendered.
Binary file added content/news/2024-05-19/364/4.webp
Binary file removed content/news/2024-06-22/380/3.jpeg
Diff not rendered.
Binary file added content/news/2024-06-22/380/3.webp
Binary file removed content/news/2024-06-22/380/4.jpeg
Diff not rendered.
Binary file added content/news/2024-06-22/380/4.webp
Binary file removed content/news/2024-06-22/380/5.jpeg
Diff not rendered.
Binary file added content/news/2024-06-22/380/5.webp
Binary file removed content/news/2024-06-22/380/6.jpeg
Diff not rendered.
Binary file added content/news/2024-06-22/380/6.webp
Binary file removed content/news/2024-08-19/404/4.jpg
Diff not rendered.
Binary file added content/news/2024-08-19/404/4.webp
Binary file removed content/news/2024-08-19/404/5.jpg
Diff not rendered.
Binary file added content/news/2024-08-19/404/5.webp
Binary file removed content/news/2025-07-14/418/5. Search.jpg
Diff not rendered.
Binary file added content/news/2025-07-14/418/5. Search.webp
Diff not rendered.
Diff not rendered.
Diff not rendered.
Binary file removed content/news/2025-09-15/440/3. Bahnhof.png
Diff not rendered.
Binary file added content/news/2025-09-15/440/3. Bahnhof.webp
Binary file removed content/news/2025-10-23/460/terminal.png
Diff not rendered.
Binary file added content/news/2025-10-23/460/terminal.webp
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Binary file removed content/news/2025-11-26/480/Wikipedia China.png
Diff not rendered.
Binary file added content/news/2025-11-26/480/Wikipedia China.webp
Binary file removed content/news/2025-11-26/480/Wikipedia Hong Kong.png
Diff not rendered.
Binary file added content/news/2025-12-16/490/1 Christmas Tree.webp
Binary file removed content/news/2025-12-16/490/2 Christmas Market.PNG
Diff not rendered.
Binary file removed content/news/2025-12-16/490/3 Search.PNG
Diff not rendered.
Binary file added content/news/2025-12-16/490/3 Search.webp
Binary file removed content/news/2025-12-16/490/4 Lapland.PNG
Diff not rendered.
Binary file added content/news/2025-12-16/490/4 Lapland.webp
Binary file removed content/news/2025-12-16/490/5 Christmas Town.PNG
Diff not rendered.
Binary file added content/news/2025-12-31/500/OrganicMaps.webp
Diff not rendered.
Diff not rendered.
Binary file removed content/news/2026-06-15/600/hatching.jpg
Diff not rendered.
Binary file added content/news/2026-06-15/600/hatching.webp
Binary file removed content/news/2026-06-15/600/new_search.jpg
Diff not rendered.
Binary file added content/news/2026-06-15/600/new_search.webp
Binary file removed content/news/2026-06-15/600/pt_routing.jpg
Diff not rendered.
Binary file added content/news/2026-06-15/600/pt_routing.webp
Diff not rendered.
Diff not rendered.
Binary file removed content/news/2026-06-29/610/1 OSGB coordinates.jpg
Diff not rendered.
Binary file removed content/news/2026-06-29/610/4 Rock texture.jpg
Diff not rendered.
Binary file added content/news/2026-06-29/610/4 Rock texture.webp
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
12 changes: 9 additions & 3 deletions templates/_news_media.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,21 @@
{%- set_global image_filenames = [] -%}
{%- for media in resource.assets | sort -%}
{%- set filename = media | split(pat="/") | last -%}
{%- set extension = filename | split(pat=".") | last | lower -%}
{%- set filename_extension = filename | split(pat=".") | last -%}
{%- set extension = filename_extension | lower -%}
{%- set url = resource.path ~ (filename | urlencode) -%}
{%- if extension == "mp4" %}
<video class="news_video" controls{% if autoplay %} autoplay muted{% else %} preload="metadata"{% endif %}>
<source src="{{ url }}" type="video/mp4">
</video>
{%- elif extension in ["jpg", "jpeg", "png", "webp"] -%}
{%- set meta = get_image_metadata(path=media) -%}
{#- Keep sharing previews published and show their WebP siblings in the gallery. -#}
{%- if extension != "webp" -%}
{%- set webp_path = (media | trim_end(pat=filename_extension)) ~ "webp" -%}
{%- if webp_path in resource.assets -%}{%- continue -%}{%- endif -%}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional fail-fast (new in this PR): README says "Do not add JPEG/PNG duplicates" for new posts, but nothing enforces it. A stray X.jpg next to X.webp is hidden here without any warning and still gets published. All 42 retained originals are currently the sharing preview: either the first image asset (first_image is still true when the hidden original is reached) or extra.preview_image (for example 630/02-bookmarks-tracks-multi-selection.jpg). Throwing for anything else turns the convention into a build check:

Suggested change
{%- if webp_path in resource.assets -%}{%- continue -%}{%- endif -%}
{%- if webp_path in resource.assets -%}
{#- Only a published sharing preview may keep its original next to its WebP. -#}
{%- if not first_image and filename != resource.extra | get(key="preview_image", default="") -%}
{{- throw(message="'" ~ media ~ "' duplicates its WebP sibling and is not the sharing preview") -}}
{%- endif -%}
{%- continue -%}
{%- endif -%}

I checked this with Zola 0.23.4. Applied to this branch, the full build output is byte-identical (diff -rq is empty). Adding 630/03-hide-track-in-list.jpg next to its WebP fails the build. It's lenient in one case: a hidden first original in a post that also has a parent -preview cover. If you take it, add a negative subtest to test_navigation.py next to the invalid_alt_key cases.


Generated by Claude Code

{%- endif -%}
{%- set_global image_filenames = [...image_filenames, filename] -%}
{%- set meta = get_image_metadata(path=media) -%}
{%- set alt = alts | get(key=filename, default=resource.title) %}
<img class="news_image{% if meta.width > meta.height %}_landscape{% endif %}" src="{{ url }}" width="{{ meta.width }}" height="{{ meta.height }}" alt="{{ alt }}" loading="{% if eager_first_image and first_image %}eager{% else %}lazy{% endif %}" decoding="async" />
{%- set_global first_image = false -%}
Expand All @@ -34,7 +40,7 @@
{%- endfor -%}
{%- for filename, _description in alts -%}
{%- if filename not in image_filenames -%}
{{- throw(message="media_alt key '" ~ filename ~ "' matches no image asset of " ~ resource.relative_path) -}}
{{- throw(message="media_alt key '" ~ filename ~ "' matches no gallery image of " ~ resource.relative_path) -}}
{%- endif -%}
{%- endfor -%}
{%- endcomponent news_media %}
7 changes: 4 additions & 3 deletions templates/_news_preview.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
{%- set folder = source_dir | trim_end(pat="/") -%}
{%- set article_id = folder | split(pat="/") | last -%}
{%- set parent_dir = folder | trim_end(pat=article_id) -%}
{%- set filename = article_id ~ "-preview.jpg" -%}
{%- set parent = get_section(path=parent_dir ~ "_index.md", lang=default_language, allow_missing=true) -%}
{%- if parent -%}
{%- for asset in parent.assets -%}
{%- if asset | split(pat="/") | last == filename -%}
{# Existing JPEG cover URLs take priority; new covers can be WebP. #}
{%- for filename in [article_id ~ "-preview.jpg", article_id ~ "-preview.webp"] -%}
{%- set asset = "/" ~ parent_dir ~ filename -%}
{%- if asset in parent.assets -%}
{%- set _preview_exists = get_image_metadata(path=asset) -%}
{{- parent.path ~ (filename | urlencode) -}}
{%- break -%}
Expand Down
10 changes: 10 additions & 0 deletions tmp/test_tooling.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
validate_media_set,
visible_text,
)
from telegram_post_all import find_media # noqa: E402
from translate_check import ERROR, WARN, check_translation, emphasis_faults # noqa: E402
from translate_md import ( # noqa: E402
_segments, localize_zola_links, register_ok, tidy, translate_post, translate_text,
Expand Down Expand Up @@ -356,6 +357,15 @@ def test_probe_builds_unknown_and_variant_targets(self):


class TelegramToolingTests(unittest.TestCase):
def test_media_discovery_prefers_webp_over_retained_preview_originals(self):
with tempfile.TemporaryDirectory() as tmp:
folder = Path(tmp)
for name in ("01-cover.JPG", "01-cover.WEBP", "02-map.PNG", "02-map.jpeg",
"02-map.webp", "03-legacy.PNG", "04-video.mp4", "05-audio.mp3", "notes.txt"):
(folder / name).write_bytes(b"media")
self.assertEqual([path.name for path in find_media(folder)],
["01-cover.JPG", "01-cover.WEBP", "02-map.webp", "03-legacy.PNG", "04-video.mp4", "05-audio.mp3"])

def test_current_tera_components_are_removed_from_posts(self):
text = (
"Before\n\n{{ <screenshot\n"
Expand Down
8 changes: 6 additions & 2 deletions tools/telegram_post_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,17 @@ def resolve(selectors: list[str], flag: str) -> set[str]:


def find_media(folder: Path) -> list[Path]:
"""Find all media files in the folder."""
"""Find media, preferring lowercase .webp siblings over JPEG and PNG originals."""
media = sorted(
p
for p in folder.iterdir()
if p.is_file() and p.suffix.lower() in MEDIA_EXTENSIONS
)
return media
webp_names = {p.name for p in media if p.suffix == ".webp"}
return [
p for p in media
if not (p.suffix.lower() in {".jpg", ".jpeg", ".png"} and p.with_suffix(".webp").name in webp_names)
]


def prepare_text(md_path: Path, site_root: Path) -> str:
Expand Down
38 changes: 28 additions & 10 deletions tools/test_navigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,10 @@ def test_news_images_and_preview_resolution(self):
(root / "static/http-assets/cover.jpg").write_bytes(jpeg)
cases = {
"auto": ("", "auto", "01-preview.WEBP"),
"legacy": ("", "legacy", "01-preview.jpg"),
"chosen": ("extra:\n preview_image: cover.webp\n", "chosen", "cover.webp"),
"parent": ("", "custom-slug", "/news/2024-01-01/parent-preview.jpg"),
"parent-webp": ("", "parent-webp", "/news/2024-01-01/parent-webp-preview.webp"),
"override": ("extra:\n preview_image: cover.webp\n", "override", "cover.webp"),
"site-root": ("extra:\n preview_image: /images/map.WEBP\n", "site-root", "/images/map.WEBP"),
"local-http-name": ("extra:\n preview_image: http-cover.jpg\n", "local-http-name", "http-cover.jpg"),
Expand All @@ -223,16 +225,21 @@ def test_news_images_and_preview_resolution(self):
parent = root / "content/news/2024-01-01"
for name in ("parent", "override"):
(parent / f"{name}-preview.jpg").write_bytes(jpeg)
(parent / f"{name}-preview.webp").write_bytes(image)
(parent / "parent-webp-preview.webp").write_bytes(image)
# The convention has an exact filename, even on case-insensitive filesystems.
(parent / "auto-preview.JPG").write_bytes(jpeg)
for name in cases:
folder = root / "content/news/2024-01-01" / name
(folder / "00-video.mp4").write_bytes(b"video")
if name != "fallback":
(folder / "01-preview.WEBP").write_bytes(image)
gallery_cover = "01-preview.webp" if name == "legacy" else "01-preview.WEBP"
(folder / gallery_cover).write_bytes(image)
(folder / "02-detail.webp").write_bytes(image)
if name in ("chosen", "override"):
(folder / "cover.webp").write_bytes(image)
if name == "legacy":
(folder / "01-preview.jpg").write_bytes(jpeg)
if name == "local-http-name":
(folder / "http-cover.jpg").write_bytes(jpeg)

Expand Down Expand Up @@ -265,7 +272,8 @@ def test_news_images_and_preview_resolution(self):
).group(1))
if preview:
self.assertEqual(article["image"], [expected])
for filename in ("01-preview.WEBP", "02-detail.webp"):
gallery_cover = "01-preview.webp" if name == "legacy" else "01-preview.WEBP"
for filename in (gallery_cover, "02-detail.webp"):
self.assertTrue((public / path.strip("/") / filename).is_file())
for output in (html, listing):
self.assertIn(f'class="news_image_landscape" src="{path}{filename}"', output)
Expand All @@ -280,10 +288,14 @@ def test_news_images_and_preview_resolution(self):
self.assertNotIn("image", article)
expected_count = 0 if name == "fallback" else 3 if name in ("chosen", "override", "local-http-name") else 2
self.assertEqual(len(re.findall(r'<img class="news_image(?:_landscape)?"', html)), expected_count)
if name in ("chosen", "parent", "override", "site-root", "local-http-name", "local-http-path"):
if name in ("chosen", "parent", "parent-webp", "override", "site-root", "local-http-name", "local-http-path"):
self.assertEqual(sitemap_images["https://example.org" + path], expected)
else:
self.assertIsNone(sitemap_images["https://example.org" + path])
if name == "legacy":
self.assertEqual((public / path.strip("/") / "01-preview.jpg").read_bytes(), jpeg)
for output in (html, listing, dated):
self.assertNotRegex(output, r'<img[^>]*src="' + re.escape(path + '01-preview.jpg') + r'"')
for name in ("parent", "override"):
filename = f"/news/2024-01-01/{name}-preview.jpg"
self.assertTrue((public / filename.lstrip("/")).is_file())
Expand Down Expand Up @@ -334,7 +346,7 @@ def test_news_media_in_articles_listings_and_date_archives(self):
write(root, f"content/news/2024-01-01/1/index{suffix}.md",
f'---\ntitle: {title}\nslug: {slug}\ndate: 2024-01-01\n'
'taxonomies:\n news: ["Releases"]\nextra:\n media_alt:\n'
f' "01-cover.WEBP": \'{description}\'\n'
f' "01-cover.webp": \'{description}\'\n'
' "04-decoration.png": ""\n---\n'
'{{ <audio title="Song" url="/news/2024-01-01/mixed/06-voice.MP3" /> }}\n')
write(root, f"content/news/2024-01-01/2/index{suffix}.md",
Expand All @@ -345,11 +357,12 @@ def test_news_media_in_articles_listings_and_date_archives(self):
(root / "content/news/2024-01-01/2/portrait.png").write_bytes((fixtures / "portrait-test.png").read_bytes())
files = {
"00-video.MP4": b"video",
"01-cover.WEBP": (fixtures / "preview-test.webp").read_bytes(),
"01-cover.webp": (fixtures / "preview-test.webp").read_bytes(),
"01-cover.jpg": (fixtures / "preview-test.jpg").read_bytes(),
"02-detail.jpeg": (fixtures / "preview-test.jpg").read_bytes(),
"03-portrait.PNG": (fixtures / "portrait-test.png").read_bytes(),
"04-decoration.png": (fixtures / "portrait-test.png").read_bytes(),
"05-spaced #image.JPG": (fixtures / "preview-test.jpg").read_bytes(),
"05-spaced #image.webp": (fixtures / "preview-test.webp").read_bytes(),
"06-voice.MP3": b"audio",
}
for name in reversed(files):
Expand All @@ -369,7 +382,8 @@ def test_news_media_in_articles_listings_and_date_archives(self):
parser = NewsMedia()
parser.feed(html)
self.assertEqual([src for src in parser.order if src.startswith(path)],
[path + quote(name, safe="") for name in sorted(files)[:6]])
[path + quote(name, safe="") for name in sorted(files)
if name not in ("01-cover.jpg", "06-voice.MP3")])
self.assertEqual(len(parser.videos), 1)
self.assertIn("controls", parser.videos[0])
if view != "article":
Expand All @@ -390,6 +404,10 @@ def test_news_media_in_articles_listings_and_date_archives(self):
self.assertNotIn('alt="Ridges & valleys at "sunset"', html)
if view == "listing":
self.assertCountEqual(parser.layouts, ["news_content_image_noflex", "news_content_image"])
self.assertEqual((root / "public" / path.lstrip("/") / "05-spaced #image.webp").read_bytes(),
files["05-spaced #image.webp"])
self.assertEqual((root / "public" / path.lstrip("/") / "01-cover.jpg").read_bytes(),
files["01-cover.jpg"])

for extension in ("PDF", "svg", "mov", "m4a", "gif", "avif", "webm"):
with self.subTest(unsupported=extension):
Expand All @@ -402,11 +420,11 @@ def test_news_media_in_articles_listings_and_date_archives(self):

source = folder / "index.md"
original = source.read_text()
for key in ("01-cover.webp", "stale.webp", "00-video.MP4", "06-voice.MP3"):
for key in ("01-cover.jpg", "01-cover.WEBP", "stale.webp", "00-video.MP4", "06-voice.MP3"):
with self.subTest(invalid_alt_key=key):
source.write_text(original.replace('"01-cover.WEBP"', f'"{key}"'))
source.write_text(original.replace('"01-cover.webp"', f'"{key}"'))
try:
self.build(root, expected_error=f"media_alt key '{key}' matches no image asset")
self.build(root, expected_error=f"media_alt key '{key}' matches no gallery image")
finally:
source.write_text(original)

Expand Down
Loading