-
Notifications
You must be signed in to change notification settings - Fork 0
/
getbib
executable file
·196 lines (167 loc) · 6.87 KB
/
getbib
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/bin/bash
# Usage: getbib [AUXFILE_NAME] [-o|--output OUTPUTFILE_NAME]
# getbib exports a bib file containing only the cited terms according to the current aux
# file. $BAKDIR contains recent backups.
AUXFILE=""
OUTPUTFILE=""
TIME=$(date +%Y.%m.%d-%H.%M.%S)
# Log files
GETBIBLOG="getbib.log"
CHECKBIBLOG=checkbib.log
# Directory to contain the backups.
BAKDIR=./backup/
if [[ $# -gt 3 ]]; then
printf "Usage: getbib [AUXFILE_NAME] [-o|--output OUTPUTFILE_NAME]\n"
exit 1
fi
# Parse the arguments
while [[ -n "$1" ]]; do
case "$1" in
-o|--output)
if [[ $# -gt 1 ]]; then
OUTPUTFILE="$2"
shift
fi
;;
*)
if [[ -z "$AUXFILE" ]]; then
AUXFILE="$1"
fi
;;
esac
shift
done
# Get the name of the aux file with full path.
# If the input is empty, then use the latest aux file under the current directory.
if [[ $# -eq 0 ]] || [[ -z "$1" ]]; then
AUXFILE=$(find ./ -maxdepth 1 -type f -name "*.aux" -print0 | xargs -r -0 ls -1 -t | head -1)
fi
AUXFILE=$(readlink -f "$AUXFILE")
AUXNAME=${AUXFILE##*/}
# AUXNAME is the name of the aux file without path or extension.
AUXNAME=${AUXNAME%.*}
if [[ -z "$AUXNAME" ]]; then
printf "\nNo aux file found in the current directory.\n"
exit 2
elif [[ ! -f "$AUXFILE" ]]; then
printf "\n%s does not exist.\n" "$AUXFILE"
exit 2
fi
# If no output file is specified, set OUTPUTFILE to $AUXNAME.bib.
if [[ -z "$OUTPUTFILE" ]]; then
OUTPUTFILE="$AUXNAME".bib
fi
# Get the full path of $OUTPUTFILE. Note that we do not resolve the link if $OUTPUTFILE exists
# and is a symlink.
OUTPUTFILE="$(cd "$(dirname -- "$OUTPUTFILE")" && pwd)/$(basename -- "$OUTPUTFILE")"
# Temporary files.
# We work on OUTPUTBIB and copy it to OUTPUTFILE in the very end. This is necessary, because
# OUTPUTFILE may be identical to $AUXNAME.bib; we do not want to destroy $AUXNAME.bib until the very
# end, because the script may be interrupted in the middle.
OUTPUTBIB="outputbib-$TIME.bib"
BIBMACROS="bibmacros-$TIME"
BIBENTRIES="bibentries-$TIME"
# Export the cited entries.
printf "\nExporting bib entries ..."
rm -f "$BIBENTRIES" "$BIBMACROS"
touch "$BIBENTRIES" "$BIBMACROS"
BIBDATAFILES=$(grep bibdata "$AUXFILE" | sed 's/.*\\bibdata{//' | sed 's/}.*$//' | sed 's/,/\ /' | tr '\n' ' ')
CITEDBIBS=$(grep bibcite "$AUXFILE" | sed 's/.*\\bibcite{//' | sed 's/}.*$//' | sort -u)
#BIBDATAFILES=`grep bibdata "$AUXFILE" | sed 's/.*\\\\bibdata{//' | sed 's/}.*$//' | sed 's/,/\ /'`
#CITEDBIBS=`grep bibcite "$AUXFILE" | sed 's/.*\\\\bibcite{//' | sed 's/}.*$//' | sort -u`
CITEDBIBS_REDUCED="$CITEDBIBS"
for FILE in $BIBDATAFILES
do
if [[ ! -f $FILE && -f $FILE.bib ]]; then
FILE=$FILE.bib
fi
CITEDBIBS="$CITEDBIBS_REDUCED"
for BIB in $CITEDBIBS
do
if grep -q "@.*{\s*$BIB\s*," "$FILE" ; then
CITEDBIBS_REDUCED=$(echo "$CITEDBIBS_REDUCED" | sed "/^\s*$BIB\s*$/d")
BIBENTRY=$(sed -n "/@.*{\s*$BIB/,/^\s*}/{p;/^\s*}/q}" "$FILE")
echo "" >> "$BIBENTRIES"
echo "$BIBENTRY" >> "$BIBENTRIES"
BIBMACRO=$(echo "$BIBENTRY" | grep "journal\s*=\|journaltitle\s*=\|series\s*=" \
| sed 's/.*journal\s*=\s*//' \
| sed 's/.*journaltitle\s*=\s*//' \
| sed 's/.*series\s*=\s*//' \
| sed s/,.*$// | sed 's/{.*}//')
if [[ -n $BIBMACRO ]]; then
echo "" >> "$BIBMACROS"
grep "@string{\s*$BIBMACRO" "$FILE" | sed 's/.*@string/@string/' >> "$BIBMACROS"
fi
fi
done
done
if ! grep -q '[^[:space:]]' "$BIBENTRIES" ; then
printf "\n%s is not an aux file containing citations or it refers to invalid bib files. Abort.\n" "$AUXFILE"
rm "$BIBENTRIES" "$BIBMACROS"
exit 3
fi
{
printf "%% bib file generated according to %s.aux at %s.\n" "$AUXNAME" "$TIME"
printf "%%\n"
printf "%% The recommended format of the bib entry label is:\n"
printf "%%\n"
printf "%% <AuthorLastnames_YearLetter>\n"
printf "%%\n"
printf "%% For example, the key for an article by Yuan may be:\n"
printf "%%\n"
printf "%% Yuan_2000a\n%%\n"
printf "%% It is advised to use the abbreviations of names of serials in Math. Rev.\n"
printf "%% See https://mathscinet.ams.org/msnhtml/serials.pdf .\n"
printf "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n\n\n"
} > "$OUTPUTBIB"
if grep -q '[^[:space:]]' "$BIBMACROS" ; then
{
printf "%% String macros for abbreviations of names of serials.\n"
printf "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%MACRO_BEGIN%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
sed -r '/^\s*$/d' "$BIBMACROS" | sort -u
printf "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%MACRO_END%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n\n"
} >> "$OUTPUTBIB"
fi
rm "$BIBMACROS"
cat "$BIBENTRIES" >> "$OUTPUTBIB"
rm "$BIBENTRIES"
# Make a backup of $OUTPUTFILE under $BAKDIR if $OUTPUTFILE exists.
FULLBIBNAME=${OUTPUTFILE//\//%}
BAKBIBFILE=$FULLBIBNAME-$TIME
BIBDIFFERENT=0
echo "" > $GETBIBLOG
if [[ -f "$OUTPUTFILE" ]]; then
printf "******** Difference between the new bib and the original one ********\n" >> $GETBIBLOG
# Remove the comments before comparing.
diff <(grep -v "^\s*%" "$OUTPUTBIB") <(grep -v "^\s*%" "$OUTPUTFILE") >> $GETBIBLOG
if [[ $? -eq 1 ]]; then
BIBDIFFERENT=1
# $BAKDIR/$FULLBIBNAME-latest_backup is always the latest backup.
mkdir -p $BAKDIR
cp "$OUTPUTFILE" "$BAKDIR/$FULLBIBNAME-latest_backup"
mv "$OUTPUTFILE" "$BAKDIR/$BAKBIBFILE"
# Keep at most 50 latest files under $BAKDIR.
(cd $BAKDIR && ls -tp | grep -v '/$' | grep "$FULLBIBNAME" | tail -n +51 | xargs -I {} rm -- {})
fi
fi
# Write the output to $OUTPUTFILE
mv "$OUTPUTBIB" "$OUTPUTFILE"
# Check $OUTPUTFILE.
# First, decide whether this is a BibLaTeX file or a BibTeX file.
if grep -q "journaltitle\s*=\s*\|date\s*=\s*\|location\s*=\s*\|type\s*=\s*" "$OUTPUTFILE" ; then
# This is likely a BibLaTeX file.
checkbib -l "$OUTPUTFILE"
else
# This is likely a BibTeX file.
checkbib "$OUTPUTFILE"
fi
echo "*************************** checkbib.log ****************************" | cat - "$CHECKBIBLOG" > "tmp-$TIME" && mv "tmp-$TIME" "$CHECKBIBLOG"
cat $GETBIBLOG >> $CHECKBIBLOG
mv $CHECKBIBLOG $GETBIBLOG
echo "Done. See $GETBIBLOG for $CHECKBIBLOG and the difference between the new bib and the original one."
if [[ $BIBDIFFERENT -eq 1 ]]; then
printf "\nA copy of the original bib file is saved at\n%s" "$BAKDIR/$BAKBIBFILE"
printf "\nA copy of the original bib file is saved at\n%s" "$BAKDIR/$BAKBIBFILE" >> $GETBIBLOG
fi
echo ""
exit 0