From 64d9cc7e4dde059ac2b0c08633d8536d606f8057 Mon Sep 17 00:00:00 2001 From: Tobias Manske Date: Thu, 25 Mar 2021 03:38:38 +0100 Subject: [PATCH] generate avif on build --- .gitignore | 2 ++ Jenkinsfile | 9 +++++ layouts/partials/cover.html | 71 +++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 layouts/partials/cover.html diff --git a/.gitignore b/.gitignore index 43e4b62..699b388 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,5 @@ hugo.linux # End of https://www.toptal.com/developers/gitignore/api/hugo docs/ +*.webp +*.avif diff --git a/Jenkinsfile b/Jenkinsfile index bfe8c5e..7d9819e 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -24,7 +24,16 @@ pipeline { stage('Build HTML') { steps { + script { + docker.withRegistry('https://registry.chaoswg.org:5000', 'docker-registry-credentials') { + def dockerImage = docker.image('registry.chaoswg.org:5000/alpine-avif-webp:latest') + dockerImage.pull() + } + } + sh 'docker pull klakegg/hugo:alpine' + + sh 'docker run -u 108:116 -v "$PWD:/src" registry.chaoswg.org:5000/alpine-avif-webp:latest' sh 'docker run -u 108:116 -v "$PWD:/src" klakegg/hugo:alpine' } } diff --git a/layouts/partials/cover.html b/layouts/partials/cover.html new file mode 100644 index 0000000..0aa1f5a --- /dev/null +++ b/layouts/partials/cover.html @@ -0,0 +1,71 @@ +{{- with .cxt}} {{/* Apply proper context from dict */}} +{{- if (and .Params.cover.image (not $.isHidden)) }} +{{- $alt := (.Params.cover.alt | default .Params.cover.caption | plainify) }} +
+ {{- $addLink := (and .Site.Params.cover.linkFullImages (not $.IsHome)) }} + {{- $cover := (.Page.Resources.ByType "image").GetMatch (printf "*%s*" (.Params.cover.image)) }} + {{- if $cover -}}{{/* i.e it is present in page bundle */}} + {{- if $addLink }}{{ end -}} + {{- $sizes := (slice "360" "480" "720" "1080" "1500") }} + {{- $processableFormats := (slice "jpg" "jpeg" "png" "tif" "bmp" "gif") }} + {{- $prod := (hugo.IsProduction | or (eq .Site.Params.env "production")) }} + {{- if (and (in $processableFormats $cover.MediaType.SubType) (ne .Site.Params.cover.responsiveImages false) (eq $prod true)) }} + {{ $alt }} + {{- else }}{{/* Unprocessable image or responsive images disabled */}} + {{ $alt }} + {{- end }} + {{- else }}{{/* For absolute urls and external links, no img processing here */}} + {{- if $addLink }}{{ end -}} + + {{ $isJPG := eq (path.Ext .Params.cover.image) ".jpg" }} + {{ $isPNG := eq (path.Ext .Params.cover.image) ".png" }} + + {{ if ($isJPG) -}} + {{ $avifPath:= replace .Params.cover.image ".jpg" ".avif" }} + {{ $avifPathStatic:= printf "static/%s" $avifPath }} + + {{ if (fileExists $avifPathStatic) -}} + + {{- end }} + + {{ $webpPath:= replace .Params.cover.image ".jpg" ".webp" }} + {{ $webpPathStatic:= printf "static/%s" $webpPath }} + + {{ if (fileExists $webpPathStatic) -}} + + {{- end }} + {{- end }} + + {{ if ($isPNG) -}} + {{ $avifPath:= replace .Params.cover.image ".png" ".avif" }} + {{ $avifPathStatic:= printf "static/%s" $avifPath }} + + {{ if (fileExists $avifPathStatic) -}} + + {{- end }} + + {{ $webpPath:= replace .Params.cover.image ".png" ".webp" }} + {{ $webpPathStatic:= printf "static/%s" $webpPath }} + + {{ if (fileExists $webpPathStatic) -}} + + {{- end }} + {{- end }} + {{ $alt }} + + {{- end }} + {{- if $addLink }}{{ end -}} + {{/* Display Caption */}} + {{- if not $.IsHome }} + {{ with .Params.cover.caption }}

{{ . | markdownify }}

{{- end }} + {{- end }} +
+{{- end }}{{/* End image */}} +{{- end -}}{{/* End context */}}