tgetref - scholarref - tools for DOI and BiBTeX reference extraction, fetching, and parsing
 (HTM) git clone git://src.adamsgaard.dk/scholarref
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       tgetref (3775B)
       ---
            1 #!/bin/sh
            2 set -e
            3 
            4 version=1.0
            5 host="https://doi.org"
            6 
            7 show_help() {
            8         echo "usage: ${0##*/} [OPTIONS] [DOIs ...]"
            9         echo "will attempt to get a BibTeX citation from $host"
           10         echo "If no DOIs are specified, this program will expect DOIs as stdin."
           11         echo
           12         echo "OPTIONS are one or more of the following:"
           13         echo "   -h, --help          show this message"
           14         echo "   -v, --version       show version and license information"
           15         echo "   -V, --verbose       show verbose information"
           16         echo "   -t, --tor-socks     use torsocks for HTTP requests"
           17         echo "   -j, --full-journal  return full journal name in citation"
           18         echo "   -a, --full-author   do not shorten author names"
           19         echo "   -n, --no-newline    suppress trailing newline but prepend with newline"
           20         echo "   -N, --notify        send desktop notification when complete"
           21         echo "   --                  do not consider any following args as options"
           22 }
           23 
           24 show_version() {
           25         echo "${0##*/} version $version"
           26         echo "Licensed under the ISC License"
           27         echo "written by Anders Damsgaard, anders@adamsgaard.dk"
           28         echo "https://src.adamsgaard.dk/scholarref"
           29 }
           30 
           31 die() {
           32         printf '%s\n' "$1" >&2
           33         exit 1
           34 }
           35 
           36 format_bibtex_key() {
           37         sed '/@/ s/_\([0-9]\)/\1/'
           38 }
           39 
           40 abbreviate_journal_name() {
           41         sed '/journal = / {
           42         s/Journal/J./
           43         s/Geophysical/Geophys./
           44         s/Research/Res./
           45         s/Geophysical/Geophys./
           46         s/Geophysics/Geophys./
           47         s/Research/Res./
           48         s/Letters/Lett./
           49         s/Mechanics/Mech./
           50         s/Glaciology/Glaciol./
           51         s/Proceedings/Proc./
           52         s/Royal/R./
           53         s/Society/Soc./
           54         s/Annals/Ann./
           55         s/Resources/Resour./
           56         s/Surface/Surf./
           57         s/Processes/Proc./
           58         s/National/Nat./
           59         s/Computers/Comput./
           60         s/Geotechnics/Geotech./
           61         s/Academy/Acad./
           62         s/Sciences/Sci./
           63         s/Review/Rev./
           64         s/Quaternary/Quat./
           65         s/Physical/Phys./
           66         s/Planetary/Planet./
           67         s/Quarterly/Q./
           68         s/Geological/Geol./
           69         s/Statistical/Stat./
           70         s/Applied/Appl./
           71         s/Physics/Phys./
           72         s/Communications/Commun./
           73         s/Geoscience/Geosci./
           74         s/Landforms/Land./
           75         s/Science/Sci./
           76         s/Annual/Ann./
           77         s/International/Int./
           78         s/Numerical/Numer./
           79         s/Methods/Meth./
           80         s/Geomechanics/Geomech./
           81         s/Analytical/Anal./
           82         s/Advances/Adv./
           83         s/Modeling/Mod./
           84         s/Systems/Sys./
           85         s/ for / /
           86         s/ of / /
           87         s/ and / /
           88         s/ in / /
           89         }'
           90 }
           91 
           92 abbreviate_author_name() {
           93         sed -e '/author = /{' -e 's/\([A-Z]\)[^ ]* \([A-Z]\)/\1. \2/g' -e '}'
           94 }
           95 
           96 strip_doi() {
           97         sed 's/^(http:\/\/|https:\/\/)?(dx\.)?(doi\.org\/)//'
           98 }
           99 
          100 get_citation() {
          101         doi=$(echo "$1" | strip_doi)
          102         url="$host/$1"
          103         [ "$verbose" = 1 ] && echo "connecting to $url" || :
          104         result="$($prefix curl --location \
          105                 --header "Accept: application/x-bibtex" \
          106                 --silent --show-error "$url")"
          107         echo "$result" | grep -q '<html>' && die 'no result found'
          108         result="$(printf "%s\n" "$result" | format_bibtex_key)"
          109         [ "$fulljournal" = 0 ] && result="$(printf "%s\n" "$result" | abbreviate_journal_name)" || :
          110         [ "$fullauthor" = 0 ] && result="$(printf "%s\n" "$result" | abbreviate_author_name)" || :
          111         result="$(printf "%s\n" "$result" | grep -v -e 'url = ' -e 'month = ' )"
          112         if [ "$nonewline" = 1 ]; then
          113                 printf "\n%s" "$result"
          114         else
          115                 printf "%s\n" "$result"
          116         fi
          117         [ "$notify" = 1 ] && notify-send "${0##*/}" "added: $(echo "$result" | cut -c-80)" || :
          118 }
          119 
          120 verbose=0
          121 fulljournal=0
          122 fullauthor=0
          123 nonewline=0
          124 notify=0
          125 prefix=""
          126 while :; do
          127         case "$1" in
          128                 -h|-\?|--help)
          129                         show_help
          130                         exit 0
          131                         ;;
          132                 -v|--version)
          133                         show_version
          134                         exit 0
          135                         ;;
          136                 -j|--full-journal)
          137                         fulljournal=1
          138                         ;;
          139                 -a|--full-author)
          140                         fullauthor=1
          141                         ;;
          142                 -n|--no-newline)
          143                         nonewline=1
          144                         ;;
          145                 -N|--notify)
          146                         notify=1
          147                         ;;
          148                 -V|--verbose)
          149                         verbose=1
          150                         ;;
          151                 -t|--tor-socks)
          152                         prefix="torsocks"
          153                         ;;
          154                 --) # end all options
          155                         shift
          156                         break
          157                         ;;
          158                 -?*)
          159                         die 'Error: Unknown option specified'
          160                         ;;
          161                 *)  # No more options
          162                         break
          163         esac
          164         shift
          165 done
          166 
          167 if [ $# -lt 1 ]; then
          168         doi="$(cat)"
          169         get_citation "$doi"
          170 else
          171         get_citation "$@"
          172 fi