Improve shortcodes to support page resources (#68)

This commit is contained in:
Fredrik Jonsson 2023-01-05 09:19:56 +01:00 • committed by GitHub
commit 7df8e982ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 230 additions and 43 deletions

View file

@ -1,6 +1,39 @@
<figure {{ with .Get "class" }}class="{{ . }}"{{ end }}> {{ $caption := .Get "caption" -}}
<audio controls preload="{{ .Get "preload" | default "metadata" }}"> {{ $class := .Get "class" -}}
{{ with .Get "src" }}<source src="{{ . | relURL }}" type="audio/mpeg">{{ end }} {{ $preload := .Get "preload" | default "metadata" -}}
{{ $src := .Get "src" -}}
{{ $resource := false -}}
{{ $static := false -}}
{{ $type := false -}}
{{ if and $src (fileExists (path.Join "/static" $src)) -}}
{{ $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 -}}
{{ $src = $resource.RelPermalink -}}
{{ $type = $resource.MediaType }}
{{ 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 -}}
<figure class="audio{{ with $class }} {{ . }}{{ end }}">
<audio controls preload="{{ $preload }}">
<source src="{{ $src }}" {{ with $type }}type="{{ . }}"{{ end }}>
</audio> </audio>
{{ with .Get "caption" }}<figcaption>{{ . }}</figcaption>{{ end }} {{ with $caption }}<figcaption>{{ . | markdownify }}</figcaption>{{ end }}
</figure> </figure>

View file

@ -8,38 +8,54 @@
{{ $size := .Get "size" -}} {{ $size := .Get "size" -}}
{{ $src := .Get "src" -}} {{ $src := .Get "src" -}}
{{ $srcset := .Get "srcset" -}} {{ $srcset := .Get "srcset" -}}
{{ $srcsetw := (slice) }}
{{ $title := .Get "title" -}} {{ $title := .Get "title" -}}
{{ $width := .Get "width" -}} {{ $width := .Get "width" -}}
{{ $maxwidth := site.Params.imageMaxWidth -}} {{ $maxwidth := site.Params.imageMaxWidth -}}
{{ $realwidth := 0 -}} {{ $realwidth := 0 -}}
{{ $realheight := 0 -}} {{ $realheight := 0 -}}
{{ if and $src (fileExists (path.Join "/assets" $src)) -}} {{ $resource := false -}}
{{ $original := resources.Get $src -}} {{ $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 -}} {{ if $size -}}
{{ $image := $original.Resize $size -}} {{ $image := $resource.Resize $size -}}
{{ $src = $image.RelPermalink -}} {{ $src = $image.RelPermalink -}}
{{ $width = $image.Width -}} {{ $width = $image.Width -}}
{{ $height = $image.Height -}} {{ $height = $image.Height -}}
{{ else -}} {{ else -}}
{{ $src = $original.RelPermalink -}} {{ $src = $resource.RelPermalink -}}
{{ $realwidth = $original.Width -}} {{ $realwidth = $resource.Width -}}
{{ $realheight = $original.Height -}} {{ $realheight = $resource.Height -}}
{{ end -}} {{ end -}}
{{ if $srcset -}} {{ if $srcset -}}
{{ range $set := split $srcset " " -}} {{ range $set := split $srcset " " -}}
{{ $image := $original.Resize (printf "%sx" $set) -}} {{ $image := $resource.Resize (printf "%sx" $set) -}}
{{ $srcsetw = $srcsetw | append (printf "%s %sw" $image.RelPermalink $set) }} {{ $srcsetw = $srcsetw | append (printf "%s %sw" $image.RelPermalink $set) }}
{{ end }} {{ end }}
{{ $width = "" -}} {{ $width = "" -}}
{{ $height = "" -}} {{ $height = "" -}}
{{ end -}} {{ end -}}
{{ else if and $src (fileExists (path.Join "/static" $src)) -}}
{{ with (imageConfig (path.Join "/static" $src)) -}}
{{ $realwidth = .Width -}}
{{ $realheight = .Height -}}
{{ end -}}
{{ end -}} {{ end -}}
{{ if and $width $height -}} {{ if and $width $height -}}
{{ $width = int $width -}} {{ $width = int $width -}}
{{ $height = int $height -}} {{ $height = int $height -}}
@ -62,6 +78,13 @@
{{ $height = (mul ($realheight) (div (add ($width) 0.0) ($realwidth))) -}} {{ $height = (mul ($realheight) (div (add ($width) 0.0) ($realwidth))) -}}
{{ $height = (math.Round $height) -}} {{ $height = (math.Round $height) -}}
{{ end -}} {{ 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 -}}
<figure class="image{{ with $class }} {{ . }}{{ end }}"> <figure class="image{{ with $class }} {{ . }}{{ end }}">
{{ with $link }}<a href="{{ . }}">{{ end -}} {{ with $link }}<a href="{{ . }}">{{ end -}}
<img src="{{ $src }}"{{ with $srcsetw }} srcset="{{ delimit . ", " }}"{{ end }}{{ with $alt }} alt="{{ . }}"{{ end }}{{ with $width }} width="{{ . }}"{{ end }}{{ with $height }} height="{{ . }}"{{ end }}> <img src="{{ $src }}"{{ with $srcsetw }} srcset="{{ delimit . ", " }}"{{ end }}{{ with $alt }} alt="{{ . }}"{{ end }}{{ with $width }} width="{{ . }}"{{ end }}{{ with $height }} height="{{ . }}"{{ end }}>

View file

@ -2,11 +2,35 @@
{{ $newtab := .Get "newtab" | default false -}} {{ $newtab := .Get "newtab" | default false -}}
{{ $src := .Get "src" -}} {{ $src := .Get "src" -}}
{{ $text := .Get "text" -}} {{ $text := .Get "text" -}}
{{ if and $src (fileExists (path.Join "/assets" $src)) -}}
{{ $original := resources.Get $src -}} {{ $resource := false -}}
{{ $src = $original.RelPermalink -}} {{ $static := false -}}
{{ if and $src (fileExists (path.Join "/static" $src)) -}}
{{ $static = true -}}
{{ $src = relURL $src -}}
{{ else if $src -}}
{{ $src = strings.TrimLeft "/" $src -}}
{{ with resources.Get $src -}}
{{ $resource = . -}}
{{ end -}} {{ end -}}
{{ with .Page.Resources.Get $src -}}
{{ $resource = . -}}
{{ end -}}
{{ end -}}
{{ if $resource -}}
{{ $src = $resource.RelPermalink -}}
{{ end -}}
{{ if not $text -}} {{ if not $text -}}
{{ $text = path.Base $src -}} {{ $text = path.Base $src -}}
{{ end -}} {{ 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 -}}
<a class="file{{ with $class }} {{ . }}{{ end }}" href="{{ $src }}"{{ if $newtab }} target="_blank" rel="noopener noreferrer"{{ end }}>{{ $text }}</a> <a class="file{{ with $class }} {{ . }}{{ end }}" href="{{ $src }}"{{ if $newtab }} target="_blank" rel="noopener noreferrer"{{ end }}>{{ $text }}</a>

View file

@ -5,37 +5,53 @@
{{ $size := .Get "size" -}} {{ $size := .Get "size" -}}
{{ $src := .Get "src" -}} {{ $src := .Get "src" -}}
{{ $srcset := .Get "srcset" -}} {{ $srcset := .Get "srcset" -}}
{{ $srcsetw := (slice) }}
{{ $width := .Get "width" -}} {{ $width := .Get "width" -}}
{{ $maxwidth := site.Params.imageMaxWidth -}} {{ $maxwidth := site.Params.imageMaxWidth -}}
{{ $realwidth := 0 -}} {{ $realwidth := 0 -}}
{{ $realheight := 0 -}} {{ $realheight := 0 -}}
{{ if and $src (fileExists (path.Join "/assets" $src)) -}} {{ $resource := false -}}
{{ $original := resources.Get $src -}} {{ $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 -}} {{ if $size -}}
{{ $image := $original.Resize $size -}} {{ $image := $resource.Resize $size -}}
{{ $src = $image.RelPermalink -}} {{ $src = $image.RelPermalink -}}
{{ $width = $image.Width -}} {{ $width = $image.Width -}}
{{ $height = $image.Height -}} {{ $height = $image.Height -}}
{{ else -}} {{ else -}}
{{ $src = $original.RelPermalink -}} {{ $src = $resource.RelPermalink -}}
{{ $realwidth = $original.Width -}} {{ $realwidth = $resource.Width -}}
{{ $realheight = $original.Height -}} {{ $realheight = $resource.Height -}}
{{ end -}} {{ end -}}
{{ if $srcset -}} {{ if $srcset -}}
{{ range $set := split $srcset " " -}} {{ range $set := split $srcset " " -}}
{{ $image := $original.Resize (printf "%sx" $set) -}} {{ $image := $resource.Resize (printf "%sx" $set) -}}
{{ $srcsetw = $srcsetw | append (printf "%s %sw" $image.RelPermalink $set) }} {{ $srcsetw = $srcsetw | append (printf "%s %sw" $image.RelPermalink $set) }}
{{ end }} {{ end }}
{{ $width = "" -}} {{ $width = "" -}}
{{ $height = "" -}} {{ $height = "" -}}
{{ end -}} {{ end -}}
{{ else if and $src (fileExists (path.Join "/static" $src)) -}}
{{ with (imageConfig (path.Join "/static" $src)) -}}
{{ $realwidth = .Width -}}
{{ $realheight = .Height -}}
{{ end -}}
{{ end -}} {{ end -}}
{{ if and $width $height -}} {{ if and $width $height -}}
{{ $width = int $width -}} {{ $width = int $width -}}
{{ $height = int $height -}} {{ $height = int $height -}}
@ -58,6 +74,13 @@
{{ $height = (mul ($realheight) (div (add ($width) 0.0) ($realwidth))) -}} {{ $height = (mul ($realheight) (div (add ($width) 0.0) ($realwidth))) -}}
{{ $height = (math.Round $height) -}} {{ $height = (math.Round $height) -}}
{{ end -}} {{ 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 -}} {{ with $link }}<a class="image-link" href="{{ . }}">{{ end -}}
<img class="image{{ with $class }} {{ . }}{{ end }}" src="{{ $src }}"{{ with $srcsetw }} srcset="{{ delimit . ", " }}"{{ end }}{{ with $alt }} alt="{{ . }}"{{ end }}{{ with $width }} width="{{ . }}"{{ end }}{{ with $height }} height="{{ . }}"{{ end }}> <img class="image{{ with $class }} {{ . }}{{ end }}" src="{{ $src }}"{{ with $srcsetw }} srcset="{{ delimit . ", " }}"{{ end }}{{ with $alt }} alt="{{ . }}"{{ end }}{{ with $width }} width="{{ . }}"{{ end }}{{ with $height }} height="{{ . }}"{{ end }}>
{{- if $link }}</a>{{ end }} {{- if $link }}</a>{{ end }}

View file

@ -6,16 +6,34 @@
{{ $link := .Get "link" -}} {{ $link := .Get "link" -}}
{{ $src := .Get "src" -}} {{ $src := .Get "src" -}}
{{ $width := .Get "width" -}} {{ $width := .Get "width" -}}
{{ if and $src (fileExists (path.Join "/assets" $src)) -}}
{{ $original := resources.Get $src -}} {{ $resource := false -}}
{{ if $inline -}} {{ $static := false -}}
{{ $src = $original.Content -}}
{{ else -}} {{ if and $src (fileExists (path.Join "/static" $src)) -}}
{{ $src = $original.RelPermalink -}} {{ $static = true -}}
{{ end -}} {{ $src = relURL $src -}}
{{ else if and $src $inline (fileExists (path.Join "/static" $src)) -}} {{ if $inline }}
{{ $src = readFile (path.Join "/static" $src) -}} {{ $src = readFile (path.Join "/static" $src) -}}
{{ end -}} {{ end -}}
{{ else if $src -}}
{{ $src = strings.TrimLeft "/" $src -}}
{{ with resources.Get $src -}}
{{ $resource = . -}}
{{ end -}}
{{ with .Page.Resources.Get $src -}}
{{ $resource = . -}}
{{ end -}}
{{ end -}}
{{ if $resource -}}
{{ if $inline -}}
{{ $src = $resource.Content -}}
{{ else -}}
{{ $src = $resource.RelPermalink -}}
{{ end -}}
{{ end -}}
{{ if and $width $height -}} {{ if and $width $height -}}
{{ $width = int $width -}} {{ $width = int $width -}}
{{ $height = int $height -}} {{ $height = int $height -}}
@ -24,6 +42,13 @@
{{ else if $height -}} {{ else if $height -}}
{{ $height = int $height -}} {{ $height = int $height -}}
{{ end -}} {{ 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 -}}
{{ if $caption }}<figure class="image image--svg{{ with $class }} {{ . }}{{ end }}">{{ end -}} {{ if $caption }}<figure class="image image--svg{{ with $class }} {{ . }}{{ end }}">{{ end -}}
{{ with $link }}<a class="image-link" href="{{ . }}">{{ end -}} {{ with $link }}<a class="image-link" href="{{ . }}">{{ end -}}
{{ if $inline -}} {{ if $inline -}}

View file

@ -1,6 +1,65 @@
<figure {{ with .Get "class" }}class="{{ . }}"{{ end }}> {{ $autoplay := .Get "autoplay" -}}
<video controls preload="{{ .Get "preload" | default "metadata" }}" {{ with .Get "width" }}width="{{ . }}"{{ end }} {{ with .Get "autoplay" }}autoplay="{{ . }}"{{ end }} {{ with .Get "loop" }}loop="{{ . }}"{{ end }}> {{ $caption := .Get "caption" -}}
{{ with .Get "src" }}<source src="{{ . | relURL }}" type="video/mp4">{{ end }} {{ $class := .Get "class" -}}
{{ $loop := .Get "loop" -}}
{{ $poster := .Get "poster" -}}
{{ $preload := .Get "preload" | default "metadata" -}}
{{ $src := .Get "src" -}}
{{ $width := .Get "width" -}}
{{ $posterres := false -}}
{{ $resource := false -}}
{{ $static := false -}}
{{ $type := false -}}
{{ if and $src (fileExists (path.Join "/static" $src)) -}}
{{ $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 -}}
{{ $src = $resource.RelPermalink -}}
{{ $type = $resource.MediaType }}
{{ end -}}
{{ if and $poster (fileExists (path.Join "/static" $poster)) -}}
{{ $poster = relURL $poster -}}
{{ else if $poster -}}
{{ $poster = strings.TrimLeft "/" $poster -}}
{{ with resources.Get $poster -}}
{{ $posterres = . -}}
{{ end -}}
{{ with .Page.Resources.Get $poster -}}
{{ $posterres = . -}}
{{ end -}}
{{ end -}}
{{ if $posterres -}}
{{ if $width -}}
{{ $image := $posterres.Resize (printf "%sx" $width) -}}
{{ $poster = $image.RelPermalink -}}
{{ else -}}
{{ $poster = $posterres.RelPermalink -}}
{{ end -}}
{{ 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 -}}
<figure class="video{{ with $class }} {{ . }}{{ end }}">
<video controls preload="{{ $preload }}" {{ with $width }}width="{{ . }}"{{ end }} {{ with $poster }}poster="{{ . }}"{{ end }} {{ with $autoplay }}autoplay="{{ . }}"{{ end }} {{ with $loop }}loop="{{ . }}"{{ end }}>
<source src="{{ $src }}" {{ with $type }}type="{{ . }}"{{ end }}>
</video> </video>
{{ with .Get "caption" }}<figcaption>{{ . }}</figcaption>{{ end }} {{ with $caption }}<figcaption>{{ . | markdownify }}</figcaption>{{ end }}
</figure> </figure>