From db793dde95bf546089e1fee8857a9f9510e6990c Mon Sep 17 00:00:00 2001 From: tdro Date: Mon, 6 Nov 2023 20:15:38 -0500 Subject: themes/default/layouts/_default/_markup/render-image: Avoid reprocessing Clean up logic and fetch with URL parse --- .../layouts/_default/_markup/render-image.html | 90 +++++++++------------- 1 file changed, 37 insertions(+), 53 deletions(-) (limited to 'themes/default/layouts') diff --git a/themes/default/layouts/_default/_markup/render-image.html b/themes/default/layouts/_default/_markup/render-image.html index 116d280..39720dc 100644 --- a/themes/default/layouts/_default/_markup/render-image.html +++ b/themes/default/layouts/_default/_markup/render-image.html @@ -1,82 +1,66 @@ -{{- $source := $.Destination | safeURL -}} -{{- $public := print "public/" $source -}} -{{- $local := "" -}} {{- $cache := "" -}} -{{- $exists := "" -}} {{- $width := "" -}} {{- $height := "" -}} {{- $srcset := "" -}} {{- $colors := "" -}} +{{- $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 (partial "function-paths.html").media "/" $author "/" $immutable -}} -{{- $cached := and (fileExists (print "public/" $storage ".webp")) (fileExists (print "public/" $storage (path.Ext $source))) -}} +{{- $storage := print $author "/media/" $immutable -}} +{{- $cached := fileExists (print "public/" $storage ".webp") -}} {{- 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/" $storage (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/" $storage ".webp") -}} + {{- with $cache = resources.Get (print "public/" $storage ".webp") -}} + {{- $width = $cache.Width -}} + {{- $height = $cache.Height -}} + {{- $colors = $cache.Colors -}} {{- $srcset = print ("" | absURL) "/" $storage ".webp" -}} + {{- $cache = print ("" | absURL) "/" $storage (path.Ext $cache) -}} {{- end -}} - {{- if fileExists (print "public/" $author "/media/" $immutable ".webp") -}} - {{- $srcset = print ("" | absURL) "/" $author "/media/" $immutable ".webp" -}} - {{- end -}} - {{- $width = .Width -}} - {{- $height = .Height -}} - {{- $colors = .Colors -}} - {{- $cache = .Permalink }} {{- end -}} {{- if lt $width $height -}} -- cgit v1.2.3