Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ The above shortcode will now only render entries of the bibliography that includ

The value does not have to be an exact match to the key so long as the value is a substring of the key.

This filtering also works to sepcify specific colections or subcollections to generate a bibliography for.
This filtering also works to specify specific collections or subcollections to generate a bibliography for.

#### Cite

Expand Down
115 changes: 96 additions & 19 deletions layouts/shortcodes/bibliography.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
}
</style>


{{/* fetch */}}

{{- $citations := slice -}}

{{- $context := . }}
Expand All @@ -15,8 +18,30 @@
{{- $authorParam := $context.Get "authorNum" | default "3" }}
{{- $authorNum := int $authorParam }}

{{ $groupId := site.Params.groupId }}
{{ $scratch := newScratch }}
{{ $scratch.Set "items" slice }}
{{ $limit := 100 }}

{{ $url := printf "https://api.zotero.org/groups/%d/" $groupId }}
{{ $resource := resources.GetRemote $url | transform.Unmarshal }}
{{ $numItems := $resource.meta.numItems }}
{{ $numPages := math.Ceil (div $numItems $limit) }}

{{ range seq 0 $numPages }}
{{ $start := mul . $limit }}
{{ $url := printf "https://api.zotero.org/groups/%d/items?start=%d&limit=%d&format=json" $groupId $start $limit }}
{{ $batch := resources.GetRemote $url | transform.Unmarshal }}
{{ if $batch }}
{{ $scratch.Add "items" $batch }}
{{ end }}
{{ end }}

{{- range $entry := site.Data.bibliography }}
{{ $items := $scratch.Get "items" }}
{{ $data := $items }}

{{ range $data }}
{{ $entry := .data }}

<!-- determines if entry should be included based on filters, checks if value is anywhere in the key -->
{{- $include := true }}
Expand Down Expand Up @@ -46,11 +71,10 @@
{{- end }}
{{- end }}
{{- end }}

<!-- adds entry to citations if it should be included based on filters and in-text citations -->
{{- if and $include (or (not $onlyCited) (in $citedTitles $entry.title)) }}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's hard for me to follow the filter logic above this, but when I disable the include check I get citations displaying. Without it, none of them display for me (even when I am not passing filters to the shortcode).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey Rebeecca, I'm trying to make the filter logic more readable right now, but it was working correctly for me, what exactly led to none of the citations displaying?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was testing on my personal site, where I started trying to integrate hugo-biblography - I wanted to do something like a customized version of the 'my publications' that zotero lets you set up on your personal profile.

However, in hindsight, that isn't the best place to test since I haven't gotten it fully working. What do you recommend for testing? Should I use the dh-tech site and switch to this branch for testing?

{{- $authors := slice }}
{{- range $entry.creators }}
{{- range $entry.creators }}
{{- if eq .creatorType "author" }}
{{- $firstInitial := "" }}
{{- if .firstName }}
Expand All @@ -74,32 +98,85 @@
{{- $citation := dict "authorStr" $authorStr "citation" $entry -}}
{{- $citations = $citations | append $citation }}
{{- end }}
{{- end }}
{{ end }}

{{$sortedCitations := sort $citations "authorStr" }}
{{$collections := site.Data.collections }}

{{ $endpoint := printf "https://api.zotero.org/groups/%d/collections?start=0&limit=100&format=json" $groupId }}
{{ $remote := resources.GetRemote $endpoint | transform.Unmarshal }}

{{- $singleCollection := false }}
{{- if $hasFilters }}
{{- range $key, $val := $context.Params }}
{{- if eq $key "collections" }}
{{- $singleCollection = true }}
{{ $collections := $remote }}
{{- $singleCollection := false }}
{{- if $hasFilters }}
{{- range $key, $val := $context.Params }}
{{- if eq $key "collections" }}
{{- $singleCollection = true }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}



<ul style="list-style-type: none;" class="apa-citation-list">
{{- range $citation := $sortedCitations }}
{{/* Build collectionMap to easily look up collections by key */}}
{{ $collectionMap := dict }}
{{ range $collections }}
{{ $k := .data.key }}
{{ $obj := merge .data (dict "children" (slice)) }}
{{ $collectionMap = merge $collectionMap (dict $k $obj) }}
{{ end }}

{{/* Build childKeysMap (parentKey => [childKey,...]) */}}
{{ $childKeysMap := dict }}
{{ range $collections }}
{{ $childKey := .data.key }}
{{ $parentKey := .data.parentCollection }}

