{{- /*
Purpose:
Inserts a figure into a page. The user of this shortcode specifies the
relative width of the figure in percentage, and an aspect ratio value in
lieu of the Y coordinate. Through CSS trickery, these two values let us
calculate the actual width and height of the image at render time in such
a way that it avoids the typical 'shifting text' problem as images are
loaded asynchronously.

Usage:

{{< image width="<NN>%" ratio="<NN>"
    link="<path to image file>"
    alt="<text to display when the image is not available>"
    title="<text to display when hovering over the image>"
    caption="<text to display below the image>"
>}}

If you omit the alt parameter, it will take on the value of the title parameter.
If you omit the title parameter, it will take on the value of the caption parameter.
In other words, if all three strings are the same, you can just supply the caption
parameter.

The width value represents the percentage of the page that the image should occupy. If
width is not supplied, it defaults to 100%.

The ratio value represents the ratio of width to height for the image. You can normally
omit this value and it will be computed automatically.

*/ -}}

{{- $width := .Get "width" -}}
{{- $ratio := .Get "ratio" -}}
{{- $link := .Get "link" -}}
{{- $caption := .Get "caption" -}}
{{- $title := or (.Get "title") (.Get "caption") -}}
{{- $alt := or (.Get "alt") (.Get "title") (.Get "caption") -}}

{{- with $caption -}}
    {{- if (strings.HasSuffix . ".") -}}
        {{- errorf "Image caption ends with a period (%s)" .Position -}}
    {{- end -}}
{{- end -}}

{{- if not $width -}}
    {{- $width = "100%" -}}
{{- end -}}

{{- /* Turn relative values for $link into absolute values */ -}}

{{- if not (or (hasPrefix $link "/") (hasPrefix $link "https://") (strings.HasPrefix $link "http://")) -}}
    {{ $pageDir := path.Dir .Page.File.Path }}
    {{- if ne .Page.Language.Lang "en" -}}
        {{- $pageDir = printf "%s/%s" .Page.Language.Lang $pageDir -}}
    {{- end -}}

    {{- if (hasPrefix $link "../") -}}
        {{- $link = printf "/%s/%s" $pageDir (slicestr $link 3) -}}
    {{- else if (hasPrefix $link "./") -}}
        {{- $link = printf "/%s/%s" $pageDir (slicestr $link 2) -}}
    {{- else -}}
        {{- $link = printf "/%s/%s" $pageDir $link -}}
    {{- end -}}
{{- end -}}


{{- $path := "" -}}
{{- if not (or (hasPrefix $link "https://") (strings.HasPrefix $link "http://")) -}}
    {{- if eq .Page.Language.Lang "en" -}}
        {{- $path = printf "content/en%s" $link -}}
    {{- else -}}
        {{- $path = printf "content%s" $link -}}
    {{- end -}}
{{- end -}}

{{- if not $ratio -}}
    {{- if eq $path "" -}}
        {{- errorf "Must specify aspect ratio for external image reference '%s' (%s)" $link .Position -}}
    {{- else if not (fileExists $path) -}}
        {{- errorf "Image '%s' was not found (%s)" $path .Position -}}
    {{- end -}}
    {{- if strings.HasSuffix $link ".svg" -}}
        {{- $file := readFile $path -}}
        {{- $w := 0.0 -}}
        {{- $h := 0.0 -}}
        {{- $viewBox := findRE "viewBox( *?)=( *?)\"(.*?)\"" $file 1 -}}
        {{- if eq (len $viewBox) 1 -}}
            {{- $viewBox = index $viewBox 0 -}}
            {{- $viewBox = index (findRE "[0123456789 \\.]+" $viewBox 1) 0 -}}
            {{- $sizes := split $viewBox " " -}}
            {{- $w = sub (float (index $sizes 2)) (float (index $sizes 0)) -}}
            {{- $h = sub (float (index $sizes 3)) (float (index $sizes 1)) -}}
        {{- else -}}
            {{- $w = index (findRE "width( *?)=( *?)\"(.*?)\"" $file 1) 0 -}}
            {{- $w = float (index (findRE "[0123456789\\.]+" $w 1) 0) -}}
            {{- $h = index (findRE "height( *?)=( *?)\"(.*?)\"" $file 1) 0 -}}
            {{- $h = float (index (findRE "[0123456789\\.]+" $h 1) 0) -}}
        {{- end -}}
        {{- $ratio = mul (div $h $w) 100 -}}
    {{- else -}}
        {{- $img := imageConfig $path -}}
        {{- $w := float $img.Width -}}
        {{- $h := float $img.Height -}}
        {{- $ratio = mul (div $h $w) 100 -}}
    {{- end -}}
{{- end -}}

{{- /* Ensure that $ratio does NOT end with % */ -}}
{{- $ratio = strings.TrimRight "%" $ratio -}}

<figure style="width: {{- $width -}}">
    <div class="wrapper-with-intrinsic-ratio" style="padding-bottom: {{- $ratio -}}%">
        <a data-skipendnotes="true" href="{{- $link -}}" title="{{- $title -}}">
            <img class="element-to-stretch" src="{{- $link -}}" alt="{{- $alt -}}" />
        </a>
    </div>
    <figcaption>{{- $caption -}}</figcaption>
</figure>