diff --git a/scripts/ref b/scripts/ref new file mode 100755 index 0000000..bdaa471 --- /dev/null +++ b/scripts/ref @@ -0,0 +1,41 @@ +#!/bin/sh + +# Generate a CSL bibliography entry from a DOI or ISBN + +if test "${1#*10.}" != "$1"; then + # DOI + curl -sLH 'Accept: application/x-bibtex' "https://doi.org/$1" \ + | pandoc -f bibtex -t markdown -s +else + # ISBN + oclc=$(curl -sI "https://www.worldcat.org/isbn/$1" \ + | sed -nE '/^Location:/ {s@.*oclc/([0-9]+).*@\1@p; q}') + + curl -s "https://openlibrary.org/api/volumes/brief/isbn/$1.json" \ + | jq --arg oclc "$oclc" ' + def splitname: + split(" ") | {family: .[-1], given: .[0:-1] | map (.[0:1] + ".")| join(" ")}; + [.records | .[]] | .[0] as $reply + | ($reply.data + $reply.details) as $data + | [{author: $data.authors | map(.name | splitname), + isbn: $data.identifiers.isbn_10[0], + edition: $data.details.edition_name | split(" ") | .[0] | sub("[a-z]+"; ""), + issued: $data.publish_date, + publisher: $data.details.publishers[0], + "publisher-place": $data.details.publish_places[0], + type: "book", + title: $data.title, + url: ("https://www.worldcat.org/oclc/" + $oclc)}]' \ + | pandoc -f csljson -t markdown -s +fi | sed ' +# delete start of header +1,3d +# insert the reference id +/^- / {i - id: CHANGE_ME +s/- / / +} +# unquote the url +/url:/ s/"//g +# delete end of header +/^---/d +$d'