{{/*treat boolean false and "false" and "" and nil as no-parent */}}
{{ $hasParent := and $parentKey (ne $parentKey false) (ne $parentKey "false") (ne $parentKey "") }}

{{ if $hasParent }}
{{ $existing := index $childKeysMap $parentKey | default (slice) }}
{{ $existing = $existing | append $childKey }}
{{ $childKeysMap = merge $childKeysMap (dict $parentKey $existing) }}
{{ end }}
{{ end }}

{{/* Attach children to parents */}}
{{ range $parentKey, $childKeySlice := $childKeysMap }}
{{ $parent := index $collectionMap $parentKey }}
{{ if $parent }}
{{ $currentChild := $parent.children | default (slice) }}
{{ range $childKeySlice }}
{{ $childObj := index $collectionMap . }}
{{ if $childObj }}
{{ $currentChild = $currentChild| append $childObj }}
{{ end }}
{{ end }}
{{ $updatedParent := merge $parent (dict "children" $currentChild) }}
{{ $collectionMap = merge $collectionMap (dict $parentKey $updatedParent) }}
{{ end }}
{{ end }}

{{/* Compute roots array we can iterate over for rendering */}}
{{ $roots := slice }}
{{ range $k, $obj := $collectionMap }}
{{ $parentKey := $obj.parentCollection }}
{{ $hasParent := and $parentKey (ne $parentKey false) (ne $parentKey "false") (ne $parentKey "") }}
{{ if not $hasParent }}
{{ $roots = $roots | append $obj }}
{{ end }}
{{ end }}

{{ $sortedCollections := sort $roots "name" }}

<ul style="list-style-type: none;" class="apa-citation-list">
{{- range $citation := $sortedCitations }}
{{- $collections := $citation.citation.collections | default (slice) }}
{{- if eq (len $collections) 0 }}
{{- partial "render-citation.html" . }}
{{- partial "render-citation.html" . }}
{{- end }}
{{- end }}
{{- end }}

{{- range $collections }}
{{- range $sortedCollections }}
{{ partial "render-collection.html" (dict "collection" . "citations" $sortedCitations "singleCollection" $singleCollection) }}
{{- end }}
</ul>
{{- end }}
</ul>
29 changes: 18 additions & 11 deletions layouts/shortcodes/cite.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@

{{- $.Page.Scratch.Add "cited_titles" $title -}}

{{- range site.Data.bibliography }}
{{- if eq .itemType "journalArticle"}}
{{- if eq .title $title }}

{{ $groupId := site.Params.groupId }}
{{ $endpoint := printf "https://api.zotero.org/groups/%d/items?start=0&limit=100&format=json" $groupId }}

{{ $remote := resources.GetRemote $endpoint }}
{{ with $remote }}
{{ $data := .Content | transform.Unmarshal }}
{{- range $data }}
{{$entry := .data}}

{{- if eq $entry.title $title }}
<p>
{{- $authors := slice -}}
{{- range .creators }}
{{- range $entry.creators }}

{{- if eq .creatorType "author" }}
{{- if .lastName }}
{{- $name := printf "%s" .lastName}}
{{- if eq $entry.creatorType "author" }}
{{- if $entry.lastName }}
{{- $name := printf "%s" $entry.lastName}}
{{- $authors = $authors | append $name }}
{{- end }}
{{- end }}
Expand All @@ -22,8 +30,7 @@
{{- $authorStr = (index (split (index $authors 0) ",") 0 ) -}}
{{- end}}
{{- if eq (len $authors) 2 }}
{{ $authorStr = printf "%s & %s " (index (split (index $authors 0) ",") 0) (index (split (index $authors 1) ",") 0)
}}
{{ $authorStr = printf "%s & %s " (index (split (index $authors 0) ",") 0) (index (split (index $authors 1) ",") 0)}}
{{- end}}
{{- if eq (len $authors) 3 }}
{{$authorStr = printf "%s et al." (index (split (index $authors 0) ",") 0 )}}
Expand All @@ -32,8 +39,8 @@
{{$authorStr = printf "%s et al." (index (split (index $authors 0) ",") 0 )}}
{{- end}}

({{$authorStr}}, {{ .date }})
({{$authorStr}}, {{ $entry.date }})
</p>
{{- end}}
{{- end}}
{{- end }}
{{- end}}