You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
5.5 KiB

{{/*
This shortcode does A LOT of work. In prior incarnations, it was
three or four shortcodes. However, as I started normalizing and
overloading, it became clear that I could consolidate the basic logic.
Yes this does a lot of things, but in doing it this way, I have
consistent glossary referencing mechanisms.
Parameters:
@param key - required for lookup of glossary entry
Optional Parameters (in likely importance order):
@param abbr - include an abbreviation; if "t" use the glossary's abbr,
otherwise use the given value.
@param plural - use the pluralization (unless you provide the "title"
parameter)
@param title - use the given "title" for title
@param noIcon - when "t" do not render the linking icon to glossary
@param noTitle - do not render the title (you'll probably want the
abbr="t")
@param link - specify the given link (see implementation below)
@param tag - specify the tag to use for the title node; defaults to
either cite or span (based on logic below)
@param dfn - add a sidenote with a description; if "t" use the
predefined; otherwise use the given.
@param itemprop - sometimes, instead of a "mention" you'll want to use
this as the tag
*/}}
{{- $key := .Get "key" | upper }}
{{- $entry := index (where $.Site.Data.glossary "key" "eq" $key) 0 }}
{{- if isset $entry "key" }}
{{- $citedTitle := default $entry.title ( default $entry.mention_as ) }}
{{- $title := "" }}
{{- if .Get "title" }}
{{- $title = .Get "title" }}
{{- else if .Get "mention_as" }}
{{- $title = .Get "mention_as" }}
{{- else if .Get "plural" }}
{{- $title = $entry.plural_title }}
{{- else }}
{{- $title = $entry.title }}
{{- end }}
{{- if eq 0 (len $title ) }}{{- errorf "Missing title for key %s in file %s" (.Get "key") (.Page.Permalink) }}{{- end }}
{{- $scratchKey := printf "glossary-%s" $entry.key }}
{{- $titleTag := "span" }}
{{- if .Get "tag" }}{{- $titleTag = .Get "tag" }}{{- else }}{{- if and ($entry.offer) (eq $title $citedTitle ) }}{{ $titleTag = "cite" }}{{- end }}{{- end }}
{{- if .Get "downcase" }}{{ $title = lower $title }}{{- end }}
{{- $abbrHTML := "" }}
{{- $abbrTitle := default $entry.title (.Get "title") }}
{{- with (or (.Get "abbr") (.Get "noTitle")) }}
{{- $abbr := "" }}
{{- if eq . "t" }}
{{- if $.Get "plural" }}
{{- with $entry.plural_abbr }}{{- $abbr = $entry.plural_abbr }}{{- else }}{{- errorf "Missing plural_abbr for %s in file %s" $key $.Page.Permalink }}{{- end }}
{{- with $entry.plural_title }}{{- $abbrTitle = $entry.plural_title }}{{- else }}{{- errorf "Missing plural_title for %s in file %s" $key $.Page.Permalink }}{{- end }}
{{- else }}
{{- $abbr = $entry.abbr }}
{{- end }}
{{- else }}
{{- $abbr = . }}
{{- end }}
{{- if $abbr }}{{- $abbrHTML = printf "<abbr title=\"%s\">%s</abbr>" $abbrTitle $abbr }}{{- else }}{{- warnf "Expected abbr for key %s in post %s" $key ($.Page.Permalink) }}{{- end }}
{{- end }}{{/* end with .Get "abbr" */}}
{{- if .Page.Scratch.Get $scratchKey }}
{{- if eq (len $abbrHTML) 0 }}{{ printf "<%s>%s</%s>" $titleTag $title $titleTag | safeHTML }}{{- else }}{{ $abbrHTML | safeHTML }}{{- end }}
{{- else }}
{{- .Page.Scratch.Set $scratchKey "t" }}
{{- $itemprop := default "mentions" ( .Get "itemprop" ) }}
{{- $itemtype := default $entry.itemtype ("Thing") }}
{{- if eq $titleTag "cite" }}{{ $itemtype = "CreativeWork" }}{{- end }}
{{- $innerTitleHtml := $title }}
{{- $reflink := "" }}
{{- if (.Get "noIcon") }}{{- else }}
{{- $reflink = printf " <small><a class=\"ref\" rel=\"tag opener\" aria-label=\"Other site-wide references of %s\" title=\"Other site-wide references of %s\" href=\"/site-map/glossary/#abbr-dfn-%s\">&#128214;</a></small>" $title $title $entry.key | safeHTML }}
{{- end }}
{{- $dfnText := "" }}
{{- with .Get "dfn" }}
{{- $dfnInnerText := "" }}{{ if eq . "t" }}{{ with $entry.description }}{{- $dfnInnerText := . }}{{- end }}{{- else }}{{- $dfnInnerText = . }}{{- end }}
{{- if ne 0 (len $dfnInnerText) }}{{- $dfnText = printf "<span class=\"sidenote-number\"><small class=\"side\" role=\"note\">(i.e., <span itemprop=\"description\">%s</span>)</small></span>" . | safeHTML }}{{- end }}{{- end }}
{{- with $entry.offer }}
{{- $innerTitleHtml = printf "<a itemprop=\"offer\" href=\"%s\">%s</a>" . $title }}
{{- else }}
{{- with $entry.same_as }}
{{- $innerTitleHtml = printf "<a itemprop=\"sameAs\" href=\"%s\">%s</a>" . $title }}
{{- else }}
{{- with .Get "link" }}
{{- if and (eq . "same_as") (isset $entry "same_as") }}
{{- $innerTitleHtml = printf "<a itemprop=\"sameAs\" href=\"%s\">%s</a>" $entry.same_as $title }}
{{- else if and (eq . "itemid") (isset $entry "itemid") }}
{{- $innerTitleHtml = printf "<a itemprop=\"sameAs\" href=\"%s\">%s</a>" $entry.itemid $title }}
{{- else if eq "http" (substr . 0 4) }}
{{- $innerTitleHtml = printf "<a href=\"%s\">%s</a>" . $title }}
{{- else if isset $entry "itemid" }}
{{- $innerTitleHtml = printf "<a href=\"%s\">%s</a>" $entry.itemid $title }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}<span itemprop="{{ $itemprop }}" itemscope itemtype="http://schema.org/{{ $itemtype }}">{{- with $entry.itemid }}<link href="{{ . }}" itemprop="sameAs">{{- end }}
{{- if .Get "noTitle" }}<meta itemprop="name" content="{{ $title }}">{{ $abbrHTML | safeHTML }}{{ $dfnText }}{{ $reflink }}{{- else }}
{{- printf "<%s itemprop=\"name\">%s</%s>%s" $titleTag $innerTitleHtml $titleTag $dfnText | safeHTML }}
{{- if eq 0 (len $abbrHTML) }}{{ $reflink }}{{- else }} ({{ $abbrHTML | safeHTML }}{{ $reflink}}){{- end }}
{{- end }}</span>{{- end }}
{{- else }}{{ errorf "Missing %s in file %s" (.Get "key") (.Page.Permalink) }}{{- end }}