refactor: citations

This commit is contained in:
Ben Goldsworthy 2025-05-15 11:08:14 +02:00
parent de341f4191
commit 810805f9ab
No known key found for this signature in database
6 changed files with 76 additions and 54 deletions

View file

@ -261,7 +261,7 @@ An epigram-style blockquote shortcode.
{{< blockquote {{< blockquote
source="Source" source="Source"
title="Film title" title="Film title"
schemaType="Movie" itemtype="Movie"
style="epigram" style="epigram"
>}} >}}
A blockquote shortcode. A blockquote shortcode.
@ -270,9 +270,9 @@ A blockquote shortcode.
{{< blockquote {{< blockquote
source="Source" source="Source"
title="Chapter title" title="Chapter title"
schemaType="Chapter" itemtype="Chapter"
titleSeries="Book title" titleSeries="Book title"
schemaTypeSeries="Book" itemtypeSeries="Book"
style="epigram" style="epigram"
>}} >}}
A blockquote shortcode. A blockquote shortcode.
@ -281,9 +281,9 @@ A blockquote shortcode.
{{< blockquote {{< blockquote
source="Source" source="Source"
title="TV episode title" title="TV episode title"
schemaType="TVEpisode" itemtype="TVEpisode"
titleSeries="TV series title" titleSeries="TV series title"
schemaTypeSeries="TVSeries" itemtypeSeries="TVSeries"
numberSeries="S01E01" numberSeries="S01E01"
style="epigram" style="epigram"
>}} >}}

View file

@ -1,6 +1,9 @@
{{- /* {{- /*
Renders a work citation with semantic markup, called using the `cite` shortcode. Renders a work citation with semantic markup, called using the `cite` shortcode.
@params sc Site context.
@params pc Page context.
@params cite URI for work. Optional. @params cite URI for work. Optional.
@params citeStyle Force a citation style. Optional. @params citeStyle Force a citation style. Optional.
@params href URI for citation, or 'asCite' to use value of `cite`. Optional. @params href URI for citation, or 'asCite' to use value of `cite`. Optional.
@ -9,18 +12,27 @@
@params title Title of the work to display. @params title Title of the work to display.
@params titleLang Language code of a foreign-language work's title. Optional. @params titleLang Language code of a foreign-language work's title. Optional.
@params titleTr English translation of a foreign-language work's title. Optional. @params titleTr English translation of a foreign-language work's title. Optional.
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.
*/ -}} */ -}}
<!-- Optional parameter overrides (for nested citations) --> <!-- Optional parameter overrides (for nested citations) -->
{{- $citeText := default .Params.cite ( .Scratch.Get "cite" ) }}
{{- $citeText := .Params.cite }}
{{- $replacedCite := replaceRE `^([a-z]+?://)?(www)?` "" $citeText }} {{- $replacedCite := replaceRE `^([a-z]+?://)?(www)?` "" $citeText }}
{{- $startOfCite := delimit ( findRE `^([a-z]+?://)?(www)?` $citeText 1 ) "" }} {{- $startOfCite := delimit ( findRE `^([a-z]+?://)?(www)?` $citeText 1 ) "" }}
{{- $cite := replaceRE `^` $startOfCite ( trim ( $replacedCite | .Page.RenderString ) "\r\n" ) -}} {{- $cite := replaceRE `^` $startOfCite ( trim ( $replacedCite | .pc.Page.RenderString ) "\r\n" ) -}}
{{- $title := trim ( default .Params.title ( .Scratch.Get "title" ) | .Page.RenderString ) "\r\n" -}} {{- $title := trim ( .Params.title | .pc.Page.RenderString ) "\r\n" -}}
{{- $titleLang := default .Params.titleLang ( .Scratch.Get "titleLang" ) -}} {{- $titleLang := .Params.titleLang -}}
{{- $titleTr := default .Params.titleTr ( .Scratch.Get "titleTr" ) -}} {{- $titleTr := .Params.titleTr -}}
{{- $itemtype := default .Params.itemtype ( .Scratch.Get "itemtype" ) -}} {{- $itemtype := .Params.itemtype -}}
{{- if ( strings.Contains $cite "#" ) -}} {{- if ( strings.Contains $cite "#" ) -}}
{{- erroridf "cite-with-fragment" "Cite parameter includes fragment identifier, which probably isn't what you want." -}} {{- erroridf "cite-with-fragment" "Cite parameter includes fragment identifier, which probably isn't what you want." -}}
@ -35,8 +47,8 @@
<!-- TODO: check `.Params.href` is URI or 'asCite' --> <!-- TODO: check `.Params.href` is URI or 'asCite' -->
{{- if ( and $itemtype ( not ( in $.Site.Data.itemtypes $itemtype ) ) ) -}} {{- if ( and $itemtype ( not ( in .sc.Site.Data.itemtypes $itemtype ) ) ) -}}
{{- erroridf "invalid-itemtype" "Invalid Schema.org type value %q for %q" $itemtype $title -}} {{- erroridf "invalid-itemtype" "Invalid Schema.org type value '%s' for %s (%q)\r\n%q" $itemtype .Params.title .sc.Page.File.Dir .Params -}}
{{- end -}} {{- end -}}
<!-- TODO: check `.Params.cite` is URI --> <!-- TODO: check `.Params.cite` is URI -->
@ -47,9 +59,15 @@
{{- end -}} {{- end -}}
{{- end -}} {{- end -}}
<!-- Variable assignment -->
{{- $hasCitedWork := ( isset .Params "title" ) }}
{{- $hasCitedSeries := ( isset .Params "titleSeries" ) -}}
<!-- Rendering --> <!-- Rendering -->
{{- $itemType := default "CreativeWork" $itemtype -}} {{- $itemType := default "CreativeWork" $itemtype -}}
{{- if ( or ( in $.Site.Data.itemtypes $itemType ) ( eq .Params.suppress "true" ) ) -}} {{- if ( or ( in .sc.Site.Data.itemtypes $itemType ) ( eq .Params.suppress "true" ) ) -}}
{{- with .Params.href -}} {{- with .Params.href -}}
<a href="{{- if eq . "asCite" -}}{{- $cite -}}{{- else -}}{{- . -}}{{- end -}}"> <a href="{{- if eq . "asCite" -}}{{- $cite -}}{{- else -}}{{- . -}}{{- end -}}">
{{- end -}} {{- end -}}
@ -89,6 +107,28 @@
{{- with .Params.href -}} {{- with .Params.href -}}
</a> </a>
{{- end -}} {{- end -}}
{{- 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 -}}
{{- else -}} {{- else -}}
{{- erroridf "invalid-schema-type" "Invalid Schema.org type value %q for %q" $itemType $title -}} {{- erroridf "invalid-schema-type" "Invalid Schema.org type value %q for %q" $itemType $title -}}
{{- end -}} {{- end -}}

