tparseref - 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
---
tparseref (1593B)
---
1 #!/bin/sh
2 set -e
3
4 die() {
5 printf '%s\n' "$1" >&2
6 exit 1
7 }
8
9 get_citation()
10 {
11 awk -v id="$1" '
12 function getval(str) {
13 sub(/.*= *{*/, "", str)
14 sub(/},*$/, "", str)
15 gsub(/[{}]/, "", str)
16 return str
17 }
18
19 {
20 if (match(tolower($0), tolower(id)))
21 found = 1
22 if (found) {
23 if (match(tolower($0), /author *=/))
24 author = getval($0)
25 else if (match(tolower($0), /year *=/))
26 year = getval($0)
27 else if (match(tolower($0), /title *=/))
28 title = getval($0)
29 else if (match(tolower($0), /journal *=/))
30 journal = getval($0)
31 else if (match(tolower($0), /volume *=/))
32 volume = getval($0)
33 else if (match(tolower($0), /number *=/))
34 number = getval($0)
35 else if (match(tolower($0), /pages *=/))
36 pages = getval($0)
37 else if (match(tolower($0), /doi *=/))
38 doi = getval($0)
39 else if (match($0, /^ *} *$/)) {
40 found = 0
41 sub(/--/, "ā", pages)
42 split(author, authors, / +and +/)
43 nauthors = length(authors)
44 for (i = 1; i <= nauthors; i++) {
45 printf "%s", authors[i]
46 if (i < nauthors - 1)
47 printf ", "
48 else if (i == nauthors - 1)
49 printf " and "
50 }
51 printf " %d ā%sā. %s", year, title, journal
52 if (length(volume) > 0)
53 printf " %s", volume
54 if (length(number) > 0)
55 printf "(%s)", number
56 if (length(pages) > 0)
57 printf ", %s.", pages
58 if (length(doi) > 0)
59 printf " doi: %s", doi
60 printf "\n"
61 }
62 }
63 }
64 ' "$BIB"
65 }
66
67 if [ $# -lt 1 ]; then
68 while read -r id; do
69 get_citation "$id"
70 done
71 else
72 get_citation "$@"
73 fi