Omphaloskepsis-2/layouts/partials/cite.html

135 lines
5 KiB
HTML
Raw Normal View History

2024-07-23 10:08:36 +02:00
{{- /*
2024-04-02 20:36:21 +01:00
Renders a work citation with semantic markup, called using the `cite` shortcode.
2025-05-15 11:08:14 +02:00
@params sc Site context.
@params pc Page context.
2024-04-02 20:36:21 +01:00
@params cite URI for work. Optional.
@params citeStyle Force a citation style. Optional.
@params href URI for citation, or 'asCite' to use value of `cite`. Optional.
@params itemtype The Schema.org `itemtype` value. Default 'CreativeWork'.
2024-04-02 20:36:21 +01:00
@params shortTitle Abbreviated display title. Optional.
@params title Title of the work to display.
@params titleLang Language code of a foreign-language work's title. Optional.
@params titleTr English translation of a foreign-language work's title. Optional.
2025-05-15 11:08:14 +02:00
For works cited that form part of a series (optional):
@params itemtypeSeries The Schema.org `itemtype` value for the work series. Default 'CreativeWork'. Optional.
@params titleSeries Title of the work series to display. Optional.
@params citeSeries URI for work series. Optional.
@params titleSeriesLang Language code of a foreign-language work series' title. Optional.
@params titleSeriesTr English translation of a foreign-language work series' title. Optional.
@params numberSeries The number or code of the quoted work within its series. Optional.
2024-07-23 10:08:36 +02:00
*/ -}}
2024-04-02 20:36:21 +01:00
<!-- Optional parameter overrides (for nested citations) -->
2025-05-15 11:08:14 +02:00
{{- $citeText := .Params.cite }}
2025-03-02 22:41:35 +01:00
{{- $replacedCite := replaceRE `^([a-z]+?://)?(www)?` "" $citeText }}
{{- $startOfCite := delimit ( findRE `^([a-z]+?://)?(www)?` $citeText 1 ) "" }}
2025-05-15 11:08:14 +02:00
{{- $cite := replaceRE `^` $startOfCite ( trim ( $replacedCite | .pc.Page.RenderString ) "\r\n" ) -}}
2024-04-02 20:36:21 +01:00
2025-05-15 11:08:14 +02:00
{{- $title := trim ( .Params.title | .pc.Page.RenderString ) "\r\n" -}}
{{- $titleLang := .Params.titleLang -}}
{{- $titleTr := .Params.titleTr -}}
{{- $itemtype := .Params.itemtype -}}
2024-04-02 20:36:21 +01:00
2024-07-23 10:08:36 +02:00
{{- if ( strings.Contains $cite "#" ) -}}
{{- erroridf "cite-with-fragment" "Cite parameter includes fragment identifier, which probably isn't what you want." -}}
{{- $cite = delimit ( split $cite "#" ) "" -}}
{{- end -}}
2024-04-02 20:36:21 +01:00
2024-07-23 10:08:36 +02:00
<!-- Validation -->
2024-04-02 20:36:21 +01:00
{{- if ( and ( isset .Params "citeStyle" ) ( not ( in ( slice "inherit" "enquote" "normal" ) .Params.citeStyle ) ) ) -}}
{{- erroridf "invalid-citeStyle" "Invalid `citeStyle` value %q for %q" .Params.citeStyle $title -}}
{{- end -}}
<!-- TODO: check `.Params.href` is URI or 'asCite' -->
2025-05-15 11:08:14 +02:00
{{- if ( and $itemtype ( not ( in .sc.Site.Data.itemtypes $itemtype ) ) ) -}}
{{- erroridf "invalid-itemtype" "Invalid Schema.org type value '%s' for %s (%q)\r\n%q" $itemtype .Params.title .sc.Page.File.Dir .Params -}}
2024-04-02 20:36:21 +01:00
{{- end -}}
2024-07-23 10:08:36 +02:00
<!-- TODO: check `.Params.cite` is URI -->
2024-04-02 20:36:21 +01:00
{{- if ( or $titleLang $titleTr ) -}}
{{- if ( not ( and $titleLang $titleTr ) ) -}}
{{- erroridf "invalid-foreign-title" "Must have both title translation and language set for foreign language title %q" $title -}}
{{- end -}}
{{- end -}}
2025-05-15 11:08:14 +02:00
<!-- Variable assignment -->
{{- $hasCitedWork := ( isset .Params "title" ) }}
{{- $hasCitedSeries := ( isset .Params "titleSeries" ) -}}
2024-04-02 20:36:21 +01:00
<!-- Rendering -->
2025-05-15 11:08:14 +02:00
{{- $itemType := default "CreativeWork" $itemtype -}}
2025-05-15 11:08:14 +02:00
{{- if ( or ( in .sc.Site.Data.itemtypes $itemType ) ( eq .Params.suppress "true" ) ) -}}
2024-04-02 20:36:21 +01:00
{{- with .Params.href -}}
<a href="{{- if eq . "asCite" -}}{{- $cite -}}{{- else -}}{{- . -}}{{- end -}}">
2024-04-02 20:36:21 +01:00
{{- end -}}
<cite
class="cite{{ with .Params.citeStyle }} cite--{{ . }}{{ end }}"
itemscope
itemprop="citation"
itemtype="https://schema.org/{{- $itemType -}}"
{{- if $titleLang -}}
lang="{{- $titleLang -}}"
title="{{- $titleTr -}}"
{{- end -}}>
{{- with $cite -}}
<meta itemprop="url" content="{{- . -}}">
2024-04-02 20:36:21 +01:00
{{- end -}}
<span itemprop="name">
{{- if $titleLang -}}
2024-07-23 10:08:36 +02:00
<i
lang="{{ $titleLang }}"
title="
{{- if $.Params.shortTitle -}}
{{- printf "%s [%s]" $title $titleTr | plainify -}}
{{- else -}}
{{- $titleTr | plainify -}}
{{- end -}}
"
>
2024-04-02 20:36:21 +01:00
{{- end -}}
{{- ( default $title .Params.shortTitle ) | markdownify | safeHTML -}}
{{- if $titleLang -}}
</i>
{{- end -}}
</span>{{- /**/ -}}
</cite>
{{- with .Params.href -}}
</a>
{{- end -}}
2025-05-15 11:08:14 +02:00
{{- if $hasCitedSeries }}
(<span itemprop="partOfSeries">
{{- partial "cite.html" (
dict
"sc" $.sc
"pc" $.pc
"Params" (
dict
"cite" .Params.citeSeries
"title" .Params.titleSeries
"titleLang" .Params.titleSeriesLang
"titleTr" .Params.titleSeriesTr
"itemtype" .Params.itemtypeSeries
"href" .Params.hrefSeries
)
) -}}
</span>
{{- with .Params.seasonNumber }}, S<span itemprop="partOfSeason">{{ . }}</span>{{- end -}}
{{- with .Params.episodeNumber }}, E<span itemprop="episodeNumber">{{ . }}</span>{{- end -}}
)
{{- end -}}
2024-04-02 20:36:21 +01:00
{{- else -}}
{{- erroridf "invalid-schema-type" "Invalid Schema.org type value %q for %q" $itemType $title -}}
{{- end -}}