forked from Nokorot/dotbin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgmarks2
executable file
·240 lines (202 loc) · 5.99 KB
/
gmarks2
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
#!/bin/bash
# Potetialy useful for extracting the titles:
# add something like this to the script
# pdfgrep -n '' notes.pdf | grep '^[0-9]*:[ \t]*[0-9]\.' | tr -s " " > tmp
# TODO: maybe add pdfjoin, which also combines the indexes
# Example of .otl format:
#<Level> <PageNr> <Title>
#1 1 1. Basic Notions and Notation
#1 2 2. Random Variables
#1 4 3. Expectation–Integrals
#1 12 4. Lp spaces
#1 14 5. Convergence of measurable functions
#1 17 6. Independence of events and independence of random variables
#1 21 7. Classical Theorems of Probability Theory
#2 21 7.1. Law of Large Numbers.
#2 22 7.2. Central Limit Theorem.
#1 22 8. Conditional Expectation
#1 24 9. Appendix: Lebesgue measure. Caratheodory extension
# TODO ::
- Comments in the otl format
#end TODO
pages() { pdfinfo "$1" | grep Pages | sed 's/^[^:]*:\s*//'; }
# count=0; for i in {1..7}; do gmarks2 i2o -a $count $i.info; count="$(( count + $(pages "$i.pdf") ))"; done
DEL='[\t]+'
OFFSET=0
usage() {
echo "$0 [o2i|i2o|ext|add] ..."
echo " o2i [opts] <file.otl>: converts a .otl file to a .info file"
echo " i2o [opts] <file.info>: converts a .info file to a .otl file"
echo " addi [opts] <file.pdf> <file.info>: Updates the meta data of a pdf."
echo " addo [opts] <file.pdf> <file.otl>: Updates the index of a pdf."
echo " ext [opts] <file.pdf>: extract the meta data from a .pdf file,"
echo " ie. a .info file"
echo " join [opts] <file.pdf>... : joins pdf files,"
}
o2i_usage(){
echo "o2i [opts] >file.otl>"
echo " converts a .otl file to a .info file"
echo "OPTIONS:"
echo " -d DEL use DEL instead of TAB for field delimiter."
echo " -a OFFSET offset the pagenumber."
echo " -h prints this message"
}
i2o_usage(){
echo "i2o [opts] <file.info>"
echo " converts a .info file to a .otl file."
echo "OPTIONS:"
echo " -h prints this message"
}
addi_usage() {
echo "addi [opts] <file.pdf> <file.info>"
echo " Updates the meta data of the pdf file."
echo "OPTIONS:"
echo " -h prints this message"
}
addo_usage() {
echo "addo [opts] <file.pdf> <file.otl>"
echo " Updates the index of a pdf."
echo "OPTIONS:"
echo " -d DEL use DEL instead of TAB for field delimiter"
echo " -a OFFSET offset the pagenumber."
echo " -h prints this message"
}
join_usage() {
echo "join [opts] <file.pdf>..."
echo " joins pdf files."
echo "OPTIONS:"
echo " -d DEL use DEL instead of TAB for field delimiter"
echo " -h prints this message"
}
o2i() {
OPTIND=0;
while getopts ":d:a:h" o; do
case "${o}" in
d) DEL="${OPTARG}" ;;
a) OFFSET="${OPTARG}" ;;
h) o2i_usage; exit 0;;
esac
done
shift $((OPTIND-1))
[ "$#" -lt 1 ] && {
echo "ERROR: Not enough arguments!"; o2i_usage; exit 1;
}
awk -F "$DEL" '{print "BookmarkBegin"}
{print "BookmarkTitle: " $3}
{print "BookmarkPageNumber: " $2+'$OFFSET'}
{print "BookmarkLevel: " $1}' "$1"
}
i2o() {
OPTIND=0;
while getopts ":a:h" o; do
case "${o}" in
h) i2o_usage; exit 0;;
a) OFFSET=${OPTARG} ;;
esac
done
shift $((OPTIND-1))
[ "$#" -lt 1 ] && {
echo "ERROR: Not enough arguments!"; i2o_usage; exit 1;
}
IFS=':'
grep Bookmark "$1" | {
# level=0; pnr=0; title=""
prt() {
[ -z "$level" ] || printf "$level\t$(( $pnr + $OFFSET ))\t$title\n"
}
while read line; do
key="$(echo "$line" | cut -d':' -f1)"
value="$(echo "$line" | sed "s/^[^:]*: *//" )"
case $key in
BookmarkBegin) prt ;;
BookmarkLevel) level="$value" ;;
BookmarkTitle) title="$value" ;;
BookmarkPageNumber) pnr="$value" ;;
esac
done
prt
}
}
addi(){
OPTIND=0;
while getopts ":h" o; do
case "${o}" in
h) addi_usage; exit 0;;
esac
done
shift $((OPTIND-1))
[ "$#" -lt 2 ] && {
echo "ERROR: Not enough arguments!"; addi_usage; exit 1;
}
pdftk "$2" update_info_utf8 "$3" output "$2-new.pdf"
}
addo(){
OUTFILE=""
OPTIND=0;
while getopts ":d:a:o:h" o; do
case "${o}" in
d) DEL="${OPTARG}" ;;
a) OFFSET="${OPTARG}" ;;
o) OUTFILE="${OPTARG}" ;;
h) addo_usage; exit 0;;
esac
done
shift $((OPTIND-1))
[ "$#" -lt 2 ] && {
echo "ERROR: Not enough arguments!"; addo_usage; exit 1;
}
[ -z "$OUTFILE" ] && OUTFILE="$1-new.pdf"
echo $@
tempfile="$(mktemp)"
o2i "$2" > "$tempfile";
cat $tempfile
pdftk "$1" update_info_utf8 "$tempfile" output "$OUTFILE"
rm $tempfile
}
join() {
OPTIND=0;
while getopts ":h" o; do
case "${o}" in
h) join_usage; exit 0;;
esac
done
shift $((OPTIND-1))
[ "$#" -lt 1 ] && {
echo "ERROR: Not enough arguments!"; join_usage; exit 1;
}
echo "Combining $@"
echo "Prosessing metadata"
for file in "$@"; do
pdftk $file dump_data_utf8 > /tmp/$file.info;
done
# TODO: Use jq
python -c '
import sys; from pdf_dump import *
off=0;
for fn in sys.argv[1:]:
root=read_info("/tmp/%s.info" % fn); info2otl(root,pgn_off=off)
off+=root["NumberOfPages"]
' "$@" > /tmp/pdfjoin.otl
o2i /tmp/pdfjoin.otl > pdfjoin.info
echo "Combining pdfs"
pdfjoin --outfile /tmp/pdfjoin.pdf -- "$@"
echo "Adding metadata"
pdftk /tmp/pdfjoin.pdf update_info_utf8 pdfjoin.info \
output pdfjoin.pdf
echo "Removing tmp-files"
for file in "$@"; do rm /tmp/$file.info; done
rm /tmp/pdfjoin.otl; rm /tmp/pdfjoin.pdf
}
main() {
case $1 in
o2i) o2i "${@:2}" ;;
i2o) i2o "${@:2}" ;;
ext) pdftk "$2" dump_data_utf8 ;;
addi) addi "${@:2}" ;;
addo) addo "${@:2}" ;;
join) join "${@:2}" ;;
-h|-help) usage ;;
*) echo "ERROR: Unknown keyword '$1'."; usage ;;
esac
}
main "$@"