Skip to content

Commit bdd3820

Browse files
committed
feat(gallery): sidecar files, EXIF/IPTC, and page-bundle auto-discovery
Adds the metadata pipeline that makes the gallery block self-curating. Per-image metadata now resolves through a four-tier waterfall, first non-empty source wins: 1. Frontmatter (items[].caption etc.) 2. Sidecar file next to the image - tries six patterns: <name>.yaml / .yml / .json and <basename>.yaml / .yml / .json, looked up in both site assets and the page bundle (covers album mode and page-resource mode alike). 3. Embedded IPTC/EXIF via Hugo's .Exif.Tags - caption from Caption-Abstract / ImageDescription / XPSubject / Description, credit from By-line / Artist / Creator. Wrapped in `try` so non-EXIF formats and bad metadata never break the build. 4. Humanised filename - alt fallback only; captions stay empty rather than fabricated from filenames. Also adds a third source mode - content.page_resources: true - auto-discovers image resources attached to the current page bundle. Drop-in for blog posts and project pages where photos live next to index.md; sidecar lookup works inside the bundle too. Fixes a leading-slash quirk: Resource.Name returns "/media/..." for site assets but a bare path for page-bundle resources; the path is now normalised so sidecar lookups land in either layout. Test site adds two sidecar YAML files (04-cabin.jpg.yaml and 06-mountain.jpg.yaml) - the masonry album demo now picks up titles, markdown captions, and links from those sidecars without listing any items[].
1 parent b67f09b commit bdd3820

3 files changed

Lines changed: 120 additions & 19 deletions

File tree

β€Žmodules/blox/layouts/_partials/functions/resolve_gallery_items.htmlβ€Ž

Lines changed: 111 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
{{/*
22
resolve_gallery_items β€” Resolves gallery block items into a list of fully-processed
3-
image props ready for the Preact component. Handles two source modes:
3+
image props ready for the Preact component. Three source modes:
44

5-
1. content.items[] β€” explicit per-image dicts with optional caption/alt/credit/link
6-
2. content.album β€” folder path relative to assets/, e.g. "media/albums/gallery"
5+
1. content.items[] β€” explicit per-image dicts (curated)
6+
2. content.album β€” folder under assets/, e.g. "media/albums/gallery"
7+
3. content.page_resources β€” auto-discover image resources in the current page bundle
78

89
For each resolved image, produces a dict with:
910
{src, srcset, width, height, aspect_ratio, lqip, alt, caption, credit, link, title}
1011

11-
Slice 1 caption resolution: frontmatter only (items[].caption). Sidecar + EXIF/IPTC
12-
resolution is intentionally deferred to Slice 2 β€” for now, missing captions stay
13-
empty in the UI, with the humanised filename surfaced as the `alt` fallback so
14-
screen readers and search engines have something useful.
12+
Metadata waterfall β€” first non-empty value per field wins:
13+
1. Frontmatter (items[].caption etc.)
14+
2. Sidecar file next to the image (.yaml / .yml / .json)
15+
β€” tried as "<full-name>.yaml" then "<basename-without-ext>.yaml"
16+
β€” looked up in both site assets and the current page bundle
17+
3. Embedded IPTC/EXIF via Hugo's .Exif.Tags
18+
β€” caption from Caption-Abstract / ImageDescription / XPSubject / Description
19+
β€” credit from By-line / Artist / Creator
20+
4. Humanised filename (alt only β€” captions stay empty when no metadata exists)
1521

1622
Inputs:
17-
.content: the block's content dict (items / album / sort / limit)
18-
.Page: current page (for page-resource image lookup)
23+
.content: the block's content dict
24+
.Page: current page (used for page-resource lookup + RenderString markdown)
1925

2026
Returns: dict { items: [...] } or empty if nothing resolves.
2127
*/}}
@@ -27,7 +33,7 @@
2733
{{ $sort_order := lower ($content.sort | default "asc") }}
2834
{{ $limit := $content.limit | default 0 }}
2935

30-
{{/* Build a unified list of {resource?, fallback_url?, meta} entries from either source mode */}}
36+
{{/* Build a unified list of {resource?, fallback_url?, meta} entries */}}
3137
{{ $candidates := slice }}
3238

3339
{{/* Source A β€” explicit items */}}
@@ -61,6 +67,23 @@
6167
{{ end }}
6268
{{ end }}
6369

70+
{{/* Source C β€” page bundle auto-discovery */}}
71+
{{ if $content.page_resources }}
72+
{{ with $page }}
73+
{{ $imgs := .Resources.ByType "image" }}
74+
{{ $sorted := sort $imgs "Name" $sort_order }}
75+
{{ range $sorted }}
76+
{{ if in $allowed_exts (lower (path.Ext .Name)) }}
77+
{{ $candidates = $candidates | append (dict
78+
"resource" .
79+
"fallback_url" ""
80+
"meta" dict
81+
) }}
82+
{{ end }}
83+
{{ end }}
84+
{{ end }}
85+
{{ end }}
86+
6487
{{/* Apply limit */}}
6588
{{ if gt $limit 0 }}
6689
{{ $candidates = first $limit $candidates }}
@@ -73,17 +96,88 @@
7396
{{ $resource := .resource }}
7497
{{ $fallback_url := .fallback_url }}
7598

76-
{{/* Per-item text fields. Caption / title / credit are rendered through
77-
Hugo's markdown pipeline at build time (inline mode β€” no wrapping <p>)
78-
so users can put **bold**, *italic*, `code`, and [links](url) in any of
79-
them without shipping a JS markdown parser. Alt stays as plain text
80-
since it lives inside an HTML attribute. */}}
99+
{{/* ---- Metadata waterfall ---- */}}
81100
{{ $alt := index $meta "alt" | default "" }}
82101
{{ $caption_raw := index $meta "caption" | default "" }}
83102
{{ $title_raw := index $meta "title" | default "" }}
84103
{{ $credit_raw := index $meta "credit" | default "" }}
85104
{{ $link := index $meta "link" | default "" }}
86105