View file

@ -33,7 +33,7 @@
class="item-tile lazy {{- with .Params.site }}item-tile--{{ . }}{{ end -}}" class="item-tile lazy {{- with .Params.site }}item-tile--{{ . }}{{ end -}}"
{{ if .Params.featured_image }} {{ if .Params.featured_image }}
{{ with .Resources.GetMatch .Params.featured_image }} {{ with .Resources.GetMatch .Params.featured_image }}
{{ if eq .MediaType.SubType "svg" }} {{ if ( in ( slice "avif" "svg" "gif" ) .MediaType.SubType ) }}
style="background-image: url({{ .RelPermalink }})" style="background-image: url({{ .RelPermalink }})"
{{ else }} {{ else }}
{{ with .Resize "1000x webp" }} {{ with .Resize "1000x webp" }}
@ -51,7 +51,7 @@
style="display: none;" style="display: none;"
src=" src="
{{- with .Resources.GetMatch .Params.featured_image -}} {{- with .Resources.GetMatch .Params.featured_image -}}
{{- if eq .MediaType.SubType "svg" -}} {{- if ( in ( slice "avif" "svg" "gif" ) .MediaType.SubType ) -}}
{{ .RelPermalink }} {{ .RelPermalink }}
{{- else -}} {{- else -}}
{{- with .Resize "1000x webp" -}} {{- with .Resize "1000x webp" -}}
@ -87,7 +87,7 @@
{{ end }} {{ end }}
{{- if .File -}} {{- if .File -}}
{{- partialCached "text/item-title.html" ( dict "pc" . "sc" $ "class" "item-tile" ) .File.Filename -}} {{- partial "text/item-title.html" ( dict "pc" . "sc" $ "class" "item-tile" ) -}}
{{- else -}} {{- else -}}
{{- partial "text/item-title.html" ( dict "pc" . "sc" $ "class" "item-tile" ) -}} {{- partial "text/item-title.html" ( dict "pc" . "sc" $ "class" "item-tile" ) -}}
{{- end -}} {{- end -}}

