You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

236 lines
8.4 KiB
HTML

{{ $params := . }}
{{ $alt := $params.alt }}
{{ $breakpoints := $params.breakpoints }}
{{ $class := $params.class }}
{{ $crossorigin := $params.crossorigin | default "anonymous" }}
{{ $densities := $params.densities }}
{{ $fetchpriority := $params.fetchpriority }}
{{ $fingerprint := $params.fingerprint | default true }}
{{ $height := $params.height }}
{{ $loading := $params.loading | default "lazy" }}
{{ $maxheight := $params.maxheight }}
{{ $maxwidth := $params.maxwidth }}
{{ $process := $params.process }}
{{ $page := $params.page }}
{{ $referrerpolicy := $params.referrerpolicy | default "no-referrer" }}
{{ $resource := $params.resource }}
{{ $sizes := $params.sizes }}
{{ $src := $params.src }}
{{ $srcset := $params.srcset }}
{{ $title := $params.title }}
{{ $width := $params.width }}
{{ if and (not $resource) (not $src) }}
{{ errorf "paige/img: no resource or src" }}
{{ end }}
{{ if and $breakpoints $densities }}
{{ errorf "paige/img: cannot generate both breakpoint and density image sets" }}
{{ end }}
{{ $intrinsicheight := $height }}
{{ $intrinsicwidth := $width }}
{{ if not $resource }}
{{ $resource = partial "paige/resource.html" (dict "page" $page "url" $src) }}
{{ end }}
{{ if $resource }}
{{ $method := "" }}
{{ $newresource := $resource }}
{{ $oldresource := $resource }}
{{ $options := slice }}
{{ $quality := "" }}
{{ $size := "" }}
{{ if $process }}
{{ if ne $process true }}
{{ range split (lower $process) " " }}
{{ if or (eq . "crop") (eq . "fill") (eq . "fit") (eq . "resize") }}
{{ $method = . }}
{{ else }}
{{ with findRE `\d+x\d+|\d+x|x\d+` . 1 }}
{{ $size = index . 0 }}
{{ else }}
{{ with findRE `q\d+` . 1 }}
{{ $quality = index . 0 }}
{{ else }}
{{ $options = $options | append . }}
{{ end }}
{{ end }}
{{ end }}
{{ end }}
{{ end }}
{{ $options = delimit $options " " }}
{{ if not $method }}
{{ $method = "resize" }}
{{ end }}
{{ if not $size }}
{{ $size = print $oldresource.Width "x" $oldresource.Height }}
{{ end }}
{{ $newoptions := print $size " " $quality " " $options }}
{{ if eq $method "crop" }}
{{ $newresource = $oldresource.Crop $newoptions }}
{{ else if eq $method "fill" }}
{{ $newresource = $oldresource.Fill $newoptions }}
{{ else if eq $method "fit" }}
{{ $newresource = $oldresource.Fit $newoptions }}
{{ else if eq $method "resize" }}
{{ $newresource = $oldresource.Resize $newoptions }}
{{ else }}
{{ errorf "paige/img: invalid method: %q" $method }}
{{ end }}
{{ end }}
{{ if $fingerprint }}
{{ $newresource = $newresource | fingerprint }}
{{ end }}
{{ $intrinsicheight = $newresource.Height }}
{{ $intrinsicwidth = $newresource.Width }}
{{ $src = $newresource.RelPermalink }}
{{ if and (not $sizes) (not $srcset) }}
{{ $partialresource := "" }}
{{ if and $process (or $breakpoints $densities) (ne $method "resize") }}
{{ $partialoptions := print $size " q100 " $options }}
{{ if eq $method "crop" }}
{{ $partialresource = $oldresource.Crop $partialoptions }}
{{ else if eq $method "fill" }}
{{ $partialresource = $oldresource.Fill $partialoptions }}
{{ else if eq $method "fit" }}
{{ $partialresource = $oldresource.Fit $partialoptions }}
{{ end }}
{{ end }}
{{ if $densities }}
{{ $densities = split $densities " " }}
{{ $srcset = slice }}
{{ $parseddensities := slice }}
{{ range $densities }}
{{ with findRE `^(\d+(\.\d+)?x|\.\d+x)$` . 1 }}
{{ $parseddensities = $parseddensities | append (strings.TrimSuffix "x" (index . 0) | float) }}
{{ else }}
{{ errorf "paige/img: invalid pixel density: %q" . }}
{{ end }}
{{ end }}
{{ $parseddensities = $parseddensities | uniq | sort }}
{{ if lt (len $parseddensities) 2 }}
{{ errorf "paige/img: must have at least two unique pixel densities" }}
{{ end }}
{{ $base := 0 }}
{{ $maxdensity := index $parseddensities (sub (len $parseddensities) 1) }}
{{ with $partialresource }}
{{ $base = div .Width $maxdensity | math.Round | int }}
{{ else }}
{{ $base = div $newresource.Width $maxdensity | math.Round | int }}
{{ end }}
{{ range $parseddensities }}
{{ if eq . $maxdensity }}
{{ continue }}
{{ end }}
{{ $imagewidth := mul $base . | math.Round | int }}
{{ $resized := "" }}
{{ with $partialresource }}
{{ $resized = .Resize (print $imagewidth "x " $quality " " $options) }}
{{ else }}
{{ $resized = $newresource.Resize (print $imagewidth "x q100 " $options) }}
{{ end }}
{{ if $fingerprint }}
{{ $resized = $resized | fingerprint }}
{{ end }}
{{ $srcset = $srcset | append (printf "%s %gx" $resized.RelPermalink .) }}
{{ end }}
{{ $srcset = $srcset | append (printf "%s %gx" $newresource.RelPermalink $maxdensity) }}
{{ $srcset = delimit $srcset ", " | string }}
{{ else if $breakpoints }}
{{ $sizes = slice }}
{{ $srcset = slice }}
{{ $maxwidth := 0 }}
{{ range slice (slice 550 576) (slice 696 768) (slice 936 992) (slice 1116 1200) (slice 1296 1400) }}
{{ $imagewidth := index . 0 }}
{{ $viewwidth := index . 1 }}
{{ if ge $imagewidth $newresource.Width }}
{{ continue }}
{{ end }}
{{ $resized := "" }}
{{ with $partialresource }}
{{ $resized = .Resize (print $imagewidth "x " $quality " " $options) }}
{{ else }}
{{ $resized = $newresource.Resize (print $imagewidth "x q100 " $options) }}
{{ end }}
{{ if $fingerprint }}
{{ $resized = $resized | fingerprint }}
{{ end }}
{{ $maxwidth = math.Max $maxwidth $imagewidth }}
{{ $sizes = $sizes | append (printf "(max-width: %dpx) %dpx" $viewwidth $imagewidth) }}
{{ $srcset = $srcset | append (printf "%s %dw" $resized.RelPermalink $imagewidth) }}
{{ end }}
{{ $sizes = $sizes | append (print $maxwidth "px") }}
{{ $sizes = delimit $sizes ", " }}
{{ $srcset = delimit $srcset ", " }}
{{ end }}
{{ end }}
{{ else }}
{{ $src = relLangURL $src }}
{{ end }}
{{ if and (or $height $maxheight) (not (or $maxwidth $width)) }}
{{ $width = "auto" }}
{{ end }}
{{ if and (or $maxwidth $width) (not (or $height $maxheight)) }}
{{ $height = "auto" }}
{{ end }}
{{ $style := slice }}
{{ if $height }}
{{ $style = $style | append (print "height: " $height) }}
{{ end }}
{{ if $maxheight }}
{{ $style = $style | append (print "max-height: " $maxheight) }}
{{ end }}
{{ if $maxwidth }}
{{ $style = $style | append (print "max-width: " $maxwidth) }}
{{ end }}
{{ if $width }}
{{ $style = $style | append (print "width: " $width) }}
{{ end }}
{{ $style = delimit $style "; " }}
<img {{ with $alt }} alt="{{ . }}" {{ end }} {{ with $class }} class="{{ . }}" {{ end }} crossorigin="{{ $crossorigin }}" {{ with $fetchpriority }} fetchpriority="{{ . }}" {{ end }} {{ with $intrinsicheight }} height="{{ . }}" {{ end }} loading="{{ $loading }}" referrerpolicy="{{ $referrerpolicy }}" {{ with $sizes }} sizes="{{ . }}" {{ end }} src="{{ $src }}" {{ with $srcset }} {{ printf `srcset="%s"` . | safeHTMLAttr }} {{ end }} {{ with $style }} style="{{ . | safeCSS }}" {{ end }} {{ with $title }} title="{{ . }}" {{ end }} {{ with $intrinsicwidth }} width="{{ . }}" {{ end }}>