Parsing YAML-data with maps into strings

I’m trying to create a list of references from a preformatted YAML-file with a list of publications. I don’t want to alter the YAML-data itself, since it is what an automatic Zotero export creates.

Here’s a shortened version of the data (/data/publications.yaml), cut down to one example:

---
references:

- id: White2024
  abstract: >-
    This chapter traces the history of tabletop role-playing games (TRPGs). It
    treats Dungeons & Dragons (D&D) as a key textual expression of TRPG
    experience with which other designs and play communities are in dialogue but
    which itself changes over time and discusses the tension between D&D as
    commercial intellectual property on one hand and as a folk cultural play
    practice intertwined with popular media fandom on another. We examine the
    design responses to D&D and the articulation of tabletop RPG practices by
    players, and discuss the impact of new technologies on TRPG publishing,
    distribution, and engagement since the turn of the century. In doing so, we
    present TRPGs as an active cultural form, rather than merely an obsolescent
    predecessor form of digital games.
  author:
    - family: White
      given: William J.
    - family: Arjoranta
      given: Jonne
    - family: Hitchens
      given: Michael
    - family: Peterson
      given: Jon
    - family: Torner
      given: Evan
    - family: Walton
      given: Jonathan
  citation-key: White2024
  container-title: The Routledge Handbook of Role-Playing Game Studies
  editor:
    - family: Zagal
      given: José P.
    - family: Deterding
      given: Sebastian
  event-place: New York
  ISBN: 978-1-00-329804-5
  issued:
    - year: 2024
  number-of-pages: '24'
  page: 68-91
  publisher: Routledge
  publisher-place: New York
  title: Tabletop Role-Playing Games
  type: chapter
  URL: https://doi.org/10.4324/9781003298045-6
...

I tried creating a shortcode that I could include in a page to get a list of publications based on this data.

{{ $publications := sort .Site.Data.publications.references "citation-key" "desc" }}
<ul>
  {{ range $publications }}
  <li>{{ .author }}. {{ .issued }}. <a href="/repository/{{ .id }}.pdf">{{ .title }}</a>. {{ .publisher }}. </li>
  {{ end }}
<ul>

This correctly parses the data into:

[map[family:White given:William J.] map[family:Arjoranta given:Jonne] map[family:Hitchens given:Michael] map[family:Peterson given:Jon] map[family:Torner given:Evan] map[family:Walton given:Jonathan]]. [map[year:2024]]. [Tabletop Role-Playing Games](http://localhost:1313/repository/White2024.pdf). Routledge.

I’m struggling with getting the maps parsed correctly into strings. I tried ranging over them in different ways, but can’t seem to find a way that would work. Can anyone help me figure out what I’m doing wrong?

{{ $publications := sort .Site.Data.publications.references "citation-key" "desc" }}
<ul>
  {{ range $publications }}
    <li>
      <a href="/repository/{{ .id }}.pdf">{{ .title }}</a>. {{ .publisher }}.
    </li>
    {{ with .author }}
      <li>Author:
        <ul>
          {{ range . }}
            <li>{{ printf "%s %s" .given .family }}</li>
          {{ end }}
        </ul>
      </li>
    {{ end }}
    {{ with .issued }}
      <li>Issued:
        <ul>
          {{ range . }}
            <li>{{ .year }}</li>
          {{ end }}
        </ul>
      </li>
    {{ end }}
  {{ end }}
</ul>

Renders something like this:

Excellent, thank you for solving this problem for me!

Could you still clarify one problem? At the beginning I’m sorting the data based on citation-key, but I originally chose to do that only because I couldn’t figure out how to sort based on a nested value, like year.

Is there a way to sort the list based on year?

“issued” is an array; it can have zero or more values. I suppose you’d want to use the earliest.

I’d range through the data and build a separate array of maps, where each map has two keys: id and first_published (or something). Then sort that array, and range through it to get the id.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.