-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsearch-docs
executable file
·289 lines (257 loc) · 6.14 KB
/
search-docs
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
#!/bin/bash
# SPDX-FileCopyrightText: 2023 - 2024 sudorook <daemon@nullcodon.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# 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 <https://www.gnu.org/licenses/>.
set -euo pipefail
ROOT="$(dirname "${0}")"
source "${ROOT}"/globals
! check_command find fzf sed && exit 3
#
# Functions
#
function print_usage {
show_header Usage:
cat << EOF
-p|--pin pin selected file(s) to ~/Desktop and ~/Sync (default: false)
-h|--help print (this) message
EOF
}
function get_common_root {
if [ ${#DIRS[@]} -eq 1 ]; then
ROOT="${DIRS[0]}"
DIRS=(".")
else
local root="${DIRS[0]}"
local cur
for cur in "${DIRS[@]}"; do
root="$(printf "%s\n%s\n" "${root}" "${cur}" |
sed -e 'N;s,^\(.*\).*\n\1.*$,\1,')"
done
ROOT="${root%%\/}"
fi
local dir
local new_dirs=()
for dir in "${DIRS[@]}"; do
if [[ "${dir}" = "${ROOT}" ]]; then
DIRS=(".")
return
else
new_dirs+=("${dir##"${ROOT}"\/}")
fi
done
DIRS=("${new_dirs[@]}")
}
function check_dirs {
local dir
local dirs=()
for dir in "${DIRS[@]}"; do
if [ -z "${dir}" ]; then
continue
elif [ -d "${dir}" ]; then
dirs+=("${dir}")
elif [ -s "${dir}" ]; then
if [ -d "$(readlink -f "${dir}")" ]; then
dirs+=("${dir}")
fi
else
show_warning "WARNING: ${dir@Q} not found."
fi
done
DIRS=("${dirs[@]}")
}
function pick_file {
pushd "${ROOT}" > /dev/null
find -L "${DIRS[@]}" -type f -path "*.caltrash*" -prune -o \
\( -iname "*.mobi" \
-o -iname "*pdf" \
-o -iname "*.azw3" \
-o -iname "*.djvu" \
-o -iname "*.html" \
-o -iname "*.epub" \
\) -print |
fzf --layout=reverse --header-first --multi --scheme=path
popd > /dev/null
}
function get_title_calibre {
local file="${1}"
local dir
local meta
dir="$(dirname "${file}")"
meta="${dir}/metadata.opf"
if [ -f "${meta}" ]; then
sed -n "s|\s*<dc:title>\(.*\)</dc:title>|\1|p" "${meta}"
fi
}
function get_title_pdfinfo {
local file="${1}"
if check_command pdfinfo; then
pdfinfo "${file}" | sed -n "s,^Title:\s\+\(.*\),\1,p"
fi
}
function get_title_ebook-meta {
local file="${1}"
if check_command ebook-meta; then
ebook-meta "${file}" | sed -n "s,^Title\s\+:\s\+\(.*\),\1,p"
fi
}
function get_title_html {
local file="${1}"
sed '/\s*<body>/Iq' "${file}" |
sed ':a;N;$!ba;s/\n//g' |
sed -n "s,.*<title>\s*\(.\+\)\s*</title>.*,\1,Ip" |
sed -e "s/&/\&/g" -e "s,<,<,g" -e "s,>,>,g" \
-e "s,<strong>,,Ig" -e "s,</strong>,,Ig" \
-e "s,<code>,,Ig" -e "s,</code>,,Ig"
}
function sanitize_string {
sed \
-e "s/: / - /g" \
-e "s/\"/'/g" \
-e "s/[<>|?\*\\\/]//g"
}
function get_title {
local file
local file_name
local file_ext
local title
file="$(basename "${1}")"
file_name="${file%.*}"
file_ext="${file##*.}"
title="$(get_title_calibre "${1}")"
if [ -z "${title}" ]; then
case "${file_ext,,}" in
pdf)
title="$(get_title_pdfinfo "${1}")"
;;
epub)
title="$(get_title_epub-meta "${1}")"
;;
html)
title="$(get_title_html "${1}")"
;;
esac
fi
if [ -z "${title}" ] || [[ "${title}" =~ .dvi$ ]]; then
title="$(echo "${file_name}" |
sed -n "s/\(.\+ - \)\?\([0-9]\+ - \)\(.*\)/\3/p")"
fi
if [ -z "${title}" ] || [[ "${title}" =~ .dvi$ ]]; then
title="$(echo "${file_name}" |
sed -e "s,.\+ - \(.\+\),\1,g" -e "s,\(.\+\)_.*,\1,g")"
fi
if [ -n "${title}" ]; then
echo "${title}.${file_ext}" | sanitize_string
else
echo "${file}" | sanitize_string
fi
}
function pin_and_sync {
local file_in="${1}"
local file_out
file_out="$(get_title "${file_in}")"
if [ -f "${HOME}/Sync/${file_out}" ]; then
show_warning "WARNING: '~/Sync/${file_out}' already exists. Skipping."
else
cp "${file_in}" "${HOME}/Sync/${file_out}"
fi
pushd "${HOME}/Desktop" > /dev/null
if [ -e "${file_out}" ]; then
show_warning "WARNING: '~/Desktop/${file_out}' already exists. Skipping."
else
ln -s ../Sync/"${file_out}" "${file_out}"
fi
popd > /dev/null
}
function open {
local file="${1}"
local ftype
local app
if check_command xdg-open xdg-mime; then
ftype="$(xdg-mime query filetype "${file}")"
app="$(xdg-mime query default "${ftype}")"
if [ -n "${ftype}" ] && [ -n "${app}" ]; then
(xdg-open "${file}" > /dev/null 2>&1 &)
else
show_warning "No default MIME association for ${file@Q}. Skipping."
return 1
fi
else
return 1
fi
}
#
# Globals
#
DIRS=(
"${calibrelibrarydir:-""}"
"${zoterodatadir:+"${zoterodatadir}"/storage}"
)
PIN=false
ROOT="${HOME}"
#
# Main
#
OPTIONS=hp
LONGOPTIONS=help,pin
PARSED=$(getopt -o "${OPTIONS}" --long "${LONGOPTIONS}" -n "${0}" -- "${@}")
eval set -- "${PARSED}"
while [ ${#} -ge 1 ]; do
case "${1}" in
-h | --help)
print_usage
exit
;;
-p | --pin)
PIN=true
shift
;;
--)
shift
break
;;
*)
show_error "Error"
exit 3
;;
esac
done
#
# Main
#
check_dirs
if [ "${#DIRS[@]}" -eq 0 ]; then
exit 3
fi
get_common_root
if [ -z "${ROOT}" ] || ! [ -d "${ROOT}" ]; then
show_error "ERROR: Parsing common parent directory failed. Exiting."
exit 3
fi
pushd "${ROOT}" > /dev/null
while read -r FILE; do
if [ -z "${FILE}" ]; then
continue
elif ! [ -f "${ROOT}/${FILE}" ]; then
show_error "ERROR: Parsing ${FILE@Q} failed. Skipping."
else
if "${PIN}"; then
pin_and_sync "${FILE}"
else
open "${FILE}"
fi
fi
done <<< "$(pick_file)"
popd > /dev/null