diff options
Diffstat (limited to 'themes/default/layouts/_default/_markup/render-image.html')
-rw-r--r-- | themes/default/layouts/_default/_markup/render-image.html | 145 |
1 files changed, 81 insertions, 64 deletions
diff --git a/themes/default/layouts/_default/_markup/render-image.html b/themes/default/layouts/_default/_markup/render-image.html index 0cf1d5a..04bf931 100644 --- a/themes/default/layouts/_default/_markup/render-image.html +++ b/themes/default/layouts/_default/_markup/render-image.html @@ -1,93 +1,110 @@ -{{- $source := $.Destination | safeURL -}} -{{- $public := print "public/" $source -}} -{{- $local := "" -}} -{{- $cache := "" -}} -{{- $exists := "" -}} -{{- $width := "" -}} -{{- $height := "" -}} -{{- $dimensions := "600x360" -}} -{{- $author := .Page.Section -}} -{{- $relURL := strings.TrimPrefix .Page.Site.BaseURL .Page.Permalink -}} -{{- $immutable := print ($relURL | humanize | urlize) "-" ($source | sha256 | truncate 8 "") -}} -{{- $fileCache := print (partial "function-paths-media.html") "/" $author "/" $immutable -}} +{{- $cache := "" -}} +{{- $width := "" -}} +{{- $height := "" -}} +{{- $srcset := "" -}} +{{- $colors := "" -}} +{{- $type := "" -}} +{{- $source := $.Destination | safeURL -}} +{{- $public := print "public/" $source -}} +{{- $fetch := print (urls.Parse $source).Scheme "://" (urls.Parse $source).Host (urls.Parse $source).RequestURI -}} +{{- $figcaption := "Image/Picture" -}} +{{- $orientation := "landscape" -}} +{{- $dimensions := "600x360" -}} +{{- $author := .Page.Section -}} +{{- $relURL := strings.TrimPrefix .Page.Site.BaseURL .Page.Permalink -}} +{{- $immutable := print ($relURL | humanize | urlize) "-" ($source | sha256 | truncate 8 "") -}} +{{- $storage := print $author "/media/" $immutable -}} +{{- $cached := fileExists (print "public/" $storage ".webp") -}} -{{- if not (or (fileExists (print "public/" $fileCache ".webp")) (fileExists (print "public/" $fileCache (path.Ext $source)))) -}} - {{- with $remote := resources.GetRemote $source -}} +{{- $moderns := slice ".webp" ".avif" -}} +{{- $fallbacks := slice ".png" ".jpg" ".gif" ".jpeg" ".svg" ".apng" ".jfif" ".pjpeg" ".pjp" -}} +{{- $mimes := slice "image/avif" "image/webp" "image/apng" "image/gif" "image/jpeg" "image/png" "image/svg+xml" -}} + +{{- if not $cached -}} + {{- with $remote := cond (default true $.Page.Site.Params.site.offline) false (resources.GetRemote $fetch) -}} {{- with .Err -}} {{- if fileExists $public -}} - {{- $local = resources.Get $public -}} - {{- with $local -}} - {{- $local = .Content | resources.FromString (print (partial "function-paths-media.html") "/" $author "/" (path.Base .)) -}} + {{- with $local := resources.Get $public -}} + {{- $local = .Content | resources.FromString (print $storage (path.Base .)) -}} + {{- $local = .Fit (print $dimensions " webp") -}} + {{- $local = $local | resources.Copy (print $storage ".webp") -}} + {{- $srcset = $local.Permalink -}} + {{- $local = .Fit $dimensions -}} + {{- $local = $local | resources.Copy (print $storage (path.Ext $local)) -}} + {{- $width = $local.Width -}} + {{- $height = $local.Height -}} + {{- $colors = $local.Colors -}} + {{- $cache = $local.Permalink -}} {{- end -}} {{- $source = print ("" | absURL) $source -}} {{- else -}} - {{- $404image := resources.Get "data/media/404.png" -}} - {{- with $404image -}} - {{- $width = .Width -}} - {{- $height = .Height -}} + {{- with $404image := resources.Get "data/media/404.png" -}} + {{- $width = $404image.Width -}} + {{- $height = $404image.Height -}} + {{- $source = $404image.Permalink -}} {{- end -}} - {{- $source = $404image.RelPermalink -}} {{- end -}} {{- else -}} - {{- $cache = .Content | resources.FromString (print (partial "function-paths-media.html") "/" $author "/" (path.Base .)) -}} + {{- with $cache = $remote.Content | resources.FromString (print $storage (path.Base $remote)) -}} + {{- $cache = .Fit (print $dimensions " webp") -}} + {{- $cache = $cache | resources.Copy (print $storage ".webp") -}} + {{- $srcset = $cache.Permalink -}} + {{- $cache = .Fit $dimensions -}} + {{- $cache = $cache | resources.Copy (print $storage (path.Ext $cache)) -}} + {{- $width = $cache.Width -}} + {{- $height = $cache.Height -}} + {{- $colors = $cache.Colors -}} + {{- $cache = $cache.Permalink -}} + {{- end -}} {{- end -}} {{- end -}} {{- else -}} - {{- $exists = resources.Get (print "public/" $fileCache (path.Ext $source)) -}} - {{- $exists = $exists.Content | resources.FromString (print (partial "function-paths-media.html") "/" $author "/" (path.Base $exists)) -}} + {{- with $cache = resources.Get (print "public/" $storage ".webp") -}} + {{- $width = $cache.Width -}} + {{- $height = $cache.Height -}} + {{- $colors = $cache.Colors -}} + {{- $type = $cache.MediaType -}} + {{- $srcset = print ("" | absURL) "/" $storage ".webp" -}} + {{- end -}} + {{- range $extension := $fallbacks -}} + {{- with resources.Get (print "public/" $storage $extension) -}} + {{- $cache = print ("" | absURL) "/" $storage $extension -}} + {{- break -}} + {{- end -}} + {{- end -}} +{{- end -}} + +{{- if lt $width $height -}} + {{- $orientation = "portrait" -}} +{{- end -}} + +{{- with $caption := $.Title -}} + {{- $figcaption = $caption | markdownify -}} {{ end }} {{- /* This comment removes trailing newlines and white spaces. */ -}} <figure> - <a href="{{ $source }}"> + <a data-orientation="{{ $orientation }}" href="{{ $source }}"> <picture> - {{- with $local }} - {{- $local = .Fit (print $dimensions " webp") }} - {{- $local = $local | resources.Copy (print (partial "function-paths-media.html") "/" $author "/" $immutable ".webp") }} - <source srcset="{{- $local.RelPermalink -}}" type="image/webp" /> - {{- $local = .Fit $dimensions }} - {{- $local = $local | resources.Copy (print (partial "function-paths-media.html") "/" $author "/" $immutable (path.Ext $local)) }} - {{- $width = $local.Width -}} - {{- $height = $local.Height -}} - {{- $writeToFile := $local.Permalink }} + {{- with $srcset }} + <source srcset="{{ $srcset }}" type="{{ or $type "image/webp" }}" /> {{- end }} - {{- with $cache }} - {{- $cache = .Fit (print $dimensions " webp") }} - {{- $cache = $cache | resources.Copy (print $author "/media/" $immutable ".webp") }} - <source srcset="{{- $cache.RelPermalink -}}" type="image/webp" /> - {{- $cache = .Fit $dimensions }} - {{- $cache = $cache | resources.Copy (print (partial "function-paths-media.html") "/" $author "/" $immutable (path.Ext $cache)) }} - {{- $width = $cache.Width -}} - {{- $height = $cache.Height -}} - {{- $cache = $cache.RelPermalink }} - {{- end -}} - {{- with $exists }} - {{- if fileExists (print "public/" $fileCache ".webp") -}} - <source srcset="/{{- print $fileCache ".webp" -}}" type="image/webp" /> - {{- end -}} - {{- if fileExists (print "public/" $author "/media/" $immutable ".webp") -}} - <source srcset="/{{- print $author "/media/" $immutable ".webp" -}}" type="image/webp" /> - {{- end -}} - {{- $width = .Width -}} - {{- $height = .Height -}} - {{- $cache = .RelPermalink }} - {{- end -}} - <img loading="lazy" + <img + loading="lazy" src="{{ or $cache $source }}" alt="{{ $.Text | htmlUnescape }}" title="{{ $.Text | htmlUnescape }}" width="{{ $width }}" height="{{ $height }}" + {{ printf `style="` | safeHTMLAttr -}} + {{- partial "image-gradient.css.html" $colors | safeHTMLAttr -}} + {{- printf `"` | safeHTMLAttr }} /> </picture> </a> <figcaption> - {{ with $.Title -}} - {{ . | markdownify }} - {{ else -}} - Image/Picture - {{ end -}} - <br> - Index: {{ with $cache }} {{ print "[Cache](" . ")" | markdownify }} · {{ end }} + {{ $figcaption }} + <br /> + Index: {{ with $cache }} {{ print "[Cache](" $cache ")" | markdownify }} · {{ end }} {{ print "[Source](" $source ")" | markdownify }} </figcaption> </figure> |