urltools.plugin.zsh 2.0 KB

123456789101112131415161718192021222324252627282930313233343536
  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. if [[ $(whence $URLTOOLS_METHOD) = "" ]]; then
  7. URLTOOLS_METHOD=""
  8. fi
  9. if [[ $(whence node) != "" && ( "x$URLTOOLS_METHOD" = "x" || "x$URLTOOLS_METHOD" = "xnode" ) ]]; then
  10. alias urlencode='node -e "console.log(encodeURIComponent(process.argv[1]))"'
  11. alias urldecode='node -e "console.log(decodeURIComponent(process.argv[1]))"'
  12. elif [[ $(whence python) != "" && ( "x$URLTOOLS_METHOD" = "x" || "x$URLTOOLS_METHOD" = "xpython" ) ]]; then
  13. alias urlencode='python -c "import sys, urllib as ul; print ul.quote_plus(sys.argv[1])"'
  14. alias urldecode='python -c "import sys, urllib as ul; print ul.unquote_plus(sys.argv[1])"'
  15. elif [[ $(whence ruby) != "" && ( "x$URLTOOLS_METHOD" = "x" || "x$URLTOOLS_METHOD" = "xruby" ) ]]; then
  16. alias urlencode='ruby -r cgi -e "puts CGI.escape(ARGV[0])"'
  17. alias urldecode='ruby -r cgi -e "puts CGI.unescape(ARGV[0])"'
  18. elif [[ $(whence php) != "" && ( "x$URLTOOLS_METHOD" = "x" || "x$URLTOOLS_METHOD" = "xphp" ) ]]; then
  19. alias urlencode='php -r "echo rawurlencode(\$argv[1]); echo \"\n\";"'
  20. alias urldecode='php -r "echo rawurldecode(\$argv[1]); echo \"\\n\";"'
  21. elif [[ $(whence perl) != "" && ( "x$URLTOOLS_METHOD" = "x" || "x$URLTOOLS_METHOD" = "xperl" ) ]]; then
  22. if perl -MURI::Encode -e 1&> /dev/null; then
  23. alias urlencode='perl -MURI::Encode -ep "uri_encode($ARGV[0]);"'
  24. alias urldecode='perl -MURI::Encode -ep "uri_decode($ARGV[0]);"'
  25. elif perl -MURI::Escape -e 1 &> /dev/null; then
  26. alias urlencode='perl -MURI::Escape -ep "uri_escape($ARGV[0]);"'
  27. alias urldecode='perl -MURI::Escape -ep "uri_unescape($ARGV[0]);"'
  28. else
  29. alias urlencode="perl -e '\$new=\$ARGV[0]; \$new =~ s/([^A-Za-z0-9])/sprintf(\"%%%02X\", ord(\$1))/seg; print \"\$new\n\";'"
  30. alias urldecode="perl -e '\$new=\$ARGV[0]; \$new =~ s/\%([A-Fa-f0-9]{2})/pack(\"C\", hex(\$1))/seg; print \"\$new\n\";'"
  31. fi
  32. fi
  33. unset URLTOOLS_METHOD