From f51e912632c0920626554a20b0656bfac5b44d9b Mon Sep 17 00:00:00 2001 From: Jake Jarvis Date: Wed, 15 Jan 2020 10:30:01 -0500 Subject: [PATCH] make optimize-image more function-like ....instead of relying on $.Scratch --- .../partials/functions/optimize-image.html | 34 +++++++++---------- layouts/partials/head/favicons.html | 4 ++- layouts/partials/head/head.html | 18 +++++----- layouts/partials/head/social-images.html | 22 ++++++------ layouts/shortcodes/image.amp.html | 6 ++-- layouts/shortcodes/image.html | 6 ++-- layouts/shortcodes/image.rss.xml | 4 +-- 7 files changed, 48 insertions(+), 46 deletions(-) diff --git a/layouts/partials/functions/optimize-image.html b/layouts/partials/functions/optimize-image.html index 477b574c..28ed32e1 100644 --- a/layouts/partials/functions/optimize-image.html +++ b/layouts/partials/functions/optimize-image.html @@ -1,25 +1,25 @@ -{{- /* Automatic resizing for HiDPI/retina images */ -}} +{{/* Automatic resizing for HiDPI/retina images */}} -{{- $original := .Page.Resources.GetMatch (.Get "src") -}} -{{- .Scratch.Set "image" $original -}} +{{ $original := .Page.Resources.GetMatch (.Get "src") }} +{{ .Scratch.Set "image" $original }} -{{- $setWidth := 910 -}} +{{ $setWidth := 910 }} -{{- if .Get "width" -}} - {{- $setWidth = (int (.Get "width")) -}} -{{- end -}} +{{ if .Get "width" }} + {{ $setWidth = (int (.Get "width")) }} +{{ end }} -{{- $retinaWidth := (mul $setWidth 2) -}} +{{ $retinaWidth := (mul $setWidth 2) }} -{{- if gt $original.Width $retinaWidth -}} - {{- $finalWidth := (printf "%dx" $retinaWidth) -}} - {{- .Scratch.Set "image" ($original.Resize $finalWidth) -}} -{{- end -}} +{{ if gt $original.Width $retinaWidth -}} + {{ $finalWidth := (printf "%dx" $retinaWidth) }} + {{ .Scratch.Set "image" ($original.Resize $finalWidth) }} +{{ end }} -{{- $image := .Scratch.Get "image" -}} +{{ $image := .Scratch.Get "image" }} -{{- $origRatio := (div (float $image.Height) $image.Width) -}} -{{- .Scratch.Set "displayWidth" $setWidth -}} -{{- .Scratch.Set "displayHeight" (math.Ceil (mul $origRatio $setWidth)) -}} +{{ $origRatio := (div (float $image.Height) $image.Width) }} +{{ $displayWidth := $setWidth }} +{{ $displayHeight := (math.Ceil (mul $origRatio $setWidth)) }} -{{- return $image -}} +{{ return (dict "Permalink" $image.Permalink "Width" $displayWidth "Height" $displayHeight) }} diff --git a/layouts/partials/head/favicons.html b/layouts/partials/head/favicons.html index 6a9a8021..3afa9e61 100644 --- a/layouts/partials/head/favicons.html +++ b/layouts/partials/head/favicons.html @@ -5,4 +5,6 @@ {{ end -}} - +{{ $s := slice -}} +{{ range (split .ico_sizes " ") }}{{ $s = $s | append (printf "%vx%v" . .) }}{{ end -}} + diff --git a/layouts/partials/head/head.html b/layouts/partials/head/head.html index eeaedb6f..62f52662 100644 --- a/layouts/partials/head/head.html +++ b/layouts/partials/head/head.html @@ -1,22 +1,22 @@ -{{ partial "head/meta" . }} -{{ partial "head/social-images" . }} -{{ partial "head/open-graph" . }} +{{ partial "head/meta" . -}} +{{ partial "head/social-images" . -}} +{{ partial "head/open-graph" . -}} -{{ partialCached "head/preload" . }} -{{ partialCached "head/styles" . }} -{{ partialCached "head/favicons" (dict "png_sizes" "192 48 32 16" "ico_sizes" "16x16 32x32 48x48") }} +{{ partialCached "head/preload" . -}} +{{ partialCached "head/styles" . -}} +{{ partialCached "head/favicons" (dict "png_sizes" "192 48 32 16" "ico_sizes" "16 32 48") -}} {{ if and .IsPage (eq .Type "notes") }} - {{ with .OutputFormats.Get "amp" }} + {{- with .OutputFormats.Get "amp" -}} - {{ end }} + {{- end }} {{ partial "head/schema-person" . }} {{ partial "head/schema-article" . }} {{ else }} {{ partial "head/schema-person" . }} -{{ end }} +{{ end -}} diff --git a/layouts/partials/head/social-images.html b/layouts/partials/head/social-images.html index a01ae34a..37cb7ac2 100644 --- a/layouts/partials/head/social-images.html +++ b/layouts/partials/head/social-images.html @@ -1,12 +1,12 @@ -{{- /* Default image */ -}} +{{/* Default image */}} {{- $defaultImagePath := (path.Join "content" $.Site.Params.defaultimage) }} -{{- $.Scratch.Set "defaultImage_url" (replace $defaultImagePath "content/" $.Site.BaseURL) }} -{{- with (imageConfig $defaultImagePath) }} +{{ $.Scratch.Set "defaultImage_url" (replace $defaultImagePath "content/" $.Site.BaseURL) }} +{{ with (imageConfig $defaultImagePath) }} {{- $.Scratch.Set "defaultImage_width" .Width }} {{- $.Scratch.Set "defaultImage_height" .Height }} -{{- end }} +{{ end -}} -{{- /* Article image (with fallback to default image */ -}} +{{/* Article image (with fallback to default image */}} {{- with .Params.image }} {{- $imagePath := (path.Join "content" $.File.Dir "images/" .) }} @@ -15,18 +15,18 @@ {{- $.Scratch.Set "socialImage_width" .Width }} {{- $.Scratch.Set "socialImage_height" .Height }} {{- end }} -{{- else }} +{{ else }} {{- $.Scratch.Set "socialImage_url" (replace $defaultImagePath "content/" $.Site.BaseURL) }} {{- with (imageConfig $defaultImagePath) }} {{- $.Scratch.Set "socialImage_width" .Width }} {{- $.Scratch.Set "socialImage_height" .Height }} {{- end }} -{{- end }} +{{ end -}} -{{- /* Site logo */ -}} +{{/* Site logo */}} {{- $logoImagePath := (path.Join "content" "logo.png") }} -{{- $.Scratch.Set "logoImage_url" (replace $logoImagePath "content/" $.Site.BaseURL) }} -{{- with (imageConfig $logoImagePath) }} +{{ $.Scratch.Set "logoImage_url" (replace $logoImagePath "content/" $.Site.BaseURL) }} +{{ with (imageConfig $logoImagePath) }} {{- $.Scratch.Set "logoImage_width" .Width }} {{- $.Scratch.Set "logoImage_height" .Height }} -{{- end }} +{{ end -}} diff --git a/layouts/shortcodes/image.amp.html b/layouts/shortcodes/image.amp.html index f27e9492..51afaff8 100644 --- a/layouts/shortcodes/image.amp.html +++ b/layouts/shortcodes/image.amp.html @@ -3,10 +3,10 @@

diff --git a/layouts/shortcodes/image.html b/layouts/shortcodes/image.html index 619b4a40..ccdc90bc 100644 --- a/layouts/shortcodes/image.html +++ b/layouts/shortcodes/image.html @@ -3,12 +3,12 @@ {{- with .Inner }}
- {{ . | markdownify | plainify | safeHTML }} + {{ . | $.Page.RenderString | plainify | safeHTML }}
{{ . | $.Page.RenderString | safeHTML }}
{{- else -}} -

{{ . | safeHTML }}

{{- end }} diff --git a/layouts/shortcodes/image.rss.xml b/layouts/shortcodes/image.rss.xml index a81fc69d..aaac9bad 100644 --- a/layouts/shortcodes/image.rss.xml +++ b/layouts/shortcodes/image.rss.xml @@ -1,8 +1,8 @@ {{- $optimized := partial "functions/optimize-image" . -}}

-{{ . | safeHTML }} + {{- with .Inner }} alt="{{ . | $.Page.RenderString | plainify | safeHTML }}" title="{{ . | $.Page.RenderString | plainify | safeHTML }}"{{ end }}> {{ with .Inner }}
{{ . | $.Page.RenderString | safeHTML }}{{ end -}}