Files
hinode/layouts/shortcodes/abbr.html
2024-01-01 12:32:39 +01:00

59 lines
1.8 KiB
HTML

<!--
Copyright © 2024 The Hinode Team / Mark Dumay. All rights reserved.
Use of this source code is governed by The MIT License (MIT) that can be found in the LICENSE file.
Visit gethinode.com/license for more details.
-->
{{ $error := false }}
<!-- Validate and initialize arguments -->
{{ if partial "utilities/IsInvalidArgs.html" (dict "structure" "abbr" "args" .Params) }}
{{ errorf "Invalid arguments: %s" .Position -}}
{{ $error = true }}
{{ end }}
<!-- Initialize arguments and default values -->
{{ $key := "" }}
{{ $data := "abbr" }}
{{ $class := "" }}
{{ $title := "" }}
{{ $entries := slice }}
{{- if .IsNamedParams }}
{{ $key = .Get "key" }}
{{- $data := .Get "data" | default "abbr" -}}
{{- $class := .Get "class" | default "" -}}
{{ else }}
{{ $key = .Get 0 }}
{{ end }}
<!-- Main code -->
{{ if not $error }}
{{/* Try language-specific file first */}}
{{ $path := path.Join (path.Dir $data) (printf "%s.%s" (path.BaseName $data) .Page.Language.Lang) (path.Ext $data) }}
{{ $entries = index site.Data $path }}
{{/* Use exact provided path as backup */}}
{{ if not $entries -}}
{{ $entries = index site.Data $data }}
{{ end }}
{{ if not $entries -}}
{{ errorf "Invalid abbrevation data '%s': %s" $data .Position -}}
{{ $error = true }}
{{ end -}}
{{ end }}
{{ if not $error }}
{{ $elements := (where $entries "id" (lower $key)) }}
{{ if gt (len $elements) 0 }}
{{ $title = index (index $elements 0) "long" }}
{{ end -}}
{{ if not $title -}}
{{ errorf "Cannot find value for '%s': %s" $key .Position -}}
{{ $error = true }}
{{ end -}}
{{ end }}
{{ if not $error }}
<abbr {{ with $class }}class="{{ . }}"{{ end }} title="{{ $title }}" tabindex="0">{{ $key }}</abbr>
{{ end }}