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