aboutsummaryrefslogtreecommitdiff
path: root/themes/default/layouts/partials/gallery-walk.html
blob: c17b0daee1179c8dbfde6a209fe1728eac971a94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
{{- $folder := .Folder -}}
{{- $files := slice -}}
{{- $dimensions := "320x320" -}}
{{- $author := strings.TrimPrefix "public/" (strings.TrimSuffix "/media/" $folder) -}}
{{- $prefix := print (partial "function-paths.html" "media") "/" $author "/gallery-walker_" -}}

{{- $mimes := slice
  "image/apng"
  "image/avif"
  "image/gif"
  "image/jpeg"
  "image/png"
  "image/svg+xml"
  "image/webp"
-}}

{{- if fileExists $folder -}}
  {{- range readDir $folder -}}
    {{- if not .IsDir -}}
      {{- $file := path.Join $folder .Name -}}
      {{- with resources.Get $file -}}
        {{- if in $mimes (string .MediaType) -}}
          {{-
            $files = $files | append (dict
              "Path" .Name
            )
          -}}
        {{- end -}}
      {{- end -}}
    {{- end -}}
    {{- if eq (len $files) 6 -}}
      {{- break -}}
    {{- end -}}
  {{- end -}}
{{- end -}}

{{- if and $files (resources.Get (index (first 1 $files) 0).Path) -}}
  {{- range $files -}}
    {{- with $image := resources.Get .Path -}}
      {{- if in $mimes (string .MediaType) -}}
        {{- $srcset := (.Content | resources.FromString (print $prefix (path.Base .))).Fit (print $dimensions " webp") -}}
        {{- $image = (.Content | resources.FromString (print $prefix (path.Base .))).Fit (print $dimensions " png") -}}
        {{- $decodedURL := print "/" (replaceRE "-" "/" (path.Base .Name) 2 | replaceRE "-.........webp" "") "/" -}}
        {{- $relatedPage := or (in $decodedURL "/messages/") (in $decodedURL "/feeds/") -}}

        {{- if not $relatedPage -}}
          {{- $decodedURL = print "/" $author "/" -}}
        {{- end -}}

        <a href="{{ $decodedURL }}">
          <picture>
            <source srcset="{{ $srcset.RelPermalink }}" type="image/webp" />
            <img loading="lazy"
              alt="{{ .Name }}"
              title="{{ .Name }}"
              width="{{ $image.Width }}"
              height="{{ $image.Height }}"
              src="{{ $image.RelPermalink }}"
              {{ printf `style="` | safeHTMLAttr -}}
              {{- partial "styles-image-gradient.html" $image.Colors | safeHTMLAttr -}}
              {{- printf `"` | safeHTMLAttr }}
            />
          </picture>
        </a>
      {{- end -}}
    {{- end -}}
  {{- end -}}
  <gallery-overlay></gallery-overlay>
{{- else -}}
  <footer>
    <code>No media found!</code>
  </footer>
{{- end -}}