hacker.tours-website/themes/zen/layouts/shortcodes/img.html

95 lines
3.2 KiB
HTML
Raw Normal View History

2024-06-13 22:14:05 +02:00
{{ $alt := .Get "alt" -}}
{{ $class := .Get "class" -}}
{{ $height := .Get "height" -}}
{{ $link := .Get "link" -}}
{{ $size := .Get "size" -}}
{{ $src := .Get "src" -}}
{{ $srcset := .Get "srcset" -}}
{{ $width := .Get "width" -}}
{{ $maxwidth := site.Params.imageMaxWidth -}}
{{ $realwidth := 0 -}}
{{ $realheight := 0 -}}
{{ $resource := false -}}
{{ $srcsetw := (slice) -}}
{{ $static := false -}}
{{ if and $src (fileExists (path.Join "/static" $src)) -}}
{{ with (imageConfig (path.Join "/static" $src)) -}}
{{ $realwidth = .Width -}}
{{ $realheight = .Height -}}
{{ end -}}
{{ $static = true -}}
{{ $src = relURL $src -}}
{{ else if $src -}}
{{ $src = strings.TrimLeft "/" $src -}}
{{ with resources.Get $src -}}
{{ $resource = . -}}
{{ end -}}
{{ with .Page.Resources.Get $src -}}
{{ $resource = . -}}
{{ end -}}
{{ end -}}
{{ if $resource -}}
{{ if $size -}}
{{ $image := $resource.Resize $size -}}
{{ $src = $image.RelPermalink -}}
{{ if or $width $height -}}
{{ $realwidth = $image.Width -}}
{{ $realheight = $image.Height -}}
{{ else -}}
{{ $width = $image.Width -}}
{{ $height = $image.Height -}}
{{ end -}}
{{ if $srcset -}}
{{ $srcsetw = $srcsetw | append (printf "%s %sw" $src (strings.TrimRight "x" $size)) -}}
{{ end -}}
{{ else -}}
{{ $src = $resource.RelPermalink -}}
{{ $realwidth = $resource.Width -}}
{{ $realheight = $resource.Height -}}
{{ end -}}
{{ if $srcset -}}
{{ range $set := split $srcset " " -}}
{{ $image := $resource.Resize (printf "%sx" $set) -}}
{{ $srcsetw = $srcsetw | append (printf "%s %sw" $image.RelPermalink $set) -}}
{{ end -}}
{{ $width = "" -}}
{{ $height = "" -}}
{{ end -}}
{{ end -}}
{{ if and $width $height -}}
{{ $width = int $width -}}
{{ $height = int $height -}}
{{ else if $width -}}
{{ $width = int $width -}}
{{ $height = $realheight -}}
{{ if and ($realwidth) (lt ($width) ($realwidth)) -}}
{{ $height = (mul ($realheight) (div (add ($width) 0.0) ($realwidth))) -}}
{{ $height = (math.Round $height) -}}
{{ end -}}
{{ else if $height -}}
{{ $width = $realwidth -}}
{{ $height = int $height -}}
{{ if and ($realheight) (lt ($height) ($realheight)) -}}
{{ $width = (mul ($realwidth) (div (add ($height) 0.0) ($realheight))) -}}
{{ $width = (math.Round $width) -}}
{{ end -}}
{{ else if and ($maxwidth) (lt ($maxwidth) ($realwidth)) -}}
{{ $width = ($maxwidth) -}}
{{ $height = (mul ($realheight) (div (add ($width) 0.0) ($realwidth))) -}}
{{ $height = (math.Round $height) -}}
{{ end -}}
{{ if not $src -}}
{{ warnf "%s shortcode number %d in \"content/%s\" is missing the \"src\" attribute." .Name .Ordinal .Page.File.Path -}}
{{ else if not (or $static $resource) -}}
{{ warnf "%s shortcode number %d in \"content/%s\" has an error in the \"src\" attribute." .Name .Ordinal .Page.File.Path -}}
{{ end -}}
{{ with $link }}<a class="image-link" href="{{ . }}">{{ end -}}
<img class="image{{ with $class }} {{ . }}{{ end }}" src="{{ $src }}"{{ with $srcsetw }} srcset="{{ delimit . ", " }}"{{ end }}{{ if isset .Params "alt" }} alt="{{ $alt }}"{{ end }}{{ with $width }} width="{{ . }}"{{ end }}{{ with $height }} height="{{ . }}"{{ end }}>
{{- if $link }}</a>{{ end }}