aboutsummaryrefslogtreecommitdiff
path: root/themes/default/layouts/_default/_markup/render-image.html
diff options
context:
space:
mode:
Diffstat (limited to 'themes/default/layouts/_default/_markup/render-image.html')
-rw-r--r--themes/default/layouts/_default/_markup/render-image.html109
1 files changed, 52 insertions, 57 deletions
diff --git a/themes/default/layouts/_default/_markup/render-image.html b/themes/default/layouts/_default/_markup/render-image.html
index b98efdf..04bf931 100644
--- a/themes/default/layouts/_default/_markup/render-image.html
+++ b/themes/default/layouts/_default/_markup/render-image.html
@@ -1,97 +1,92 @@
-{{- $source := $.Destination | safeURL -}}
-{{- $public := print "public/" $source -}}
-{{- $local := "" -}}
{{- $cache := "" -}}
-{{- $exists := "" -}}
{{- $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 "") -}}
-{{- $fileCache := print (partial "function-paths.html").media "/" $author "/" $immutable -}}
-{{- $cached := and (fileExists (print "public/" $fileCache ".webp")) (fileExists (print "public/" $fileCache (path.Ext $source))) -}}
+{{- $storage := print $author "/media/" $immutable -}}
+{{- $cached := fileExists (print "public/" $storage ".webp") -}}
+
+{{- $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 $source) -}}
+ {{- 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.html").media "/" $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.Permalink -}}
{{- end -}}
{{- else -}}
- {{- $cache = .Content | resources.FromString (print (partial "function-paths.html").media "/" $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.html").media "/" $author "/" (path.Base $exists)) -}}
-{{- end -}}
-
-{{- with $local }}
- {{- $local = .Fit (print $dimensions " webp") }}
- {{- $local = $local | resources.Copy (print (partial "function-paths.html").media "/" $author "/" $immutable ".webp") }}
- {{- $srcset = $local.Permalink -}}
- {{- $local = .Fit $dimensions }}
- {{- $local = $local | resources.Copy (print (partial "function-paths.html").media "/" $author "/" $immutable (path.Ext $local)) }}
- {{- $width = $local.Width -}}
- {{- $height = $local.Height -}}
- {{- $colors = $local.Colors -}}
- {{- $writeToFile := $local.Permalink }}
-{{- end }}
-
-{{- with $cache }}
- {{- $cache = .Fit (print $dimensions " webp") }}
- {{- $cache = $cache | resources.Copy (print $author "/media/" $immutable ".webp") }}
- {{- $srcset = $cache.Permalink -}}
- {{- $cache = .Fit $dimensions }}
- {{- $cache = $cache | resources.Copy (print (partial "function-paths.html").media "/" $author "/" $immutable (path.Ext $cache)) }}
- {{- $width = $cache.Width -}}
- {{- $height = $cache.Height -}}
- {{- $colors = $cache.Colors -}}
- {{- $cache = $cache.Permalink }}
-{{- end -}}
-
-{{- with $exists }}
- {{- if fileExists (print "public/" $fileCache ".webp") -}}
- {{- $srcset = print "/" $fileCache ".webp" -}}
+ {{- 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 -}}
- {{- if fileExists (print "public/" $author "/media/" $immutable ".webp") -}}
- {{- $srcset = print "/" $author "/media/" $immutable ".webp" -}}
+ {{- range $extension := $fallbacks -}}
+ {{- with resources.Get (print "public/" $storage $extension) -}}
+ {{- $cache = print ("" | absURL) "/" $storage $extension -}}
+ {{- break -}}
+ {{- end -}}
{{- end -}}
- {{- $width = .Width -}}
- {{- $height = .Height -}}
- {{- $colors = .Colors -}}
- {{- $cache = .Permalink }}
{{- end -}}
{{- if lt $width $height -}}
{{- $orientation = "portrait" -}}
{{- end -}}
-{{- with $.Title -}}
- {{- $figcaption = . | markdownify -}}
+{{- with $caption := $.Title -}}
+ {{- $figcaption = $caption | markdownify -}}
{{ end }}
{{- /* This comment removes trailing newlines and white spaces. */ -}}
<figure>
<a data-orientation="{{ $orientation }}" href="{{ $source }}">
<picture>
{{- with $srcset }}
- <source srcset="{{ $srcset }}" type="image/webp" />
+ <source srcset="{{ $srcset }}" type="{{ or $type "image/webp" }}" />
{{- end }}
<img
loading="lazy"
@@ -101,7 +96,7 @@
width="{{ $width }}"
height="{{ $height }}"
{{ printf `style="` | safeHTMLAttr -}}
- {{- partial "styles-image-gradient.html" $colors | safeHTMLAttr -}}
+ {{- partial "image-gradient.css.html" $colors | safeHTMLAttr -}}
{{- printf `"` | safeHTMLAttr }}
/>
</picture>