line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Text::MicroMason::Filters; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
559
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
22
|
|
4
|
1
|
|
|
1
|
|
4
|
use Carp; |
|
1
|
|
|
|
|
12
|
|
|
1
|
|
|
|
|
62
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
417
|
use Safe; |
|
1
|
|
|
|
|
30342
|
|
|
1
|
|
|
|
|
79
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
###################################################################### |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
# Output filtering |
11
|
1
|
|
|
1
|
|
14
|
use vars qw( %Filters ); |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
835
|
|
12
|
|
|
|
|
|
|
$Filters{p} = \&Text::MicroMason::Base::_printable; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
$Filters{h} = eval { |
15
|
|
|
|
|
|
|
require HTML::Entities; sub { HTML::Entities::encode( $_[0], q[<>&'"] ) } |
16
|
|
|
|
|
|
|
} || eval { require CGI; \&CGI::escapeHTML }; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
$Filters{u} = eval { require URI::Escape; \&URI::Escape::uri_escape }; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub defaults { |
21
|
1
|
|
|
1
|
0
|
10
|
(shift)->NEXT('defaults'), filters => \%Filters, default_filters => '', default_policy => 'combine' |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
###################################################################### |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# $perl_code = $mason->assemble( @tokens ); |
27
|
|
|
|
|
|
|
sub assemble { |
28
|
20
|
|
|
20
|
1
|
26
|
my $self = shift; |
29
|
20
|
|
|
|
|
40
|
my @tokens = @_; |
30
|
|
|
|
|
|
|
# warn "Filter assemble"; |
31
|
20
|
|
|
|
|
59
|
foreach my $position ( 0 .. int( $#tokens / 2 ) ) { |
32
|
39
|
100
|
|
|
|
79
|
if ( $tokens[$position * 2] eq 'expr' ) { |
33
|
20
|
|
|
|
|
31
|
my $token = $tokens[$position * 2 + 1]; |
34
|
20
|
100
|
|
|
|
113
|
my $filt_flags = ($token =~ s/(?
|
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
|
|
|
|
|
24
|
my @unparsed; |
41
|
20
|
100
|
100
|
|
|
70
|
push @unparsed, $self->{default_filters} unless $filt_flags and $self->{default_policy} eq 'override'; |
42
|
20
|
|
|
|
|
30
|
push @unparsed, $filt_flags; |
43
|
20
|
100
|
|
|
|
38
|
if (my @filters = $self->parse_filters(@unparsed)) { |
44
|
17
|
|
|
|
|
66
|
$token = '$m->filter( ' . join(', ', map "'$_'", @filters ) . ', ' . |
45
|
|
|
|
|
|
|
'join "", do { ' . $token . '} )'; |
46
|
|
|
|
|
|
|
} |
47
|
20
|
|
|
|
|
52
|
$tokens[$position * 2 + 1] = $token; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
} |
50
|
20
|
|
|
|
|
53
|
$self->NEXT('assemble', @tokens ); |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# @flags = $mason->parse_filters( @filter_strings ); |
54
|
|
|
|
|
|
|
sub parse_filters { |
55
|
20
|
|
|
20
|
1
|
22
|
my $self = shift; |
56
|
|
|
|
|
|
|
|
57
|
20
|
|
|
|
|
23
|
my $no_ns; |
58
|
20
|
|
|
|
|
24
|
my $short = join '', 'n', grep { length($_) == 1 } keys %{ $self->{filters} }; |
|
78
|
|
|
|
|
137
|
|
|
20
|
|
|
|
|
51
|
|
59
|
31
|
|
100
|
|
|
137
|
reverse grep { not $no_ns ||= /^n$/ } reverse |
60
|
20
|
100
|
|
|
|
38
|
map { /^[$short]{2,5}$/ ? split('') : split(/[\s\,]+/) } @_; |
|
38
|
|
|
|
|
175
|
|
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
|
|
|
|
|
|
|
sub filter_functions { |
70
|
2
|
|
|
2
|
1
|
477
|
my $self = shift; |
71
|
2
|
50
|
|
|
|
6
|
my $filters = ( ref $self ) ? $self->{filters} : \%Filters; |
72
|
2
|
50
|
|
|
|
9
|
if ( scalar @_ == 0 ) { |
|
|
50
|
|
|
|
|
|
73
|
0
|
|
|
|
|
0
|
%$filters |
74
|
|
|
|
|
|
|
} elsif ( scalar @_ == 1 ) { |
75
|
0
|
|
|
|
|
0
|
my $key = shift; |
76
|
0
|
0
|
|
|
|
0
|
if ( ! ref $key ) { |
77
|
0
|
|
|
|
|
0
|
$filters->{ $key } |
78
|
|
|
|
|
|
|
} else { |
79
|
0
|
|
|
|
|
0
|
@{ $filters }{ @$key } |
|
0
|
|
|
|
|
0
|
|
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
} else { |
82
|
2
|
|
|
|
|
13
|
%$filters = ( %$filters, @_ ); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
# @functions = $mason->get_filter_functions( @flags_or_functions ); |
87
|
|
|
|
|
|
|
sub get_filter_functions { |
88
|
17
|
|
|
17
|
1
|
23
|
my $self = shift; |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
map { |
91
|
17
|
50
|
|
|
|
25
|
( ref $_ eq 'CODE' ) ? $_ : $self->{filters}{ $_ } |
|
20
|
50
|
|
|
|
80
|
|
92
|
|
|
|
|
|
|
or $self->croak_msg("No definition for a filter named '$_'" ); |
93
|
|
|
|
|
|
|
} @_ |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
# $result = $mason->filter( @filters, $content ); |
97
|
|
|
|
|
|
|
sub filter { |
98
|
17
|
|
|
17
|
1
|
276
|
my $self = shift; |
99
|
17
|
|
|
|
|
26
|
local $_ = pop; |
100
|
|
|
|
|
|
|
|
101
|
17
|
|
|
|
|
40
|
foreach my $function ( $self->get_filter_functions( @_ ) ) { |
102
|
20
|
|
|
|
|
52
|
$_ = &$function($_) |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
$_ |
105
|
17
|
|
|
|
|
484
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
###################################################################### |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
1; |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
__END__ |