diff --git a/LICENSE b/LICENSE index abf8c86..56d00df 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2021-2022 Daniel F. Dickinson, Henk Verlinde +Copyright (c) 2021-2022 Henk Verlinde Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 1b031b5..d5cfa91 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Hyas images -Image render hook + shortcode for Hyas sites. +Image render hook, shortcode + partial for Hyas sites. ## Status @@ -29,12 +29,23 @@ Add to `./config/_default/module.toml`: Add to `./config/_default/params.toml`: ```bash -# Images -imageResponsive = true -imageConvertTo = "webp" -imageImageSizes = ["480","720","1080","1280","1600","2048"] -singleSize = false -imageAddClass = "img-fluid lazyload blur-up" +# lazyimg +[lazyimg] + resizer = "auto" + renderer = "lqip-webp" + + # Resizer options: + lqipSize = "120x Gaussian" + maxSize = "1920x" + responsiveSizes = [ "320x", "640x", "768x", "1024x", "1366x", "1600x", "1920x" ] + resizeOptions = "Lanczos q95" + + # Renderer options: + class = "img-fluid" + # alt = "" + noscript = true + + errorHandler = "warning" ``` ## Usage @@ -44,4 +55,4 @@ See the Hyas docs: [Images](https://gethyas.com/docs/recipes/images/) ## Credits -Based on [DFD Hugo image handling module](https://github.com/danielfdickinson/image-handling-mod-hugo-dfd) +Based on [hugo-mods/lazyimg](https://github.com/hugo-mods/lazyimg) diff --git a/config/_default/params.toml b/config/_default/params.toml index 7b1cada..73addf0 100644 --- a/config/_default/params.toml +++ b/config/_default/params.toml @@ -1,6 +1,17 @@ -# Images -imageResponsive = true -imageConvertTo = "webp" -imageImageSizes = ["480","720","1080","1280","1600","2048"] -singleSize = false -imageAddClass = " img-fluid lazyload blur-up" +# lazyimg +[lazyimg] + resizer = "auto" + renderer = "lqip-webp" + + # Resizer options: + lqipSize = "120x Gaussian" + maxSize = "1920x" + responsiveSizes = [ "320x", "640x", "768x", "1024x", "1366x", "1600x", "1920x" ] + resizeOptions = "Lanczos q95" + + # Renderer options: + class = "img-fluid" + # alt = "" + noscript = true + + errorHandler = "warning" diff --git a/layouts/_default/_markup/render-image.html b/layouts/_default/_markup/render-image.html index 6bec2dc..e4d4752 100644 --- a/layouts/_default/_markup/render-image.html +++ b/layouts/_default/_markup/render-image.html @@ -1,6 +1,8 @@ -{{- $markdownImageLinkClass := .Page.Params.imageMarkdownLinkClass | default .Page.Site.Params.imageMarkdownLinkClass -}} -{{- $markdownImageClass := .Page.Params.imageMarkdownClass | default .Page.Site.Params.imageMarkdownClass -}} -{{- $markdownImageWrapper := .Page.Params.imageMarkdownAddWrapper | default .Page.Site.Params.imageMarkdownAddWrapper -}} -{{- $image := partial "helpers/lib/image-handling/find-image-src" (dict "src" .Destination "page" .Page "getRelative" true "ignoreBundleAssets" false) -}} -{{- partial "helpers/wrapped-image" (dict "alt" .Text "altRendered" true "title" .Title "image" $image "page" .Page "noImageWrapper" (not $markdownImageWrapper) "linkClass" $markdownImageLinkClass "class" $markdownImageClass) -}} -{{- /* Remove trailing newlines */ -}} +{{ $img := .Destination }} +{{ $alt := .Text -}} +{{ $params := (dict "img" $img "alt" $alt) -}} +{{ if not (resources.Get $params.img) -}} + {{ $params = merge $params (dict "img" (.Page.Resources.GetMatch $params.img)) -}} +{{ end -}} + +{{ partial "lazyimg" $params }} diff --git a/layouts/partials/error-handler/error.html b/layouts/partials/error-handler/error.html new file mode 100644 index 0000000..d98bcfd --- /dev/null +++ b/layouts/partials/error-handler/error.html @@ -0,0 +1 @@ +{{ errorf "image path './assets/%s' does not exist. please check the 'assets' directory." .img }} \ No newline at end of file diff --git a/layouts/partials/error-handler/warning-remote.html b/layouts/partials/error-handler/warning-remote.html new file mode 100644 index 0000000..5422d0c --- /dev/null +++ b/layouts/partials/error-handler/warning-remote.html @@ -0,0 +1,12 @@ +{{ $params := . }} +{{ $url := .img | absURL }} +{{ warnf "image path './assets/%s' does not exist. trying remote get via %s" .img $url }} +{{ $res := false }} +{{ with $resource := resources.GetRemote $url }} + {{ with .Err }} + {{ warnf "could not get remotely: %s: %#v" .Error .Data }} + {{ else }} + {{ $res = merge $params (dict "img" $resource) }} + {{ end }} +{{ end }} +{{ return $res }} \ No newline at end of file diff --git a/layouts/partials/error-handler/warning.html b/layouts/partials/error-handler/warning.html new file mode 100644 index 0000000..468b3eb --- /dev/null +++ b/layouts/partials/error-handler/warning.html @@ -0,0 +1 @@ +{{ warnf "image path './assets/%s' does not exist. please check the 'assets' directory." .img }} \ No newline at end of file diff --git a/layouts/partials/helpers/featured-image-link.html b/layouts/partials/helpers/featured-image-link.html deleted file mode 100644 index 09c31e3..0000000 --- a/layouts/partials/helpers/featured-image-link.html +++ /dev/null @@ -1,13 +0,0 @@ -{{- $ctx := . -}} -{{- $curPage := .Page -}} -{{- $featuredImageLink := "" -}} -{{- $featuredImage := partial "helpers/featured-image" $curPage -}} -{{- if $featuredImage }} - {{- $generatedImage := partial "helpers/lib/image-handling/generate-images" (dict "page" $curPage "image" $featuredImage "singleSize" true) -}} - {{- if $generatedImage -}} - {{- $featuredImageLink = $generatedImage.finalSrc -}} - {{- else -}} - {{- $featuredImageLink = $featuredImage.url -}} - {{- end -}} -{{- end -}} -{{- return $featuredImageLink -}} \ No newline at end of file diff --git a/layouts/partials/helpers/featured-image.html b/layouts/partials/helpers/featured-image.html deleted file mode 100644 index 3f3786e..0000000 --- a/layouts/partials/helpers/featured-image.html +++ /dev/null @@ -1,11 +0,0 @@ -{{- $ctx := . -}} -{{- $curPage := . -}} -{{- $featuredImage := partial "helpers/lib/image-handling/find-featured-images" (dict "page" $curPage "getRelative" false) -}} -{{- with $featuredImage -}} - {{- if reflect.IsSlice . -}} - {{- range first 1 . -}} - {{- $featuredImage = . -}} - {{- end -}} - {{- end -}} -{{- end -}} -{{- return $featuredImage -}} \ No newline at end of file diff --git a/layouts/partials/helpers/featured-images.html b/layouts/partials/helpers/featured-images.html deleted file mode 100644 index a18f05d..0000000 --- a/layouts/partials/helpers/featured-images.html +++ /dev/null @@ -1,5 +0,0 @@ -{{- $ctx := . -}} -{{- $curPage := .Page -}} -{{- $featuredImages := $curPage.Scratch.Get "non-existent-scratch" -}} -{{- $featuredImages = partial "helpers/lib/image-handling/find-featured-images" (dict "page" $curPage "getRelative" false) -}} -{{- return $featuredImages -}} \ No newline at end of file diff --git a/layouts/partials/helpers/lib-output/image-handling/img-element.html b/layouts/partials/helpers/lib-output/image-handling/img-element.html deleted file mode 100644 index 8ac4e78..0000000 --- a/layouts/partials/helpers/lib-output/image-handling/img-element.html +++ /dev/null @@ -1,16 +0,0 @@ -{{- $ctx := . -}} -{{- $inImgMap := .imgMap -}} -{{- $inPage := .page -}} -{{- $inAlt := .alt -}} -{{- $inTitle := .title -}} -{{- $inUseSrcSet := .useSrcSet -}} -{{- $inSizesAttr := .sizesAttr -}} -{{- $inLoading := .loading -}} -{{- $inClass := .class -}} -{{- $loading := $inLoading | default $inPage.Params.imageLoading | default $inPage.Site.Params.imageLoading -}} - -{{- /* Clean up trailing whitespace */ -}} diff --git a/layouts/partials/helpers/lib-output/image-handling/source-element.html b/layouts/partials/helpers/lib-output/image-handling/source-element.html deleted file mode 100644 index 453eb21..0000000 --- a/layouts/partials/helpers/lib-output/image-handling/source-element.html +++ /dev/null @@ -1,10 +0,0 @@ -{{- $ctx := . -}} -{{- $inImgMap := .imgMap -}} -{{- $inPage := .page -}} -{{- $inUseSrcSet := .useSrcSet -}} -{{- $inSizesAttr := .sizesAttr -}} -{{- $inMedia := .media -}} - \ No newline at end of file diff --git a/layouts/partials/helpers/lib-output/image-handling/thumbnail-or-full-picture.html b/layouts/partials/helpers/lib-output/image-handling/thumbnail-or-full-picture.html deleted file mode 100644 index ee0b2a4..0000000 --- a/layouts/partials/helpers/lib-output/image-handling/thumbnail-or-full-picture.html +++ /dev/null @@ -1,123 +0,0 @@ -{{- $ctx := . -}} -{{- $inWidth := .width -}} -{{- $inHeight := .height -}} -{{- $inThumbnailWidth := .thumbnailWidth -}} -{{- $inThumbnailHeight := .thumbnailHeight -}} -{{- $inAlt := .alt -}} -{{- $inTitle := .title -}} -{{- $inImage := .image -}} -{{- $inPage := .page -}} -{{- $inCaption := .caption -}} -{{- $inClass := .class -}} -{{- $inImageSizes := .imageSizes -}} -{{- $inThumbnailSizes := .thumbnailSizes -}} -{{- $inSingleSize := .singleSize -}} -{{- $inConvertTo := .convertTo -}} -{{- $inDoThumbnails := .thumbnails -}} -{{- $inDoFullSize := .fullSize -}} -{{- $inSizesAttr := .sizesAttr -}} -{{- $inThumbnailSizesAttr := .thumbnailSizesAttr -}} -{{- $inMinThumbnailViewport := .minThumbnailViewport -}} -{{- $inLoading := .loading -}} -{{- $inLink := .link -}} -{{- $inLinkClass := .linkClass -}} -{{- $inTarget := .target -}} -{{- $inRel := .rel -}} -{{- $inAddLink := .addLink -}} -{{- $inAddClass := .addClass -}} - -{{- $fullImgMap := (dict "finalSrc" "") -}} - -{{- $thumbnailImgMap := (dict "finalSrc" "") -}} - -{{- $thumbnailSizes := $inThumbnailSizes | default $inPage.Params.imageThumbnailSizes | default $inPage.Site.Params.imageThumbnailSizes | default (slice "180" "360" "512") -}} -{{- $thumbnailWidth := $inThumbnailWidth | default $inPage.Params.imageThumbnailWidth | default $inPage.Site.Params.imageThumbnailWidth | default $inWidth | default "512" -}} -{{- $thumbnailHeight := $inThumbnailHeight | default $inPage.Params.imageThumbnailHeight | default $inPage.Site.Params.imageThumbnailHeight | default "auto" -}} - -{{- $thumbnails := $inDoThumbnails | default $inPage.Params.imageThumbnails | default $inPage.Site.Params.imageThumbnails | default false -}} -{{- $fullSize := $inDoFullSize | default $inPage.Params.imageFullSize | default $inPage.Site.Params.imageFullSize | default true -}} -{{- $minThumbnailViewport := $inMinThumbnailViewport | default $inPage.Params.imageThumbnailViewport | default $inPage.Site.Params.imageThumbnailViewport | default "768px" -}} -{{- $thumbnailMedia := printf "(min-width: %s)" $minThumbnailViewport -}} - -{{- with $inPage -}} - {{- if reflect.IsMap $inImage -}} - {{- if $fullSize -}} - {{- $fullImgMap = partial "helpers/lib/image-handling/generate-images" (dict "width" $inWidth "height" $inHeight "image" $inImage "page" . "imageSizes" $inImageSizes "singleSize" $inSingleSize "convertTo" $inConvertTo) -}} - {{- else -}} - {{- $thumbnailWidth = $inThumbnailWidth | default $inWidth | default .Params.imageThumbnailWidth | default .Site.Params.imageThumbnailWidth | default "512" -}} - {{- $thumbnailHeight = $inThumbnailHeight | default $inHeight | default .Params.imageThumbnailHeight | default .Site.Params.imageThumbnailHeight | default "auto" -}} - {{- end -}} - - {{- if $thumbnails -}} - {{- $thumbnailImgMap = partial "helpers/lib/image-handling/generate-images" (dict "width" $thumbnailWidth "height" $thumbnailHeight "image" $inImage "page" . "imageSizes" $thumbnailSizes "singleSize" $inSingleSize "convertTo" $inConvertTo) -}} - {{- end -}} - {{- else if not (reflect.IsSlice $inImage) -}} - {{- $fullImgMap = (dict "finalSrc" $inImage "srcSet" (slice)) -}} - {{- else -}} - {{- errorf (printf "Multiple images given when only a single image expected on page '%s'" $inPage) -}} - {{- end -}} - - {{- $alt := "" -}} - {{- if or $inAlt $inCaption }} - {{- with $inAlt -}} - {{- $alt = . -}} - {{- else -}} - {{- $alt = $inCaption | .RenderString | plainify -}} - {{- end -}} - {{- end -}} - - {{- $class := $inClass -}} - {{- $addClass := default .Params.imageAddClass | default .Site.Params.imageAddClass -}} - {{- if and $inAddClass (not $class) -}} - {{- $class = $addClass -}} - {{- end -}} - - {{- $sizesAttr := $inSizesAttr | default .Params.imageSizesAttr | default .Site.Params.imageSizesAttr | default "80vw" -}} - {{- $thumbnailSizesAttr := $inSizesAttr | default .Params.imageThumbnailSizesAttr | default .Site.Params.imageThumbnailSizesAttr | default "20vw" -}} - {{- $linkHref := "" -}} - {{- $imgFinalSrc := "" -}} - - {{- if not (and (eq $fullImgMap.finalSrc "") (eq $thumbnailImgMap.finalSrc "")) -}} - {{- if and $fullSize (ne $fullImgMap.finalSrc "") -}} - {{- $imgFinalSrc = $fullImgMap.finalSrc -}} - {{- else -}} - {{- $imgFinalSrc = $thumbnailImgMap.finalSrc -}} - {{- end -}} - {{- end -}} - {{- if and $inAddLink (not (strings.HasPrefix $imgFinalSrc "data:image")) -}} - {{- $linkHref = $imgFinalSrc -}} - {{- else if (ne $inLink "") -}} - {{- $linkHref = $inLink -}} - {{- end -}} - {{- $rel := $inRel | default "noopener noreferrer" -}} - {{- if $linkHref -}} - - {{- end -}} - {{- $usePicture := false -}} - {{- if (and (ne $fullImgMap.finalSrc "") (ne $thumbnailImgMap.finalSrc "")) -}} - {{- $usePicture = true -}} - {{- end -}} - {{- if $usePicture -}} - - {{- if and $thumbnails (ne $thumbnailImgMap.finalSrc "") -}} - {{- $sourceOptions := (dict "imgMap" $thumbnailImgMap "page" . "useSrcSet" (cond (ge (len $thumbnailImgMap.srcSet) 1) true false) "sizesAttr" $thumbnailSizesAttr "media" $thumbnailMedia) -}} - {{- partial "helpers/lib-output/image-handling/source-element" $sourceOptions -}} - {{- end -}} - {{- if and $fullSize (ne $fullImgMap.finalSrc "") -}} - {{- $sourceOptions := (dict "imgMap" $fullImgMap "page" . "useSrcSet" (cond (ge (len $fullImgMap.srcSet) 1) true false) "sizesAttr" $sizesAttr) -}} - {{- partial "helpers/lib-output/image-handling/source-element" $sourceOptions -}} - {{- end -}} - {{- $class := "" -}} - {{- end -}} - {{- if and $fullSize (ne $fullImgMap.finalSrc "") -}} - {{- $imgOptions := (dict "imgMap" $fullImgMap "page" . "alt" $alt "title" $inTitle "loading" $inLoading "useSrcSet" (cond (and (not $usePicture) (ge (len $fullImgMap.srcSet) 1)) true false) "class" $class "sizesAttr" $sizesAttr) -}} - {{- partial "helpers/lib-output/image-handling/img-element" $imgOptions -}} - {{- else -}} - {{- $imgOptions := (dict "imgMap" $thumbnailImgMap "page" . "alt" $alt "title" $inTitle "loading" $inLoading "useSrcSet" (cond (and (not $usePicture) (ge (len $thumbnailImgMap.srcSet) 1)) true false) "class" $class "sizesAttr" $thumbnailSizesAttr) -}} - {{- partial "helpers/lib-output/image-handling/img-element" $imgOptions -}} - {{- end -}} - {{- if $usePicture -}} - - {{- end -}} - {{- if $linkHref -}}{{- end -}} -{{- end -}} diff --git a/layouts/partials/helpers/lib/image-handling/find-featured-images.html b/layouts/partials/helpers/lib/image-handling/find-featured-images.html deleted file mode 100644 index 705c995..0000000 --- a/layouts/partials/helpers/lib/image-handling/find-featured-images.html +++ /dev/null @@ -1,38 +0,0 @@ -{{- $curPage := .page -}} -{{- $relativeUrl := .getRelative -}} -{{- $featuredImages := $curPage.Scratch.Get "non-existent-scratch" -}} -{{- $foundImages := $curPage.Scratch.Get "non-existent-scratch" -}} -{{- $foundImagesByName := $curPage.Scratch.Get "non-existent-scratch" -}} -{{- $image := $curPage.Scratch.Get "non-existent-scratch" -}} -{{- /* Try page-level images first */ -}} -{{- $featuredParam := $curPage.Params.imageFeatured | default $curPage.Params.imageCover | default $curPage.Params.imageThumbnail | default $curPage.Params.featured_image -}} -{{- if $featuredParam -}} - {{- $foundImages = partial "helpers/lib/image-handling/find-image-maps" (dict "page" $curPage "images" $featuredParam "ignoreBundleAssets" false "ignoreSiteAssets" false "ignoreStaticImages" true "getRelative" $relativeUrl "finalSearch" false) -}} -{{- end -}} -{{- $imageResources := $curPage.Resources.ByType "image" -}} -{{ if and (not $foundImages) $imageResources -}} - {{- $foundImagesByName = $imageResources.Match "*feature*" -}} -{{- end -}} -{{ if and (not $foundImages) (not $foundImagesByName) $imageResources -}} - {{- $foundImagesByName = $imageResources.Match "{*cover*,*thumbnail*}" -}} -{{- end -}} -{{ if and (not $foundImages) (not $foundImagesByName) $featuredParam -}} - {{- $foundImages = partial "helpers/lib/image-handling/find-image-maps" (dict "page" $curPage "images" $featuredParam "ignoreBundleAssets" true "ignoreSiteAssets" true "ignoreStaticImages" false "getRelative" $relativeUrl "finalSearch" true) -}} -{{- end -}} -{{- with $foundImagesByName -}} - {{- if reflect.IsSlice . -}} - {{- $featuredImages = (slice) -}} - {{- range . -}} - {{ $image := partial "helpers/lib/image-handling/generate-featured-map" (dict "page" $curPage "imageResource" . "getRelative" $relativeUrl) -}} - {{- $featuredImages = $featuredImages | append $image -}} - {{- end -}} - {{- else -}} - {{ $image := partial "helpers/lib/image-handling/generate-featured-map" (dict "page" $curPage "imageResource" . "getRelative" $relativeUrl) -}} - {{- $featuredImages = $image -}} - {{- end -}} -{{- else -}} - {{- with $foundImages -}} - {{- $featuredImages = . -}} - {{- end -}} -{{- end -}} -{{- return $featuredImages -}} \ No newline at end of file diff --git a/layouts/partials/helpers/lib/image-handling/find-image-maps.html b/layouts/partials/helpers/lib/image-handling/find-image-maps.html deleted file mode 100644 index 87a7588..0000000 --- a/layouts/partials/helpers/lib/image-handling/find-image-maps.html +++ /dev/null @@ -1,39 +0,0 @@ -{{- $ctx := . -}} -{{- $curPage := .page -}} -{{- $inImages := .images -}} -{{- $getRelative := .getRelative -}} -{{- $ignoreBundleAssets := .ignoreBundleAssets -}} -{{- $ignoreSiteAssets := .ignoreSiteAssets -}} -{{- $ignoreStaticImages := .ignoreStaticImages -}} -{{- $finalSearch := .finalSearch | default true -}} -{{- $skipItem := $curPage.Scratch.Get "non-existent-scratch" -}} -{{- $images := (slice) -}} -{{- if reflect.IsMap $inImages -}} - {{- with (partial "helpers/lib/image-handling/find-image-src" (dict "src" $inImages.src "alt" (cond (isset $inImages "alt") $inImages.alt $skipItem) "title" (cond (isset $inImages "title") $inImages.title $skipItem) "page" $curPage "getRelative" $getRelative "ignoreBundleAssets" $ignoreBundleAssets "ignoreSiteAssets" $ignoreSiteAssets "ignoreStaticImages" $ignoreStaticImages "finalSearch" $finalSearch)) -}} - {{- $images = $images | append . -}} - {{- end -}} -{{- else if reflect.IsSlice $inImages -}} - {{- range $inImages -}} - {{- if reflect.IsMap . -}} - {{- with (partial "helpers/lib/image-handling/find-image-src" (dict "src" .src "alt" (cond (isset . "alt") .alt $skipItem) "title" (cond (isset . "title") .title $skipItem) "page" $curPage "getRelative" $getRelative "ignoreBundleAssets" $ignoreBundleAssets "ignoreSiteAssets" $ignoreSiteAssets "ignoreStaticImages" $ignoreStaticImages "finalSearch" $finalSearch)) -}} - {{- $images = $images | append . -}} - {{- end -}} - {{- else if reflect.IsSlice . -}} - {{- with (partial "helpers/lib/image-handling/find-image-src" (dict "src" (index . 0) "alt" (cond (isset . 1) (index . 1) $skipItem) "title" (cond (isset . 2) (index . 2) $skipItem) "page" $curPage "getRelative" $getRelative "ignoreBundleAssets" $ignoreBundleAssets "ignoreSiteAssets" $ignoreSiteAssets "ignoreStaticImages" $ignoreStaticImages "finalSearch" $finalSearch)) -}} - {{- $images = $images | append . -}} - {{- end -}} - {{- else -}} - {{- with (partial "helpers/lib/image-handling/find-image-src" (dict "src" . "page" $curPage "getRelative" $getRelative "ignoreBundleAssets" $ignoreBundleAssets "ignoreSiteAssets" $ignoreSiteAssets "ignoreStaticImages" $ignoreStaticImages "finalSearch" $finalSearch)) -}} - {{- $images = $images | append . -}} - {{- end -}} - {{- end -}} - {{- end -}} -{{- else -}} - {{- with (partial "helpers/lib/image-handling/find-image-src" (dict "src" $inImages "page" $curPage "getRelative" $getRelative "ignoreBundleAssets" $ignoreBundleAssets "ignoreSiteAssets" $ignoreSiteAssets "ignoreStaticImages" $ignoreStaticImages "finalSearch" $finalSearch)) -}} - {{- $images = $images | append . -}} - {{- end -}} -{{- end -}} -{{- if eq (len $images) 0 -}} - {{- $images = $curPage.Scratch.Get "non-existent-scratch" -}} -{{- end -}} -{{- return $images -}} \ No newline at end of file diff --git a/layouts/partials/helpers/lib/image-handling/find-image-src.html b/layouts/partials/helpers/lib/image-handling/find-image-src.html deleted file mode 100644 index 1c1c02b..0000000 --- a/layouts/partials/helpers/lib/image-handling/find-image-src.html +++ /dev/null @@ -1,95 +0,0 @@ -{{- $destination := .src -}} -{{- $curPage := .page -}} -{{- $getRelative := .getRelative -}} -{{- $ignoreBundleAssets := .ignoreBundleAssets -}} -{{- $ignoreSiteAssets := .ignoreSiteAssets -}} -{{- $ignoreStaticImages := .ignoreStaticImages -}} -{{- $alt := .alt -}} -{{- $title := .title -}} -{{- $finalSearch := .finalSearch | default true -}} -{{- $external := false -}} -{{- $base64 := false -}} -{{- $finalDestination := "" -}} -{{- $finalSrc := "" -}} -{{- $path := "" -}} -{{- $imageResource := $curPage.Scratch.Get "non-existent-scratch" -}} -{{- if strings.HasPrefix $destination "data:image" -}} - {{- $base64 = true -}} -{{- else -}} - {{- with urls.Parse $destination -}} - {{- if or .Host .Scheme -}} - {{- $external = true -}} - {{- end -}} - {{- $path = .Path -}} - {{- else -}} - {{- errorf printf "Unable to parse image src '%s'" $destination -}} - {{- end -}} -{{- end -}} -{{- if and $path (not $external) (not $base64) -}} - {{- /* For compatibility with Git repos, strip relative paths; the repos need them */ -}} - {{- /* but resources expect a different 'root' */ -}} - {{- if hasPrefix $path ".." -}} - {{ $newPath := "" -}} - {{ $doneRelStrip := false -}} - {{- range split $path "/" -}} - {{- if or (ne . "..") $doneRelStrip -}} - {{- $newPath = path.Join $newPath . -}} - {{- $doneRelStrip = true -}} - {{- end -}} - {{- end -}} - {{- if ne $newPath "" -}} - {{- $path = $newPath -}} - {{- end -}} - {{- end -}} - {{- /* Prefer page bundle assets unless we are ignoring page bundles */ -}} - {{- if ne $ignoreBundleAssets true -}} - {{- $imageResources := $curPage.Resources.ByType "image" -}} - {{- $imageResource = $imageResources.GetMatch (printf "*%s*" $path) -}} - {{- end -}} - {{- /* Next prefer assets from 'assets' directory */ -}} - {{- if and (not $imageResource) (ne $ignoreSiteAssets true) -}} - {{- $imageResource = resources.Get $path -}} - {{- end -}} - {{- /* Finally (for local images), try the 'static' directory (not a resource so no Hugo Pipes) */ -}} - {{- if and (not $imageResource) (ne $ignoreStaticImages true) -}} - {{- $finalDestination = partial "helpers/lib/image-handling/get-static-image" (dict "destination" $path "getRelative" $getRelative) -}} - {{- else if $imageResource -}} - {{- /* For images resources, get the resulting link to the image */ -}} - {{- $finalDestination = partial "helpers/lib/image-handling/get-resource-link" (dict "imageResource" $imageResource "getRelative" $getRelative) -}} - {{- end -}} - {{- if and (or (not $finalDestination) (eq $finalDestination "")) (ne $finalSearch false) -}} - {{- if $curPage.File -}} - {{- errorf (printf "Image %s is missing and not external for page %s" $path $curPage.File.Path) -}} - {{- else -}} - {{- errorf (printf "Image %s is missing and not external for page %s" $path $curPage.Title) -}} - {{- end -}} - {{- end -}} -{{- else if $external -}}{{- /* Use external URLs verbatim (not as a resource so no Hugo Pipes; we don't use 0.92.x only functionality, yet) */ -}} - {{- $finalDestination = $destination -}} -{{- else if $base64 -}} - {{- $finalSrc = $destination -}} -{{- end -}} -{{- $image := (dict "imageResource" $imageResource "link" $finalDestination "url" $finalDestination "specialSrc" $finalSrc) -}} -{{- with $image.imageResource -}} - {{- if partial "helpers/lib/image-handling/is-processable" . -}} - {{- $image = $image | merge (dict "width" .Width "height" .Height) -}} - {{- end -}} -{{- end -}} -{{- with ($alt | default $curPage.Params.imageFeaturedAlt | default $curPage.Params.imageCoverAlt | default $curPage.Params.imageThumbnailAlt | default $curPage.Params.featuredImageAlt | default $curPage.Params.featuredAlt) -}} - {{ $image = $image | merge (dict "alt" .) -}} -{{- end -}} -{{- with ($title | default $curPage.Params.imageFeaturedTitle | default $curPage.Params.imageCoverTitle | default $curPage.Params.imageThumbnailTitle | default $curPage.Params.featuredImageTitle | default $curPage.Params.featuredTitle) -}} - {{ $image = $image | merge (dict "title" .) -}} -{{- end -}} -{{- if $finalDestination -}} - {{- $secUrl := $finalDestination | absURL -}} - {{- with urls.Parse $secUrl -}} - {{- if eq .Scheme "https" -}} - {{- $image = $image | merge (dict "secure_url" $secUrl) -}} - {{- end -}} - {{- end -}} -{{- end -}} -{{- if not (or $image.imageResource $image.link $image.specialSrc) -}} - {{- $image = $curPage.Scratch.Get "non-existent-scratch" -}} -{{- end -}} -{{- return $image -}} diff --git a/layouts/partials/helpers/lib/image-handling/generate-featured-map.html b/layouts/partials/helpers/lib/image-handling/generate-featured-map.html deleted file mode 100644 index 0d77510..0000000 --- a/layouts/partials/helpers/lib/image-handling/generate-featured-map.html +++ /dev/null @@ -1,42 +0,0 @@ -{{- $ctx := . -}} -{{- $curPage := .page -}} -{{- $imageResource := .imageResource -}} -{{- $getRelative := .getRelative }} -{{- $image := $curPage.Scratch.Get "non-existent-scratch" -}} -{{- with $imageResource -}} - {{- if eq $getRelative true -}} - {{- $image = (dict "imageResource" . "link" .RelPermalink "url" .RelPermalink) -}} - {{- else -}} - {{- $image = (dict "imageResource" . "link" .Permalink "url" .Permalink) -}} - {{- end -}} - {{- with . -}} - {{- if partial "helpers/lib/image-handling/is-processable" . -}} - {{- $image = $image | merge (dict "width" .Width "height" .Height) -}} - {{- else -}} - {{- with ($curPage.Params.imageFeaturedWidth | default $curPage.Params.imageCoverWidth | default $curPage.Params.featuredImageWidth | default $curPage.Params.featuredWidth) -}} - {{ $image = $image | merge (dict "width" .) -}} - {{- end -}} - {{- with ($curPage.Params.imageFeaturedHeight | default $curPage.Params.imageCoverHeight | default $curPage.Params.featuredImageHeight | default $curPage.Params.featuredHeight) -}} - {{ $image = $image | merge (dict "height" .) -}} - {{- end -}} - {{- end -}} - {{- end -}} - {{- with ($curPage.Params.imageFeaturedAlt | default $curPage.Params.imageCoverAlt | default $curPage.Params.imageThumbnailAlt | default $curPage.Params.featuredImageAlt | default $curPage.Params.featuredAlt) -}} - {{ $image = $image | merge (dict "alt" .) -}} - {{- end -}} - {{- with ($curPage.Params.imageFeaturedTitle | default $curPage.Params.imageCoverTitle | default $curPage.Params.imageThumbnailTitle | default $curPage.Params.featuredImageTitle | default $curPage.Params.featuredTitle) -}} - {{ $image = $image | merge (dict "title" .) -}} - {{- end -}} - {{- if $image.url -}} - {{- $secUrl := $image.url | absURL -}} - {{- with urls.Parse $secUrl -}} - {{- if eq .Scheme "https" -}} - {{- $image = $image | merge (dict "secure_url" $secUrl) -}} - {{- end -}} - {{- end -}} - {{- end -}} - {{- if not (or $image.imageResource $image.link) -}} - {{- $image = $curPage.Scratch.Get "non-existent-scratch" -}} - {{- end -}} -{{- end -}} -{{- return $image -}} \ No newline at end of file diff --git a/layouts/partials/helpers/lib/image-handling/generate-images.html b/layouts/partials/helpers/lib/image-handling/generate-images.html deleted file mode 100644 index 0574dbf..0000000 --- a/layouts/partials/helpers/lib/image-handling/generate-images.html +++ /dev/null @@ -1,119 +0,0 @@ -{{- /* returns a dict (map) containing the final 'src' (URL) for the full image and - * a (possibly empty) slice containing the elements to be used by a 'srcset' attribute - */ -}} -{{- $ctx := . -}} -{{- $inWidth := .width -}} -{{- $inHeight := .height -}} -{{- $inImage := .image -}}{{- /* Image dict has .imageResource if available and has .link */ -}} -{{- $inPage := .page -}} -{{- $inImageSizes := .imageSizes -}} -{{- $inSingleSize := .singleSize -}} -{{- $inConvertTo := .convertTo -}} -{{- $inConvertMethod := .convertMethod | default "Resize" -}} -{{- $inRelativeUrl := .getRelative | default true -}} - -{{- $fullImage := $inImage.imageResource -}} -{{- $finalSrc := "" -}} -{{- $srcSet := (slice) -}} -{{- $fullWidth := $inWidth -}} -{{- $fullHeight := $inHeight -}} - -{{- with $inPage -}} - {{- if $fullImage -}} - {{- $fullConvertedImage := $fullImage -}} - - {{- if and (ne $inHeight "auto") (ne $inHeight "") -}} - {{- $fullHeight = $inHeight | int -}} - {{- else -}} - {{- $fullHeight = "" -}} - {{- end -}} - - {{- if (partial "helpers/lib/image-handling/is-processable" $fullImage) -}} - {{- $fullWidth = $inWidth | default $fullImage.Width | int -}} - {{- $responsiveImages := $inPage.Params.imageResponsive | default $inPage.Site.Params.imageResponsive | default true -}} - {{- $convertTo := $inConvertTo | default $inPage.Params.imageConvertTo | default $inPage.Site.Params.imageConvertTo -}} - {{- $imageSizes := $inImageSizes | default $inPage.Params.imageImageSizes | default $inPage.Site.Params.imageImageSizes | default (slice "480" "720" "1080" "1280" "1600" "2048") -}} - {{- $singleSize := not $responsiveImages -}} - {{- if and $inSingleSize (ne $inSingleSize false) -}} - {{- $responsiveImages = false -}} - {{- $singleSize = true -}} - {{- if $fullHeight -}} - {{- $imageSizes = (slice (printf "%dx%d" $fullWidth $fullHeight)) -}} - {{- else -}} - {{- $imageSizes = (slice (printf "%dx" $fullWidth)) -}} - {{- end -}} - {{- end -}} - - {{- $imageConversion := "" -}} - {{- $imageOptions := "" -}} - - {{- if $fullHeight -}} - {{- $imageOptions = printf "%dx%d" $fullWidth $fullHeight -}} - {{- else -}} - {{- $imageOptions = printf "%dx" $fullWidth -}} - {{- end -}} - - {{- with $convertTo -}} - {{- if $fullHeight -}} - {{- $imageOptions = printf "%dx%d %s" $fullWidth $fullHeight . -}} - {{- else -}} - {{- $imageOptions = printf "%dx %s" $fullWidth . -}} - {{- end -}} - {{- $imageConversion = printf " %s" . -}} - {{- end -}} - - {{- if eq $inConvertMethod "Resize" -}} - {{- if or (ne $fullImage.Width $fullWidth) (ne $fullImage.Height $fullHeight) $convertTo -}} - {{- $fullConvertedImage = $fullImage.Resize $imageOptions -}} - {{- else -}} - {{- $fullConvertedImage = $fullImage -}} - {{- end -}} - {{- else if eq $inConvertMethod "Fit" -}} - {{- if or (eq $fullHeight "") (eq (int $fullHeight) 0) -}} - {{- errorf (printf "Must specify a height with 'Fit' for image '%s' on page '%s'" $fullImage.RelPermalink $inPage) -}} - {{- end -}} - {{- $fullConvertedImage = $fullImage.Fit $imageOptions -}} - {{- else if eq $inConvertMethod "Fill" -}} - {{- if or (eq $fullHeight "") (eq (int $fullHeight) 0) -}} - {{- errorf (printf "Must specify a height with 'Fill' for image '%s' on page '%s'" $fullImage.RelPermalink $inPage) -}} - {{- end -}} - {{- $fullConvertedImage = $fullImage.Fill $imageOptions -}} - {{- else if eq $inConvertMethod "GrowFit" -}} - {{- if or (eq $fullHeight "") (eq (int $fullHeight) 0) -}} - {{- errorf (printf "Must specify a height with 'GrowFit' for image '%s' on page '%s'" $fullImage.RelPermalink $inPage) -}} - {{- end -}} - {{- $fullConvertedImage = $fullImage.Resize (printf "%dx%s" $fullWidth $imageConversion) -}} - {{- $fullConvertedImage = $fullConvertedImage.Fit (printf "%dx%d%s" $fullWidth $fullHeight $imageConversion) -}} - {{- else -}} - {{- $fullConvertedImage = $fullImage.Resize (printf "%dx%d%s" $fullImage.Width $fullImage.Height $imageConversion) -}} - {{- end -}} - {{- $finalSrc = $fullConvertedImage.RelPermalink -}} - {{- $fullHeight = $fullConvertedImage.Height -}} - {{- $fullWidth = $fullConvertedImage.Width -}} - {{- $fullImage = $fullConvertedImage -}} - {{- if not $singleSize -}} - {{- $srcSetImageSizes := $imageSizes -}} - {{- if $responsiveImages -}} - {{- $srcSet = partial "helpers/lib/image-handling/generate-srcset-slice" (dict "imageSizes" $imageSizes "imageConversion" $imageConversion "fullImage" $fullImage "fullConvertedImage" $fullConvertedImage "getRelative" $inRelativeUrl) -}} - {{- end -}} - {{- end -}} - {{- else -}}{{- /* Is an image but not of a processable format */ -}} - {{- $finalSrc = $fullImage.RelPermalink -}} - {{- end -}} - {{- else if not $inImage.specialSrc -}}{{- /* Is not a known image format for Hugo */ -}} - {{- $finalSrc = $inImage.link -}} - {{- else -}} - {{- $finalSrc = $inImage.specialSrc | safeHTMLAttr -}} - {{- end -}} -{{- end -}} -{{- if $finalSrc -}} - {{- if not $inImage.specialSrc -}} - {{- if not $inRelativeUrl -}} - {{- $finalSrc = $finalSrc | absURL -}} - {{- else -}} - {{- $finalSrc = $finalSrc | absURL | relURL -}} {{- /* Canonicalize url */ -}} - {{- end -}} - {{- end -}} -{{- end -}} -{{- $retMap := (dict "finalSrc" $finalSrc "srcSet" $srcSet "width" $fullWidth "height" $fullHeight "fullImage" $fullImage) -}} -{{- return $retMap -}} diff --git a/layouts/partials/helpers/lib/image-handling/generate-srcset-slice.html b/layouts/partials/helpers/lib/image-handling/generate-srcset-slice.html deleted file mode 100644 index 7244b8e..0000000 --- a/layouts/partials/helpers/lib/image-handling/generate-srcset-slice.html +++ /dev/null @@ -1,21 +0,0 @@ -{{- /* Returns a slice containing strings that can be used by a 'srcset' attribute - * if you add appropriate delimiters. - */ -}} -{{- $ctx := . -}} -{{- $srcSet := (slice) -}} -{{- $imageSizes := .imageSizes -}} -{{- $imageConversion := .imageConversion -}} -{{- $fullImage := .fullImage -}} -{{- $fullConvertedImage := .fullConvertedImage -}} -{{- $lastSize := 0 -}} -{{- range $size := $imageSizes -}} - {{- /* Don't generate images larger than original size; it's a waste of bandwidth and storage */ -}} - {{- if ge $fullImage.Width $size -}} - {{- $srcSet = $srcSet | append (printf "%s %dw" ($fullImage.Resize (printf "%dx%s" (int $size) $imageConversion)).RelPermalink (int $size)) -}} - {{- $lastSize = int $size -}} - {{- end -}} -{{- end -}} -{{- if ne $lastSize $fullConvertedImage.Width -}} - {{- $srcSet = $srcSet | append (printf "%s %dw" $fullConvertedImage.RelPermalink $fullConvertedImage.Width) -}} -{{- end -}} -{{- return $srcSet -}} \ No newline at end of file diff --git a/layouts/partials/helpers/lib/image-handling/get-resource-link.html b/layouts/partials/helpers/lib/image-handling/get-resource-link.html deleted file mode 100644 index 7a0e293..0000000 --- a/layouts/partials/helpers/lib/image-handling/get-resource-link.html +++ /dev/null @@ -1,12 +0,0 @@ -{{- $ctx := . -}} -{{- $imageResource := .imageResource -}} -{{- $getRelative := .getRelative -}} -{{- $imageLink := "" -}} -{{- if $imageResource -}} - {{- if not $getRelative -}} - {{- $imageLink = $imageResource.Permalink -}} - {{- else -}} - {{- $imageLink = $imageResource.RelPermalink -}} - {{- end -}} -{{- end -}} -{{- return $imageLink -}} \ No newline at end of file diff --git a/layouts/partials/helpers/lib/image-handling/get-static-image.html b/layouts/partials/helpers/lib/image-handling/get-static-image.html deleted file mode 100644 index 24fac81..0000000 --- a/layouts/partials/helpers/lib/image-handling/get-static-image.html +++ /dev/null @@ -1,29 +0,0 @@ -{{- $destination := .destination -}} -{{- $getRelative := .getRelative -}} -{{- $finalDestination := "" -}} -{{- $isFile := false -}} -{{- $foundFile := false -}} -{{- if $destination -}} - {{- /* fileExists operates relative to site root not relative to applicable directory */ -}} - {{- $filePath := path.Join "/static" $destination -}}{{- /* Non-resource images must be in 'static' */ -}} - {{- if fileExists $filePath -}} - {{- $foundFile = true -}} - {{- else -}} - {{- $filePath = path.Join "/static" site.Language.Lang $destination -}} - {{- if fileExists $filePath -}} - {{- $foundFile = true -}} - {{- end -}} - {{- end -}} - {{- if eq $foundFile true -}} - {{- if not $getRelative -}} - {{- $finalDestination = $destination | absURL -}} - {{- else -}} - {{- $finalDestination = $destination | absURL | relURL -}}{{- /* Canonicalize the URL */ -}} - {{- end -}} - {{- else if strings.HasPrefix $destination "data:image" -}} - {{- $finalDestination = $destination -}} - {{- end -}} -{{- end -}} -{{- return $finalDestination -}} -{{- /* Remove trailing newlines */ -}} - diff --git a/layouts/partials/helpers/lib/image-handling/is-processable.html b/layouts/partials/helpers/lib/image-handling/is-processable.html deleted file mode 100644 index 4500923..0000000 --- a/layouts/partials/helpers/lib/image-handling/is-processable.html +++ /dev/null @@ -1,15 +0,0 @@ -{{- /* Context is the original image resource */ -}} -{{- /* Returns true if the image is processable by Hugo image processing, false if not */ -}} -{{- $image := . -}} -{{- $isProcessable := false -}} -{{- $processableFormats := (slice "jpg" "jpeg" "png" "tif" "tiff" "bmp" "gif") -}} -{{- $hugoVersion := split (replaceRE ".*v([0-9]+).([0-9]+).([0-9]+).*" "$1 $2 $3" site.Hugo.Version) " " -}}{{- /* We have to use the deprecated .Hugo.Version to support 0.80.0 */ -}} -{{- if and (eq (index $hugoVersion 0) 0) (ge (index $hugoVersion 1) 83) -}} - {{- if hugo.IsExtended -}} - {{ $processableFormats = $processableFormats | append "webp" -}} - {{- end -}} -{{- end -}} -{{- if in $processableFormats $image.MediaType.SubType -}} - {{- $isProcessable = true -}} -{{- end -}} -{{- return $isProcessable -}} \ No newline at end of file diff --git a/layouts/partials/helpers/lib/metadata/gather-media-image.html b/layouts/partials/helpers/lib/metadata/gather-media-image.html deleted file mode 100644 index 8a10c97..0000000 --- a/layouts/partials/helpers/lib/metadata/gather-media-image.html +++ /dev/null @@ -1,6 +0,0 @@ -{{- $curPage := . -}} -{{- $images := partial "helpers/lib/metadata/gather-media-images" $curPage -}} -{{- if reflect.IsSlice $images -}} - {{- $images = index $images 0 -}} -{{- end -}} -{{- return $images -}} \ No newline at end of file diff --git a/layouts/partials/helpers/lib/metadata/gather-media-images.html b/layouts/partials/helpers/lib/metadata/gather-media-images.html deleted file mode 100644 index a27cc0f..0000000 --- a/layouts/partials/helpers/lib/metadata/gather-media-images.html +++ /dev/null @@ -1,17 +0,0 @@ -{{- $curPage := . -}} -{{- $images := (slice) -}} -{{- with .Params.images -}} - {{- $images = partial "helpers/lib/image-handling/find-image-maps" (dict "images" . "page" $curPage "getRelative" false "ignoreBundleAssets" false "ignoreSiteAssets" false) -}} -{{- end -}} -{{- if eq (len $images) 0 -}} - {{- with $curPage.Site.Params.images -}} - {{- $images = partial "helpers/lib/image-handling/find-image-maps" (dict "images" . "page" $curPage "getRelative" false "ignoreBundleAssets" true "ignoreSiteAssets" false) -}} - {{- end -}} -{{- end -}} -{{- if eq (len $images) 0 -}} - {{- $images = partial "helpers/lib/image-handling/find-featured-images" (dict "page" $curPage "getRelative" false "ignoreBundleAssets" false "ignoreSiteAssets" false) -}} -{{- end -}} -{{- if $images -}} - {{- $images = partial "helpers/lib/microformats/generate-microformat-images" (dict "page" $curPage "images" $images) -}} -{{- end -}} -{{- return $images -}} \ No newline at end of file diff --git a/layouts/partials/helpers/lib/microformats/generate-microformat-images.html b/layouts/partials/helpers/lib/microformats/generate-microformat-images.html deleted file mode 100644 index c1d51d5..0000000 --- a/layouts/partials/helpers/lib/microformats/generate-microformat-images.html +++ /dev/null @@ -1,38 +0,0 @@ -{{- $finalImages := (slice) -}} -{{- $inPage := .page -}} -{{- $inImages := .images -}} -{{- $microformatWidth := .Page.Params.microformatWidth | default .Page.Site.Params.microformatWidth | default 1200 | int -}} -{{- $microformatHeight := .Page.Params.microformatHeight | default .Page.Site.Params.microformatHeight | default 630 | int -}} -{{- $microformatSizingMethod := .Page.Params.microformatSizingMethod | default .Page.Site.Params.microformatSizingMethod | default "Fill" -}} -{{- with $inImages -}} - {{- if reflect.IsMap . -}} - {{- $image := .imageResource -}} - {{- $link := .link -}} - {{- $alt := .alt -}} - {{- if $image -}}{{- /* If we have an image resource, process the image */ -}} - {{ $imageMap := partial "helpers/lib/image-handling/generate-images" (dict "page" $inPage "singleSize" true "image" . "width" $microformatWidth "height" $microformatHeight "convertMethod" $microformatSizingMethod "getRelative" false) -}} - {{ $finalImages = $finalImages | append (dict "image" $imageMap.fullImage "link" $imageMap.finalSrc "width" $imageMap.width "height" $imageMap.height "alt" $alt) -}} - {{- else -}}{{- /* If we don't have an image resource, just keep the map (which has the .link for the image) */ -}} - {{- $finalImages = $finalImages | append . -}} - {{- end -}} - {{- else if reflect.IsSlice . -}} - {{- range . -}} - {{- if reflect.IsMap . -}} - {{- $image := .imageResource -}} - {{- $link := .link -}} - {{- $alt := .alt -}} - {{- if $image -}}{{- /* If we have an image resource, process the image */ -}} - {{ $imageMap := partial "helpers/lib/image-handling/generate-images" (dict "page" $inPage "singleSize" true "image" . "width" $microformatWidth "height" $microformatHeight "convertMethod" $microformatSizingMethod "getRelative" false) -}} - {{ $finalImages = $finalImages | append (dict "image" $imageMap.fullImage "link" $imageMap.finalSrc "url" $imageMap.finalSrc "width" $imageMap.width "height" $imageMap.height "alt" $alt) -}} - {{- else -}}{{- /* If we don't have an image resource, just keep the map (which has the .link for the image) */ -}} - {{- $finalImages = $finalImages | append . -}} - {{- end -}} - {{- else -}} - {{- $finalImages = $finalImages | append . -}} - {{- end -}} - {{- end -}} - {{- else -}} - {{- $finalImages = $finalImages | append . -}} - {{- end -}} -{{- end -}} -{{- return $finalImages -}} \ No newline at end of file diff --git a/layouts/partials/helpers/wrapped-image.html b/layouts/partials/helpers/wrapped-image.html deleted file mode 100644 index 44aa746..0000000 --- a/layouts/partials/helpers/wrapped-image.html +++ /dev/null @@ -1,144 +0,0 @@ -{{- $ctx := . -}} -{{- $inWidth := .width -}} -{{- $inHeight := .height -}} -{{- $inThumbnailWidth := .thumbnailWidth -}} -{{- $inThumbnailHeight := .thumbnailHeight -}} -{{- $inAlt := .alt -}} -{{- $inAltRendered := .altRendered -}} -{{- $inTitle := .title -}} -{{- $inImage := .image -}} -{{- $inPage := .page -}} -{{- $inClass := .class -}} -{{- $inLink := .link -}} -{{- $inLinkClass := .linkClass -}} -{{- $inTarget := .target -}} -{{- $inRel := .rel -}} -{{- $inImageWrapper := .imageWrapper -}} -{{- $inCaption := .caption -}} -{{- $inCaptionRendered := .captionRendered -}} -{{- $inAttr := .attr -}} -{{- $inAttrLink := .attrLink -}} -{{- $inImageClass := .imageClass -}} -{{- $inNoImageWrapper := .noImageWrapper -}} -{{- $inImageSizes := .imageSizes -}} -{{- $inThumbnailSizes := .thumbnailSizes -}} -{{- $inSingleSize := .singleSize -}} -{{- $inConvertTo := .convertTo -}} -{{- $inDoThumbnails := .thumbnails -}} -{{- $inDoFullSize := .fullSize -}} -{{- $inSizesAttr := .sizesAttr -}} -{{- $inThumbnailSizesAttr := .thumbnailSizesAttr -}} -{{- $inMinThumbnailViewport := .minThumbnailViewport -}} -{{- $inLoading := .loading -}} -{{- $inNoVisibleCaption := .noVisibleCaption -}} -{{- $altRendered := false -}} -{{- $captionRendered := false -}} - -{{- with $inPage -}} - {{- $addLink := false -}} - {{- $link := $inLink -}} - {{- $haveLink := false -}} - {{- if $inLink -}} - {{- $haveLink = true -}} - {{- else -}} - {{- $link = "" -}} - {{- end -}} - {{- $addFullLink := .Params.imageLinkFull | default .Site.Params.imageLinkFull | default false -}} - {{- if and (not $haveLink) $addFullLink -}} - {{- $addLink = true -}} - {{- end -}} - - {{- $alt := "" -}} - {{- if or $inAlt $inCaption }} - {{- with $inAlt -}} - {{- $alt = . -}} - {{- $altRendered = eq $inAltRendered true }} - {{- else -}} - {{- if not (eq $inCaptionRendered true) -}} - {{- $alt = $inCaption | .RenderString | plainify -}} - {{- $altRendered = true -}} - {{- else -}} - {{- $alt = $inCaption | plainify -}} - {{- $captionRendered = true -}} - {{- $altRendered = true -}} - {{- end -}} - {{- end -}} - {{- end -}} - - {{- $imageWrapper := $inImageWrapper -}} - {{- $class := $inClass -}} - {{- $caption := $inCaption -}} - {{- $captionRendered = eq $inCaptionRendered true -}} - - {{- $addWrapper := .Params.imageAddWrapper | default .Site.Params.imageAddWrapper -}} - {{- if and $addWrapper (not $imageWrapper) -}} - {{- $imageWrapper = $addWrapper -}} - {{- end -}} - - {{- $addClass := .Params.imageAddClass | default .Site.Params.imageAddClass -}} - {{- if and $addClass (not $class) -}} - {{- $class = $addClass -}} - {{- end -}} - - {{- $altAsCaption := .Params.imageAltAsCaption | default .Site.Params.imageAltAsCaption -}} - {{- if and $altAsCaption (not $caption) -}} - {{- $caption = $inAlt -}} - {{- $captionRendered = $altRendered -}} - {{- end -}} - - {{- $renderedCaption := "" -}} - {{- if $captionRendered -}} - {{- $renderedCaption = $caption -}} - {{- else -}} - {{- $renderedCaption = ($caption | $inPage.RenderString (dict "display" "block")) -}} - {{- end -}} - - {{- if $inNoImageWrapper -}} - {{- $imageWrapper = "" -}} - {{- end -}} - - {{- if $imageWrapper -}} - {{- if $class -}} - {{- printf "\n<%s class=\"%s\">" $imageWrapper $class | safeHTML -}} - {{- else -}} - {{- printf "\n<%s>" $imageWrapper | safeHTML -}} - {{- end -}} - {{- end -}} - - {{- $pictureOptions := (dict "width" $inWidth "height" $inHeight "thumbnailWidth" $inThumbnailWidth "thumbnailHeight" $inThumbnailHeight "alt" $alt "title" $inTitle "image" $inImage "page" $inPage "caption" $caption "class" (cond (eq $imageWrapper "") $class $inImageClass) "linkClass" $inLinkClass "imageSizes" $inImageSizes "thumbnailSizes" $inThumbnailSizes "singleSize" $inSingleSize "convertTo" $inConvertTo "thumbnails" $inDoThumbnails "fullSize" $inDoFullSize "sizesAttr" $inSizesAttr "thumbnailSizeAttr" $inThumbnailSizesAttr "loading" $inLoading "addLink" $addLink "link" $link "target" $inTarget "rel" $inRel "minThumbnailViewport" $inMinThumbnailViewport) -}} - {{- partial "helpers/lib-output/image-handling/thumbnail-or-full-picture" $pictureOptions -}} - - {{- if $imageWrapper -}} - {{- if or $inTitle $caption $inAttr -}} - {{- if (eq $imageWrapper "figure") -}}
{{- else if not $inTitle -}}{{- else -}}
{{- end -}} - {{- if ne $inNoVisibleCaption true -}} - {{- with $inTitle -}} -

