{{- /*
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 URLs */ -}}

{{- $prefix := slicestr $link 0 1 -}}
{{- if (ne $prefix "/") -}}
    {{- $prefix := slicestr $link 0 8 -}}
    {{- if (ne $prefix "https://") -}}
        {{- $prefix := slicestr $link 0 3 -}}
        {{- if (eq $prefix "../") -}}
            {{- $link = printf "/%s%s" .Page.Dir (slicestr $link 3) -}}
        {{- else -}}
            {{- $link = printf "/%s%s" .Page.Dir $link -}}
        {{- end -}}
    {{- end -}}
{{- end -}}

{{- $path := printf "content/%s" $link }}
{{- if strings.HasSuffix $link ".svg" -}}
    {{- $file := readFile $path -}}
    {{- $viewBox := index (findRE "viewBox( *?)=( *?)\"(.*?)\"" $file 1) 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)) -}}
    {{- $ratio = mul (div $h $w) 100 -}}
{{- else -}}
    {{- if not $ratio -}}
        {{- $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 class="not-for-endnotes" href="{{- $link -}}" title="{{- $title -}}">
            <img class="element-to-stretch" src="{{- $link -}}" alt="{{- $alt -}}" />
        </a>
    </div>
    <figcaption>{{- $caption -}}</figcaption>
</figure>
