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
|
#!/bin/bash --
#
# Shell script to query the Wikipedia.
#
# It can be used to output Wikipedia articles to the console, but can also
# just open the article in any browser.
#
# Author: Christian Brabandt <cb@256bit.org>
# License: Public Domain
VERSION=0.05
set -e
function display_help(){
cat << EOF
NAME
This script uses text-browser to query and render Wikipedia
articles. The output will be printed to standard out.
SYNOPSIS
`basename $0` [-BCnNoOpPsSuU] [-b prog] [-c patt] [-i patt] [-l lang] query
`basename $0` -o [-b prog] [-l lang] query
`basename $0` [-h]
`basename $0` -v
-n do not colorize -N simple colorization (alias -C)
-p display using a pager -P don't use pager
-o open Wikipedia article in browser -O don't open in browser
-s display only a summary -S display whole article
-u Just output the query URL -U display article, not only url
-v display version -h display help
-i patt colorize pattern (case insensitive)
-I patt colorize pattern (case-sensitive, alias -c)
-b prog use prog as browser (by default to invoke elinks, links2,
links, lynx or w3m, if found)
-l lang use language (currently supported are: af, als, ca, cs, da,
de, en, eo, es, fi, fr, hu, ia, is, it, la, lb, nds, nl, nn,
no, pl, pt, rm, ro, simple, sk, sl, sv, tr)
Query can be any term to search for at Wikipedia. Special
characters will be taken care of. Note that only one query term is
supported, however this term can consist of one or more words.
Configuration can also be controlled by creating a runcontrol file
.`basename $0`rc your home directory.
Note that when requesting to open the article in a browser, other
parameters will be ignored. The same holds for the options -h and
-v.
EOF
}
function getVersion(){
cat <<EOF
$(basename "${0}") Version: ${VERSION}
EOF
}
function errorExit(){
echo "$*" >&2
exit 3
}
function colorize(){
if [ "${IGNCASE}" = "true" ]; then
OUTPUT=$(echo -e "`cat`"|sed -s "s|\(${PATT}\)|\\\033\[0;31m\1\\\033\[0m|gi")
else
OUTPUT=$(echo -e "`cat`"|sed -s "s|\(${PATT}\)|\\\033\[0;31m\1\\\033\[0m|g")
fi
echo -e "${OUTPUT}"
}
function stripOutput(){
if [ "${LOCAL}" = "de" ]; then
MARKER='Diese Seite'
MARKER2='Bearbeiten'
elif [ "${LOCAL}" = "en" -o "${LOCAL}" = "simple" ]; then
MARKER='Views'
MARKER2='edit'
elif [ "${LOCAL}" = "fr" ]; then
MARKER='Affichages'
MARKER2='modifier'
elif [ "${LOCAL}" = "nl" ]; then
MARKER='Views'
MARKER2='bewerk'
elif [ "${LOCAL}" = "sv" ]; then
MARKER='Visningar'
MARKER2='redigera'
elif [ "${LOCAL}" = "es" ]; then
MARKER='Views'
MARKER2='editar'
elif [ "${LOCAL}" = "pt" ]; then
MARKER='Vistas'
MARKER2='editar'
elif [ "${LOCAL}" = "pl" ]; then
MARKER='Views'
MARKER2='Edytuj'
elif [ "${LOCAL}" = "it" ]; then
MARKER='Views'
MARKER2='modifica'
elif [ "${LOCAL}" = "da" ]; then
MARKER='Views'
MARKER2='redigr\\|rediger'
elif [ "${LOCAL}" = "eo" ]; then
MARKER='Vidoj'
MARKER2='redaktu'
elif [ "${LOCAL}" = "no" ]; then
MARKER='Visninger'
MARKER2='rediger'
elif [ "${LOCAL}" = "nn" ]; then
MARKER='Visningar'
MARKER2='endre'
elif [ "${LOCAL}" = "fi" ]; then
MARKER='Views'
MARKER2='muokkaa'
elif [ "${LOCAL}" = "ca" ]; then
MARKER='Views'
MARKER2='edita'
elif [ "${LOCAL}" = "ro" ]; then
MARKER='Vizualizari'
MARKER2='modifica'
elif [ "${LOCAL}" = "cs" ]; then
MARKER='Zobrazen'
MARKER2='editovat'
elif [ "${LOCAL}" = "sk" ]; then
MARKER='Zobrazen'
MARKER2='prava'
elif [ "${LOCAL}" = "sl" ]; then
MARKER='Ogledov'
MARKER2='spremeni'
elif [ "${LOCAL}" = "lb" ]; then
MARKER='Views'
MARKER2='nneren'
elif [ "${LOCAL}" = "la" ]; then
MARKER='Views'
MARKER2='recensere'
elif [ "${LOCAL}" = "rm" ]; then
MARKER='Views'
MARKER2='edit'
elif [ "${LOCAL}" = "ia" ]; then
MARKER='Views'
MARKER2='modificar'
elif [ "${LOCAL}" = "is" ]; then
MARKER='Views'
MARKER2='breyta'
elif [ "${LOCAL}" = "hu" ]; then
MARKER='Views'
MARKER2='szerkeszts'
elif [ "${LOCAL}" = "tr" ]; then
MARKER='Views'
MARKER2='degistir'
elif [ "${LOCAL}" = "af" ]; then
MARKER='Views'
MARKER2='wysig'
elif [ "${LOCAL}" = "nds" ]; then
MARKER='Views'
MARKER2='nnern'
elif [ "${LOCAL}" = "als" ]; then
MARKER='Views'
MARKER2='ndere'
else
MARKER='Views\\|References\\|Visible links'
fi
# Now comes the magic: Strip everything from Marker to end,
# cause this is only the linkdump
SED='sed -e "/${MARKER}/,$ D" -e "s|\\[[0-9]*\\]||g" -e "s|\\[IMG\\]||g"'
if [ -n "${MARKER2}" ]; then
echo "`cat`"| eval ${SED} -e '"s|\[${MARKER2}\]||g"'
else
echo "`cat`"| eval ${SED}
fi
}
function openurl(){
"${BROWSER}" "${URL}"
}
function summary(){
if [ "${COLOR}" = "true" ]; then
"${BROWSER}" -dump "${URL}" |grep -v copyright | head -n 22 \
| tail -n 17 |stripOutput | colorize
else
"${BROWSER}" -dump "${URL}" |grep -v copyright | head -n 22 \
| tail -n 17 | stripOutput
fi
}
function getInfo(){
LINES=$("${BROWSER}" -dump "${URL}" |wc -l)
LINES=$(expr ${LINES} - 6)
if [ "${COLOR}" = "false" ]; then
"${BROWSER}" -dump "${URL}"| tail -n ${LINES} |stripOutput
else
"${BROWSER}" -dump "${URL}"| tail -n ${LINES} |stripOutput |colorize
fi
}
# First read in the Run configuration File, if one is found
if [ -r ~/.`basename $0`rc ]; then
source ~/.`basename $0`rc
fi
# Process commandline parameters
while getopts "BCnNoOpPsSuUvhl:b:c:i:B:" ARGS
do
case ${ARGS} in
b) ABROWSER=${OPTARG} ;;
B) ABROWSER='' ;;
c) IGNCASE="false";COLOR="true"; PATT=${OPTARG} ;;
C) COLOR="true" ;;
i) IGNCASE="true";COLOR="true"; PATT=${OPTARG} ;;
I) IGNCASE="false";COLOR="true"; PATT=${OPTARG} ;;
l) LOCAL=${OPTARG} ;;
n) COLOR="false" ;;
N) COLOR="true" ;;
o) USEBROWSER="true" ;;
O) USEBROWSER="false" ;;
p) PAGER="true" ;;
P) PAGER="false" ;;
s) SHORT="true" ;;
S) SHORT="false" ;;
u) OUTPUTURL="true" ;;
U) OUTPUTURL="false" ;;
v) getVersion; exit 0 ;;
h) display_help; exit 0 ;;
*) display_help; exit 1 ;;
esac
done
shift `expr ${OPTIND} - 1`
# Setting Up some Variables, to determine, what actually to do
if [ -z "$1" ]; then
display_help
exit 1;
fi
IGNCASE=$(echo ${IGNCASE:="false"})
PAGER=$(echo ${PAGER:="false"})
if [ "$PAGER" = "true" ]; then
{ PAGER=$(which less) || PAGER=$(which more) ; } #|| errorExit "Kein Pager gefunden!" ;
fi
#fi
PAGER=$(echo ${PAGER/less/less -Rr})
COLOR=$(echo ${COLOR:="false"})
if [ "$COLOR" = "true" -a -z "${PATT}" ]; then
PATT="$*"
fi
# Per default we use the german localized version of
# Wikipedia
LOCAL=$(echo ${LOCAL:="en"})
# Check for Alternative Browser
if [ -n "${ABROWSER}" ]; then
BROWSER=$(which "${ABROWSER}") || errorExit "${ABROWSER} nicht gefunden"
else
{ BROWSER=$(which elinks) ||
BROWSER=$(which links2) ||
BROWSER=$(which lynx) ||
BROWSER=$(which w3m) ||
BROWSER=$(which links.main) ||
BROWSER=$(which links) ; } || errorExit "kein Browser gefunden"
fi
# Open page in Browser?
USEBROWSER=$(echo ${USEBROWSER:="false"})
# Output only a summary?
SHORT=$(echo ${SHORT:="false"})
# Output only the URL?
OUTPUTURL=$(echo ${OUTPUTURL:="false"})
# Now translate the input into something, we can use for the Wikipedia
ARGUMENT="$(echo "$*" |sed -e 's||%C3%BC|g' -e 's||%C3%A4|g' -e 's||%C3%B6|g' -e 's|[[:space:]]|_|g' -e 's|\"||g' -e 's||%C3%9F|g')"
LOCAL="$(echo "${LOCAL}"|tr [:upper:] [:upper:])"
URL="http://${LOCAL}.wikipedia.org/wiki/${ARGUMENT}"
#errorExit "PAGER: $PAGER Browser: $BROWSER Local: $LOCAL COLOR: $COLOR PATT: $PATT IGNCASE: $IGNCASE"
# Depending on some Variables, we do some different things here
if [ "${USEBROWSER}" = "true" ]; then
openurl
exit 0;
fi
if [ "${SHORT}" = "true" ]; then
summary
exit 0;
fi
if [ "${OUTPUTURL}" = "true" ]; then
if [ "${COLOR}" = "false" ]; then
echo "${URL}"
else
echo -e "\033[0;34m${URL}\033[0m"
fi
exit 0;
fi
if [ "$PAGER" != "false" ]; then
getInfo | eval ${PAGER}
else
getInfo
fi
# vim: ft=sh
|