Skip to content

Instantly share code, notes, and snippets.

@VisruthSK
Created May 9, 2025 01:05
Show Gist options
  • Select an option

  • Save VisruthSK/2e3bc7ab48b8d72aa0f782ecfc266768 to your computer and use it in GitHub Desktop.

Select an option

Save VisruthSK/2e3bc7ab48b8d72aa0f782ecfc266768 to your computer and use it in GitHub Desktop.
<!-- Built off of https://lucidmanager.org/productivity/hugo-bibliography/ -->
<!-- https://creativecommons.org/licenses/by-sa/4.0/ -->
{{- $bib_key := .Page.Params.bib -}}
{{- $all_bib_data_from_site := slice -}}
{{- $data_found_and_valid := false -}}
{{- $error_message := "" -}}
{{- if $bib_key -}}
{{- if isset site.Data $bib_key -}}
{{- $retrieved_data := index site.Data $bib_key -}}
{{- if $retrieved_data -}}
{{- if or (eq (printf "%T" $retrieved_data) "[]interface {}") (eq (printf "%T" $retrieved_data) "[]map[string]interface{}") (reflect.IsSlice $retrieved_data) -}}
{{- $all_bib_data_from_site = $retrieved_data -}}
{{- $data_found_and_valid = true -}}
{{- else -}}
{{- $error_message = printf "Bibliography data for key '%s' in site.Data is not a list/array (type: %T)." $bib_key $retrieved_data -}}
{{- end -}}
{{- else -}}
{{- $error_message = printf "Bibliography data for key '%s' is empty after retrieval from site.Data." $bib_key -}}
{{- end -}}
{{- else -}}
{{- $error_message = printf "Bibliography data for key '%s' not found in site.Data. Ensure 'data/%s.[json|yaml|toml]' exists." $bib_key $bib_key -}}
{{- end -}}
{{- else -}}
{{- $error_message = "'bib' parameter not found in page front matter. This shortcode requires it to list cited works." -}}
{{- end -}}
{{- $cited_bib_for_display := slice -}}
{{- if $data_found_and_valid -}}
{{- $cited_ids_on_page := .Page.Scratch.Get "cited_bib_ids" | default (slice) -}}
{{- if gt (len $cited_ids_on_page) 0 -}}
{{- range $item := $all_bib_data_from_site -}}
{{- if in $cited_ids_on_page $item.id -}}
{{- $cited_bib_for_display = $cited_bib_for_display | append $item -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- if $bib_key }} {{/* Only attempt to render bibliography if a bib_key was defined */}}
{{- if $error_message }}
<div class="has-background-danger has-text-white" style="padding: 1em; border-radius: 4px;">
<strong>Bibliography Error:</strong> {{ $error_message }}
</div>
{{- else if not $data_found_and_valid -}}
{{/* This case implies bib_key was set, but data wasn't valid or found, and $error_message might not have been set by initial checks.
The previous error_message block should ideally catch this.
This could be a fallback or removed if the above error handling is comprehensive. */}}
<div class="has-background-warning has-text-black" style="padding: 1em; border-radius: 4px;">
<strong>Bibliography Notice:</strong> Could not load bibliography data for '{{ $bib_key }}'.
</div>
{{- else -}} {{/* Data is valid, proceed to display filtered list or appropriate message */}}
<h2>Bibliography</h2>
<div class="box">
{{- if eq (len $cited_bib_for_display) 0 -}}
{{- $cited_ids_on_page_check := .Page.Scratch.Get "cited_bib_ids" | default (slice) -}}
{{- if eq (len $cited_ids_on_page_check) 0 -}}
<p>No items have been cited on this page.</p>
{{- else -}}
<p>The items cited on this page were not found in the bibliography data for '{{ $bib_key }}'. Please check citation IDs and the content of 'data/{{$bib_key}}.[json|yaml|toml]'.</p>
{{- end -}}
{{- else -}}
{{- range $currentItem := $cited_bib_for_display -}}
<p>
{{- if or $currentItem.author $currentItem.editor -}}
{{- if $currentItem.author -}}
{{- $n_auth := len $currentItem.author -}}
{{- if le $n_auth 2 -}}
{{- range $index_auth, $auth := (first 2 $currentItem.author) -}}
{{- with $auth }}{{- index . "non-dropping-particle" | default "" }}{{- .family | default "N/A" }}, {{ .given | default "N/A" }}{{ end -}}
{{- if and (gt $n_auth 1) (eq $index_auth 0) }} and {{ end -}}
{{- end -}}
{{- else -}}
{{- with (first 1 $currentItem.author) }}{{- range . }}{{- index . "non-dropping-particle" | default "" }}{{- .family | default "N/A" }}, {{ .given | default "N/A" }}{{- end }}{{ end }} et al.
{{- end -}}
{{- else if $currentItem.editor -}}
{{- $n_ed := len $currentItem.editor -}}
{{- if le $n_ed 2 -}}
{{- range $index_ed, $ed := (first 2 $currentItem.editor) -}}
{{- with $ed }}{{- index . "non-dropping-particle" | default "" }}{{- .family | default "N/A" }}{{ end -}}
{{- if and (gt $n_ed 1) (eq $index_ed 0) }} and {{ end -}}
{{- end -}}
{{- else -}}
{{- with (first 1 $currentItem.editor) }}{{- range . }}{{- index . "non-dropping-particle" | default "" }} {{- .family | default "N/A" }}{{- end }}{{ end }} et al.
{{- end }}
(Ed{{- if gt $n_ed 1 }}s{{- end }}.)
{{- end -}}
{{- else -}}
Anonymous
{{- end -}}
{{- with $currentItem.issued -}}
{{- with index . "date-parts" -}}
{{- with index . 0 -}}
{{- with index . 0 -}}
({{- . -}})
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}.
{{- with $currentItem.title -}}
{{- if or (eq $currentItem.type "book") (eq $currentItem.type "thesis") -}}
<em>{{ . | title }}</em>
{{- else -}}
{{ . }}
{{- end -}}
{{- if ne (substr . -1) "?" }}.{{ end }}
{{- end -}}
{{- if or (eq $currentItem.type "article") (eq $currentItem.type "article-journal") -}}
{{- with index $currentItem "container-title" -}}<i>{{ . | title }}</i>{{ end -}},
{{- if $currentItem.volume }} {{ $currentItem.volume }}{{ end -}}
{{- if $currentItem.issue }}({{ $currentItem.issue }}){{ end -}}
{{- if $currentItem.page }}, {{ replace $currentItem.page "-" "–" }}{{ end -}}.
{{- end -}}
{{- if eq $currentItem.type "book" -}}
{{- with $currentItem.publisher }} {{ . }}.{{ end -}}
{{- end -}}
{{- if eq $currentItem.type "chapter" -}}
In:
{{- if $currentItem.editor -}}
{{- $n_ed_chap := len $currentItem.editor -}}
{{- if le $n_ed_chap 2 -}}
{{- range $index_ed_chap, $ed_chap := (first 2 $currentItem.editor) -}}
{{- with $ed_chap }}{{- index . "non-dropping-particle" | default "" }}{{- .family | default "N/A" }}{{ end -}}
{{- if and (gt $n_ed_chap 1) (eq $index_ed_chap 0) }} and {{ end -}}
{{- end -}}
{{- else -}}
{{- with (first 1 $currentItem.editor) }}{{- range . }}{{- index . "non-dropping-particle" | default "" }} {{- .family | default "N/A" }}{{- end }}{{ end }} et al.
{{- end }}
(Ed{{- if gt $n_ed_chap 1 }}s{{- end }}.)
{{- end }},
{{- with index $currentItem "container-title" }} <i>{{ . | title }}</i>{{ end -}}
{{- $place := index $currentItem "publisher-place" -}}
{{- if or $currentItem.publisher $place }} ({{ end -}}
{{- with $currentItem.publisher }}{{ . }}{{ end -}}
{{- if and $currentItem.publisher $place }}, {{ end -}}
{{- with $place }}{{ . }}{{ end -}}
{{- if or $currentItem.publisher $place }}){{ end -}}.
{{- end -}}
{{- if eq $currentItem.type "thesis" -}}
{{- with $currentItem.genre }}{{ . }}. {{ end -}}{{- with $currentItem.publisher }}{{ . }}.{{ end -}}
{{- end -}}
{{- if eq $currentItem.type "paper-conference" -}}
In: {{ with index $currentItem "container-title" }}<em>{{ . | title }}</em>{{ end -}}
{{- with index $currentItem "publisher-place" }}, {{ . }}{{ end -}}.
{{- end -}}
{{- if eq $currentItem.type "report" -}}
{{- with $currentItem.title }}{{ . }}{{ end -}},
{{- if $currentItem.number }} (Report No. {{ $currentItem.number }}).{{ end -}}
{{- with $currentItem.publisher }} {{ . }}.{{ end -}}
{{- end -}}
{{- if $currentItem.DOI -}}
doi: <a href="https://doi.org/{{ $currentItem.DOI }}" title="{{ $currentItem.title | default " Link to DOI" }}"
target="_blank" rel="noopener">{{ $currentItem.DOI }}</a>.
{{- end -}}
{{- if and $currentItem.URL (not $currentItem.DOI) -}}
<a href="{{ $currentItem.URL }}" title="{{ $currentItem.title | default " Link to URL" }}" target="_blank" rel="noopener">{{ $currentItem.URL }}</a>.
{{- end -}}
</p>
{{- end -}}
{{- end -}}
</div>
{{- end -}}
{{- end -}}
{{- $first_param := .Get 0 -}}
{{- $second_param := .Get 1 -}}
{{- $id := "" -}}
{{- $tp := "" -}}
{{- if $first_param -}}
{{- if and (in (slice "p" "t" "f") (lower $first_param)) $second_param -}}
{{- $tp = lower $first_param -}}
{{- $id = $second_param -}}
{{- else -}}
{{- $tp = "p" -}}
{{- $id = $first_param -}}
{{- end -}}
{{- end -}}
{{- $bib_key := .Page.Params.bib -}}
{{- $error_message := "" -}}
{{- $entry_data_list := slice -}}
{{- $data_loaded_and_entry_found := false -}}
{{- $bib_data_from_site_data := false }}
{{- if not $bib_key -}}
{{- $error_message = "'bib' parameter not found in page front matter. Please set 'bib: your_bib_file_key' in the page's front matter." -}}
{{- else if not $id -}}
{{- $error_message = "Citation ID not provided or could not be determined. Usage: {{< cite \"ID\" >}} or {{< cite \"type\" \"ID\" >}}" -}}
{{- else -}}
{{- if isset site.Data $bib_key -}}
{{- $bib_data_from_site_data = index site.Data $bib_key -}}
{{- if not $bib_data_from_site_data -}}
{{- $error_message = printf "Bibliography data for key '%s' is empty after retrieval from site.Data. Check 'data/%s.json' (or .yaml/.toml)." $bib_key $bib_key -}}
{{- else -}}
{{- $bib_data_for_query := slice -}}
{{- if or (eq (printf "%T" $bib_data_from_site_data) "[]interface {}") (eq (printf "%T" $bib_data_from_site_data) "[]map[string]interface{}") (reflect.IsSlice $bib_data_from_site_data) -}}
{{- $bib_data_for_query = $bib_data_from_site_data -}}
{{- else -}}
{{- $error_message = printf "Bibliography data for key '%s' in site.Data is not a list/array (type: %T). It should be a JSON/YAML/TOML array of citation objects in 'data/%s.[json|yaml|toml]'." $bib_key $bib_data_from_site_data $bib_key -}}
{{- end -}}
{{- if not $error_message -}}
{{- $entry_data_list = where $bib_data_for_query "id" $id -}}
{{- if eq (len $entry_data_list) 0 -}}
{{- $error_message = printf "Citation with ID '%s' not found in bibliography data for key '%s' (from site.Data)." $id $bib_key -}}
{{- else if gt (len $entry_data_list) 1 -}}
{{- $error_message = printf "Multiple entries found for ID '%s' in bibliography data for key '%s' (from site.Data). IDs should be unique." $id $bib_key -}}
{{- else -}}
{{- $data_loaded_and_entry_found = true -}}
{{- if not (.Page.Scratch.Get "cited_bib_ids") -}}
{{- .Page.Scratch.Set "cited_bib_ids" (slice) -}}
{{- end -}}
{{- $cited_ids_slice := .Page.Scratch.Get "cited_bib_ids" -}}
{{- if not (in $cited_ids_slice $id) -}}
{{- .Page.Scratch.Set "cited_bib_ids" ($cited_ids_slice | append $id) -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- else -}}
{{- $error_message = printf "Bibliography data for key '%s' not found in site.Data. Ensure 'data/%s.json' (or .yaml/.toml) exists and is named correctly." $bib_key $bib_key -}}
{{- end -}}
{{- end -}}
{{- if $error_message -}}
<span class="has-background-danger has-text-white" style="padding: 0.25em 0.5em; border-radius: 3px;">Cite Error: {{ $error_message }}</span>
{{- else if $data_loaded_and_entry_found -}}
{{- $currentItem := index $entry_data_list 0 -}}
{{- with $currentItem -}}
{{- $authors_editors_short := "" -}}
{{- $authors_editors_full := "" -}}
{{- $year_output := "" -}}
{{- with .issued -}}
{{- with index . "date-parts" -}}
{{- with index . 0 -}}
{{- with index . 0 -}}
{{- $year_output = . -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- if not $year_output }}{{ $year_output = "n.d." }}{{ end -}}
{{- $is_editor := false -}}
{{- $creators := .author -}}
{{- if not $creators }}{{ $creators = .editor }}{{ $is_editor = true }}{{ end -}}
{{- if $creators -}}
{{- $n_creators := len $creators -}}
{{- $short_names_list := slice -}}
{{- $full_names_list := slice -}}
{{- range $index, $creator := (first 3 $creators) -}}
{{- $particle := index $creator "non-dropping-particle" | default "" -}}
{{- $family := $creator.family | default "N/A" -}}
{{- $given := $creator.given | default "N/A" -}}
{{- $p_space := cond (ne $particle "") (printf "%s " $particle) "" -}}
{{- $short_names_list = $short_names_list | append (printf "%s%s" $p_space $family) -}}
{{- $full_names_list = $full_names_list | append (printf "%s%s, %s" $p_space $family $given) -}}
{{- end -}}
{{- if eq $n_creators 1 -}}
{{- $authors_editors_short = index $short_names_list 0 -}}
{{- $authors_editors_full = index $full_names_list 0 -}}
{{- else if eq $n_creators 2 -}}
{{- $authors_editors_short = printf "%s and %s" (index $short_names_list 0) (index $short_names_list 1) -}}
{{- if eq $tp "p" -}}
{{- $authors_editors_short = printf "%s & %s" (index $short_names_list 0) (index $short_names_list 1) -}}
{{- end -}}
{{- $authors_editors_full = delimit $full_names_list " and " -}}
{{- else -}}
{{- $authors_editors_short = printf "%s et al." (index $short_names_list 0) -}}
{{- with (first 1 $creators) }}{{- range . -}}
{{- $particle := index . "non-dropping-particle" | default "" -}}
{{- $p_space := cond (ne $particle "") (printf "%s " $particle) "" -}}
{{- $authors_editors_full = printf "%s%s, %s et al." $p_space (.family | default "N/A") (.given | default "N/A") -}}
{{- end -}}{{- end -}}
{{- end -}}
{{- if $is_editor -}}
{{- $ed_suffix := printf " (Ed%s.)" (cond (gt $n_creators 1) "s" "") -}}
{{- $authors_editors_short = printf "%s%s" $authors_editors_short $ed_suffix -}}
{{- $authors_editors_full = printf "%s%s" $authors_editors_full $ed_suffix -}}
{{- end -}}
{{- else -}}
{{- $authors_editors_short = "Anonymous" -}}
{{- $authors_editors_full = "Anonymous" -}}
{{- end -}}
{{- if eq $tp "p" -}}
({{- $authors_editors_short }}, {{ $year_output -}})
{{- else if eq $tp "t" -}}
{{- $authors_editors_short }} ({{ $year_output }})
{{- else if eq $tp "f" -}}
<span class="citation-entry">
{{- $authors_editors_full -}}
{{- with $year_output }} ({{ . }}){{ end -}}.
{{- with .title -}}
&nbsp;{{- if or (eq $.type "book") (eq $.type "thesis") -}}
<em>{{ . | title }}</em>
{{- else -}}
{{ . | chomp -}}
{{- end -}}
{{- if ne (substr . -1) "?" }}{{ if ne (substr . -1) "." }}.{{ end }}{{ end }}&nbsp;
{{- else -}}
&nbsp;
{{- end -}}
{{- if or (eq .type "article") (eq .type "article-journal") -}}
{{- with index . "container-title" -}}<i>{{ . | title }}</i>{{ end -}},
{{- with .volume }} {{ . }}{{ end -}}
{{- with .issue }}({{ . }}){{ end -}}
{{- with .page }},&nbsp;{{ replace . "-" "–" | chomp }}{{ end -}}.
{{- end -}}
{{- if eq .type "book" -}}
{{- with .publisher }} {{ . | chomp }}.{{ end -}}
{{- end -}}
{{- if eq .type "chapter" -}}
In:
{{- if .editor -}}
{{- $chap_eds := .editor -}}
{{- $n_chap_eds := len $chap_eds -}}
{{- $chap_ed_names := slice -}}
{{- range $idx, $ed := (first 2 $chap_eds) -}}
{{- $particle := index $ed "non-dropping-particle" | default "" -}}
{{- $p_space := cond (ne $particle "") (printf "%s " $particle) "" -}}
{{- $chap_ed_names = $chap_ed_names | append (printf "%s%s, %s" $p_space ($ed.family | default "N/A") ($ed.given | default "N/A")) -}}
{{- end -}}
{{- if le $n_chap_eds 2 -}}
{{- delimit $chap_ed_names " and " -}}
{{- else -}}
{{- with (first 1 $chap_eds) }}{{- range . -}}
{{- $particle := index . "non-dropping-particle" | default "" -}}
{{- $p_space := cond (ne $particle "") (printf "%s " $particle) "" -}}
{{- printf "%s%s, %s" $p_space (.family | default "N/A") (.given | default "N/A") -}}
{{- end -}}{{- end }} et al.
{{- end }}
(Ed{{- if gt $n_chap_eds 1 }}s{{- end }}.),
{{- end }}&nbsp;
{{- with index . "container-title" }}<i>{{ . | title }}</i>{{ end -}}
{{- $place := index . "publisher-place" | default "" -}}
{{- $pub := .publisher | default "" -}}
{{- if or $pub $place }} ({{ end -}}
{{- with $pub }}{{ . | chomp }}{{ end -}}
{{- if and $pub $place }}, {{ end -}}
{{- with $place }}{{ . | chomp }}{{ end -}}
{{- if or $pub $place }}){{ end -}}.
{{- end -}}
{{- if eq .type "thesis" -}}
{{- with .genre }} {{ . | chomp }}.{{ end -}}&nbsp;{{- with .publisher }}{{ . | chomp }}.{{ end -}}
{{- end -}}
{{- if eq .type "paper-conference" -}}
In: {{ with index . "container-title" }}<em>{{ . | title }}</em>{{ end -}}
{{- with index . "publisher-place" }}, {{ . | chomp }}{{ end -}}.
{{- end -}}
{{- if eq .type "report" -}}
{{- with .number }} (Report No. {{ . | chomp }}).{{ end -}}
{{- with .publisher }} {{ . | chomp }}.{{ end -}}
{{- end -}}
{{- if .DOI -}}
&nbsp;doi: <a href="https://doi.org/{{ .DOI }}" title="{{ $.title | default (printf "Link to DOI %s" .DOI) }}" target="_blank" rel="noopener">{{ .DOI }}</a>.
{{- end -}}
{{- if and .URL (not .DOI) -}}
&nbsp;<a href="{{ .URL }}" title="{{ $.title | default (printf "Link to %s" .URL) }}" target="_blank" rel="noopener">{{ .URL }}</a>.
{{- end -}}
</span>
{{- else -}}
<span class="has-background-warning has-text-black" style="padding: 0.25em 0.5em; border-radius: 3px;">Cite Error: Invalid type '{{ $tp | plainify }}'. Use 'p', 't', or 'f'.</span>
{{- end -}}
{{- end -}}
{{- else if not $error_message -}}
<span class="has-background-danger has-text-white" style="padding: 0.25em 0.5em; border-radius: 3px;">Cite Error: Unknown issue, or ID '{{ $id | plainify }}' not found and no specific error was set.</span>
{{- end -}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment