line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Text::MicroMason::Filters; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
626
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
31
|
|
4
|
1
|
|
|
1
|
|
5
|
use Carp; |
|
1
|
|
|
|
|
14
|
|
|
1
|
|
|
|
|
71
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
518
|
use Safe; |
|
1
|
|
|
|
|
37350
|
|
|
1
|
|
|
|
|
54
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
###################################################################### |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
# Output filtering |
11
|
1
|
|
|
1
|
|
7
|
use vars qw( %Filters ); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
697
|
|
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
|
32
|
my $self = shift; |
29
|
20
|
|
|
|
|
45
|
my @tokens = @_; |
30
|
|
|
|
|
|
|
# warn "Filter assemble"; |
31
|
20
|
|
|
|
|
74
|
foreach my $position ( 0 .. int( $#tokens / 2 ) ) { |
32
|
39
|
100
|
|
|
|
100
|
if ( $tokens[$position * 2] eq 'expr' ) { |
33
|
20
|
|
|
|
|
40
|
my $token = $tokens[$position * 2 + 1]; |
34
|
20
|
100
|
|
|
|
127
|
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
|
|
|
|
|
38
|
my @unparsed; |
41
|
20
|
100
|
100
|
|
|
86
|
push @unparsed, $self->{default_filters} unless $filt_flags and $self->{default_policy} eq 'override'; |
42
|
20
|
|
|
|
|
64
|
push @unparsed, $filt_flags; |
43
|
20
|
100
|
|
|
|
50
|
if (my @filters = $self->parse_filters(@unparsed)) { |
44
|
17
|
|
|
|
|
80
|
$token = '$m->filter( ' . join(', ', map "'$_'", @filters ) . ', ' . |
45
|
|
|
|
|
|
|
'join "", do { ' . $token . '} )'; |
46
|
|
|
|
|
|
|
} |
47
|
20
|
|
|
|
|
64
|
$tokens[$position * 2 + 1] = $token; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
} |
50
|
20
|
|
|
|
|
63
|
$self->NEXT('assemble', @tokens ); |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# @flags = $mason->parse_filters( @filter_strings ); |
54
|
|
|
|
|
|
|
sub parse_filters { |
55
|
20
|
|
|
20
|
1
|
25
|
my $self = shift; |
56
|
|
|
|
|
|
|
|
57
|
20
|
|
|
|
|
29
|
my $no_ns; |
58
|
20
|
|
|
|
|
28
|
my $short = join '', 'n', grep { length($_) == 1 } keys %{ $self->{filters} }; |
|
78
|
|
|
|
|
170
|
|
|
20
|
|
|
|
|
66
|
|
59
|
31
|
|
100
|
|
|
168
|
reverse grep { not $no_ns ||= /^n$/ } reverse |
60
|
20
|
100
|
|
|
|
45
|
map { /^[$short]{2,5}$/ ? split('') : split(/[\s\,]+/) } @_; |
|
38
|
|
|
|
|
234
|
|
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
|
597
|
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
|
|
|
|
|
11
|
%$filters = ( %$filters, @_ ); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
# @functions = $mason->get_filter_functions( @flags_or_functions ); |
87
|
|
|
|
|
|
|
sub get_filter_functions { |
88
|
17
|
|
|
17
|
1
|
28
|
my $self = shift; |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
map { |
91
|
17
|
50
|
|
|
|
60
|
( ref $_ eq 'CODE' ) ? $_ : $self->{filters}{ $_ } |
|
20
|
50
|
|
|
|
98
|
|
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
|
340
|
my $self = shift; |
99
|
17
|
|
|
|
|
33
|
local $_ = pop; |
100
|
|
|
|
|
|
|
|
101
|
17
|
|
|
|
|
40
|
foreach my $function ( $self->get_filter_functions( @_ ) ) { |
102
|
20
|
|
|
|
|
75
|
$_ = &$function($_) |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
$_ |
105
|
17
|
|
|
|
|
597
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
###################################################################### |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
1; |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
__END__ |