Skip to content

Instantly share code, notes, and snippets.

@nichtich
Last active July 30, 2023 19:30
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nichtich/97f06bbfa249f5d33e2d to your computer and use it in GitHub Desktop.
Save nichtich/97f06bbfa249f5d33e2d to your computer and use it in GitHub Desktop.
Get CSL-Data for a given DOI
@article{Williams_2010,
doi = {10.1371/journal.pone.0010676},
url = {http://dx.doi.org/10.1371/journal.pone.0010676},
year = 2010,
month = {may},
publisher = {Public Library of Science ({PLoS})},
volume = {5},
number = {5},
pages = {e10676},
author = {Jeffrey T. Williams and Kent E. Carpenter and James L. Van Tassell and Paul Hoetjes and Wes Toller and Peter Etnoyer and Michael Smith},
editor = {Brian Gratwicke},
title = {Biodiversity Assessment of the Fishes of Saba Bank Atoll, Netherlands Antilles},
journal = {{PLoS} {ONE}}
}
[
{
"DOI": "10.1371/journal.pone.0010676",
"URL": "http://dx.doi.org/10.1371/journal.pone.0010676",
"author": [
{
"family": "Williams",
"given": "Jeffrey T."
},
{
"family": "Carpenter",
"given": "Kent E."
},
{
"family": "Tassell",
"given": "James L. Van"
},
{
"family": "Hoetjes",
"given": "Paul"
},
{
"family": "Toller",
"given": "Wes"
},
{
"family": "Etnoyer",
"given": "Peter"
},
{
"family": "Smith",
"given": "Michael"
}
],
"container-title": "PLoS ONE",
"editor": [
{
"family": "Gratwicke",
"given": "Brian"
}
],
"id": "Williams_2010",
"issue": "5",
"issued": {
"date-parts": [
[
2010,
5
]
]
},
"page": "e10676",
"publisher": "Public Library of Science (PLoS)",
"title": "Biodiversity Assessment of the Fishes of Saba Bank Atoll, Netherlands Antilles",
"type": "article-journal",
"volume": "5"
}
]

This uses BibTeX as intermediary format. The full XML format provided by CrossRef may contain more information, but a mapping to MODS or directly to CSL-Data must be defined. Conversion of BibTeX to CSL-Data is done by pandoc-citeproc

#!/bin/bash
DOI=10.1371/journal.pone.0010676
curl -H accept:application/x-bibtex http://data.crossref.org/$DOI > bibtex.bib &&
pandoc-citeproc --bib2json bibtex.bib > csl-data.json

To create a citation from CSL-Data, use any CSL-Processor. This is how I do it. First I create a YAML file with Catmandu:

catmandu convert JSON --multiline 1 to YAML \
    --fix 'prepend(id,@);copy_field(id,nocite);retain_field(nocite)' \
    < csl-data.json > pandoc-yaml.yml
echo ... >> pandoc-yaml.yml # unless https://github.com/LibreCat/Catmandu/issues/110 is fixed

Sure it's easier to create the file by hand, but then it's not automatic.

$ cat pandoc-yaml.yml
---
nocite: "@Williams_2010"
...

Then convert with pandoc to any format you like (HTML, ODT, LaTeX, plain text...)

pandoc --bibliography csl-data.json pandoc-yaml.yml -t plain
pandoc --bibliography csl-data.json pandoc-yaml.yml -t html
pandoc --bibliography csl-data.json pandoc-yaml.yml -t latex
...

The result in HTML:

Williams, Jeffrey T., Kent E. Carpenter, James L. Van Tassell, Paul Hoetjes, Wes Toller, Peter Etnoyer, and Michael Smith. 2010. “Biodiversity Assessment of the Fishes of Saba Bank Atoll, Netherlands Antilles.” Edited by Brian Gratwicke. PLoS ONE 5 (5) (May): e10676. doi:10.1371/journal.pone.0010676. http://dx.doi.org/10.1371/journal.pone.0010676.

#!/bin/bash
DOI=10.1371/journal.pone.0010676
curl -H accept:application/x-bibtex http://data.crossref.org/$DOI > bibtex.bib && \
pandoc-citeproc --bib2json bibtex.bib > csl-data.json
---
nocite: '@Williams_2010'
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment