-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwktr
More file actions
executable file
·148 lines (124 loc) · 4.44 KB
/
wktr
File metadata and controls
executable file
·148 lines (124 loc) · 4.44 KB
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
#!/bin/bash
#--------------------------------------------------------------------------
#
# ▌ ▐
# ▌ ▌▌▗▘▜▀ ▙▀▖
# ▐▐▐ ▛▚ ▐ ▖▌
# ▘▘ ▘ ▘ ▀ ▘
#
# Description:
# Translate a term by looking it up in Wiktionary
#
#--------------------------------------------------------------------------
#
# Found this useful? Appalling? Appealing? Please let me know.
# The Unabashed welcomes your impressions.
#
# You will find the
# unabashed
# at the location opposite to
# moc • thgimliam
#
#--------------------------------------------------------------------------
#
# License:
# 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/>.
#
#--------------------------------------------------------------------------
# Program name from its filename
prog=${0##*/}
# Some colors
LIGHTRED='\e[1;31m'
LIGHTPURPLE='\e[1;35m'
YELLOW='\e[1;33m'
NC='\e[0m'
# Calling for help is the same as calling without arguments.
case $1 in --help|-[h?]) $prog; exit 1 ;; esac
# Usage if less than three arguments are given
[[ $# -lt 3 ]] && {
clear
echo -e "
${LIGHTRED}Description:${NC}
Translate a term by looking it up in Wiktionary
${LIGHTRED}Usage:${NC}
${YELLOW}$prog [lang_origin] [lang_destination] terms${NC}
${LIGHTRED}Examples:${NC}
Try this for English-to-Estonian translations:
${YELLOW}$prog en et apple keyboard \"thank you\"${NC}
[en] apple [et] õun
[en] keyboard [et] klaviatuur
[en] thank you [et] aitäh ·· aitüma ·· tänan ·· täname
Try this for English-to-Japanese translations:
${YELLOW}$prog en ja melon ice sushi${NC}
[en] melon [ja] メロン
[en] ice [ja] 氷 ·· アイス ·· ドライアイス ·· アイス ·· アイス ·· 凍る
[en] sushi [ja] 鮨 ·· 鮓 ·· 寿司 ·· すし
${LIGHTRED}Limitations:${NC}
Doesn't behave well with some languages with different dialects, like Chinese.
${LIGHTRED}Dependencies:${NC}
Must have curl and xsel installed.
Try ${YELLOW}sudo apt-get install curl xsel${NC}
"
exit 1
}
#----------------------------------------------------------------------------
# Check for missing commands
needed_commands="curl sed xsel"
missing_counter=0
for needed_command in $needed_commands; do
if ! hash "$needed_command" >/dev/null 2>&1; then
printf "Command not found in PATH: %s\n" "$needed_command" >&2
: $((missing_counter++))
fi
done
if [[ $missing_counter -eq 1 ]]; then
printf "At least %d command is missing, install it\n" "$missing_counter" >&2
exit 1
elif [[ $missing_counter -gt 1 ]]; then
printf "At least %d commands are missing, install them\n" "$missing_counter" >&2
exit 2
fi
#----------------------------------------------------------------------------
lang_orig="$1"
lang_dest="$2"
shift 2
# Temp files
mkdir -p $HOME/tmp &&
tmpfile=$(mktemp --tmpdir=$HOME/tmp ${PROG}.XXXXXXXXXX)
while [[ $# -ne 0 ]]
do
term_orig="$1"
term_dest=$(\
curl -s -L "https://$lang_orig.wiktionary.org/wiki/$term_orig" |
sed 's/<span class/\n&/g' |
sed '/class=.extiw/d' |
grep "lang=.$lang_dest." |
sed 's/ title/\ntitle/g' |
grep -oP 'title="[^"]*">[^<]*(?=<.a><.span>)' |
sed 's/.*>//' |
sed "s/ (page does not exist)//g" |
sed "s/$lang_dest://g" |
sed "s,Special:Search/,,g" |
sed ':a;$!N;s/ *\n\+ */ ·· /;ta;')
input="[$lang_orig] $term_orig"
[[ "$term_dest" == "" ]] &&
output="[$lang_dest] * not found *" ||
output="[$lang_dest] $term_dest"
echo "$input = $output" >> $tmpfile
shift
done
<$tmpfile column -s= -t
rm $tmpfile
exit 0
#------------------END of PROGRAM----------------------------