fix url patterns with escape/unescape magic

This commit is contained in:
Jorge Morante 2016-05-03 09:05:05 +02:00
parent ae2be9e8f7
commit b54d2d5e41
1 changed files with 22 additions and 4 deletions

View File

@ -1,6 +1,8 @@
#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
COLON_ESCAPE_SEQUENCE="!!!!COLON!!!!"
HINTS=(p o i u l k j t r e wj wt wr we ww wq wf wd ws wa qp qo qi qu ql qk qj qt qr qe qw qq qf qd qs qa fp fo fi fu fl fk fj ft fr fe fw fq ff fd fs fa dp do di du dl dk dj dt dr de dw dq df dd ds da sp so si su sl sk sj st sr se sw sq sf sd ss sa ap ao ai au al ak aj at ar ae aw aq af ad as aa)
match_lookup_table=''
@ -12,9 +14,22 @@ function highlight() {
printf "\033[1;33m%s\033[0m" "$1"
}
function escape_colons(){
echo $1 | sed "s/:/$COLON_ESCAPE_SEQUENCE/${2:-g}"
}
function unescape_colons(){
echo $1 | sed "s/$COLON_ESCAPE_SEQUENCE/:/${2:-g}"
}
function escape_match() {
# escapes all colons except the first one
unescape_colons "$(escape_colons "$1")" "1"
}
function lookup_match() {
local input=$1
echo -e "$match_lookup_table" | grep -i "^$input:" | cut -f2 -d:
unescape_colons $(echo -e "$match_lookup_table" | grep -i "^$input:" | cut -f2 -d:)
}
lines=''
@ -31,11 +46,14 @@ i=0
OLDIFS=$IFS
IFS=$(echo -en "\n\b") # wtf bash?
for match in $matches ; do
#TODO improve escape/unscape hackery here
match=$(escape_match "$match")
hint=$(get_hint $i)
linenumber=$(echo $match | cut -f1 -d:)
text=$(echo $match | cut -f2 -d:)
output=$(echo -ne "$output" | sed "${linenumber}s!$text!$(highlight $text) $(highlight "[$hint]")!g")
match_lookup_table="$match_lookup_table\n$hint:$text"
escaped_text=$(echo $match | cut -f2 -d:)
unescaped_text=$(unescape_colons $escaped_text)
output=$(echo -ne "$output" | sed "${linenumber}s!${unescaped_text//!/\\!}!$(highlight ${unescaped_text//!/\\!}) $(highlight "[${hint//!/\\!}]")!g")
match_lookup_table="$match_lookup_table\n$hint:$escaped_text"
i=$((i + 1))
done
IFS=$OLDIFS