-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdmenu_eo.sh
executable file
·486 lines (425 loc) · 12.5 KB
/
dmenu_eo.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#!/usr/bin/env bash
#
# This program allows the use of dmenu to view information from the ESPDIC
# Copyright (c) 2017 Zachary Matthews.
#
# 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, version 3.
#
# 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/>.
set -euo pipefail
x_system=false
h_system=false
sub_w=false
menu=false
rebuild=false
quiet=false
silent=false
dmenu="dmenu"
# Get default system languae as default locale setting
locale=$(locale | grep "LANG" | cut -d= -f2 | cut -d_ -f1)
build_dicts="es,oc,ko,vi"
# Dictionary cache dir
cachedir=${XDG_CACHE_HOME:-"$HOME/.cache"}
cachedir="$cachedir/dmenu_eo"
# Check cache dir and create if missing
mkdir -p "$cachedir"
installed_cache=$cachedir/installed
declare -A dictnames dictabbrev dictcache sources
# Dictionary names
dictnames["es"]="ESPDIC"
dictnames["oc"]="O'Connor And Hayes"
dictnames["ko"]="Komputeko"
dictnames["vi"]="Vikipedio"
dictabbrev["ESPDIC"]="es"
dictabbrev["O'CONNOR AND HAYES"]="oc"
dictabbrev["KOMPUTEKO"]="ko"
dictabbrev["VIKIPEDIO"]="vi"
dictcache["es"]="$cachedir/espdic"
dictcache["oc"]="$cachedir/oconnor_hayes"
dictcache["ko"]="$cachedir/komputeko"
# Dictionary sources
sources["es"]="http://www.denisowski.org/Esperanto/ESPDIC/espdic.txt"
sources["oc"]="http://www.gutenberg.org/files/16967/16967-0.txt"
sources["ko"]="https://komputeko.net/Komputeko-ENEO.pdf"
vikipedio_search="https://eo.wikipedia.org/w/api.php?action=opensearch&search="
# Set default dictionary
choice=""
print_usage() {
echo "Usage: dmenu_eo [OPTION]..."
echo "Options(Agordoj):"
echo " -d, --dict=DICT the DICT to be browsed(options below)"
echo " --vortaro=DICT la DICT foliota(elektoj malsupre)"
echo " --en display all messages in English"
echo " prezenti ĉiujn mesaĝojn angle"
echo " --eo display all messages in Esperanto"
echo " prezenti ĉiujn mesaĝojn Esperante"
echo " --help display this help message"
echo " --helpi prezenti ĉi tiun mesaĝon de helpo"
echo " -h, --hsystem add H-system entries to dictionary(during re/build)"
echo " --hsistemo aldoni H-sistemajn vortarerojn(dum re/konstrui)"
echo " -m, --menu select dictionary to browse from a menu"
echo " --menuo elekti vortaron por folii per menuo"
echo " -q, --quiet suppress all messages, except error messages"
echo " --mallaŭta kaŝi ĉiujn mesaĝojn, krom eraraj mesaĝoj"
echo " -r, --rebuild rebuild dictionary with specified systems"
echo " --rekonstrui rekonstrui vortaron per difinitaj sistemoj"
echo " --rofi override the default and use rofi instead of dmenu"
echo " transpasi la defaŭlto kaj uzi rofi anstataŭ dmenu"
echo " --silent supress all messages"
echo " --silenta kaŝi ĉiujn mesaĝojn"
echo " --version show the version information for dmenu_eo"
echo " --versio elmontri la versia informacio de dmenu_eo"
echo " -w use w when building with the X-system instead of ux"
echo " uzi w anstataŭ ux kiam konstruanta per X-sistemo"
echo " -x, --xsystem add X-system entries to dictionary(during re/build)"
echo " --xsistemo aldoni X-sistemajn vortarerojn(dum re/konstrui)"
echo ""
echo "Dictionaries(Vortaroj):"
echo " ES: ESPDIC"
echo " OC: O'Connor and Hayes Dictionary"
echo " KO: Komputeko"
echo " VI: Vikipedio"
echo ""
echo "Exit Status(Elira Kodo):"
echo " 0 if OK"
echo " 0 se bona"
echo " 1 if general problem"
echo " 1 se ĝenerala problemo"
echo " 2 if serious problem"
echo " 2 se serioza problemo"
echo " 64 if programming issue"
echo " 64 se problemo de programado"
exit 0
}
print_version() {
echo "dmenu_eo, version 0.1"
echo "Copyright (C) 2016-2018 Zachary Matthews"
echo "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>"
echo ""
echo "This is free software; you are free to change and redistribute it."
echo "There is NO WARRANTY, to the extent permitted by law."
exit 0
}
print_std() {
if (! $quiet && ! $silent); then
# If enough parameters and locale is eo, else use en
if [ "$#" -gt "1" ] && [[ "$locale" == "eo" ]]; then
echo "$2"
else
echo "$1"
fi
fi
}
print_err() {
if (! $silent); then
# If enough parameters and locale is eo, else use en
if [ "$#" -gt "1" ] && [[ "$locale" == "eo" ]]; then
echo "$2" 1>&2
else
echo "$1" 1>&2
fi
fi
}
check_valid() {
# returns 0 if dictionary is available in dictnames
if [[ -v dictnames["$1"] ]]; then
return 0;
# Written in non-standard way because standard version always returns failure
elif [[ ${dictabbrev["${1^^}"]+0} ]]; then
return 0;
else
print_err "$1 is not a valid dictionary" "$1 ne estas valida vortaro"
return 1;
fi
}
build_dictionaries() {
inst_list=()
IFS=","
for dict in $build_dicts; do
build_dictionary "$dict"
inst_list+=("${dictnames["$dict"]}")
done
# Write list of installed dictionaries
printf "%s\\n" "${inst_list[@]}" > "$installed_cache"
}
build_dictionary() {
check_valid "$1"
if [[ $1 == "es" ]] ||
[[ $1 == "oc" ]] ||
[[ $1 == "ko" ]]; then
download_dictionary "$1"
format_dictionary "$1"
fi
}
download_dictionary() {
print_std "Downloading ${dictnames["$1"]}..." "Elŝutas ${dictnames["$1"]}..."
wget -o /dev/null -O "${dictcache["$1"]}_pre" ${sources["$1"]} >> /dev/null
# shellcheck disable=SC2181
if [ "$?" -ne 0 ]; then
print_err "Wget of ${dictnames["$1"]} failed." "Wget de ${dictnames["$1"]} paneis."
exit 1
else
print_std " Done" " Finita"
fi
}
format_dictionary() {
print_std "Formatting ${dictnames["$1"]}..." "Preparas ${dictnames["$1"]}..."
if [[ $1 == "es" ]]; then
# Convert DOS newline to Unix
sed 's/.$//' "${dictcache["es"]}_pre" |
# Remove header
sed '/ESPDIC/d' >> "${dictcache["es"]}"
fi
if [[ $1 == "oc" ]]; then
sed 's/.$//' "${dictcache["oc"]}_pre" |
# Clean O'Connor/Hayes preamble
sed '/= A =/,$!d' |
# Clean O'Connor/Hayes after dictionary
sed '/\*/,$d' |
# Clear extra lines
sed '/^\s*$/d' |
# Remove extra .'s
sed -r 's/(\.|\. \[.+)$//g' >> "${dictcache["oc"]}"
fi
if [[ $1 == "ko" ]]; then
# Convert Komputeko to text
pdftotext -layout "${dictcache["ko"]}_pre" - |
# Clear Formatting lines
sed -r '/(^\s|^$)/d' |
# Clear Header
sed '/^speco/d' |
# Remove part of speech definitions
sed -r 's/^[a-z] {2,}//' |
# Replace first multispace per line with :
sed -r 's/ {2,}/: /' |
# Replace remaining multispace per line with ,
sed -r 's/ {2,}/, /g' >> "${dictcache["ko"]}"
fi
rm "${dictcache["$1"]}_pre"
format_hat_system "${dictcache["$1"]}"
print_std " Done" " Finita"
}
format_hat_system() {
if ($x_system); then
if ($sub_w); then
u_sub='s/\xc5\xad/w/g; s/\xc5\xac/W/g;'
else
u_sub=' s/\xc5\xad/ux/g; s/\xc5\xac/UX/g;'
fi
# Add lines using X-system to dictionary
sed -i -e "/\\xc4\\x89\\|\\xc4\\x9d\\|\\xc4\\xb5\\|\\xc4\\xa5\\|\\xc5\\xad\\|\\xc5\\x9d\\|\\xc4\\xa4\\|\\xc4\\x88\\|\\xc4\\x9c\\|\\xc4\\xb4\\|\\xc5\\x9c\\|\\xc5\\xac/{p; s/\\xc4\\x89/cx/g; s/\\xc4\\x9d/gx/g; s/\\xc4\\xb5/jx/g; s/\\xc4\\xa5/hx/g; s/\\xc5\\x9d/sx/g; s/\\xc4\\x88/CX/g; s/\\xc4\\x9c/GX/g; s/\\xc4\\xa4/HX/g; s/\\xc4\\xb4/JX/g; s/\\xc5\\x9c/SX/g; $u_sub}" "$1"
fi
if ($h_system); then
# Add lines using H-system to dictionary
sed -i -e '/\xc4\x89\|\xc4\x9d\|\xc4\xb5\|\xc4\xa5\|\xc5\xad\|\xc5\x9d\|\xc4\xa4\|\xc4\x88\|\xc4\x9c\|\xc4\xb4\|\xc5\x9c\|\xc5\xac/{p; s/\xc4\x89/ch/g; s/\xc4\x9d/gh/g; s/\xc4\xb5/jh/g; s/\xc4\xa5/hh/g; s/\xc5\xad/u/g; s/\xc5\x9d/sh/g; s/\xc4\xa4/Hh/g; s/\xc4\x88/Ch/g; s/\xc4\x9c/Gh/g; s/\xc4\xb4/Jh/g; s/\xc5\x9c/Sh/g; s/\xc5\xac/U/g;}' "$1"
fi
}
rebuild_dictionary() {
# Remove old dictionaries
for dict in ${dictcache[*]}; do
rm -f "$dict"
done
# Build dictionary
build_dictionaries
exit 0
}
check_dictionaries() {
while read -r entry; do
check_valid "$entry" || return
dict=${dictabbrev["${entry^^}"]}
if [[ $dict == "es" ]] ||
[[ $dict == "oc" ]] ||
[[ $dict == "ko" ]]; then
check_dictionary "$dict"
fi
done < "$installed_cache"
}
check_dictionary() {
if [[ ! -f ${dictcache["$1"]} || ! -s ${dictcache["$1"]} ]]; then
print_std "Building missing dictionary" "Konstruas mankan vortaron"
build_dictionary "$1"
fi
}
check_depends() {
check_depend "wget"
check_depend "dmenu"
}
check_depend() {
if ! type "$1" &> /dev/null; then
print_err "${1^} is not installed. Please install $1." "${1^} ne estas instalita. Bonvolu instali $1."
exit 1
fi
}
search_vikipedio() {
check_depend "jq"
cmd="$dmenu -p \"Vikipedio:\" < /dev/null"
input=$(eval "$cmd")
if [ -z "$input" ]; then
exit 0
fi
declare -A results
IFS=$'\n'
# Get search results from vikipedio
search=$(wget -o /dev/null -O - "$vikipedio_search$input")
# Get array of search results with corresponding URLs
mapfile -t keys < <(jq -r '.[1]|join("\n")' <<< "$search")
mapfile -t vals < <(jq -r '.[3]|join("\n")' <<< "$search")
# If no results found
if [ -v keys ]; then
if [[ "$locale" == "eo" ]]; then
msg="Ne trovitaj rezultoj"
else
msg="No results found"
fi
$dmenu <<< "$msg" >> /dev/null
exit 0
fi
# Combine keys with values for simple map of results
for ((i=0; i < ${#keys[*]}; i++)); do
results["${keys[i]}"]=${vals[i]}
done
# Select link to open
cmd="$dmenu -l 10 <<< \"${keys[*]}\""
input=$(eval "$cmd")
if [ -z "$input" ]; then
exit 0
fi
# Open selected link
xdg-open "${results[$input]}"
}
get_choice() {
if [ -n "$choice" ]; then
print_err "A dictionary option has already been chosen. Only use one flag of -m or -d." "Elekto de vortaro jam elektis. Nur uzu unu flagon de -m aŭ -d."
exit 1
fi
check_valid "$1"
case ${1^^} in
ES|ESPDIC)
choice="${dictcache["es"]}"
;;
OC|O\'CONNOR\ AND\ HAYES)
choice="${dictcache["oc"]}"
;;
KO|KOMPUTEKO)
choice="${dictcache["ko"]}"
;;
VI|VIKIPEDIO)
search_vikipedio
exit 0
;;
"")
# If escape is pressed, quit
exit 0
;;
*)
print_err "$1 is not a valid option for a dictionary." "$1 ne estas valida elekto por vortaro."
exit 1;
;;
esac
}
menu() {
# Open select menu
cmd=$(echo -e "$dmenu -i -l 10 < \"$installed_cache\"")
get_choice "$(eval "$cmd")"
}
main() {
check_depends
# Getopt
local short=d:hmqrwx
local long=dict:,en,eo,vortaro:,hsystem,hsistemo,menu,menuo,quiet,mallauxta,mallauta,mallaŭta,rebuild,rekonstrui,rofi,silent,silenta,xsystem,xsistemo,help,helpi,version,versio
parsed=$(getopt --options $short --longoptions $long --name "$0" -- "$@")
# shellcheck disable=SC2181
if [[ $? != 0 ]]; then
# Getopt not getting arguments correctly
exit 2
fi
eval set -- "$parsed"
# Deal with command-line arguments
while true; do
case $1 in
-d|--dict|--vortaro)
get_choice "$2"
shift
;;
--en)
locale="en"
;;
--eo)
locale="eo"
;;
--help|--helpi)
print_usage
;;
-h|--hsystem|--hsistemo)
h_system=true
;;
-m|--menu|--menuo)
menu=true
;;
-q|--quiet|--mallauxta|--mallauta|--mallaŭta)
quiet=true
;;
-r|--rebuild|--rekonstrui)
rebuild=true
;;
--rofi)
check_depend "rofi"
dmenu="rofi -dmenu"
;;
--silent|--silenta)
silent=true
;;
--version|--versio)
print_version
;;
-w)
sub_w=true
;;
-x|--xsystem|--xsistemo)
x_system=true
;;
--)
shift
break
;;
*)
# Unknown option
print_err "$1 argument not properly handled." "$1 argumento ne prave uzis."
exit 64
;;
esac
shift
done
if ($rebuild); then
rebuild_dictionary
fi
if ($menu); then
menu
fi
# If no dictionaries are installed
if [[ ! -f $installed_cache || ! -s $installed_cache ]]; then
# Assume X-system by default, unless h-system is set
if ! ($h_system); then
x_system=true
fi
build_dictionaries
fi
# Check for missing dictionaries
check_dictionaries
# If no dictionary has been selected
if [ -z "$choice" ]; then
get_choice "$(head -1 "$installed_cache")"
fi
# Display dictionary
cmd="$dmenu -l 10 < \"$choice\" >> /dev/null"
eval "$cmd"
}
main "$@"