This commit is contained in:
Ben Goldsworthy 2024-04-02 20:36:21 +01:00
parent 187900fd5b
commit 1fd9adcb52
No known key found for this signature in database
23 changed files with 1800 additions and 98 deletions

View file

@ -1,8 +1,34 @@
{{- $src := $.Page.Resources.GetMatch (.Get "src") -}}
<!--
Shortcode to render a media file as part of content where the place in text flow is not important.
@params src Resource `src` path.
@params link URI or Page Resource to wrap resource in. Optional.
@params rel Force relationship value. Optional.
-->
<!-- Variable assignment -->
{{- $src := $.Page.Resources.GetMatch ( .Get "src" ) -}}
{{- $linkedResource := $.Page.Resources.GetMatch ( .Get "link" ) -}}
{{- $valid_rel_values := partialCached "util/get_valid_rel_values.html.tmpl" . -}}
<!-- Validation -->
{{- if not ( in ( slice "image" "video" ) $src.ResourceType ) -}}
{{- errorf "Resource '%q' resource type '%q' is not valid (%q)" ( .Get "src" ) $src.ResourceType .Page.File.Path -}}
{{- end -}}
{{- if ( not $src ) -}}
{{- errorf "No Page Resource found for src '%q' (%q)" ( .Get "src" ) .Page.File.Path -}}
{{- end -}}
{{- $linkedResource := $.Page.Resources.GetMatch (.Get "link") -}}
{{- with .Get "rel" -}}
{{- if not ( in $valid_rel_values . ) -}}
{{- errorf "Invalid rel value '%q' (%q)" . .Page.File.Path -}}
{{- end -}}
{{- end -}}
<!-- Rendering -->
<figure class="article__figure figure{{ with .Get "class" }} {{ . }}{{ end }}" role="group">
{{- if .Get "link" -}}
@ -14,53 +40,21 @@
{{- end -}}
{{- if eq $src.ResourceType "image" -}}
<picture class="figure__picture"
itemprop="image">
<img class="u-photo figure__image"
{{ if ( or ( eq $src.MediaType.SubType "svg" ) ( eq $src.MediaType.SubType "gif" ) ) }}
src="{{ $src.RelPermalink }}"
{{ else }}
{{ with $src.Resize "1200x webp" }}
src="{{ .RelPermalink }}"
{{ end }}
width="{{ $src.Width }}"
height="{{ $src.Height }}"
{{ end }}
{{- with $src.Params.alt }}
alt="{{ . }}"
{{- end -}}
{{- with $src.Params.title }} title="{{ . }}" {{ end -}}
loading="lazy"
role="img"
/>
</picture>
{{- partial "media/picture.html.tmpl" $src -}}
{{- else if eq $src.ResourceType "video" -}}
{{- if eq $src.MediaType "video/ogg" -}}
<audio class="u-audio figure__audio" itemprop="audio" controls>
<source src="{{ $src.RelPermalink }}" type="{{ $src.MediaType }}" />
<p>Your browser doesn't support embedded audio, <a href="{{ $src.RelPermalink }}">view the audio here</a>.</p>
</audio>
{{- partial "media/audio.html.tmpl" $src -}}
{{- else -}}
<video class="u-video figure__video"
controls
src="{{ $src.RelPermalink }}"
poster="
{{- if $src.Params.poster -}}
{{- $posterSrc := $.Page.Resources.GetMatch ( .Get $src.Params.poster ) -}}
{{- with $posterSrc.Resize "1200x webp" -}}
{{- .RelPermalink -}}
{{- end -}}
{{- else -}}
{{- erroridf "a11y-video-poster" "No poster defined for resource %q (%q)" $src.RelPermalink .Page.File.Path -}}
{{- end -}}
"
itemprop="video">
Your browser doesn't support embedded video, <a href="{{ $src.RelPermalink }}">view the video here</a>.
</video>
{{- partial "media/video.html.tmpl" ( dict "." $src "page_file_path" .Page.File.Path ) -}}
{{- end -}}
{{- else if ( or ( ne $src.ResourceType "image" ) ( ne $src.ResourceType "video" ) ) -}}
{{- errorf "No handling for resource of type %q" $src.ResourceType -}}
{{- end -}}
{{- if .Get "link" }}</a>{{ end -}}
{{- if or ( .Get "caption" ) ( .Get "title" ) ( $src.Params.attr ) -}}