sub sanitize_name { # make uri-sane filenames # We assume Unicode on input. # XXX Maybe use whatever SocialText used to create titles # First, downgrade to ASCII chars (or transliterate if possible) @_ = unidecode(@_); for( @_ ) { s/['"]//gi; s/[^a-zA-Z0-9.-]/ /gi; s/\s+/_/g; s/_-_/-/g; s/^_+//g; s/_+$//g; }; wantarray ? @_ : $_[0]; }; #### #!perl -w use strict; use Test::More; use Data::Dumper; use App::ImageStream::Image; use utf8; binmode DATA, ':utf8'; my @tests = map { s!\s+$!!g; [split /\|/] } grep {!/^\s*#/} ; push @tests, ["String\nWith\n\nNewlines\r\nEmbedded","String_With_Newlines_Embedded"]; push @tests, ["String\tWith \t Tabs \tEmbedded","String_With_Tabs_Embedded"]; push @tests, ["","",'Empty String']; plan tests => 1+@tests*2; for (@tests) { my $name= $_->[2] || $_->[1]; is App::ImageStream::Image::sanitize_name($_->[0]), $_->[1], $name; is App::ImageStream::Image::sanitize_name($_->[1]), $_->[1], "'$name' is idempotent"; }; is_deeply [App::ImageStream::Image::sanitize_name( 'Lenny', 'Motörhead' )], ['Lenny','Motorhead'], "Multiple arguments also work"; __DATA__ Grégory|Gregory Leading Spaces|Leading_Spaces Trailing Space|Trailing_Space Ævar Arnfjörð Bjarmason|AEvar_Arnfjord_Bjarmason forward/slash|forward_slash Ümloud feat. ß|Umloud_feat._ss /foo/bar/index.html|foo_bar_index.html|filename with path