tjabbrev - 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
---
tjabbrev (2402B)
---
1 #!/bin/sh
2
3 abbrevfile="$(dirname "$0")/../share/scholarref/journal_abbreviations"
4 version=0.1
5
6 show_help() {
7 echo "usage: ${0##*/} [OPTIONS] [QUERY]"
8 echo "returns a journal title abbreviation. If QUERY is not specified, an"
9 echo "interactive dmenu(1) prompt for searching the abbreviation list."
10 echo
11 echo "OPTIONS are one or more of the following:"
12 echo " -h, --help show this message"
13 echo " -v, --version show version and license information"
14 echo " -n, --notify send result as desktop notification"
15 echo " -c, --clip send abbreviation to clipboard (requires xclip)"
16 echo " -- do not consider any following args as options"
17 echo
18 echo "Known bug: Unabbreviated words are also given a trailing dot."
19 }
20
21 show_version() {
22 echo "${0##*/} version $version"
23 echo "Licensed under the GNU Public License, v3+"
24 echo "written by Anders Damsgaard, anders@adamsgaard.dk"
25 echo "https://src.adamsgaard.dk/scholarref"
26 }
27
28 die() {
29 printf '%s\n' "$1" >&2
30 exit 1
31 }
32
33 parse_abbreviation() {
34 sed -e 's/.* //' | \
35 tr '[:upper:]' '[:lower:]' | \
36 awk 'BEGIN{FS=" ";ORS=""} {
37 for (i=1; i<=NF; i++) {
38 {sub(".", substr(toupper($i),1,1), $i)} print $i". " }
39 }
40 END{print "\n"}' | \
41 sed 's/ $//'
42 }
43
44 interactive_get_query() {
45 dmenu -i -l 20 -p "Title:"
46 }
47
48 notify=0
49 clip=0
50 while :; do
51 case "$1" in
52 -h|-\?|--help)
53 show_help
54 exit 0
55 ;;
56 -v|--version)
57 show_version
58 exit 0
59 ;;
60 -n|--notify)
61 notify=1
62 ;;
63 -c|--clip)
64 clip=1
65 ;;
66 --) # end all options
67 shift
68 break
69 ;;
70 -?*)
71 die 'Error: Unknown option specified'
72 ;;
73 *) # No more options
74 break
75 esac
76 shift
77 done
78
79 if [ ! -r "$abbrevfile" ]; then
80 die "error: abbreviation file '$abbrevfile' not found"
81 fi
82
83 if [ $# -gt 0 ]; then
84 line="$(grep -i "$*" "$abbrevfile")"
85 if [ "$(printf "%s\n" "$line" | wc -l)" -gt 1 ]; then
86 line="$(printf "%s\n" "$line" | interactive_get_query)"
87 fi
88 else
89 line="$(cat "$abbrevfile" | interactive_get_query)"
90 fi
91
92 if [ -z "$line" ]; then
93 die 'error: journal name not found'
94 elif [ "$(printf "%s\n" "$line" | wc -l)" -gt 1 ]; then
95 die "error: more than one journal matches query:\n$line"
96 fi
97
98 abbrev="$(echo "$line" | parse_abbreviation)"
99 if [ -z "$abbrev" ]; then
100 die 'error: journal abbreviation not found'
101 fi
102
103 echo "$abbrev"
104 if [ "$notify" = 1 ]; then
105 notify-send "$abbrev"
106 fi
107 if [ "$clip" = 1 ]; then
108 echo "$abbrev" | xclip -i
109 fi