-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbibtex-to-meta
executable file
·76 lines (65 loc) · 2.54 KB
/
bibtex-to-meta
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
#!/bin/sh
# Generate metadata for pdftk based on DOI
# The GPLv3 License (GPLv3)
#
# Copyright (c) 2024 Ivan Boikov
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
if [ -t 0 ] && [ -z "$1" ]; then
echo "Usage: bibtex-to-meta [STRING]\n\nGenerate pdftk metadata file based on a Bibtex entry.\nWith no STRING, read from standard input."
exit
fi
parse_bibtex(){
echo "$1" | grep -Po "$2={.+?}" | awk -F '[{}]' '{print $2}'
}
add_pdf_meta(){
# the format used by pdftk
printf "InfoBegin\nInfoKey: %s\nInfoValue: %s\n" "$1" "$2"
}
flip_name_surname(){
name="$(echo "$1" | cut -d, -f2 | xargs)"
# -s to handle cases w/o ',' like the last author of 10.1109/jqe.1983.1071749
surname="$(echo "$1" | cut -s -d, -f1 | xargs)"
echo "$name $surname"
}
process_bibtex(){
author="$(parse_bibtex "$1" "author")"
# convert Bibtex-style author list to natural style
author="$(echo "$author" | sed 's/ and /\n/g' | while read -r line ; do
flip_name_surname "$line"
done)"
author="$(echo "$author" | paste -sd "," - | sed 's/,/, /g')"
title="$(parse_bibtex "$1" "title")"
journal="$(parse_bibtex "$1" "journal")"
# maybe more fallbacks?
if [ -z "$journal" ]; then journal="$(parse_bibtex "$1" "booktitle")" ; fi
year="$(parse_bibtex "$1" "year")"
volume="$(parse_bibtex "$1" "volume")"
number="$(parse_bibtex "$1" "number")"
pages="$(parse_bibtex "$1" "pages")"
doi="$(parse_bibtex "$1" "DOI" | tr '[:lower:]' '[:upper:]')"
subject=""
# ISO 690 (kind of, no guarantees)
[ -n "$journal" ] && subject="$subject, $journal"
[ -n "$year" ] && subject="$subject, $year"
[ -n "$volume" ] && subject="$subject, vol. $volume"
[ -n "$number" ] && subject="$subject, no. $number"
[ -n "$pages" ] && subject="$subject, p. $pages"
[ -n "$doi" ] && subject="$subject, doi:$doi"
add_pdf_meta "Title" "$title"
add_pdf_meta "Author" "$author"
add_pdf_meta "Subject" "${subject#??}" # cut off ", " in the beginning
}
[ ! -t 0 ] && process_bibtex "$(cat)"
[ -n "$1" ] && process_bibtex "$1"