80f151e56d7d6d30858acf581141fa84bfd79bc7

Author
Brian Hartvigsen <brian.andrew@brianandjenny.com>
Committer
Brian Hartvigsen <brian.andrew@brianandjenny.com>
Date

Message

urltools for Everyone

#1445 is great, if you use node.  For those that don't this should mean we can find a reasonable fallback.
Users can force urltools to use a specific system by setting `URLTOOLS_METHOD` to one of:
* `node`
* `python`
* `ruby`
* `php`
* `perl`

Note that you can't (currently) choose which `perl` sublibrary to use, it will use either `URI::Encode`, `URI::Escape`, or regular expressions in that order of preference.

Diff

 1diff --git a/plugins/urltools/urltools.plugin.zsh b/plugins/urltools/urltools.plugin.zsh
 2index 3de74ae3da10b5acb240e05a4a74f34fd1070447..4ddfff8ce23b6b0e5d0f6ab6c16ce5ada14389dd 100644
 3--- a/plugins/urltools/urltools.plugin.zsh
 4+++ b/plugins/urltools/urltools.plugin.zsh
 5@@ -4,6 +4,33 @@
 6 # Taken from:
 7 # http://ruslanspivak.com/2010/06/02/urlencode-and-urldecode-from-a-command-line/
 8 
 9-alias urlencode='node -e "console.log(encodeURIComponent(process.argv[1]))"'
10+if [[ $(whence $URLTOOLS_METHOD) = "" ]]; then
11+    URLTOOLS_METHOD=""
12+fi
13 
14-alias urldecode='node -e "console.log(decodeURIComponent(process.argv[1]))"'
15+if [[ $(whence node) != "" && ( "x$URLTOOLS_METHOD" = "x"  || "x$URLTOOLS_METHOD" = "xnode" ) ]]; then
16+    alias urlencode='node -e "console.log(encodeURIComponent(process.argv[1]))"'
17+    alias urldecode='node -e "console.log(decodeURIComponent(process.argv[1]))"'
18+elif [[ $(whence python) != "" && ( "x$URLTOOLS_METHOD" = "x" || "x$URLTOOLS_METHOD" = "xpython" ) ]]; then
19+    alias urlencode='python -c "import sys, urllib as ul; print ul.quote_plus(sys.argv[1])"'
20+    alias urldecode='python -c "import sys, urllib as ul; print ul.unquote_plus(sys.argv[1])"'
21+elif [[ $(whence ruby) != "" && ( "x$URLTOOLS_METHOD" = "x" || "x$URLTOOLS_METHOD" = "xruby" ) ]]; then
22+    alias urlencode='ruby -r cgi -e "puts CGI.escape(ARGV[0])"'
23+    alias urldecode='ruby -r cgi -e "puts CGI.unescape(ARGV[0])"'
24+elif [[ $(whence php) != "" && ( "x$URLTOOLS_METHOD" = "x" || "x$URLTOOLS_METHOD" = "xphp" ) ]]; then
25+    alias urlencode='php -r "echo rawurlencode(\$argv[1]); echo \"\n\";"'
26+    alias urldecode='php -r "echo rawurldecode(\$argv[1]); echo \"\\n\";"'
27+elif [[ $(whence perl) != "" && ( "x$URLTOOLS_METHOD" = "x" || "x$URLTOOLS_METHOD" = "xperl" ) ]]; then
28+    if perl -MURI::Encode -e 1&> /dev/null; then
29+        alias urlencode='perl -MURI::Encode -ep "uri_encode($ARGV[0]);"'
30+        alias urldecode='perl -MURI::Encode -ep "uri_decode($ARGV[0]);"'
31+    elif perl -MURI::Escape -e 1 &> /dev/null; then
32+        alias urlencode='perl -MURI::Escape -ep "uri_escape($ARGV[0]);"'
33+        alias urldecode='perl -MURI::Escape -ep "uri_unescape($ARGV[0]);"'
34+    else
35+        alias urlencode="perl -e '\$new=\$ARGV[0]; \$new =~ s/([^A-Za-z0-9])/sprintf(\"%%%02X\", ord(\$1))/seg; print \"\$new\n\";'"
36+        alias urldecode="perl -e '\$new=\$ARGV[0]; \$new =~ s/\%([A-Fa-f0-9]{2})/pack(\"C\", hex(\$1))/seg; print \"\$new\n\";'"
37+    fi
38+fi
39+
40+unset URLTOOLS_METHOD