-
Notifications
You must be signed in to change notification settings - Fork 0
/
trimbib
executable file
·386 lines (342 loc) · 14 KB
/
trimbib
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#!/bin/bash
# Usage: trimbib [-l|--latex] [-t|--tex] [NAME_OF_BIBTEX_FILE] [-o|--output NAME_OF_OUTPUT_FILE]
# This script trims bib files using biber in the following way.
# 1. Only one of the duplicated entries is kept. (But which one? I am not sure what is biber's rule.)
# 2. Items are ordered by author, year, and the entry key.
# 3. Macros are sorted alphabetically.
# 4. The file is indented.
#
# If -l|--latex is present, then a BibLaTeX file will be produced;
# If -t|--tex is present, then a BibTeX file will be produced;
# if neither -l|--latex nor -t|--tex is present, then trimbib will try to decide the type of the
# input file and generate a file of the same type; if the type cannot the decided, then a BibTeX
# file will be generated.
USER_SPECIFY_LATEX=0
USER_SPECIFY_TEX=0
BIBFILE=""
OUTPUTFILE=""
TIME=$(date +%Y.%m.%d-%H.%M.%S)
# Log files
TRIMBIBLOG="trimbib.log"
CHECKBIBLOG="checkbib.log"
# Directory to contain the backups.
BAKDIR=./backup/
# Check whether biber is available with a high enough version.
BIBER_MINVER=2.17
if ! command -v biber &> /dev/null ; then
printf "\nbiber version >= %s is needed, but biber is not found.\n\n" "$BIBER_MINVER"
exit 3
fi
BIBER_VER="$(biber --version | grep -Eo '[0-9]\.[0-9]+' | head -1)"
if (( $(echo "$BIBER_MINVER > $BIBER_VER" | bc -l) )); then
printf "\nbiber version >= %s is needed, but the current version is %s.\n\n" "$BIBER_MINVER" "$BIBER_VER"
exit 3
fi
if [[ $# -gt 4 ]]; then
printf "Usage: trimbib [-l|--latex] [-t|--tex] [BIBFILE_NAME] [-o|--output NAME_OF_OUTPUT_FILE]\n"
exit 1
fi
# Parse the arguments
while [[ -n "$1" ]]; do
case "$1" in
-l|--latex)
USER_SPECIFY_LATEX=1
;;
-t|--tex)
USER_SPECIFY_TEX=1
;;
-o|--output)
if [[ $# -gt 1 ]]; then
OUTPUTFILE="$2"
shift
fi
;;
*)
if [[ -z "$BIBFILE" ]]; then
BIBFILE="$1"
fi
;;
esac
shift
done
# Get the name of the bib file with full path.
# If $BIBFILE is a symlink, we work on its target.
# If the input is empty, then use the latest bib file under the current directory.
if [[ -z "$BIBFILE" ]]; then
BIBFILE=$(find ./ -maxdepth 1 -type f -name "*.bib" -print0 | xargs -r -0 ls -1 -t | head -1)
fi
BIBFILE="$(readlink -f "$BIBFILE")"
if [[ -z "${BIBFILE##*/}" ]]; then
printf "\nNo bib file found.\n"
exit 2
elif [[ ! -f "$BIBFILE" ]]; then
printf "\n%s does not exist.\n" "$BIBFILE"
exit 2
fi
if grep -q "journaltitle[[:space:]]*=[[:space:]]*\|date[[:space:]]*=[[:space:]]*\|location[[:space:]]*=[[:space:]]*\|type[[:space:]]*=[[:space:]]*" "$BIBFILE" ; then
USER_INPUT_LATEX=1
else
USER_INPUT_LATEX=0
fi
if [[ $USER_SPECIFY_LATEX -eq 1 && $USER_SPECIFY_TEX -eq 1 ]] ; then
printf "\n-l|--latex and -t|--t cannot be present simultaneously.\n"
exit 1
elif [[ $USER_SPECIFY_LATEX -eq 1 ]] ; then
BIBLATEX=1
elif [[ $USER_SPECIFY_LATEX -ne 1 ]] ; then
BIBLATEX=0
elif [[ $USER_INPUT_LATEX -eq 1 ]] ; then
BIBLATEX=1
else
BIBLATEX=0
fi
# If no output file is specified, set OUTPUTFILE to $BIBFILE.
if [[ -z "$OUTPUTFILE" ]]; then
OUTPUTFILE=$BIBFILE
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")"
# Check the bib file before trimming.
if [[ $USER_INPUT_LATEX -eq 1 ]] ; then
checkbib -l "$BIBFILE"
else
checkbib "$BIBFILE"
fi
read -n1 -s -r -p \
$'Continue to trim the bib file? [Y/n] \nRevise it before trimming if it contains major mistakes.\n' KEY
if ! [[ "$KEY" == 'Y' || "$KEY" == 'y' || "$KEY" == "" ]]; then
exit 1
fi
echo "**************** checkbib.log before trimming ****************" | cat - $CHECKBIBLOG > "tmp-$TIME" && mv "tmp-$TIME" $CHECKBIBLOG
mv $CHECKBIBLOG $CHECKBIBLOG.bak
# Temporary files.
INPUTBIB="inputbib-$TIME.bib"
OUTPUTBIB="outputbib-$TIME.bib"
TMPBIB="tmpbib-$TIME.bib"
BIBMACROS="bibmacros-$TIME"
BIBEROUT="biberout-$TIME"
BIBENTRIES="bibentries-$TIME"
cp "$BIBFILE" "$INPUTBIB"
# Pre-process the bib file if the output is a BibTeX file. This is necessary if the input is BibLaTeX.
LINEBREAKER="LINEBREAKERSTARTING$(date +%s)LINEBREAKERENDING"
if [[ $BIBLATEX -eq 0 ]] ; then
sed '/^[[:space:]]*%/d' "$INPUTBIB" | sed 's/%.*$//' | awk -v ORS="$LINEBREAKER" 1 \
| sed "s/$LINEBREAKER[[:space:]]*@/\n@/g" | sed "s/$LINEBREAKER//g" > "$TMPBIB"
{
grep -v "techreport\|phdthesis\|masterthesis" "$TMPBIB"
grep techreport "$TMPBIB" | sed 's/^[[:space:]]*@report/@techreport/' | sed 's/type[[:space:]]*=[[:space:]]*{.*}[[:space:]]*,//'
grep phdthesis "$TMPBIB" | sed 's/^[[:space:]]*@thesis/@phdthesis/' | sed 's/type[[:space:]]*=[[:space:]]*{.*}[[:space:]]*,//' \
| sed 's/institution/school/'
grep masterthesis "$TMPBIB" | sed 's/^[[:space:]]*@thesis/@masterthesis/' | sed 's/type[[:space:]]*=[[:space:]]*{.*}[[:space:]]*,//' \
| sed 's/institution/school/'
} > "$OUTPUTBIB"
rm -f "$TMPBIB"
sed -i 's/,[[:space:]]*journaltitle[[:space:]]*=[[:space:]]*/,journal=/' "$OUTPUTBIB"
sed -i 's/,[[:space:]]*date[[:space:]]*=[[:space:]]*/,year=/' "$OUTPUTBIB"
sed -i 's/,[[:space:]]*location[[:space:]]*=[[:space:]]*/,address=/' "$OUTPUTBIB"
mv "$OUTPUTBIB" "$INPUTBIB"
fi
# Print the macros in $INPUTBIB to $BIBMACROS
printf "\nSorting the macros ..."
grep "^[[:space:]]*@string{\|^[[:space:]]*@String{\|^[[:space:]]*@STRING{" "$INPUTBIB" | sed 's/.*@/@/' \
| sed 's/@String/@string/' | sed 's/@STRING/@string/' \
| sed 's/\.[[:space:]]*"[[:space:]]*}/\."}/' \
| sed 's/[[:space:]]*=[[:space:]]*/\ =\ /' \
> "$BIBMACROS"
# Trim the bib entries by biber
printf "\nInvoking biber to trim the bib entries ...\n"
# Configuration of biber.
################################################################################
cat > biber.conf <<EOF
<?xml version="1.0"?>
<config>
<output_align>true</output_align>
<output_fieldcase>lower</output_fieldcase>
<!--!!!The following lines make biber to sort the bib entries according to year, author, and title.!!!-->
<sortingtemplate name="tool">
<sort order="1" sort_direction="ascending">
<sortitem order="1">author</sortitem>
</sort>
<sort order="2" sort_direction="ascending">
<sortitem order="1">year</sortitem>
</sort>
<sort order="3" sort_direction="ascending">
<sortitem order="1">entrykey</sortitem>
</sort>
</sortingtemplate>
<!--!!!By default, biber removes the "nonstandard fields".!!!-->
<!--!!!The following lines ensure that the nonstandard fields are retained in certain entries.!!!-->
<!--!!!See https://tex.stackexchange.com/questions/415028/prevent-biber-tool-from-removing-non-standard-fields-in-bib-files !!!-->
<datamodel>
<fields>
<!--!!!Put the name of the desired fields here.!!!-->
<field fieldtype="field" datatype="literal">_number</field>
<field fieldtype="field" datatype="literal">_publisher</field>
<field fieldtype="field" datatype="literal">note</field>
<field fieldtype="field" datatype="literal">journal</field>
<field fieldtype="field" datatype="literal">school</field>
<field fieldtype="field" datatype="literal">address</field>
</fields>
<entryfields>
<!--!!!For article, retain "_number", "_publisher", "note", "journal".!!!-->
<entrytype>article</entrytype>
<field>_number</field>
<field>_publisher</field>
<field>note</field>
<field>journal</field>
</entryfields>
<entryfields>
<!--!!!For masterthesis, retain "school", "address", "note".!!!-->
<entrytype>masterthesis</entrytype>
<field>school</field>
<field>address</field>
<field>note</field>
</entryfields>
<entryfields>
<!--!!!For phdthesis, retain "school", "address", "note".!!!-->
<entrytype>phdthesis</entrytype>
<field>school</field>
<field>address</field>
<field>note</field>
</entryfields>
<entryfields>
<!--!!!For techreport, retain "number", "institution", "address", "note".!!!-->
<entrytype>techreport</entrytype>
<field>number</field>
<field>institution</field>
<field>address</field>
<field>note</field>
</entryfields>
</datamodel>
EOF
if [[ $BIBLATEX -eq 1 ]] ; then
cat >> biber.conf <<EOF
</config>
EOF
else
cat >> biber.conf <<EOF
<!--!!!By default, biber maps "masterthesis" to "thesis" with a "type = {masterthesis}".!!!-->
<!--!!!It does similar things on "phdthesis".!!!-->
<!--!!!In addition, it maps "techreport" to "report" with type={techreport}.!!!-->
<!--!!!The following lines revokes this behavior.!!!-->
<sourcemap>
<maps datatype="bibtex">
<map>
<map_step map_type_source="mastersthesis" map_type_target="masterthesis" map_final="1"/>
</map>
<map>
<map_step map_type_source="phdthesis" map_type_target="phdthesis" map_final="1"/>
</map>
<map>
<map_step map_type_source="techreport" map_type_target="techreport" map_final="1"/>
</map>
</maps>
</sourcemap>
</config>
EOF
fi
################################################################################
# Invoke biber.
# 1. "--output-safechars --output-safecharsset=full" tells biber to encode all non-ascii characters
# into LaTeX macros. By default, this is disabled. Similar behavior can be invoked by
# "--decodecharsset=null"; however, as of 2021-03-22, this approach seems buggy, because it will
# cause biber to remove all the curly braces enclosing only one letter, e.g., {H}ilbert.
# 2. By default, biber uses "location" instead of "address", "journaltitle" instead of "journal",
# and "date" instead of year (due to the difference between BibTeX and BibLaTeX). We use
# "--output-field-replace" to disable such behavior.
if [[ $BIBLATEX -eq 1 ]] ; then
biber --output-file "$BIBEROUT" \
--tool --configfile=biber.conf \
--output-safechars --output-safecharsset=full \
--output_indent=4 "$INPUTBIB"
else
biber --output-file "$BIBEROUT" \
--tool --configfile=biber.conf \
--output-safechars --output-safecharsset=full \
--output_indent=4 \
--output-field-replace=location:address,journaltitle:journal,date:year "$INPUTBIB"
fi
rm biber.conf
rm "$INPUTBIB"
rm "$INPUTBIB.blg"
# Print the trimmed bib entries into $BIBENTRIES.
touch "$BIBEROUT" # $BIBEROUT is not generated if biber encounters errors.
gawk -v line="$(gawk '/@STRING|@String|@string/{n=NR}END{print n}' "$BIBEROUT")" 'NR>line' "$BIBEROUT" > "$BIBENTRIES"
rm "$BIBEROUT"
if ! grep -q '[^[:space:]]' "$BIBENTRIES" ; then
printf "\n%s is not a bib file or contains no bib entry. Abort.\n" "$BIBFILE"
rm "$OUTPUTBIB" "$BIBMACROS" "$BIBENTRIES" "$CHECKBIBLOG.bak"
exit 3
fi
# Generate the new bib file.
{
printf "%% bib file generated at %s.\n" "$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"
#grep -q -v '^[[:space:]]*$' "$BIBMACROS" > /dev/null # Check whether $BIBMACROS is empty.
if grep -q -v '^[[:space:]]*$' "$BIBMACROS" > /dev/null ; then
{
printf "%% String macros for abbreviations of names of serials.\n"
printf "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%MACRO_BEGIN%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
sed -r '/^[[:space:]]*$/d' "$BIBMACROS" | sort -u
printf "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%MACRO_END%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n\n"
} >> "$OUTPUTBIB"
fi
rm "$BIBMACROS"
# On Linux, rfrio is always expanded to {Rev. fr. inform. rech. op\'{e}r.} in $BIBENTRIES. The
# following line changes it back.
sed "s|\=[[:space:]]{Rev. fr. inform. rech. op\\\\'{e}r.}|\= rfrio|g" "$BIBENTRIES" >> "$OUTPUTBIB"
rm "$BIBENTRIES"
# Make a backup of $OUTPUTFILE under $BAKDIR if it exists and the trimmed file is different from
# the original $OUTPUTFILE.
FULLBIBNAME=${OUTPUTFILE//\//%}
BAKBIBFILE=$FULLBIBNAME-$TIME
BIBDIFFERENT=0
if [[ -f "$OUTPUTFILE" ]]; then
printf "****** Difference between the trimmed bib and the original %s ******\n" "$OUTPUTFILE" > $TRIMBIBLOG
# Remove the comments before comparing.
diff <(grep -v "^[[:space:]]*%" "$OUTPUTBIB") <(grep -v "^[[:space:]]*%" "$OUTPUTFILE") >> $TRIMBIBLOG
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 the bib file after trimming.
if [[ $BIBLATEX -eq 1 ]] ; then
checkbib -l "$OUTPUTFILE"
else
checkbib "$OUTPUTFILE"
fi
{
printf "\n***************** checkbib.log after trimming ****************\n"
cat $CHECKBIBLOG
printf "\n"
} >> $CHECKBIBLOG.bak
mv $CHECKBIBLOG.bak $CHECKBIBLOG
cat "$CHECKBIBLOG" | cat - "$TRIMBIBLOG" > "tmp-$TIME" && mv "tmp-$TIME" $TRIMBIBLOG
rm $CHECKBIBLOG
printf "Done. See %s for %s and the difference due to the trimming.\n" "$TRIMBIBLOG" "$CHECKBIBLOG"
if [[ $BIBDIFFERENT -eq 1 ]]; then
printf "\nA copy of the original bib file is saved at %s" "$BAKDIR/$BAKBIBFILE"
printf "\nA copy of the original bib file is saved at %s" "$BAKDIR/$BAKBIBFILE" >> $TRIMBIBLOG
fi
printf "\n"
exit 0