| 1 | package Text::Filter::URI; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 2 | ▶ | use Text::Unidecode; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 2 | ▶ | use base qw(Exporter Text::Filter); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 2 | our @EXPORT_OK; | |||||||||
| 11 | |||||||||||
| 12 | 2 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 2 | @EXPORT_OK = qw(filter_uri); | |||||||||
| 14 | } | ||||||||||
| 15 | |||||||||||
| 16 | =encoding utf8 | ||||||||||
| 17 | |||||||||||
| 18 | =head1 NAME | ||||||||||
| 19 | |||||||||||
| 20 | Text::Filter::URI - Filter a string to meet URI requirements | ||||||||||
| 21 | |||||||||||
| 22 | =cut | ||||||||||
| 23 | |||||||||||
| 24 | 2 | our $VERSION = '0.03'; | |||||||||
| 25 | |||||||||||
| 26 | |||||||||||
| 27 | =head1 SYNOPSIS | ||||||||||
| 28 | |||||||||||
| 29 | Use either the exported function or the OO interface: | ||||||||||
| 30 | |||||||||||
| 31 | use Text::Filter::URI qw( filter_uri ); | ||||||||||
| 32 | |||||||||||
| 33 | my $uri = filter_uri("A text which needs to be filtered "); | ||||||||||
| 34 | # $uri = "a-text-which-needs-to-be-filtered" | ||||||||||
| 35 | |||||||||||
| 36 | my $f = Text::Filter::URI->new(input => $input, output => $output); | ||||||||||
| 37 | $f->filter; | ||||||||||
| 38 | |||||||||||
| 39 | See L<Text::Filter> for details on C<$input> and C<$output>. | ||||||||||
| 40 | |||||||||||
| 41 | =head1 EXPORT | ||||||||||
| 42 | |||||||||||
| 43 | =cut | ||||||||||
| 44 | |||||||||||
| 45 | 3 | ▶ | sub filter_uri { | ||||||||
|
Subroutine
filter_uri: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 46 | 3 | my @input = @_; | |||||||||
| 47 | 3 | my $output = []; | |||||||||
| 48 | 3 | my $f = Text::Filter::URI->new( input => [@input], output => $output); | |||||||||
| 49 | 3 | $f->filter; | |||||||||
| 50 | 3 | ▶ | return wantarray ? @{$output} : $output->[0]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | =head2 filter_uri | ||||||||||
| 54 | |||||||||||
| 55 | This method can be exported using | ||||||||||
| 56 | use Text::Filter::URI qw( filter_uri ); | ||||||||||
| 57 | |||||||||||
| 58 | It expects a string or an array of strings and returns the filtered strings accordingly. | ||||||||||
| 59 | |||||||||||
| 60 | =head1 METHODS | ||||||||||
| 61 | |||||||||||
| 62 | These methods are used for the OO interface. This allows you to use the full power of L<Text::Filter>. | ||||||||||
| 63 | |||||||||||
| 64 | =head2 new | ||||||||||
| 65 | |||||||||||
| 66 | The constructor C<new> takes a hash for configuration. See L<Text::Filter/CONSTRUCTOR> for more information on these settings. | ||||||||||
| 67 | |||||||||||
| 68 | There is one additional parameter: | ||||||||||
| 69 | |||||||||||
| 70 | =head3 separator | ||||||||||
| 71 | |||||||||||
| 72 | Define an individual string for separating the words. Defaults to C<->. | ||||||||||
| 73 | |||||||||||
| 74 | =cut | ||||||||||
| 75 | |||||||||||
| 76 | 5 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 77 | 5 | my $class = shift; | |||||||||
| 78 | 5 | my %backup = @_; | |||||||||
| 79 | 5 | my %param = (separator => '-', @_); | |||||||||
| 80 | 5 | delete $backup{separator}; | |||||||||
| 81 | 5 | my $self = $class->SUPER::new(%backup); | |||||||||
| 82 | 5 | $self->{separator} = $param{separator}; | |||||||||
| 83 | 5 | bless($self, $class); | |||||||||
| 84 | 5 | return $self; | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | =head2 filter | ||||||||||
| 88 | |||||||||||
| 89 | Call this method after calling C<new> to actually filter the C<$input>. | ||||||||||
| 90 | |||||||||||
| 91 | Unicode characters get encoded to their ascii equivalents using the L<Text::Unidecode>. This module maps characters like C<รค> to the ascii character C<a>. | ||||||||||
| 92 | This method contains several regular expressions which convert every not word character (C<\W>) and the underscore to a blank. Blanks at the beginning and the end are removed. All remaining blanks are replaced by the separator (defaults to C<->). Then it creates a lowercased version of the string. | ||||||||||
| 93 | |||||||||||
| 94 | |||||||||||
| 95 | =cut | ||||||||||
| 96 | |||||||||||
| 97 | 5 | ▶ | sub filter { | ||||||||
|
Subroutine
filter: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 98 | 5 | my $self = shift; | |||||||||
| 99 | 2 | ▶ | { no locale; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 100 | 5 | my $line; | |||||||||
| 101 | 5 | ▶ | while ( defined($line = $self->readline) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 102 | 14 | $line = unidecode($line); | |||||||||
| 103 | 14 | $line =~ s/[\W_]/ /g; | |||||||||
| 104 | 14 | $line =~ s/^\s*|\s*$//g; | |||||||||
| 105 | 14 | $line =~ s/\s+/$self->{separator}/g; | |||||||||
| 106 | 14 | $line = lc($line); | |||||||||
| 107 | 14 | $self->writeline($line); | |||||||||
| 108 | } | ||||||||||
| 109 | } | ||||||||||
| 110 | } | ||||||||||
| 111 | |||||||||||
| 112 | |||||||||||
| 113 | |||||||||||
| 114 | =head1 AUTHOR | ||||||||||
| 115 | |||||||||||
| 116 | Moritz Onken, C<< <onken@netcubed.de> >> | ||||||||||
| 117 | |||||||||||
| 118 | =head1 BUGS | ||||||||||
| 119 | |||||||||||
| 120 | Please report any bugs or feature requests to C<bug-text-filter-uri at rt.cpan.org>, or through | ||||||||||
| 121 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Text-Filter-URI>. I will be notified, and then you'll | ||||||||||
| 122 | automatically be notified of progress on your bug as I make changes. | ||||||||||
| 123 | |||||||||||
| 124 | |||||||||||
| 125 | =head1 COPYRIGHT & LICENSE | ||||||||||
| 126 | |||||||||||
| 127 | Copyright 2008 Moritz Onken, all rights reserved. | ||||||||||
| 128 | |||||||||||
| 129 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 130 | under the same terms as Perl itself. | ||||||||||
| 131 | |||||||||||
| 132 | |||||||||||
| 133 | =cut | ||||||||||
| 134 | |||||||||||
| 135 | 2 | 1; # End of Text::Filter::URI | |||||||||