{{ . }}

- {{- end -}} - {{- if or $caption $inAttr -}} - {{- /* Can't have 'p' inside 'span' and rendering markdown sometimes results in a 'p' wrapper. - * We ensure we get a 'p' (display block) so that we after we convert them we have the - * expected spans (for CSS) - */ -}} - {{- (replaceRE "<(/?)p>" "<${1}span>" $renderedCaption) | safeHTML -}} - {{- with $inAttrLink -}} - - {{- end -}} - {{- (replaceRE "<(/?)p>" "<${1}span>" ($inAttr | $inPage.RenderString (dict "display" "block"))) | safeHTML -}} - {{- if $inAttrLink -}}{{- end -}} - {{- end -}} - {{- end -}} - {{- if (eq $imageWrapper "figure") -}}
{{- else if not $inTitle -}}{{- else -}}{{- end -}} - {{- end -}} - {{- printf "" $imageWrapper | safeHTML -}} - {{- else -}} - {{- if or $caption $inAttr -}} - {{- (replaceRE "<(/?)p>" "<${1}span>" $renderedCaption) | safeHTML -}} - {{- with $inAttrLink -}} - - {{- end -}} - {{- replaceRE "<(/?)p>" "<$1{s}pan>" ($inAttr | $inPage.RenderString (dict "display" "block")) | safeHTML -}} - {{- if $inAttrLink }}{{ end }} - {{- end -}} - {{- end -}} -{{- end -}} diff --git a/layouts/partials/lazyimg-setup-nojs.html b/layouts/partials/lazyimg-setup-nojs.html new file mode 100644 index 0000000..0dd597d --- /dev/null +++ b/layouts/partials/lazyimg-setup-nojs.html @@ -0,0 +1,2 @@ +{{ partial "partials/lazyimg-setup" }} + \ No newline at end of file diff --git a/layouts/partials/lazyimg-setup.html b/layouts/partials/lazyimg-setup.html new file mode 100644 index 0000000..49bb22e --- /dev/null +++ b/layouts/partials/lazyimg-setup.html @@ -0,0 +1,6 @@ +{{- $lsPath := "lazyimg/lazysizes/lazysizes.min.js" -}} +{{- with (resources.Get $lsPath) -}} + +{{- else -}} + {{ errorf "could not locate %s" $lsPath }} +{{- end }} diff --git a/layouts/partials/lazyimg.html b/layouts/partials/lazyimg.html new file mode 100644 index 0000000..952c233 --- /dev/null +++ b/layouts/partials/lazyimg.html @@ -0,0 +1,61 @@ +{{/* Parameter Parsing */}} +{{ $usage := `lazyimg: + Usage like: + - {{ partial "lazyimg" "image.jpg" }} + - {{ partial "lazyimg" (resources.Get "image.jpg") }} + + ...or more verbose by specifying a dict overriding the site.Params.lazyimg, e.g.: + - {{ partial "lazyimg" (dict "resizer" "auto" "renderer" "lqip") }} + - {{ partial "lazyimg" (dict "img" "image.jpg" "responsiveSizes" (slice "320px" "640px") "resizer" "responsive") }} + + Please ensure that image exists in ./assets directory. +` }} + +{{/* Parse and normalize input parameters: */}} +{{ $params := merge site.Params.lazyimg (dict + "img" . + "resizer" (default "responsive" site.Params.lazyimg.resizer) + "renderer" (default "lqip" site.Params.lazyimg.renderer) +)}} +{{ if reflect.IsMap . }} + {{ $userParams := dict }} + {{/* Normalize by transforming all keys to lower-case: */}} + {{ range $k, $v := . -}} + {{ $userParams = $userParams | merge (dict (lower $k) $v) }} + {{- end -}} + {{ $params = merge $params $userParams }} +{{ end }} + +{{/* Attempt to load image resource: */}} +{{ $resType := (printf "%T" $params.img) }} +{{ $resLoaded := in $resType "resource" }} +{{ if not $resLoaded }} + {{ with $res := resources.Get $params.img }} + {{ $params = merge $params (dict "img" $res) }} + {{ $resLoaded = true }} + {{ end }} +{{ end }} + +{{/* Handle error if not loaded yet: */}} +{{ if not $resLoaded }} + {{/* Try if error-handler is able to resolve it */}} + {{ $params = partial (printf "error-handler/%s" site.Params.lazyimg.errorHandler) $params }} + {{ if $params }} + {{ $resLoaded = in (printf "%T" $params.img) "resource" }} + {{ end }} +{{ end }} +{{/* Only proceed if resource has been loaded: */}} +{{ if $resLoaded }} + {{/* Special handling for WebP renderers: */}} + {{ if in $params.renderer "webp" }} + {{/* Override user-supplied target format */}} + {{ $resizeOptionsWebP := printf "%s webp" ($params.resizeoptions | replaceRE "(jpg|png|tif|bmp|gif)" "") }} + {{ $paramsWebP := merge $params (dict "resizeoptions" $resizeOptionsWebP) }} + {{ $imagesWebP := partial (printf "resizer/%s" $params.resizer) $paramsWebP }} + {{ $imagesRegular := partial (printf "resizer/%s" $params.resizer) $params }} + {{ partial (printf "renderer/%s" $params.renderer) (merge $params (dict "imgDict" $imagesWebP "imgDictFallback" $imagesRegular)) }} + {{ else }} + {{ $sizes := partial (printf "resizer/%s" $params.resizer) $params }} + {{ partial (printf "renderer/%s" $params.renderer) (merge $params (dict "imgDict" $sizes)) }} + {{ end }} +{{ end }} diff --git a/layouts/partials/renderer/_srcset.html b/layouts/partials/renderer/_srcset.html new file mode 100644 index 0000000..74226d7 --- /dev/null +++ b/layouts/partials/renderer/_srcset.html @@ -0,0 +1,8 @@ +{{/* Input: Input: dict {"responsive": [RESOURCE, ...]}. Output: srcset string */}} + +{{ $images := .responsive }} +{{ $srcset := slice }} +{{ range $images }} + {{ $srcset = append (printf "%s %dw" .Permalink .Width) $srcset }} +{{ end }} +{{ return (delimit $srcset ",\n") }} \ No newline at end of file diff --git a/layouts/partials/renderer/figure-webp.html b/layouts/partials/renderer/figure-webp.html new file mode 100644 index 0000000..b829ab4 --- /dev/null +++ b/layouts/partials/renderer/figure-webp.html @@ -0,0 +1 @@ +{{ partial "renderer/figure" . }} diff --git a/layouts/partials/renderer/figure.html b/layouts/partials/renderer/figure.html new file mode 100644 index 0000000..89c64b9 --- /dev/null +++ b/layouts/partials/renderer/figure.html @@ -0,0 +1,29 @@ +{{ $class := .class }} +{{ $alt := .alt }} +{{ $caption := .caption }} +{{ $noscriptSupport := .noscript }} +{{ $imgDict := .imgDict }} +{{- if $noscriptSupport -}} + +{{- end -}} +
+{{ . }} +{{ with $caption }}
{{ . }}
{{ end }} +
diff --git a/layouts/partials/renderer/img.html b/layouts/partials/renderer/img.html new file mode 100644 index 0000000..580c831 --- /dev/null +++ b/layouts/partials/renderer/img.html @@ -0,0 +1,20 @@ +{{ $class := .class }} +{{ $alt := .alt }} +{{ $imgDict := .imgDict }} + +{{ . }} diff --git a/layouts/partials/renderer/lqip-webp.html b/layouts/partials/renderer/lqip-webp.html new file mode 100644 index 0000000..38cd5bd --- /dev/null +++ b/layouts/partials/renderer/lqip-webp.html @@ -0,0 +1,22 @@ +{{ $noscriptSupport := .noscript }} +{{ $imgDict := .imgDict }} +{{ $imgDictFallback := .imgDictFallback }} +{{- if $noscriptSupport -}} + +{{- end -}} + + + {{ partial "renderer/img" (merge . (dict "imgDict" $imgDictFallback)) }} + diff --git a/layouts/partials/renderer/lqip.html b/layouts/partials/renderer/lqip.html new file mode 100644 index 0000000..5b575d2 --- /dev/null +++ b/layouts/partials/renderer/lqip.html @@ -0,0 +1,7 @@ +{{ $noscriptSupport := .noscript }} +{{- if $noscriptSupport -}} + +{{- end -}} +{{ partial "renderer/img" . }} diff --git a/layouts/partials/renderer/native.html b/layouts/partials/renderer/native.html new file mode 100644 index 0000000..788478a --- /dev/null +++ b/layouts/partials/renderer/native.html @@ -0,0 +1,9 @@ +{{ $class := .class }} +{{ $alt := .alt }} +{{ $imgDict := .imgDict }} + +{{ . }} \ No newline at end of file diff --git a/layouts/partials/renderer/simple.html b/layouts/partials/renderer/simple.html new file mode 100644 index 0000000..a0cc7f6 --- /dev/null +++ b/layouts/partials/renderer/simple.html @@ -0,0 +1,13 @@ +{{ $class := .class }} +{{ $alt := .alt }} +{{ $noscriptSupport := .noscript }} +{{ $imgDict := .imgDict }} +{{- if $noscriptSupport -}} + +{{- end -}} +{{ . }} \ No newline at end of file diff --git a/layouts/partials/renderer/webp.html b/layouts/partials/renderer/webp.html new file mode 100644 index 0000000..7558ce2 --- /dev/null +++ b/layouts/partials/renderer/webp.html @@ -0,0 +1,11 @@ +{{ $class := .class }} +{{ $noscriptSupport := .noscript }} +{{ $imgDict := .imgDict }} +{{ $imgDictFallback := .imgDictFallback }} + + + + {{ partial "renderer/simple" (merge . (dict "imgDict" $imgDictFallback)) }} + \ No newline at end of file diff --git a/layouts/partials/resizer/auto.html b/layouts/partials/resizer/auto.html new file mode 100644 index 0000000..fffdb7f --- /dev/null +++ b/layouts/partials/resizer/auto.html @@ -0,0 +1,35 @@ +{{/* Input: Params. Output: dict {"default": RESOURCE, "lqip": RESOURCE, "responsive": [RESOURCE, ...]} */}} + +{{ $res := .img }} +{{ $resizeOptions := .resizeoptions }} +{{ $sizes := .responsivesizes }} + +{{ $imageDict := partial "resizer/simple" . }} +{{ $default := $imageDict.default }} +{{ $name := replace (path.Base ($res.Permalink)) (path.Ext $res.Name) "" }} + +{{/* Responsive img sizing from LQIP width to default width */}} +{{ $startWidth := (default 100 $imageDict.lqip.Width) }} +{{ $sizes := seq $startWidth 50 $imageDict.default.Width }} +{{ $n := 5 }} +{{ if gt $n (len $sizes) -}} + {{ $n = len $sizes }} +{{- end }} +{{/* Partition the sizes */}} +{{ $part := int (div (len $sizes) $n) }} +{{ $wset := slice }} +{{ range (seq 1 (sub $n 1)) -}} + {{ $wset = append (index $sizes (mul $part .)) $wset }} +{{- end }} +{{ $wset = (uniq (append (int $imageDict.default.Width) $wset)) }} + +{{/* Fill responsive slice: */}} +{{ $responsive := slice }} +{{ $break := false }} +{{- range $wset -}} + {{- $resized := $res.Resize (printf "%dx %s" . $resizeOptions) -}} + {{- $resized := slice $resized | resources.Concat (printf "%s-%dx%s" $name . (path.Ext $default.Permalink)) -}} + {{- $responsive = append $resized $responsive -}} +{{- end -}} + +{{- return merge (dict "responsive" $responsive) $imageDict -}} \ No newline at end of file diff --git a/layouts/partials/resizer/responsive.html b/layouts/partials/resizer/responsive.html new file mode 100644 index 0000000..f9dde99 --- /dev/null +++ b/layouts/partials/resizer/responsive.html @@ -0,0 +1,26 @@ +{{/* Input: Params. Output: dict {"default": RESOURCE, "lqip": RESOURCE, "responsive": [RESOURCE, ...]} */}} + +{{ $res := .img }} +{{ $resizeOptions := .resizeoptions }} +{{ $sizes := .responsivesizes }} + +{{ $imageDict := partial "resizer/simple" . }} +{{ $default := $imageDict.default }} +{{ $name := replace (path.Base ($res.Permalink)) (path.Ext $res.Name) "" }} + +{{/* Fill responsive slice. Abort loop with $break when size exceeded (needed due to templating restrictions). */}} +{{ $responsive := slice }} +{{ $break := false }} +{{- range $sizes -}} + {{ if not $break }} + {{- $resized := $res.Resize (printf "%s %s" . $resizeOptions) -}} + {{- $resized := slice $resized | resources.Concat (printf "%s-%s%s" $name . (path.Ext $default.Permalink)) -}} + {{- $responsive = append $resized $responsive -}} + + {{- if or (gt $resized.Width $default.Width) (gt $resized.Height $default.Height) -}} + {{- $break = true -}} + {{- end -}} + {{- end -}} +{{- end -}} + +{{- return merge (dict "responsive" $responsive) $imageDict -}} diff --git a/layouts/partials/resizer/simple.html b/layouts/partials/resizer/simple.html new file mode 100644 index 0000000..8aa39b9 --- /dev/null +++ b/layouts/partials/resizer/simple.html @@ -0,0 +1,16 @@ +{{/* Input: Params. Output: dict {"default": RESOURCE, "lqip": RESOURCE} */}} + +{{ $res := .img }} +{{ $maxSize := .maxsize }} +{{ $lqipSize := .lqipsize }} +{{ $resizeOptions := .resizeoptions }} + +{{ $name := replace (path.Base ($res.Permalink)) (path.Ext $res.Name) "" }} + +{{/* Resize base images */}} +{{ $lqip := $res.Resize (printf "%s %s" $lqipSize $resizeOptions ) }} +{{ $lqip := slice $lqip | resources.Concat (printf "%s-%s%s" $name $lqipSize (path.Ext $lqip.Permalink)) }} +{{ $default := $res.Resize (printf "%s %s" $maxSize $resizeOptions) }} +{{ $default := slice $default | resources.Concat (printf "%s-%s%s" $name $maxSize (path.Ext $default.Permalink)) }} + +{{ return dict "lqip" $lqip "default" $default }} \ No newline at end of file diff --git a/layouts/shortcodes/figure.html b/layouts/shortcodes/figure.html deleted file mode 100644 index 70141e1..0000000 --- a/layouts/shortcodes/figure.html +++ /dev/null @@ -1 +0,0 @@ -{{- partial "helpers/wrapped-image" (dict "width" (.Get "width") "height" (.Get "height") "alt" (.Get "alt") "title" (.Get "title") "image" (partial "helpers/lib/image-handling/find-image-src" (dict "src" (.Get "src") "page" .Page "getRelative" (.Get "getRelative" | default true) "ignoreBundleAssets" (.Get "ignoreBundleAssets" | default false) "ignoreSiteAssets" (.Get "ignoreSiteAssets" | default false) "ignoreStaticImages" (.Get "ignoreStaticImages" | default true))) "page" .Page "link" (.Get "link") "target" (.Get "target") "rel" (.Get "rel") "imageWrapper" (default "figure" (.Get "imageWrapper")) "caption" (.Get "caption") "attr" (.Get "attr") "attrLink" (.Get "attrLink") "class" (.Get "class") "singleSize" (.Get "singleSize") "thumbnails" (.Get "thumbnails")) -}} diff --git a/layouts/shortcodes/lazyimg.html b/layouts/shortcodes/lazyimg.html new file mode 100644 index 0000000..8deb80d --- /dev/null +++ b/layouts/shortcodes/lazyimg.html @@ -0,0 +1,10 @@ +{{ $params := dict }} +{{ if .IsNamedParams }} + {{ $params = .Params }} +{{ else }} + {{ $params (dict "img" (.Get 0)) }} +{{ end }} +{{ if not (resources.Get $params.img) }} + {{ $params = merge $params (dict "img" (.Page.Resources.GetMatch $params.img)) }} +{{ end }} +{{ partial "lazyimg" $params }} diff --git a/package-lock.json b/package-lock.json index d29f55f..804eca8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@hyas/images", - "version": "0.2.3", + "version": "0.3.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@hyas/images", - "version": "0.2.3", + "version": "0.3.0", "license": "MIT", "devDependencies": { "auto-changelog": "^2.4.0", @@ -362,9 +362,9 @@ "dev": true }, "node_modules/uglify-js": { - "version": "3.17.0", - "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.17.0.tgz", - "integrity": "sha512-aTeNPVmgIMPpm1cxXr2Q/nEbvkmV8yq66F3om7X3P/cvOXQ0TMQ64Wk63iyT1gPlmdmGzjGpyLh1f3y8MZWXGg==", + "version": "3.17.2", + "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.17.2.tgz", + "integrity": "sha512-bbxglRjsGQMchfvXZNusUcYgiB9Hx2K4AHYXQy2DITZ9Rd+JzhX7+hoocE5Winr7z2oHvPsekkBwXtigvxevXg==", "dev": true, "optional": true, "bin": { @@ -665,9 +665,9 @@ "dev": true }, "uglify-js": { - "version": "3.17.0", - "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.17.0.tgz", - "integrity": "sha512-aTeNPVmgIMPpm1cxXr2Q/nEbvkmV8yq66F3om7X3P/cvOXQ0TMQ64Wk63iyT1gPlmdmGzjGpyLh1f3y8MZWXGg==", + "version": "3.17.2", + "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.17.2.tgz", + "integrity": "sha512-bbxglRjsGQMchfvXZNusUcYgiB9Hx2K4AHYXQy2DITZ9Rd+JzhX7+hoocE5Winr7z2oHvPsekkBwXtigvxevXg==", "dev": true, "optional": true }, diff --git a/package.json b/package.json index 2ce74dd..77f1814 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@hyas/images", "description": "Hyas images", - "version": "0.2.3", + "version": "0.3.0", "browserslist": [ "defaults" ],