Welcome to mirror list, hosted at ThFree Co, Russian Federation.

img.html « shortcodes « layouts - gitlab.com/rmaguiar/hugo-theme-color-your-world.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9976e5fae5553a199906f2fa7cfbcd3a40d54577 (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
<!--
  Usage:
    img "weird_cat.jpg" "Something that can (or not) be a cat."
    img src="weird_cat.jpg" alt="Something that can (or not) be a cat."
    img class="border" src="weird_cat.jpg" alt="Something that can (or not) be a cat."
    img "weird_cat.jpg" "Something that can (or not) be a cat." "border"

  Default available classes:
    border
    borderless
-->

{{ $file    := .Get "src"   | default (.Get 0) }}
{{ $altText := .Get "alt"   | default (.Get 1) }}
{{ $class   := .Get "class" | default (.Get 2) }}

<!-- Image processing options -->
{{ $imageProc := (dict "highRes" (.Site.Params.imageProc.highRes | default .Site.Data.default.imageProc.highRes) "mediumRes" (.Site.Params.imageProc.mediumRes | default .Site.Data.default.imageProc.mediumRes) "lowRes" (.Site.Params.imageProc.lowRes | default .Site.Data.default.imageProc.lowRes)) }}

<!-- Default image path -->
{{ $imgPath := .Page.Param "imgPath" }}

{{ if $imgPath }}
  {{ $file = path.Join $imgPath $file }}
{{ end }}

{{ with $.Page.Resources.GetMatch $file }}

  <!--
    HACK
    Reduce reflow by generating a placeholder with similar size
  -->
  
  {{ $mediumRes := (index $imageProc.mediumRes 0) }}

  {{ $encodedPlaceholder := (printf "data:image/png;base64,%s" (((resources.Get "img/pixel.gif").Resize (printf "%vx%v %s" (.Resize $mediumRes).Width (.Resize $mediumRes).Height "png")).Content | base64Encode)) | safeURL }}
  
  {{ $outputSet := slice }}
  {{ $inputFile := . }}
  
  {{ range $imageProc }}
    {{ $outputSet = $outputSet | append (printf "%s %s" (($inputFile.Resize (index . 0)).RelPermalink) (index . 1)) }}
  {{ end }}

  <img
    {{ with $class }}class="{{ . }}"{{ end }}
    loading="lazy"
    srcset='{{ delimit $outputSet ", " }}'
    src="{{ $encodedPlaceholder }}"
    width="{{ (.Resize $mediumRes).Width }}"
    height="{{ (.Resize $mediumRes).Height }}"
    {{ with $altText }}alt="{{ . }}"{{ end }}
  />

{{ end }}