This commit is contained in:
Ben Goldsworthy 2023-01-29 13:50:16 -05:00
parent ad769e95d8
commit 9e8626d8ba
21 changed files with 2767 additions and 1523 deletions

View file

@ -1,44 +1,63 @@
{{ $img := $.Page.Resources.GetMatch (.Get "src") }}
{{ $file := $.Page.Resources.GetMatch (.Get "link") }}
{{ $src := $.Page.Resources.GetMatch (.Get "src") }}
{{ $link := $.Page.Resources.GetMatch (.Get "link") }}
<figure{{ with .Get "class" }} class="{{ . }}"{{ end }}>
{{- if .Get "link" -}}
{{- if $file }}
<a href="{{ $file.RelPermalink }}"{{ with .Get "rel" }} rel="{{ . }}"{{ end }}>
{{- if $link }}
<a href="{{ $link.RelPermalink }}"{{ with .Get "rel" }} rel="{{ . }}"{{ end }}>
{{ else }}
<a href="{{ .Get "link" }}"{{ with .Get "target" }} target="{{ . }}"{{ end }}{{ with .Get "rel" }} rel="{{ . }}"{{ end }}>
{{ end }}
{{- end -}}
{{ if $img }}
<img class="figure__image"
{{ with $img.Resize (printf "%dx%d webp" $img.Width $img.Height) }}
src="{{ .RelPermalink }}"
{{ end }}
{{- if or ($img.Params.alt) (.Get "caption") }}
alt="{{ with $img.Params.alt }}{{ . }}{{ else }}{{ .Get "caption" | markdownify| plainify }}{{ end }}"
{{- end -}}
{{- with $img.Params.title }} title="{{ . }}"{{ end -}}
width="{{ $img.Width }}"
height="{{ $img.Height }}"
/>
{{ end }}
{{- if eq $src.ResourceType "image" -}}
<picture class="figure__picture">
<img class="figure__image"
{{ with $src.Resize (printf "%dx%d webp" $src.Width $src.Height) }}
src="{{ .RelPermalink }}"
{{ end }}
{{- if or ($src.Params.alt) (.Get "caption") }}
alt="{{ with $src.Params.alt }}{{ . }}{{ else }}{{ .Get "caption" | markdownify| plainify }}{{ end }}"
{{- end -}}
{{- with $src.Params.title }} title="{{ . }}"{{ end -}}
width="{{ $src.Width }}"
height="{{ $src.Height }}"
/>
</picture>
{{- else if eq $src.ResourceType "video" -}}
{{- if eq $src.MediaType "video/ogg" -}}
<audio controls>
<source src="{{ $src.RelPermalink }}" type="{{ $src.MediaType }}" />
<p>Your browser doesn't support embedded audio, but you can view the audio <a href="{{ $src.RelPermalink }}">here</a>.</p>
</audio>
{{- else -}}
<video class="figure__video"
controls
src="{{ $src.RelPermalink }}"
poster="{{ if $src.Params.poster }}{{ .Get $src.Params.poster }}{{ else }}{{ warnf "No poster defined for resource %q" $src.RelPermalink }}{{ end }}"
>
Your browser doesn't support embedded video, but you can view the video <a href="{{ $src.RelPermalink }}">here</a>.
</video>
{{- 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") ($img.Params.attr) -}}
{{- if or (.Get "caption") ($src.Params.attr) -}}
<figcaption class="figure__caption{{ if not (.Get "caption") }} figure__caption--no-height{{ end }}">
{{- if $img.Params.attrlink -}}
<a class="figcaption__attrlink" href="{{ $img.Params.attrlink }}"
{{- with $img.Params.attrtarget }} target="{{ . }}"{{ end -}}
{{- with $img.Params.attrrel }} rel="{{ . }}"{{ end -}}
{{- if $src.Params.attrlink -}}
<a class="figcaption__attrlink" href="{{ $src.Params.attrlink }}"
{{- with $src.Params.attrtarget }} target="{{ . }}"{{ end -}}
{{- with $src.Params.attrrel }} rel="{{ . }}"{{ end -}}
>
{{- end -}}
{{- with $img.Params.attr -}}<p class="figcaption__attr">{{ . | safeHTML }}{{ with $img.Params.attrlicence }} <span class="figcaption__licence">{{ . | safeHTML }}</span>{{ end }}</p>{{- end -}}
{{- if $img.Params.attrlink -}}
{{- with $src.Params.attr -}}<p class="figcaption__attr">{{ . | safeHTML }}{{ with $src.Params.attrlicence }} <span class="figcaption__licence">{{ . | safeHTML }}</span>{{ end }}</p>{{- end -}}
{{- if $src.Params.attrlink -}}
</a>
{{- end -}}
{{- with $img.Params.title -}}<h4 class="figcaption__title">{{ . }}</h4>{{- end -}}
{{- if .Get "caption" -}}
<p class="figcaption__caption">{{ .Get "caption" | markdownify }}</p>
{{- end -}}
{{- with $src.Params.title -}}<h4 class="figcaption__title">{{ . }}</h4>{{- end -}}
{{- with .Get "caption" -}}<p class="figcaption__caption">{{ . | markdownify }}</p>{{- end -}}
</figcaption>
{{- end -}}
</figure>

View file

@ -0,0 +1,30 @@
<!-- count how many times we've called this shortcode; load the css if it's the first time -->
{{- if not ($.Page.Scratch.Get "figurecount") }}<link rel="stylesheet" href={{ "css/hugo-easy-gallery.css" | relURL }} />{{ end }}
{{- $.Page.Scratch.Add "figurecount" 1 }}
{{ $baseURL := .Site.BaseURL }}
<div class="gallery caption-position-{{ with .Get "caption-position" | default "bottom" }}{{.}}{{end}} caption-effect-{{ with .Get "caption-effect" | default "slide" }}{{.}}{{end}} hover-effect-{{ with .Get "hover-effect" | default "zoom" }}{{.}}{{end}} {{ if ne (.Get "hover-transition") "none" }}hover-transition{{end}}" itemscope itemtype="http://schema.org/ImageGallery">
{{- with (.Get "dir") -}}
<!-- If a directory was specified, generate figures for all of the images in the directory -->
{{- $files := readDir ( print "content/" $.Page.File.Dir . ) }}
{{- range $files -}}
<!-- skip files that aren't images, or that include the thumb suffix in their name -->
{{- $isimg := lower .Name | findRE "\\.(gif|jpg|jpeg|tiff|png|bmp|webp|avif|jxl)" }}<!-- is the current file an image? -->
{{- if $isimg }}
{{- $caption := .Name | replaceRE "\\..*" "" | humanize }}<!-- humanized filename without extension -->
{{- $linkURL := print $.Page.Permalink ($.Get "dir") "/" .Name | absURL }}<!-- absolute URL to hi-res image -->
<div class="box">
<figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
<div class="img" style="background-image: url('{{ $linkURL }}');" >
<img itemprop="thumbnail" src="{{ $linkURL }}" alt="{{ $caption }}" /><!-- <img> hidden if in .gallery -->
</div>
<a href="{{ $linkURL }}" itemprop="contentUrl"></a><!-- put <a> last so it is stacked on top -->
</figure>
</div>
{{- end }}
{{- end }}
{{- else -}}
<!-- If no directory was specified, include any figure shortcodes called within the gallery -->
{{ .Inner }}
{{- end }}
</div>