tscholarref - 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
---
tscholarref (957B)
---
1 #!/bin/sh
2
3 die() {
4 printf '%s\n' "$1" >&2
5 exit 1
6 }
7
8 addref=0
9 refgetter=getref
10 while :; do
11 case "$1" in
12 -a|--add)
13 addref=1
14 ;;
15 -r|--refer)
16 refgetter=getrefer
17 ;;
18 --) # end all options
19 shift
20 break
21 ;;
22 -?*)
23 die 'error: unknown option specified'
24 ;;
25 *) # No more options
26 break
27 esac
28 shift
29 done
30
31 regexmatch() {
32 printf '%s' "$1" | grep -qE "$2"
33 }
34
35 get_reference() {
36 if regexmatch "$1" '^(doi:[/]*|https*://(dx\.)*doi.org/)*10\.[0-9\.]+/'; then
37 printf '%s\n' "10${1#*10}" | $refgetter -n
38 else
39 getdoi "$@" | $refgetter -n
40 fi
41 }
42
43 if [ "$addref" = 1 ]; then
44 if [ -z "$BIB" ]; then
45 die 'BIB env variable not set, should point to .bib file'
46 fi
47 get_reference "$@" >> "$BIB"
48 if [ "$?" -eq 0 ]; then
49 key="$(grep '@.*{' "$BIB" | tail -n 1 | sed 's/.*{//;s/,$//')"
50 echo "Citation $key added to $BIB"
51 notify-send "${0##/*} Citation $key added"
52 else
53 die 'could not find reference'
54 fi
55 else
56 get_reference "$@"
57 fi