-
Notifications
You must be signed in to change notification settings - Fork 1
/
pdfhscript
executable file
·223 lines (199 loc) · 4.8 KB
/
pdfhscript
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
#!/bin/sh
#
# external script for hovacui
#
# l: show annotation and actions in the current page in a browser
# s: save the document
# A,N: add or edit notes in filename-notes.txt
# A: add a note for the current page
# N: edit the notes for the current page if some exist
# E: create a modified document
# arguments
KEY="$1"
ID="$2"
FILE="$3"
PAGE="$4"
BOXNUMBER="$5"
SCROLLX="$6"
SCROLLY="$7"
TOTPAGES="$8"
TEXTBOX="$9"
DEST="${10}"
RECT="${11}"
# actions required by multiple keystrokes
[ "$KEY" = 'A' ] && EDIT=true
[ "$KEY" = 'N' ] && EDIT=true
$EDIT false && [ "$RECT" != "[]" ] && YANK=true
[ "$KEY" = 'y' ] && YANK=true && COPIED=true
# test
if [ "$KEY" = '0' ]; # test echo and move
then
if [ $PAGE = $TOTPAGES ];
then
echo -n "last page"
exit 0
else
echo "$((PAGE+1)) 0 0 0"
exit 1
fi
elif echo "$KEY" | grep -q '^[123]$'; # test reload
then
# same position
[ "$KEY" = '1' ] && echo "$PAGE $BOXNUMBER $SCROLLX $SCROLLY"
# new position
[ "$KEY" = '2' ] && echo "2 $BOXNUMBER $SCROLLX $SCROLLY"
# read position from cache file, if enabled and existing
[ "$KEY" = '3' ] && echo "-1 0 0 0"
if echo "$FILE" | grep -q edit-1.pdf;
then
echo ${FILE%%-edit-1.pdf}.pdf
else
echo ${FILE%%.pdf}-edit-1.pdf
fi
exit 1
fi
# links in page
if [ "$KEY" = 'l' ];
then
pdfannot -w -d "$FILE" $PAGE > hovacui-out.html
[ $? = 0 ] && rm hovacui-out.html && \
echo -n "no links in page" && exit
if [ "$DISPLAY" = "" ];
then
elinks -remote 'openUrl(file:./hovacui-out.html,new-tab)' \
> /dev/null 2>&1
[ $? != 0 ] && echo -n "elinks not running" && exit
elinks -remote 'reload()'
echo -n "passed links to elinks"
exit
fi
if [ "$BROWSER" != "" ];
then
$BROWSER hovacui-out.html &
echo -n "passed links to $BROWSER"
exit
fi
echo -n "no browser available"
exit
fi
# save a copy of the document
if [ "$KEY" = 's' ];
then
I=1
while [ -e selection-$I.pdf ];
do
I=$((I+1))
done
cp "$FILE" selection-$I.pdf
echo -n "saved as selection-$I.pdf"
exit
fi
# interactively operate on the selected rectangle
if [ "$KEY" = 'E' ];
then
which pdfinteractive > /dev/null || \
{ echo -n 'pdfinteractive not installed'; exit; }
which pdfdrawover > /dev/null || \
{ echo -n 'pdfdrawover not installed'; exit; }
if [ "$DISPLAY" = '' ];
then
pdfinteractive "$@" 3>&1 > /dev/tty
else
xterm -e pdfinteractive "$@" 3>&1
fi
A=$(cat pdfinteractive-ret.txt)
rm -f pdfinteractive-ret.txt
exit $A
fi
# copy text in page or rectangle for pasting
if $YANK false;
then
if [ "$RECT" != "[]" ];
then
BOX="-b $RECT"
else
T=$(echo $TEXTBOX | tr - , | tr -d '[]')
TX1=$(echo $T | cut -d, -f1 | cut -d. -f1)
TY1=$(echo $T | cut -d, -f2 | cut -d. -f1)
TX2=$(echo $T | cut -d, -f3 | cut -d. -f1)
TY2=$(echo $T | cut -d, -f4 | cut -d. -f1)
D=$(echo $DEST | tr - , | tr -d '[]')
DX1=$(echo $D | cut -d, -f1 | cut -d. -f1)
DY1=$(echo $D | cut -d, -f2 | cut -d. -f1)
DX2=$(echo $D | cut -d, -f3 | cut -d. -f1)
DY2=$(echo $D | cut -d, -f4 | cut -d. -f1)
BOX="-b ["
BOX=$BOX$([ $TX1 -gt $DX1 ] && echo $TX1 || echo $DX1),
BOX=$BOX$([ $TY1 -gt $DY1 ] && echo $TY1 || echo $DY1)-
BOX=$BOX$([ $TX2 -lt $DX2 ] && echo $TX2 || echo $DX2),
BOX=$BOX$([ $TY2 -lt $DY2 ] && echo $TY2 || echo $DY2)]
fi
pdftoroff -t -i $PAGE:$PAGE $BOX "$FILE" | \
sed '/./,$ ! d' | $COPIER 2>&1 > /dev/null
[ "$?" = 0 ] && $COPIED false && echo -n "text copied" && exit;
fi
# edit the notes of the pdf file
if $EDIT false;
then
BASE=$(echo "$FILE" | sed -e 's,.pdf,,' -e 's,$,-notes.txt,')
[ "$KEY" = 'N' ] && CHECK=true
if [ -e "$BASE" ];
then
if ! [ -f "$BASE" ];
then
echo -n "$BASE is not a regular file"
exit
fi
if $CHECK false && \
grep -A2 "^## Page $PAGE$" "$BASE" | tail -2 | grep -q "^##";
then
echo -n "no notes for page $PAGE, next: "
sed '
: begin
/\n##/ { P; D; b }
s,\n, ,g
N
b begin
' "$BASE" | \
sed -n "/^## Page $PAGE /,$ p" | \
{ grep -m1 '## Page [0-9]* .' || echo ' none'; } | \
cut -d' ' -f3 | \
tr -d '\n'
exit
fi
else
if $CHECK false;
then
echo -n "no notes for page $PAGE"
exit
fi
echo "# $(basename "$FILE")" > "$BASE"
echo >> "$BASE"
for P in $(seq 1 $TOTPAGES);
do
echo "## Page $P" >> "$BASE"
echo >> "$BASE"
done
echo '## End' >> "$BASE"
fi
LINEFILE=$HOME/.cache/hovacui/line
rm -f $FILELINE
vi +"/## Page $PAGE$" "$BASE" > /dev/tty
setterm --cursor off > /dev/tty
if [ -f $LINEFILE ];
then
# requires, in .vimrc:
# autocmd BufUnload *-notes.txt execute ''
# \| execute '!echo ' . line('.') . ' >
# $HOME/.cache/hovacui/line'
L=$(cat $LINEFILE)
[ "$L" = "" ] && rm -f $LINEFILE && exit 0
N=$(head -n "$L" "$BASE" | grep '^## Page [0-9][0-9]*' | \
tail -1 | cut -d' ' -f3)
[ "$N" = "$PAGE" ] && exit 0
[ "$N" = '' ] && N=1
echo -n "$N 0 0 0"
rm -f $LINEFILE
exit 1
fi
fi