From edba5d203e3fd01f9a73d20c19c64b29abb7df92 Mon Sep 17 00:00:00 2001 From: Ben Goldsworthy Date: Wed, 4 Jun 2025 17:57:21 +0200 Subject: [PATCH] use `get-image` partial --- layouts/cv/section.html | 13 +++-- layouts/partials/images/get-image.html | 55 ++++++++++++++++--- layouts/partials/item-tile.html | 28 ++++------ layouts/partials/media/picture.html | 29 +++++++--- layouts/partials/media/video.html | 4 +- .../single/main-header/item-tile.html | 6 +- layouts/shortcodes/figure.html | 2 +- layouts/shortcodes/picture.html | 4 +- 8 files changed, 94 insertions(+), 47 deletions(-) diff --git a/layouts/cv/section.html b/layouts/cv/section.html index 4c979b1..c20bd28 100644 --- a/layouts/cv/section.html +++ b/layouts/cv/section.html @@ -23,11 +23,14 @@ {{ if .IsDescendant ( .GetPage "/cv/organisations" ) }}
+ {{- with .Params.featured_image -}} + {{- with ( $.Resources.GetMatch . ) -}} + style="background-image: url({{- partialCached "images/get-image.html" ( dict "img" . ) .Title -}}){{ with .Params.bg -}}; background-color: {{ . -}};{{- end -}}" + {{- else -}} + {{- errorf "No Page Resource found for src '%q' (%q).\n\n\tAvailable Page Resources are:\n\t\t%v" . $.pc.Page.File.Path $.pc.Page.Resources -}} + {{- end -}} + {{- end -}} + > diff --git a/layouts/partials/images/get-image.html b/layouts/partials/images/get-image.html index 1ea645a..4b4a00c 100644 --- a/layouts/partials/images/get-image.html +++ b/layouts/partials/images/get-image.html @@ -1,25 +1,62 @@ -{{- /* - Renders an image, resizing it if valid. + + + +{{- $img := .img -}} +{{- $hint := default ( index $img.Params "hint" ) "" }} +{{- $size := cond ( isset . "size" ) .size "" }} +{{- $isResizable := not ( in ( slice "avif" "svg" "gif" ) $img.MediaType.SubType ) -}} +{{- $format := cond ( isset . "format" ) .format "webp" }} + +{{- $permalink := "" -}} +{{- if ( not ( isset . "img" ) ) -}} + {{- errorf "No img set" -}} +{{- end -}} + {{- if ne .img.MediaType.MainType "image" -}} {{- errorf "Image '%q' is not an image" .img }} {{- end -}} +{{- if ( isset . "size" ) -}} + {{- if $isResizable -}} + {{- $width := ( first 1 ( split .size "x" ) ) -}} + {{- if gt $width $img.Width -}} + {{- warnf "Requested size %s is more than image width %d (%q)" $width $img.Width $img.File.Path -}} + {{- end -}} + {{- else -}} + {{- warnf "Size set on non-resizable image of type %s; ignoring" $img.MediaType.SubType -}} + {{- end -}} +{{- end -}} -{{- $isResizable := not ( in ( slice "avif" "svg" "gif" ) .img.MediaType.SubType ) -}} {{- if $isResizable -}} - {{- with .img.Resize "1200x webp" -}} - {{- .RelPermalink -}} + {{- if $size -}} + {{- if eq $size "same" -}} + {{- $permalink = ( $img.Process ( printf "%s %s" $format $hint ) ).RelPermalink -}} + {{- else -}} + {{- $permalink = ( $img.Resize ( printf "%s %s %s" $size $format $hint ) ).RelPermalink -}} + {{- end -}} + {{- else -}} + {{- if gt $img.Width 1200 -}} + {{- $permalink = ( $img.Resize ( printf "1200x %s %s" $format $hint ) ).RelPermalink -}} + {{- else if gt $img.Width 800 -}} + {{- $permalink = ( $img.Resize ( printf "800x %s %s" $format $hint ) ).RelPermalink -}} + {{- else -}} + {{- $permalink = ( $img.Process ( printf "%s %s" $format $hint ) ).RelPermalink -}} + {{- end -}} {{- end -}} {{- else }} - {{- .img.RelPermalink -}} + {{- $permalink = $img.RelPermalink -}} {{- end -}} + +{{- return $permalink -}} diff --git a/layouts/partials/item-tile.html b/layouts/partials/item-tile.html index 4c56c25..6a7fe3e 100644 --- a/layouts/partials/item-tile.html +++ b/layouts/partials/item-tile.html @@ -32,17 +32,14 @@
{{ if .Params.featured_image -}} @@ -51,15 +48,10 @@ style="display: none;" src=" {{- with .Resources.GetMatch .Params.featured_image -}} - {{- if ( in ( slice "avif" "svg" "gif" ) .MediaType.SubType ) -}} - {{ .RelPermalink }} - {{- else -}} - {{- with .Resize "1000x webp" -}} - {{- .RelPermalink -}} - {{- end -}} - {{- end -}} + {{- partialCached "images/get-image.html" ( dict "img" . "size" "500x" ) ( printf "%s-500x" .Title ) -}} {{- else -}} - {{ .Params.featured_image }} + {{- warnf "Featured image %s is not a page resource" .Params.featured_image -}} + {{- .Params.featured_image -}} {{ end }} " alt=" diff --git a/layouts/partials/media/picture.html b/layouts/partials/media/picture.html index f611416..cb26816 100644 --- a/layouts/partials/media/picture.html +++ b/layouts/partials/media/picture.html @@ -1,12 +1,15 @@ -{{- $src := . -}} +{{- $src := .src -}} +{{- $hint := default ( index $src.Params "hint" ) "" }} +{{- $format := cond ( and ( isset . "raw" ) .raw ) "" "webp" -}} {{- $isResizable := not ( in ( slice "svg" "gif" ) $src.MediaType.SubType ) -}} @@ -17,18 +20,30 @@ itemprop="image" > {{ if $isResizable -}} - - + {{- if gt $src.Width 1200 -}} + + {{- end -}} + {{- if gt $src.Width 800 -}} + + {{- end -}} {{- end }} {{- $src := $.Page.Resources.GetMatch ( .Get "src" ) -}} +{{- $raw := default false ( .Get "raw" ) -}} {{- $linkedResource := $.Page.Resources.GetMatch ( .Get "link" ) -}} {{- $valid_rel_values := partialCached "util/get_valid_rel_values.html" . -}} @@ -37,7 +39,7 @@ {{- end -}} {{- end -}} -{{- partial "media/picture.html" $src -}} +{{- partial "media/picture.html" ( dict "src" $src "raw" $raw ) -}} {{- if .Get "link" -}} {{- end -}}