-
-
Save manboubird/935a387bbc9023bfa828 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function is_command_exists() { | |
if hash $1 2>/dev/null; then echo 0; | |
else echo 1; fi | |
} | |
if [ $(is_command_exists w3m) -eq 0 -a $(is_command_exists rlwrap) -eq 0 -a $(is_command_exists peco) -eq 0 ];then | |
function dic-rlwrap { | |
HISTORY=$HOME/.dic_history | |
rlwrap -H $HISTORY bash -c ' | |
function is_command_exists() { | |
if hash $1 2>/dev/null; then echo 0; | |
else echo 1; fi | |
} | |
function _dic() { | |
URL=$1 | |
QUERY=$2 | |
FILTER=$3 | |
CUT_TAIL_NUM=$4 | |
CSS_SEL=$5 | |
if [ -n "$CSS_SEL" -a $(is_command_exists python) -eq 0 -a $(is_command_exists scrape) -eq 0 -a $(is_command_exists curl) -eq 0 ];then | |
# transformation with css selector | |
# https://github.com/jeroenjanssens/data-science-at-the-command-line/blob/master/tools/scrape | |
curl "${URL}${QUERY}" -s | scrape -be "${CSS_SEL}" | w3m -dump -T text/html | |
else | |
# transformation with grep & cut | |
w3m "${URL}${QUERY}" | grep -A 1000 "${FILTER}" | sed -e :a -e "$d;N;2,${CUT_TAIL_NUM}ba" -e "P;D" | |
fi | |
printf "Cited Link:\n${URL}${QUERY}\n" | |
} | |
function weblio() { | |
CSS_SEL="div#main" | |
_dic "http://ejje.weblio.jp/content/" "$1" "用例" "140" "$CSS_SEL" | less | |
} | |
function alc() { | |
CSS_SEL="div.itemPostion, li > span.midashi, li > div" | |
_dic "http://eow.alc.co.jp/search?q=" "$1" "$1" "12" "$CSS_SEL" | less | |
} | |
DIC=alc | |
while read -p "$DIC > " INPUT; do | |
case "$INPUT" in | |
-ls ) | |
echo "Dictionaries:" | |
for s in alc weblio; do echo "$s"; done | |
continue | |
;; | |
-dic* ) | |
v=$(echo $INPUT | tr -s " " | cut -d" " -f2) | |
case "$v" in | |
"alc" | "weblio" ) | |
DIC=$v | |
echo "Set dictionary to $DIC" | |
;; | |
* ) echo "Unknown dictionary $v" | |
esac | |
continue | |
;; | |
esac | |
$DIC "$INPUT" | peco --prompt "$INPUT >" | |
done | |
' | |
} | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment