-
Notifications
You must be signed in to change notification settings - Fork 6
/
arxiv2bibtex
executable file
·70 lines (55 loc) · 1.96 KB
/
arxiv2bibtex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/bash
# Script for generating a bibtex entry from a given arXiv identifier
#
# Usage: arxiv2bibtex arXiv
# arxiv2bibtex http://arxiv.org/abs/arXiv
#
# Example: arxiv2bibtex 1412.3740 >> ~/World.bib
# Example: arxiv2bibtex math/0602029 >> references.bib (old arXiv format)
# Issue help message if necessary
if [ $# = 0 ] || [ "x$1" = "x--help" ]; then
>&2 echo "Usage: $(basename $0) arXiv"
>&2 echo "or: $(basename $0) http://arxiv.org/abs/arXiv"
>&2 echo "will try and retrieve a bibtex entry for the arXiv identifier given."
>&2 echo
>&2 echo "Example: $(basename $0) 1701.06092"
>&2 echo " $(basename $0) 1701.06092v1"
>&2 echo " $(basename $0) http://arxiv.org/abs/1701.06092v1"
>&2 echo " $(basename $0) math/0602029"
>&2 echo " $(basename $0) http://arxiv.org/abs/math/0602029"
exit 1
fi
# Unify the calling syntax
arxivpattern='([0-9]{4}\.[0-9]{4,}([v][0-9]*)?|[a-z]{1,}/[0-9]{7})$'
if [[ $1 =~ $arxivpattern ]]; then
arxiv=${BASH_REMATCH[1]}
else
echo "Failed to find arXiv pattern."
exit 1
fi
# echo $arxiv
# arxivnodots="${arxiv//./}"
#
# URL="http://adsabs.harvard.edu/cgi-bin/bib_query?arXiv:$arxiv"
#
# # Get URL of bibtex entry
# URL=$(wget -O - $URL | grep 'Bibtex entry for' | perl -ne '/<a href="(.*?)">/ and print "$1\n"')
#
# wget -O - $URL
# Better?
URL="https://arxiv2bibtex.org/?q=$arxiv&format=biblatex"
ENTRY=$(wget -O - $URL -q | xmllint --xpath '//div[@id="biblatex"]/textarea/text()' -)
# If we have bibtool (https://ctan.org/pkg/bibtool) we can use it to format the key
if [ -x "$(command -v bibtool)" ] ; then
ENTRY=$(echo "$ENTRY" | bibtool -f "%n(author)%4d(year)_preprint" \
-- "new.entry.type {online}" \
-- 'fmt.name.name = ""' \
-- 'add.field {type = "Preprint"}' \
-- 'print.align.key = 0'\
-- 'print.line.length = 1000'\
-- 'print.use.tab = 0'\
)
ENTRY=${ENTRY/#$'\n'} # Remove a leading newline.
fi
echo "$ENTRY"
echo # Add an additional newline