|
1 | 1 | {{/* |
2 | 2 | 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: |
4 | 4 |
|
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 |
7 | 8 |
|
8 | 9 | For each resolved image, produces a dict with: |
9 | 10 | {src, srcset, width, height, aspect_ratio, lqip, alt, caption, credit, link, title} |
10 | 11 |
|
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) |
15 | 21 |
|
16 | 22 | 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) |
19 | 25 |
|
20 | 26 | Returns: dict { items: [...] } or empty if nothing resolves. |
21 | 27 | */}} |
|
27 | 33 | {{ $sort_order := lower ($content.sort | default "asc") }} |
28 | 34 | {{ $limit := $content.limit | default 0 }} |
29 | 35 |
|
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 */}} |
31 | 37 | {{ $candidates := slice }} |
32 | 38 |
|
33 | 39 | {{/* Source A β explicit items */}} |
|
61 | 67 | {{ end }} |
62 | 68 | {{ end }} |
63 | 69 |
|
| 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 | + |
64 | 87 | {{/* Apply limit */}} |
65 | 88 | {{ if gt $limit 0 }} |
66 | 89 | {{ $candidates = first $limit $candidates }} |
|
73 | 96 | {{ $resource := .resource }} |
74 | 97 | {{ $fallback_url := .fallback_url }} |
75 | 98 |
|
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 ---- */}} |
81 | 100 | {{ $alt := index $meta "alt" | default "" }} |
82 | 101 | {{ $caption_raw := index $meta "caption" | default "" }} |
83 | 102 | {{ $title_raw := index $meta "title" | default "" }} |
84 | 103 | {{ $credit_raw := index $meta "credit" | default "" }} |
85 | 104 | {{ $link := index $meta "link" | default "" }} |
86 | 105 |
|
| 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) ---- */}} |
87 | 181 | {{ $caption := $caption_raw }} |
88 | 182 | {{ if and $caption_raw $page }} |
89 | 183 | {{ $caption = $page.RenderString (dict "display" "inline") $caption_raw }} |
|
135 | 229 | ) }} |
136 | 230 | {{ end }} |
137 | 231 |
|
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. */}} |
142 | 234 | {{ if not $entry.alt }} |
143 | 235 | {{ $fallback_alt := "" }} |
144 | 236 | {{ with $caption_raw }} |
|
0 commit comments