-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.sh
386 lines (320 loc) · 10 KB
/
common.sh
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
createTmpDir () {
# Create temporary dir
tmpDir='pdfForts.XXXXXXXXXX';
tmpStorage=$(mktemp -t -d "${tmpDir}") || {
guiError "Couldn't create temporary dir"
exit 1;
}
}
deleteTmpDir () {
trap 'rm -rf "${tmpStorage}"' 0 # remove directory when script finishes
trap 1 2 3 15 # terminate script when receiving signal
}
checkConfig () {
curScript="${0}"
fullName=${curScript##*/}
baseName="${fullName%.*}"
# Test if config exists
userConfig="${HOME}/.pdfForts/${baseName}.conf"
if [[ ! -f "${userConfig}" ]]; then
# User config does not exist, copy default to user
mkdir -p "${HOME}/.pdfForts/"
cp "/usr/share/kservices5/ServiceMenus/pdfForts/${baseName}.conf" "${userConfig}"
guiInfo "No configuration file for ${baseName} has been found.
A default one was copied to ${userConfig}.
Please edit that one to suit your needs.
Next Kate will be opened with the config file."
kate -b "${userConfig}"
guiInfo "You have now a user config for ${baseName}.
Retry again on the PDF files."
exit;
fi
source "${userConfig}"
}
getSaveFile () {
fFile="${1}"
fMessage="${2}"
fExt="${3}"
fName=${fFile%.*}
saveFile=$(guiFileSave "${fName} - ${fMessage}.${fExt}") || exit;
}
getFileInfo () {
filePath=${1%/*}"/"
fileName=${1##*/}
fileBase=${fileName%%.*}
fileExt=${name##*.}
}
padZero () {
# Pad the counting to 50 numbers
unset "paddedCount"
paddedCount=$(printf "%020d%s\n" "${1}")
}
sortFiles () {
files="${1}"
# Create empty array
declare -a filesUnsorted=();
# Loop through files
for files; do
# Test if it is a file
if [[ -f "${files}" ]]; then
filesUnsorted=("${filesUnsorted[@]}" "${files}")
fi
done
# Sort selected files alphabetically
readarray -t filesSorted < <(for a in "${filesUnsorted[@]}"; do printf "%s\n" "${a}"; done | sort)
}
extractMetaData () {
curFile="${1}"
pdftk "${curFile}" dump_data > "${metaFile}.html"
cat "${metaFile}.html" | recode html..utf8 > "${metaFile}"
pdftk "${curFile}" cat output "${noBookmark}"
}
convertSpecialCharsToASCII () {
curTitle="${1}"
# Create associative array where key is the string to look for and value the replacement
declare -A replArray
replArray[ä]='\344'
replArray[ö]='\366'
replArray[ü]='\374'
replArray[Ä]='\304'
replArray[Ö]='\326'
replArray[Ü]='\334'
replArray[ß]='\337'
for k in "${!replArray[@]}"; do
curTitle="${curTitle//$k/${replArray[$k]}}"
done
}
convertMetaToBookmark () {
unset "curTitle"
unset "curLvl"
unset "curNr"
unset "haystack"
mapfile -t haystack < "${1}"
needle="BookmarkTitle"
rm "${bookMarks}"
if [[ "${docBookmarks}" -eq "1" ]]; then
if [[ "${levelBookmarks}" -eq "1" ]]; then
adjustLvl="1"
printf "%s\n" "+-|${fileBase}|1" >> "${bookMarks}"
else
printf "%s\n" "+-|${fileBase}|1" >> "${bookMarks}"
fi
fi
for ((i=0; i < ${#haystack[@]}; ++i)); do
# Get pages number
if [[ "${haystack[$i]}" =~ "NumberOfPages:" ]]; then
curDocPages="${haystack[$i]:15}"
fi
# Get bookmark meta data
if [[ "${haystack[$i]}" =~ "${needle}" ]]; then
curTitle="${haystack[$i]:15}"
j=$[i+1]
k=$[i+2]
if [[ "${haystack[$j]}" =~ "BookmarkLevel:" ]]; then
tmpLvl="${haystack[$j]:15}"
curLvl=$((tmpLvl + adjustLvl))
fi
if [[ "${haystack[$k]}" =~ "BookmarkPageNumber:" ]]; then
curNr="${haystack[$k]:20}"
fi
curDash=""
while [[ "${curLvl}" -gt "1" ]]; do
curDash="${curDash}--"
((curLvl--))
done
printf "%s\n" "+-${curDash}|${curTitle}|${curNr}" >> "${bookMarks}"
fi
done
}
convertBookmarkToPdfmark () {
bookmarkFile="${1}"
unset "titleArr"
declare -A "titleArr"
unset "levelArr"
declare -A "levelArr"
unset "numberArr"
declare -A "numberArr"
unset "outputArr"
declare -A "outputArr"
unset "lvl"
declare -A "lvl"
unset "fileContent"
unset "collapseArr"
declare -A "collapseArr"
numberOfPages=0
mapfile -t fileContent < "${bookmarkFile}"
bookmarkLine=1
for lineContent in "${fileContent[@]}"; do
unset arrLine
IFS='|' read -ra arrLine <<< "${lineContent}"
curDash="${#arrLine[0]}"
curLvl=$((curDash / 2 - 1))
curCollapse="${arrLine[0]:0:1}"
if [[ "${curCollapse}" == "+" ]]; then
collapseArr["${bookmarkLine}"]="-1"
else
collapseArr["${bookmarkLine}"]="1"
fi
curTitle="${arrLine[1]}"
convertSpecialCharsToASCII "${curTitle}"
curNr="${arrLine[2]}"
levelArr["${bookmarkLine}"]="${curLvl}"
titleArr["${bookmarkLine}"]="${curTitle}"
numberArr["${bookmarkLine}"]="${curNr}"
((bookmarkLine++))
done
# Loop through the array in reverse order
lastLevel=0
for ((x=${#titleArr[@]}; x >= 1; --x)); do
curTitle="${titleArr[$x]}"
curLevel="${levelArr[$x]}"
curNumber="${numberArr[$x]}"
curCollapseMultiplier="${collapseArr[$x]}"
# Equal level or sublevel
if [[ "${curLevel}" -ge "${lastLevel}" ]]; then
page=$((curPage + curNumber))
lvl[$curLevel]=$(( lvl[$curLevel] += 1 ))
outputArr[$x]="[ /Title (${curTitle}) /Page ${page} /OUT pdfmark"
lastLevel="${curLevel}"
fi
# Parent level
if [[ "${curLevel}" -lt "${lastLevel}" ]]; then
page=$((curPage + curNumber))
lvl[$curLevel]=$(( lvl[$curLevel] += 1 ))
subLvl=$((${curLevel}+1))
countLvl="${lvl[${subLvl}]}"
countLvl=$((countLvl * curCollapseMultiplier))
outputArr[$x]="[ /Count ${countLvl} /Page ${page} /Title (${curTitle}) /OUT pdfmark"
lvl[${subLvl}]="0"
lastLevel="${curLevel}"
fi
done
for (( a = 0 ; a <= ${#outputArr[@]} ; a++ )); do
if [[ "${outputArr[$a]}" ]]; then
printf "%s\n" "${outputArr[$a]}" >> "${tmpStorage}/pdfmarks"
fi
done
}
selectTemplate () {
# Prompt to use default template or custom template
tplSelect=$(guiYesNo "Select Default Template" "Press YES if you want to use the default template located at ${defaultTemplate}
Press NO to select a different template.
NOTICE: All '_replace_' strings in the selected template will be replaced by a string selected later")
case "${tplSelect}" in
1) # Yes selected
tplSelected="${defaultTemplate}"
;;
2) # No selected
tplSelected=$(guiFileSelect "${HOME}") || exit;
;;
esac
}
checkPrograms () {
# Check for KDialog or Zenity
type -P "kdialog" &>/dev/null && continue || {
type -P "zenity" &>/dev/null && continue || {
printf "%s\n" "Couldn't find KDialog or Zenity on your system." > "/tmp/pdfFortsError.txt"
printf "%s\n" "Please install and re-run this script." >> "/tmp/pdfFortsError.txt"
kate -b "/tmp/pdfFortsError.txt"
exit 1;
}
}
# Check other required programs
for curCmd in ${reqCmds}; do
type -P ${curCmd} &>/dev/null && continue || {
guiError "Couldn't find the '${curCmd} program.
Please install and re-run this script.";
}
done
}
chkConfOption () {
chkOption="${1}"
confValue="${2}"
if [[ "${chkOption}" == "" ]]; then
printf "%s\n" " " >> "${userConfig}"
printf "%s\n" "${confValue}" >> "${userConfig}"
guiInfo "Added a new option to the end of your config file located at: "${userConfig}".
Kate will be launched for you to review."
kate -b "${userConfig}"
fi
}
chkCancelButton () {
if [[ "${1}" -eq 1 ]]; then
guiError "Abort. Cancel button pressed."
exit 1;
fi
}
guiError () {
if type kdialog &>/dev/null; then
kdialog --error "${1}"
else
zenity --error --text="${1}"
fi
exit 1;
}
guiInfo () {
if type kdialog &>/dev/null; then
kdialog --msgbox "${1}"
else
zenity --info --text="${1}"
fi
}
guiPassword () {
local output
if type kdialog &>/dev/null; then
output=$(kdialog --title "${1}" --password "${2}")
chkCancelButton "${?}"
else
output=$(zenity --password --title "${1}")
chkCancelButton "${?}"
fi
printf "%s" "${output}"
}
guiFileSelect () {
local output
if type kdialog &>/dev/null; then
output=$(kdialog --getopenfilename "${1}")
chkCancelButton "${?}"
else
output=$(zenity --file-selection --filename="${1}/")
chkCancelButton "${?}"
fi
printf "%s" "${output}"
}
guiFileSave () {
local output
if type kdialog &>/dev/null; then
output=$(kdialog --getsavefilename "${1}") || exit;
chkCancelButton "${?}"
else
output=$(zenity --file-selection --save --filename="${1}")
chkCancelButton "${?}"
fi
printf "%s" "${output}"
}
guiInput () {
local output
if type kdialog &>/dev/null; then
output=$(kdialog --title "${1}" --inputbox "${2}" "${3}")
chkCancelButton "${?}"
else
output=$(zenity --entry --title "${1}" --text="${2}" --entry-text="${3}")
chkCancelButton "${?}"
fi
printf "%s" "${output}"
}
guiYesNo () {
local output
if type kdialog &>/dev/null; then
output=$(kdialog --radiolist "${1}" 1 "Yes" on 2 "No" off) || exit;
chkCancelButton "${?}"
else
output=$(zenity --list --radiolist --text "${1}" --hide-header --column "1" --column "2" TRUE "Yes" FALSE "No") || exit;
chkCancelButton "${?}"
case "${output}" in
"Yes") output="1" ;;
"No") output="2" ;;
esac
fi
printf "%s" "${output}"
}