{{ $is_list := .is_list }}
{{ $page := .page }}
{{ $link := "" }}

{{/* ***** TODO: Deprecate `url_preprint` in favour of `url_pdf` given there's now a dedicated "Preprint" pub. type. ***** */}}
{{ with $page.Params.url_preprint }}
  {{ $resource := $page.Resources.GetMatch . }}
  {{ if $resource }}
    {{ $link = $resource.RelPermalink }}
  {{ else }}
    {{ $link = . | relURL }}
  {{ end }}
<a class="btn btn-outline-primary my-1 mr-1{{ if $is_list }} btn-sm{{end}}" href="{{ $link }}" target="_blank" rel="noopener">
  {{ i18n "btn_preprint" }}
</a>
{{ end }}
{{ $pdf := "" }}
{{ $resource := $page.Resources.GetMatch (printf "%s.pdf" $page.File.ContentBaseName) }}
{{ with $resource }}
  {{ $pdf = .RelPermalink }}
{{ else }}
  {{ if $page.Params.url_pdf }}
    {{ $pdf = $page.Params.url_pdf | relURL }}
  {{ end }}
{{ end }}

{{ with $pdf }}
<a class="btn btn-outline-primary my-1 mr-1{{ if $is_list }} btn-sm{{end}}" href="{{ . }}" target="_blank" rel="noopener">
  {{ i18n "btn_pdf" }}
</a>
{{ end }}
{{ $resource := $page.Resources.GetMatch "*.bib" }}
{{ with $resource }}
<button type="button" class="btn btn-outline-primary my-1 mr-1{{ if $is_list }} btn-sm{{end}} js-cite-modal"
        data-filename="{{ .RelPermalink }}">
  {{ i18n "btn_cite" }}
</button>
{{ end }}
{{ with $page.Params.url_code }}
  {{ $resource := $page.Resources.GetMatch . }}
  {{ if $resource }}
    {{ $link = $resource.RelPermalink }}
  {{ else }}
    {{ $link = . | relURL }}
  {{ end }}
<a class="btn btn-outline-primary my-1 mr-1{{ if $is_list }} btn-sm{{end}}" href="{{ $link }}" target="_blank" rel="noopener">
  {{ i18n "btn_code" }}
</a>
{{ end }}
{{ with $page.Params.url_dataset }}
  {{ $resource := $page.Resources.GetMatch . }}
  {{ if $resource }}
    {{ $link = $resource.RelPermalink }}
  {{ else }}
    {{ $link = . | relURL }}
  {{ end }}
<a class="btn btn-outline-primary my-1 mr-1{{ if $is_list }} btn-sm{{end}}" href="{{ $link }}" target="_blank" rel="noopener">
  {{ i18n "btn_dataset" }}
</a>
{{ end }}
{{ if $page.Params.projects }}
{{ range $page.Params.projects }}
  {{ with (site.GetPage (printf "project/%s" .)) }}
  <a class="btn btn-outline-primary my-1 mr-1{{ if $is_list }} btn-sm{{end}}" href="{{ .RelPermalink }}">
    {{ i18n "btn_project" }}
  </a>
  {{ else }}
    {{/* errorf "The `projects` parameter in `content/%s` references a project file, `content/project/%s`, which cannot be found. Please either set `projects = []` or fix the reference." $page.File.Path . */}}
  {{ end }}
{{ end }}
{{ else }}
{{ with $page.Params.url_project }}
<a class="btn btn-outline-primary my-1 mr-1{{ if $is_list }} btn-sm{{end}}" href="{{ . }}" target="_blank" rel="noopener">
  {{ i18n "btn_project" }}
</a>
{{ end }}
{{ end }}
{{ with $page.Params.url_poster }}
  {{ $resource := $page.Resources.GetMatch . }}
  {{ if $resource }}
    {{ $link = $resource.RelPermalink }}
  {{ else }}
    {{ $link = . | relURL }}
  {{ end }}
<a class="btn btn-outline-primary my-1 mr-1{{ if $is_list }} btn-sm{{end}}" href="{{ $link }}" target="_blank" rel="noopener">
  {{ i18n "btn_poster" }}
</a>
{{ end }}
{{ if $page.Params.slides }}
  {{ with (site.GetPage (printf "slides/%s" $page.Params.slides)) }}
  <a class="btn btn-outline-primary my-1 mr-1{{ if $is_list }} btn-sm{{end}}" href="{{ .RelPermalink }}" target="_blank">
    {{ i18n "btn_slides" }}
  </a>
  {{ end }}
{{ else }}
{{ with $page.Params.url_slides }}
  {{ $resource := $page.Resources.GetMatch . }}
  {{ if $resource }}
    {{ $link = $resource.RelPermalink }}
  {{ else }}
    {{ $link = . | relURL }}
  {{ end }}
<a class="btn btn-outline-primary my-1 mr-1{{ if $is_list }} btn-sm{{end}}" href="{{ $link }}" target="_blank" rel="noopener">
  {{ i18n "btn_slides" }}
</a>
{{ end }}
{{ end }}
{{ with $page.Params.url_video }}
  {{ $resource := $page.Resources.GetMatch . }}
  {{ if $resource }}
    {{ $link = $resource.RelPermalink }}
  {{ else }}
    {{ $link = . | relURL }}
  {{ end }}
<a class="btn btn-outline-primary my-1 mr-1{{ if $is_list }} btn-sm{{end}}" href="{{ $link }}" target="_blank" rel="noopener">
  {{ i18n "btn_video" }}
</a>
{{ end }}
{{ with $page.Params.url_source }}
  {{ $resource := $page.Resources.GetMatch . }}
  {{ if $resource }}
    {{ $link = $resource.RelPermalink }}
  {{ else }}
    {{ $link = . | relURL }}
  {{ end }}
<a class="btn btn-outline-primary my-1 mr-1{{ if $is_list }} btn-sm{{end}}" href="{{ $link }}" target="_blank" rel="noopener">
  {{ i18n "btn_source" }}
</a>
{{ end }}
{{ with $page.Params.doi }}
<a class="btn btn-outline-primary my-1 mr-1{{ if $is_list }} btn-sm{{end}}" href="https://doi.org/{{ . }}" target="_blank" rel="noopener">
  DOI
</a>
{{ end }}
{{ range $page.Params.links }}
  {{ $pack := or .icon_pack "fas" }}
  {{ $pack_prefix := $pack }}
  {{ if in (slice "fab" "fas" "far" "fal") $pack }}
    {{ $pack_prefix = "fa" }}
  {{ end }}
  {{ $link := .url }}
  {{ $scheme := (urls.Parse $link).Scheme }}
  {{ $target := "" }}
  {{ if not $scheme }}
    {{ $resource := $page.Resources.GetMatch .url }}
    {{ if $resource }}
      {{ $link = $resource.RelPermalink }}
    {{ else }}
      {{ $link = .url | relURL }}
    {{ end }}
  {{ else if in (slice "http" "https") $scheme }}
    {{ $target = "target=\"_blank\" rel=\"noopener\"" }}
  {{ end }}
  <a class="btn btn-outline-primary my-1 mr-1{{ if $is_list }} btn-sm{{end}}" href="{{ $link | safeURL }}" {{ $target | safeHTMLAttr }}>
    {{ if .icon }}<i class="{{ $pack }} {{ $pack_prefix }}-{{ .icon }} {{if .name}}mr-1{{end}}"></i>{{end}}
    {{ with .name }}{{ . | safeHTML }}{{end}}
  </a>
{{ end }}
