{{- /*  hugo-video shortcode
/*
/*    This file is part of hugo-video shortcode.
/*    A Hugo component shortcode to embed videos using the HTML video element.
/*
/*    @source     https://github.com/martignoni/hugo-video
/*    @license    https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
/*
*/ -}}

{{- $video_src := or (.Get "src") (.Get 0) -}}

{{/*
  <video controls muted playsinline>
    <source src="{{ $video_src }}" type="video/mp4">
</video>  */}}

{{- $video_mp4 := "" -}}
{{- $video_webm := "" -}}
{{- $video_ogg := "" -}}
{{- $video_dl := "" -}}
{{- $width := .Get 1 | default "100%" }}

{{- $filenotfound := true -}}
{{- $unsupportedfile := true -}}

{{- /* Find in page bundle all valid video files with matching name. */ -}}
{{- with $video_src -}}
  {{- $filenotfound = false -}}

    {{- if or (strings.HasSuffix . "mp4") (strings.HasSuffix . "m4v") -}}
        {{- $unsupportedfile = false -}}
        {{- $video_mp4 = . -}}
    {{- end -}}

    {{- if (strings.HasSuffix . "webm") -}}
      {{- $unsupportedfile = false -}}
      {{- $video_webm = . -}}
    {{- end -}}

    {{- if (strings.HasSuffix . "ogv") -}}
      {{- $unsupportedfile = false -}}
      {{- $video_ogg = . -}}
    {{- end -}}

{{- end -}}

{{- if $filenotfound -}}
    {{- /* No file of given name was found, we stop here. */ -}}
    {{- errorf "No file with filename %v found." $video_src -}}
{{- else if $unsupportedfile -}}
    {{- errorf "No valid video file with filename %v found." $video_src -}}
{{- else -}}
<video  {{ if ne (.Get "controls") "false" }}controls {{ end }}
        width="{{ or (.Get "width") $width }}"
        {{ if ne (.Get "autoplay") "false" }}autoplay {{ end }}
        {{ if ne (.Get "muted") "false" }}muted {{ end }}
        {{ if ne (.Get "loop") "false" }}loop {{ end }}
        playsinline>
  {{- with $video_webm }}
    <source src="{{ . }}" type="video/webm">
    {{- $video_dl = . -}}
  {{- end }}
  {{- with $video_ogg }}
    <source src="{{ . }}" type="video/ogg">
    {{- $video_dl = . -}}
  {{- end }}
  {{- with $video_mp4 }}
    <source src="{{ . }}" type="video/mp4">
    {{- $video_dl = . -}}
  {{- end }}
  <span>{{ i18n "videoUnsupported" $video_dl | safeHTML}}</span>
</video>
{{- end -}}

