| 1 | package String::MFN; | ||||||||||
| 2 | |||||||||||
| 3 | 3 | require 5.008; | |||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 3 | ▶ | use Encode; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 3 | require Exporter; | |||||||||
| 10 | 3 | our @ISA = qw( Exporter ); | |||||||||
| 11 | 3 | our @EXPORT = qw( &mfn ); | |||||||||
| 12 | |||||||||||
| 13 | =head1 NAME | ||||||||||
| 14 | |||||||||||
| 15 | String::MFN - Normalize a string to produce a sane Unix filename | ||||||||||
| 16 | |||||||||||
| 17 | =head1 VERSION | ||||||||||
| 18 | |||||||||||
| 19 | Version 1.29 | ||||||||||
| 20 | |||||||||||
| 21 | =cut | ||||||||||
| 22 | |||||||||||
| 23 | 3 | our $VERSION = '1.29'; | |||||||||
| 24 | |||||||||||
| 25 | =head1 SYNOPSIS | ||||||||||
| 26 | |||||||||||
| 27 | use String::MFN; | ||||||||||
| 28 | |||||||||||
| 29 | my $clean = mfn($dirty); | ||||||||||
| 30 | |||||||||||
| 31 | =head1 DESCRIPTION | ||||||||||
| 32 | |||||||||||
| 33 | String::MFN exports a single function, C<mfn()>, which modifies a | ||||||||||
| 34 | string to resemble a sane Unix filename. | ||||||||||
| 35 | |||||||||||
| 36 | In a nutshell, this means lowercasing everything and either getting | ||||||||||
| 37 | rid of "funny" characters or replacing them with sane equivalents | ||||||||||
| 38 | which allow the string to maintain some structure. See the test suite | ||||||||||
| 39 | for a battery of examples. | ||||||||||
| 40 | |||||||||||
| 41 | =head1 FUNCTIONS | ||||||||||
| 42 | |||||||||||
| 43 | =head2 mfn | ||||||||||
| 44 | |||||||||||
| 45 | Normalizes a string. Returns the normalized string. If no argument is | ||||||||||
| 46 | given, C<$_> is used. | ||||||||||
| 47 | |||||||||||
| 48 | =cut | ||||||||||
| 49 | |||||||||||
| 50 | 67 | ▶ | sub mfn { | ||||||||
|
Subroutine
mfn: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 51 | 67 | ▶ | my $string = ( @_ ? $_[0] : $_ ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 67 | Encode::_utf8_on($string); | |||||||||
| 53 | |||||||||||
| 54 | # phase 1 - sanitize | ||||||||||
| 55 | 67 | $string =~ s/(\p{Lowercase})(\p{Uppercase})/$1_$2/g; # inCap to in_Cap | |||||||||
| 56 | 67 | $string =~ s/[\{\[\(\<>)\]\}~\|\/]/-/g; # {[(<>)]}~|/ to '-' | |||||||||
| 57 | 67 | $string =~ s/[\p{Zs}\t]+/_/g; # whitespace to '_' | |||||||||
| 58 | 67 | $string =~ s/\&+/_and_/g; # '&' to "_and_" | |||||||||
| 59 | 67 | $string =~ s/[^\p{Alphabetic}\p{Nd}\-\.\+_]//g; # drop not-word chars | |||||||||
| 60 | |||||||||||
| 61 | # phase 2 - condense | ||||||||||
| 62 | 67 | $string =~ s/_+-+/-/g; # collapse _- sequences | |||||||||
| 63 | 67 | $string =~ s/-+_+/-/g; # collapse -_ sequences | |||||||||
| 64 | 67 | $string =~ s/[\-\_]+\././g; # collapse [-_]. sequences | |||||||||
| 65 | 67 | $string =~ s/\.[\-\_]+/./g; # collapse .[-_] sequences | |||||||||
| 66 | 67 | $string =~ s/\-{2,}/-/g; # collapse repeating -, | |||||||||
| 67 | 67 | $string =~ s/\_{2,}/_/g; # _, | |||||||||
| 68 | 67 | $string =~ s/\.{2,}/./g; # and . | |||||||||
| 69 | 67 | $string =~ s/^(\-|\_|\.)+//; # remove leading -_. | |||||||||
| 70 | 67 | $string =~ s/(\-|\_|\.)+$//; # remove trailing -_. (rare) | |||||||||
| 71 | 67 | ▶ | if ($string =~ /\.(\w+?)$/) { # collapse repeating extensions | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 20 | my $ext = $1; | |||||||||
| 73 | 20 | $string =~ s/(\.$ext)+$/\.$ext/; | |||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | 67 | return lc($string); # slam lowercase | |||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | =head1 TODO | ||||||||||
| 80 | |||||||||||
| 81 | =over | ||||||||||
| 82 | |||||||||||
| 83 | =item * | ||||||||||
| 84 | |||||||||||
| 85 | Add "classic" ASCII-oriented function for extra strictness | ||||||||||
| 86 | |||||||||||
| 87 | =item * | ||||||||||
| 88 | |||||||||||
| 89 | Add track/sequence number stuff to mfn(1p) | ||||||||||
| 90 | |||||||||||
| 91 | =back | ||||||||||
| 92 | |||||||||||
| 93 | =head1 BUGS | ||||||||||
| 94 | |||||||||||
| 95 | =over | ||||||||||
| 96 | |||||||||||
| 97 | =item * | ||||||||||
| 98 | |||||||||||
| 99 | C<mfn()> forces Perl's C<_is_utf8> flag on, but does not attempt to | ||||||||||
| 100 | verify that the data being passed to it is valid UTF-8. | ||||||||||
| 101 | |||||||||||
| 102 | =back | ||||||||||
| 103 | |||||||||||
| 104 | Please report any bugs or feature requests to | ||||||||||
| 105 | C<bug-string-mfn@rt.cpan.org>, or through the web interface at | ||||||||||
| 106 | L<http://rt.cpan.org>. I will be notified, and then you'll automatically | ||||||||||
| 107 | be notified of progress on your bug as I make changes. | ||||||||||
| 108 | |||||||||||
| 109 | =head1 AUTHOR | ||||||||||
| 110 | |||||||||||
| 111 | Shawn Boyette, C<< <mdxi@cpan.org> >> | ||||||||||
| 112 | |||||||||||
| 113 | =head1 COPYRIGHT & LICENSE | ||||||||||
| 114 | |||||||||||
| 115 | Copyright 2003-2007 Shawn Boyette, All Rights Reserved. | ||||||||||
| 116 | |||||||||||
| 117 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 118 | under the same terms as Perl itself. | ||||||||||
| 119 | |||||||||||
| 120 | =cut | ||||||||||
| 121 | |||||||||||
| 122 | 3 | 1; # End of String::MFN | |||||||||