106+
{{/* Tier 2 β€” sidecar file lookup. Tries:
107+
<name>.yaml/.yml/.json (full filename, Lightroom-style convention)
108+
<basename>.yaml/.yml/.json (filename minus extension)
109+
in both site assets and the page bundle. */}}
110+
{{ $sidecar := false }}
111+
{{ if $resource }}
112+
{{/* Resource.Name comes back with a leading slash for site assets (e.g.
113+
"/media/albums/gallery/01.jpg"); strip it so resources.Get treats the
114+
path as relative to assets/. Page-bundle resource names have no
115+
leading slash, so TrimPrefix is a no-op for those. */}}
116+
{{ $name := strings.TrimPrefix "/" $resource.Name }}
117+
{{ $basename := strings.TrimSuffix (path.Ext $name) $name }}
118+
{{ $patterns := slice
119+
(printf "%s.yaml" $name)
120+
(printf "%s.yml" $name)
121+
(printf "%s.json" $name)
122+
(printf "%s.yaml" $basename)
123+
(printf "%s.yml" $basename)
124+
(printf "%s.json" $basename)
125+
}}
126+
{{ range $pattern := $patterns }}
127+
{{ if not $sidecar }}
128+
{{ $candidate := resources.Get $pattern }}
129+
{{ if not $candidate }}{{ $candidate = resources.GetMatch $pattern }}{{ end }}
130+
{{ if and (not $candidate) $page }}
131+
{{ $candidate = $page.Resources.GetMatch $pattern }}
132+
{{ end }}
133+
{{ if $candidate }}
134+
{{ $sidecar = $candidate }}
135+
{{ end }}
136+
{{ end }}
137+
{{ end }}
138+
{{ end }}
139+
140+
{{ if $sidecar }}
141+
{{ $data := transform.Unmarshal $sidecar }}
142+
{{ if reflect.IsMap $data }}
143+
{{ if not $caption_raw }}{{ $caption_raw = index $data "caption" | default "" }}{{ end }}
144+
{{ if not $title_raw }}{{ $title_raw = index $data "title" | default "" }}{{ end }}
145+
{{ if not $credit_raw }}{{ $credit_raw = index $data "credit" | default "" }}{{ end }}
146+
{{ if not $alt }}{{ $alt = index $data "alt" | default "" }}{{ end }}
147+
{{ if not $link }}{{ $link = index $data "link" | default "" }}{{ end }}
148+
{{ end }}
149+
{{ end }}
150+
151+
{{/* Tier 3 β€” embedded IPTC/EXIF. Lightroom/Capture One JPEG exports carry
152+
Caption-Abstract + By-line + Copyright; this picks them up automatically. */}}
153+
{{ $exif_tags := dict }}
154+
{{ if $resource }}
155+
{{ $exif_try := try $resource.Exif }}
156+
{{ if and (not $exif_try.Err) $exif_try.Value }}
157+
{{ $exif_tags = $exif_try.Value.Tags | default dict }}
158+
{{ end }}
159+
{{ end }}
160+
161+
{{ if $exif_tags }}
162+
{{ if not $caption_raw }}
163+
{{ range $key := (slice "Caption-Abstract" "ImageDescription" "XPSubject" "Description") }}
164+
{{ if not $caption_raw }}
165+
{{ $v := index $exif_tags $key }}
166+
{{ if $v }}{{ $caption_raw = printf "%v" $v }}{{ end }}
167+
{{ end }}
168+
{{ end }}
169+
{{ end }}
170+
{{ if not $credit_raw }}
171+
{{ range $key := (slice "By-line" "Artist" "Creator") }}
172+
{{ if not $credit_raw }}
173+
{{ $v := index $exif_tags $key }}
174+
{{ if $v }}{{ $credit_raw = printf "%v" $v }}{{ end }}
175+
{{ end }}
176+
{{ end }}
177+
{{ end }}
178+
{{ end }}
179+
180+
{{/* ---- Inline-Markdown render of text fields (RenderString display:inline) ---- */}}
87181
{{ $caption := $caption_raw }}
88182
{{ if and $caption_raw $page }}
89183
{{ $caption = $page.RenderString (dict "display" "inline") $caption_raw }}
@@ -135,10 +229,8 @@
135229
) }}
136230
{{ end }}
137231

138-
{{/* Alt fallback: plainified caption β†’ humanised filename. Caption itself
139-
is NOT filled from filename β€” empty captions stay empty in the UI
140-
(Slice 1 behaviour). Plainify strips the HTML produced by RenderString
141-
so alt stays attribute-safe. */}}
232+
{{/* Tier 4 β€” alt fallback: plainified caption β†’ humanised filename. Caption
233+
itself stays empty when no metadata source has supplied one. */}}
142234
{{ if not $entry.alt }}
143235
{{ $fallback_alt := "" }}
144236
{{ with $caption_raw }}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
title: "The cabin"
2+
caption: "*Winter quarters* β€” where the team writes up over the slow months"
3+
credit: "Photo: [Picsum](https://picsum.photos)"
4+
alt: "Snow-dusted wooden cabin among pine trees"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
title: "Summit"
2+
caption: "Final ridge of the **Highland traverse**, with the trig point in view"
3+
credit: "Photo: [Picsum](https://picsum.photos)"
4+
link: "/expeditions/highland-traverse/"
5+
keywords: [highland, traverse, summit]

0 commit comments

Comments
Β (0)