blib/lib/Text/MicroMason/Filters.pm

filter: Statementstmt 87.3% 55 / 63 Branchbran 70.0% 14 / 20 Conditioncond 66.6% 8 / 12 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 100% 11 / 11 Podpod 83.3% 5 / 6 totaltotal 80.8% 97 / 120 CC 16 SCAR 16.2
CC
16
Cov
81%
filter_functions
10.7
assemble
7.0
get_filter_functions
3.5
CRAP 5.1
« blib/lib/Text/MicroMason/LineNumbers.pm ↑ Summary blib/lib/Text/MicroMason/ServerPages.pm »
1 package Text::MicroMason::Filters;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Safe;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 ######################################################################
9  
10 # Output filtering
11 1 ▶use vars qw( %Filters );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 $Filters{p} = \&Text::MicroMason::Base::_printable;
13  
14 $Filters{h} = eval {
15 3 ▶ require HTML::Entities; sub { HTML::Entities::encode( $_[0], q[<>&'"] ) }
Subroutine __ANON__: called CC=1 SCAR=0.0
16 0 ▶ } || eval { require CGI; \&CGI::escapeHTML };
Condition33%1/3
eval { do { require HTML::Entities; sub { HTML::Entities::encode($_[0], q[<>&'"]); } } } || eval { do { require CGI; \&CGI::escapeHTML } }
l!l&&r!l&&!r
100
MC/DC0%0/2
eval { do { require HTML::Entities; sub { HTML::Entities::encode($_[0], q[<>&'"]); } } } || eval { do { require CGI; \&CGI::escapeHTML } }
eval { do { require HTML::Entities; sub { HTML::Entities::encode($_[0], q[<>&'"]); } } } eval { do { require CGI; \&CGI::escapeHTML } }
Truth table33%1/3
A || B
A= eval { do { require HTML::Entities; sub { HTML::Entities::encode($_[0], q[<>&'"]); } } }B= eval { do { require CGI; \&CGI::escapeHTML } }
ABresult
1X1
011
000
17  
18 1 $Filters{u} = eval { require URI::Escape; \&URI::Escape::uri_escape };
19  
20 1 ▶sub defaults {
Subroutine defaults: called CC=1 SCAR=0.0
Pod undocumented
21 1 (shift)->NEXT('defaults'), filters => \%Filters, default_filters => '', default_policy => 'combine'
22 }
23  
24 ######################################################################
25  
26 # $perl_code = $mason->assemble( @tokens );
27 20 ▶sub assemble {
Subroutine assemble: called CC=7 SCAR=19.5
Pod documented
28 20 my $self = shift;
29 20 my @tokens = @_;
30 # warn "Filter assemble";
31 20 foreach my $position ( 0 .. int( $#tokens / 2 ) ) {
32 39 ▶ if ( $tokens[$position * 2] eq 'expr' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tokens[$position * 2] eq "expr") 20 19 39
33 20 my $token = $tokens[$position * 2 + 1];
34 20 ▶ my $filt_flags = ($token =~ s/(?<!\|)\| # starts with a pipe preceded by not-a-pipe
Branch: 1 branch
BranchTrueFalseTotal
$token =~ s/(?<!\|)\| # starts with a pipe preceded by not-a-pipe \s* # optional white space (\w+(?:[\s\,]+\w+)*) # \w+ optionally delimited by spaces andor commas \s*\z # optional whitespace and the end of string //x ? : 14 6 20
35 \s* # optional white space
36 (\w+(?:[\s\,]+\w+)*) # \w+ optionally delimited by spaces andor commas
37 \s*\z # optional whitespace and the end of string
38 //x) ? $1 : '';
39
40 20 my @unparsed;
41 20 ▶ push @unparsed, $self->{default_filters} unless $filt_flags and $self->{default_policy} eq 'override';
Branch: 1 branch
BranchTrueFalseTotal
unless $filt_flags and $self->{"default_policy"} eq "override" 18 2 20
Condition100%3/3
$filt_flags and $self->{"default_policy"} eq "override"
!ll&&!rl&&r
6122
MC/DC100%2/2
$filt_flags and $self->{"default_policy"} eq "override"
$filt_flags $self->{"default_policy"} eq "override"
Truth table100%3/3
A and B
A= $filt_flagsB= $self->{"default_policy"} eq "override"
ABresult
0X0
100
111
42 20 push @unparsed, $filt_flags;
43 20 ▶ if (my @filters = $self->parse_filters(@unparsed)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@filters) = $self->parse_filters(@unparsed)) 17 3 20
44 17 $token = '$m->filter( ' . join(', ', map "'$_'", @filters ) . ', ' .
45 'join "", do { ' . $token . '} )';
46 }
47 20 $tokens[$position * 2 + 1] = $token;
48 }
49 }
50 20 $self->NEXT('assemble', @tokens );
51 }
52  
53 # @flags = $mason->parse_filters( @filter_strings );
54 20 ▶sub parse_filters {
Subroutine parse_filters: called CC=3 SCAR=11.0
Pod documented
55 20 my $self = shift;
56
57 20 my $no_ns;
58 20 my $short = join '', 'n', grep { length($_) == 1 } keys %{ $self->{filters} };
59 31 ▶ reverse grep { not $no_ns ||= /^n$/ } reverse
Condition100%3/3
$no_ns ||= /^n$/
l!l&&r!l&&!r
6520
MC/DC100%2/2
$no_ns ||= /^n$/
$no_ns /^n$/
Truth table100%3/3
A ||= B
A= $no_nsB= /^n$/
ABresult
1X1
011
000
60 20 ▶ map { /^[$short]{2,5}$/ ? split('') : split(/[\s\,]+/) } @_;
Branch: 1 branch
BranchTrueFalseTotal
/^[$short]{2,5}$/ ? : 2 36 38
61 }
62  
63 ######################################################################
64  
65 # %functions = $mason->filter_functions();
66 # $function = $mason->filter_functions( $flag );
67 # @functions = $mason->filter_functions( \@flags );
68 # $mason->filter_functions( $flag => $function, ... );
69 2 ▶sub filter_functions {
Subroutine filter_functions: called CC=5 SCAR=23.7
Pod documented
70 2 my $self = shift;
71 2 ▶ my $filters = ( ref $self ) ? $self->{filters} : \%Filters;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 2 0 2
72 2 ▶ if ( scalar @_ == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @_ == 0) { } 0 2 2
elsif (scalar @_ == 1) { } 0 2 2
73 0 %$filters
74 } elsif ( scalar @_ == 1 ) {
75 0 my $key = shift;
76 0 ▶ if ( ! ref $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $key) { } 0 0 0
77 0 $filters->{ $key }
78 } else {
79 0 @{ $filters }{ @$key }
80 }
81 } else {
82 2 %$filters = ( %$filters, @_ );
83 }
84 }
85  
86 # @functions = $mason->get_filter_functions( @flags_or_functions );
87 17 ▶sub get_filter_functions {
Subroutine get_filter_functions: called CC=3 SCAR=12.5
Pod documented
88 17 my $self = shift;
89
90 map {
91 17 ▶ ( ref $_ eq 'CODE' ) ? $_ : $self->{filters}{ $_ }
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'CODE' ? : 0 20 20
Condition33%1/3
(ref $_ eq 'CODE' ? $_ : $self->{'filters'}{$_}) || $self->croak_msg("No definition for a filter named '${_}'")
l!l&&r!l&&!r
2000
MC/DC0%0/2
(ref $_ eq 'CODE' ? $_ : $self->{'filters'}{$_}) || $self->croak_msg("No definition for a filter named '${_}'")
(ref $_ eq 'CODE' ? $_ : $self->{'filters'}{$_}) $self->croak_msg("No definition for a filter named '${_}'")
Truth table33%1/3
A || B
A= (ref $_ eq 'CODE' ? $_ : $self->{'filters'}{$_})B= $self->croak_msg("No definition for a filter named '${_}'")
ABresult
1X1
011
000
92 or $self->croak_msg("No definition for a filter named '$_'" );
93 } @_
94 }
95  
96 # $result = $mason->filter( @filters, $content );
97 17 ▶sub filter {
Subroutine filter: called CC=2 SCAR=6.9
Pod documented
98 17 my $self = shift;
99 17 local $_ = pop;
100
101 17 foreach my $function ( $self->get_filter_functions( @_ ) ) {
102 20 $_ = &$function($_)
103 }
104 $_
105 17 }
106  
107 ######################################################################
108  
109 1 1;
110  
111 __END__
« blib/lib/Text/MicroMason/LineNumbers.pm ↑ Summary blib/lib/Text/MicroMason/ServerPages.pm »