-
Notifications
You must be signed in to change notification settings - Fork 0
/
wikipedia.sh
152 lines (125 loc) · 2.98 KB
/
wikipedia.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
#!/usr/bin/env bash
##
## wikipedia - open specific Wikipedia article from command line
## Copyright (C) 2020-2023 Daniel Haase
##
## 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 <http://www.gnu.org/licenses/gpl.txt>.
##
set -o errexit
set -o nounset
set -o pipefail
set -o noclobber
TITLE="wikipedia"
VERSION="0.3.5"
BROWSER_COMMAND="${BROWSER_COMMAND:-"firefox --new-tab"}"
DEFAULT_LANGUAGE="en"
function check_command {
if ! command -v "${1}" &>/dev/null; then
echo >&2 "no such command \"${1}\""
exit 1
fi
}
function print_version {
cat <<-EOF
${TITLE} ${VERSION}
copyright (c) 2020-2023 Daniel Haase
EOF
}
function print_usage {
print_version
cat <<-EOF
usage: ${TITLE} [-l <language>] <phrase>...
${TITLE} [-V|-h]
<phrase>...
arbitrarily many phrases to search for on Wikipedia
-l <language> | --language <language>
two- or three-digit language code to search Wikipedia in
-V | --version
print version information and exit
-h | --help
print this usage description and exit
EOF
}
function fail_usage {
print_usage >&2
exit 2
}
function prepare_query() {
local -a phrases=("$@")
local query="${phrases[*]}"
query="$(echo -n "${query:0:1}" | tr "[:lower:]" "[:upper:]")${query:1}"
echo "${query[*]// /_}"
}
declare -r command_name="${BROWSER_COMMAND%% *}"
check_command "${command_name}"
check_command "cat"
check_command "tr"
declare language="${DEFAULT_LANGUAGE}"
declare -a phrases=()
declare expanded_command
expanded_command="$(command -v "${command_name}") "
expanded_command+="${BROWSER_COMMAND/#${command_name} /}"
if [[ $# -eq 1 ]]; then
case "${1}" in
-V | --version)
print_version
exit 0
;;
-h | --help)
print_usage
exit 0
;;
-*)
fail_usage
;;
*)
phrases=("${1}")
;;
esac
elif [[ $# -eq 2 ]]; then
case "${1}" in
-*)
fail_usage
;;
*)
phrases=("$@")
;;
esac
elif [[ $# -gt 2 ]]; then
case "${1}" in
-l | --language)
if [[ "${#2}" -lt 2 || "${#2}" -gt 3 ]]; then
fail_usage
fi
language="${2}"
shift 2
;;
-*)
fail_usage
;;
*) ;;
esac
phrases=("$@")
fi
declare url="https://www.wikipedia.org/"
declare query
query="$(prepare_query "${phrases[@]}")"
if [[ -n "${query}" ]]; then
url="https://${language}.wikipedia.org/wiki/${query}"
fi
if ! eval "${expanded_command} ${url} &>/dev/null &"; then
echo >&2 "failed to open browser \"${command_name}\""
exit 3
fi
exit 0