View file

@ -9,14 +9,6 @@
For works cited (optional): For works cited (optional):
@params ibid Whether to use ibidem as the source. Optional. @params ibid Whether to use ibidem as the source. Optional.
@see partials/cite.html for full list of args. @see partials/cite.html for full list of args.
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.
*/ */
-}} -}}
@ -35,7 +27,6 @@
{{- $isSourceHidden := default ( not ( or ( isset .Params "source" ) ( isset .Params "ibid" ) ) ) ( index .Params "hideSource" ) -}} {{- $isSourceHidden := default ( not ( or ( isset .Params "source" ) ( isset .Params "ibid" ) ) ) ( index .Params "hideSource" ) -}}
{{- $hasCitedWork := ( isset .Params "title" ) }} {{- $hasCitedWork := ( isset .Params "title" ) }}
{{- $hasCitedSeries := ( isset .Params "titleSeries" ) -}}
<!-- Validation --> <!-- Validation -->
@ -79,20 +70,8 @@
{{- if $hasCitedWork -}} {{- if $hasCitedWork -}}
, ,
<span itemprop="isBasedOn" <span itemprop="isBasedOn"
>{{- partial "cite.html" . -}} >{{- partial "cite.html" ( dict "sc" $ "pc" . "Params" .Params ) -}}
{{- if $hasCitedSeries }} </span>
(
{{- .Scratch.Set "cite" .Params.citeSeries -}}
{{- .Scratch.Set "title" .Params.titleSeries -}}
{{- .Scratch.Set "titleLang" .Params.titleSeriesLang -}}
{{- .Scratch.Set "titleTr" .Params.titleSeriesTr -}}
{{- .Scratch.Set "itemtype" .Params.itemtypeSeries -}}
{{- .Scratch.Set "href" .Params.hrefSeries -}}
{{- partial "cite.html" . -}}
{{- with .Params.numberSeries }}, {{ . | safeHTML -}}{{- end -}}
)
{{- end -}}</span
>
{{- end -}} {{- end -}}
{{- with $comment -}} {{- with $comment -}}
{{- printf " (%s)" . -}} {{- printf " (%s)" . -}}

View file

@ -9,7 +9,9 @@
@params title Title of the work to display. @params title Title of the work to display.
@params titleLang Language code of a foreign-language work's title. Optional. @params titleLang Language code of a foreign-language work's title. Optional.
@params titleTr English translation of a foreign-language work's title. Optional. @params titleTr English translation of a foreign-language work's title. Optional.
*/ -}} */
-}}
<!-- Validation --> <!-- Validation -->
@ -21,8 +23,9 @@
{{- erroridf "missing-cite" "No cite found for citation '%q' (%q)" .Params.title .Page.File.Path -}} {{- erroridf "missing-cite" "No cite found for citation '%q' (%q)" .Params.title .Page.File.Path -}}
{{- end -}} {{- end -}}
<!-- Rendering --> <!-- Rendering -->
{{- $citationId := printf "%s%s" ( cond ( isset .Params "cite" ) .Params.cite .Params.title ) ( default "" .Params.shortTitle ) -}} {{- $citationId := printf "%s%s" ( cond ( isset .Params "cite" ) .Params.cite .Params.title ) ( default "" .Params.shortTitle ) -}}
{{- partialCached "cite.html" . $citationId -}} {{- partial "cite.html" ( dict "sc" $ "pc" . "Params" .Params ) -}}

View file

@ -21,7 +21,7 @@
itemprop="image"> itemprop="image">
<img itemprop="thumbnail" <img itemprop="thumbnail"
class="u-photo gallery__thumbnail" class="u-photo gallery__thumbnail"
{{ if ( or ( eq .MediaType.SubType "svg" ) ( eq .MediaType.SubType "gif" ) ) }} {{ if ( in ( slice "avif" "svg" "gif" ) .MediaType.SubType ) }}
src="{{ .RelPermalink }}" src="{{ .RelPermalink }}"
{{ else }} {{ else }}
{{ with .Resize "600x webp" }} {{ with .Resize "600x webp" }}