The use-case example is saving the contents of http://example.com
as a filename on your computer, but with the unsafe characters (i.e. :
and /
) escaped.
The classic way is to use a regex to strip all non-alphanumeric-dash-underscore characters out, but then that makes it impossible to reverse the filename into a URL. Is there a way, possibly a combination of CGI.escape
and another filter, to sanitize the filename for both Windows and *nix? Even if the tradeoff is a much longer filename?
edit:
Example with CGI.escape
CGI.escape 'http://www.example.com/Hey/whatsup/1 2 3.html#hash'
#=> "http%3A%2F%2Fwww.example.com%2FHey%2Fwhatsup%2F1+2+3.html%23hash"
A couple things...are %
signs completely safe as file characters? Unfortunately, CGI.escape
doesn't convert spaces in a malformed URL to %20
on the first pass, so I suppose any translation method would require changing all spaces to +
with a gsub
and then applying CGI.escape