File Coverage

blib/lib/Text/Filter/URI.pm
Criterion Covered Total %
statement 24 40 60.0
branch 0 2 0.0
condition n/a
subroutine 8 9 88.8
pod 3 3 100.0
total 35 54 64.8


line stmt bran cond sub pod time code
1             package Text::Filter::URI;
2              
3 2     2   53218 use warnings;
  2         4  
  2         64  
4 2     2   11 use strict;
  2         4  
  2         66  
5              
6 2     2   2176 use Text::Unidecode;
  2         7140  
  2         142  
7              
8 2     2   16 use base qw(Exporter Text::Filter);
  2         4  
  2         1378  
9              
10             our @EXPORT_OK;
11              
12             BEGIN {
13 2     2   439 @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             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 for details on C<$input> and C<$output>.
40              
41             =head1 EXPORT
42              
43             =cut
44              
45             sub filter_uri {
46 1     1 1 9 my @input = @_;
47 1         2 my $output = [];
48 1         9 my $f = Text::Filter::URI->new( input => [@input], output => $output);
49 0         0 $f->filter;
50 0 0       0 return wantarray ? @{$output} : $output->[0];
  0         0  
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.
63              
64             =head2 new
65              
66             The constructor C takes a hash for configuration. See L 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             sub new {
77 1     1 1 2 my $class = shift;
78 1         5 my %backup = @_;
79 1         3 my %param = (separator => '-', @_);
80 1         2 delete $backup{separator};
81 1         131 my $self = $class->SUPER::new(%backup);
82 0           $self->{separator} = $param{separator};
83 0           bless($self, $class);
84 0           return $self;
85             }
86              
87             =head2 filter
88              
89             Call this method after calling C to actually filter the C<$input>.
90              
91             Unicode characters get encoded to their ascii equivalents using the L. This module maps characters like C<ä> to the ascii character C.
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             sub filter {
98 0     0 1   my $self = shift;
99 2     2   2819 { no locale;
  2         475  
  2         10  
  0            
100 0           my $line;
101 0           while ( defined($line = $self->readline) ) {
102 0           $line = unidecode($line);
103 0           $line =~ s/[\W_]/ /g;
104 0           $line =~ s/^\s*|\s*$//g;
105 0           $line =~ s/\s+/$self->{separator}/g;
106 0           $line = lc($line);
107 0           $self->writeline($line);
108             }
109             }
110             }
111              
112              
113              
114             =head1 AUTHOR
115              
116             Moritz Onken, C<< >>
117              
118             =head1 BUGS
119              
120             Please report any bugs or feature requests to C, or through
121             the web interface at L. 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             1; # End of Text::Filter::URI