aboutsummaryrefslogtreecommitdiff
path: root/themes/default/layouts/_default/_markup/render-image.html
blob: 3767cb899ae84aa62e15d198f728a701245e8f9b (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
{{- $source       := $.Destination | safeURL -}}
{{- $public       := print "public/" $source -}}
{{- $local        := "" -}}
{{- $cache        := "" -}}
{{- $exists       := "" -}}
{{- $width        := "" -}}
{{- $height       := "" -}}
{{- $srcset       := "" -}}
{{- $colors       := "" -}}
{{- $figcaption   := "Image/Picture" -}}
{{- $orientation  := "landscape" -}}
{{- $dimensions   := "600x360" -}}
{{- $author       := .Page.Section -}}
{{- $relURL       := strings.TrimPrefix .Page.Site.BaseURL .Page.Permalink -}}
{{- $immutable    := print ($relURL | humanize | urlize) "-" ($source | sha256 | truncate 8 "") -}}
{{- $fileCache    := print (partial "function-paths.html" "media") "/" $author "/" $immutable -}}
{{- $cached       := and (fileExists (print "public/" $fileCache ".webp")) (fileExists (print "public/" $fileCache (path.Ext $source))) -}}

{{- if not $cached -}}
  {{- with $remote := resources.GetRemote $source -}}
    {{- with .Err -}}
      {{- if fileExists $public -}}
        {{- $local = resources.Get $public -}}
        {{- with $local -}}
          {{- $local = .Content | resources.FromString (print (partial "function-paths.html" "media") "/" $author "/" (path.Base .)) -}}
        {{- end -}}
        {{- $source = print ("" | absURL) $source -}}
      {{- else -}}
        {{- $404image := resources.Get "data/media/404.png" -}}
        {{- with $404image -}}
          {{- $width  = .Width -}}
          {{- $height = .Height -}}
        {{- end -}}
        {{- $source = $404image.Permalink -}}
      {{- end -}}
    {{- else -}}
      {{- $cache = .Content | resources.FromString (print (partial "function-paths.html" "media") "/" $author "/" (path.Base .)) -}}
    {{- end -}}
  {{- end -}}
{{- else -}}
  {{- $exists = resources.Get (print "public/" $fileCache (path.Ext $source)) -}}
  {{- $exists = $exists.Content | resources.FromString (print (partial "function-paths.html" "media") "/" $author "/" (path.Base $exists)) -}}
{{- end -}}

{{- with $local }}
  {{- $local = .Fit (print $dimensions " webp") }}
  {{- $local = $local | resources.Copy (print (partial "function-paths.html" "media") "/" $author "/" $immutable ".webp") }}
  {{- $srcset = $local.Permalink -}}
  {{- $local = .Fit $dimensions }}
  {{- $local = $local | resources.Copy (print (partial "function-paths.html" "media") "/" $author "/" $immutable (path.Ext $local)) }}
  {{- $width = $local.Width -}}
  {{- $height = $local.Height -}}
  {{- $colors = $local.Colors -}}
  {{- $writeToFile := $local.Permalink }}
{{- end }}

{{- with $cache }}
  {{- $cache = .Fit (print $dimensions " webp") }}
  {{- $cache = $cache | resources.Copy (print $author "/media/" $immutable ".webp") }}
  {{- $srcset = $cache.Permalink -}}
  {{- $cache = .Fit $dimensions }}
  {{- $cache = $cache | resources.Copy (print (partial "function-paths.html" "media") "/" $author "/" $immutable (path.Ext $cache)) }}
  {{- $width = $cache.Width -}}
  {{- $height = $cache.Height -}}
  {{- $colors = $cache.Colors -}}
  {{- $cache = $cache.Permalink }}
{{- end -}}

{{- with $exists }}
  {{- if fileExists (print "public/" $fileCache ".webp") -}}
    {{- $srcset = print "/" $fileCache ".webp" -}}
  {{- end -}}
  {{- if fileExists (print "public/" $author "/media/" $immutable ".webp") -}}
    {{- $srcset = print "/" $author "/media/" $immutable ".webp" -}}
  {{- end -}}
  {{- $width = .Width -}}
  {{- $height = .Height -}}
  {{- $colors = .Colors -}}
  {{- $cache = .Permalink }}
{{- end -}}

{{- if lt $width $height -}}
  {{- $orientation = "portrait" -}}
{{- end -}}

{{- with $.Title -}}
  {{- $figcaption = . | markdownify -}}
{{ end }}
{{- /* This comment removes trailing newlines and white spaces. */ -}}
<figure>
  <a data-orientation="{{ $orientation }}" href="{{ $source }}">
    <picture>
      {{- with $srcset }}
      <source srcset="{{ $srcset }}" type="image/webp" />
      {{- end }}
      <img
        loading="lazy"
        src="{{ or $cache $source }}"
        alt="{{ $.Text | htmlUnescape }}"
        title="{{ $.Text | htmlUnescape }}"
        width="{{ $width }}"
        height="{{ $height }}"
        {{ printf `style="` | safeHTMLAttr -}}
        {{- partial "styles-image-gradient.html" $colors | safeHTMLAttr -}}
        {{- printf `"` | safeHTMLAttr }}
      />
    </picture>
  </a>
  <figcaption>
  {{ $figcaption }}
  <br>
  Index: {{ with $cache }} {{ print "[Cache](" $cache ")" | markdownify }} &middot; {{ end }}
  {{ print "[Source](" $source ")" | markdownify }}
  </figcaption>
</figure>
{{- /* This comment removes trailing newlines and white spaces. */ -}}