line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Text::MicroMason::HTMLTemplate; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
require Text::MicroMason::Base; |
4
|
|
|
|
|
|
|
require Text::MicroMason::TemplateDir; |
5
|
|
|
|
|
|
|
require Text::MicroMason::StoreOne; |
6
|
|
|
|
|
|
|
require Text::MicroMason::HasParams; |
7
|
|
|
|
|
|
|
push @ISA, map "Text::MicroMason::$_", qw( TemplateDir StoreOne HasParams ); |
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
490
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
467
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
###################################################################### |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
my %param_mapping = ( ### <<== INCOMPLETE ### |
14
|
|
|
|
|
|
|
global_vars => 'loop_global_vars', |
15
|
|
|
|
|
|
|
cache => '-CompileCache', |
16
|
|
|
|
|
|
|
path => '-TemplatePaths', |
17
|
|
|
|
|
|
|
); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
###################################################################### |
20
|
|
|
|
|
|
|
|
21
|
5
|
|
|
5
|
1
|
42
|
sub output { (shift)->execute_again( @_ ) } |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
###################################################################### |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
my $prefix_re = '[tT][mM][pP][lL]_'; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub lex_token { |
28
|
|
|
|
|
|
|
# warn " Lexer: " . pos($_) . " of " . length($_) . "\n"; |
29
|
|
|
|
|
|
|
# Tags in format "", "", or "" |
30
|
33
|
100
|
|
33
|
1
|
329
|
/\G \<(\/?)($prefix_re\w+)\s*(.*?)\> /gcxs |
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
31
|
|
|
|
|
|
|
? ( ( $1 ? "tmpl_end" : lc($2) ) => { $_[0]->parse_args($3) } ) : |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# Things that don't match the above |
34
|
|
|
|
|
|
|
/\G ( (?: [^<] | <(?!\/?$prefix_re) )+ ) /gcxs ? ( 'text' => $1 ) : |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
# Lexer error |
37
|
|
|
|
|
|
|
() |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub parse_args { |
41
|
15
|
|
|
15
|
1
|
13
|
my $self = shift; |
42
|
15
|
|
|
|
|
18
|
my $args = "$_[0]"; |
43
|
15
|
100
|
|
|
|
39
|
return () unless length($args); |
44
|
9
|
100
|
|
|
|
35
|
return ( name => $args ) unless ( $args =~ /=/ ); |
45
|
5
|
|
|
|
|
5
|
my @tokens; |
46
|
5
|
|
|
|
|
9
|
until ( $args =~ /\G\z/gc ) { |
47
|
5
|
100
|
0
|
|
|
28
|
push ( @tokens, |
|
|
50
|
|
|
|
|
|
48
|
|
|
|
|
|
|
$args =~ /\G \s* (\w+) \= (?: \"([^\"]+)\" | ( \w+ ) ) (?= \s | \z ) /gcxs |
49
|
|
|
|
|
|
|
? ( lc($1) => ( defined($2) ? $2 : $3 ) ) : |
50
|
|
|
|
|
|
|
$args =~ /\G ( .{0,20} ) /gcxs |
51
|
|
|
|
|
|
|
&& die "Couldn't find applicable parsing rule at '$1'\n" |
52
|
|
|
|
|
|
|
); |
53
|
|
|
|
|
|
|
} |
54
|
5
|
|
|
|
|
23
|
@tokens; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
###################################################################### |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub assemble_tmpl_var { |
60
|
3
|
|
|
3
|
0
|
3
|
my ($self, $args) = @_; |
61
|
|
|
|
|
|
|
|
62
|
3
|
|
|
|
|
4
|
my $output = "\$m->param( '$args->{name}' )"; |
63
|
3
|
50
|
|
|
|
6
|
if ( defined $args->{default} ) { |
64
|
0
|
|
|
|
|
0
|
$output = "local \$_ = $output; defined ? \$_ : '$args->{default}'" |
65
|
|
|
|
|
|
|
} |
66
|
3
|
50
|
|
|
|
4
|
if ( $args->{escape} ) { |
67
|
0
|
|
|
|
|
0
|
$output = "\$m->filter( $output, '$args->{escape}' )" |
68
|
|
|
|
|
|
|
} |
69
|
3
|
|
|
|
|
7
|
expr => "$output;" |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub assemble_tmpl_include { |
73
|
0
|
|
|
0
|
0
|
0
|
my ($self, $args) = @_; |
74
|
|
|
|
|
|
|
file => $args->{name} |
75
|
0
|
|
|
|
|
0
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub assemble_tmpl_loop { |
78
|
2
|
|
|
2
|
0
|
3
|
my ($self, $args) = @_; |
79
|
2
|
100
|
|
|
|
4
|
if ( ! $self->{loop_context_vars} ) { |
80
|
1
|
|
|
|
|
4
|
perl => q/foreach my $args ( $m->param( '/ . $args->{name} . q/' ) ) { |
81
|
|
|
|
|
|
|
local $m->{params} = [ $args, $m->{loop_global_vars} ? @{$m->{params}} : () ];/ |
82
|
|
|
|
|
|
|
} else { |
83
|
1
|
|
|
|
|
3
|
perl => q/my @loop = $m->param( '/ . $args->{name} . q/' ); |
84
|
|
|
|
|
|
|
foreach my $count ( 0 .. $#loop ) { |
85
|
|
|
|
|
|
|
my $args = $loop[ $count ]; |
86
|
|
|
|
|
|
|
my %loop_context = ( |
87
|
|
|
|
|
|
|
__counter__ => $count, |
88
|
|
|
|
|
|
|
__odd__ => ( $count % 2 ), |
89
|
|
|
|
|
|
|
__first__ => ( $count == 0 ), |
90
|
|
|
|
|
|
|
__inner__ => ( $count > 0 and $count < $#loop ), |
91
|
|
|
|
|
|
|
__last__ => ( $count == $#loop ), |
92
|
|
|
|
|
|
|
); |
93
|
|
|
|
|
|
|
local $m->{params} = [ $args, \%loop_context, $m->{loop_global_vars} ? @{$m->{params}} : () ]; |
94
|
|
|
|
|
|
|
/ |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub assemble_tmpl_if { |
99
|
2
|
|
|
2
|
0
|
3
|
my ($self, $args) = @_; |
100
|
2
|
|
|
|
|
8
|
perl => q/if ( $m->param( '/ . $args->{name} . q/' ) ) { / |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub assemble_tmpl_unless { |
104
|
2
|
|
|
2
|
0
|
3
|
my ($self, $args) = @_; |
105
|
2
|
|
|
|
|
8
|
perl => q/if ( ! $m->param( '/ . $args->{name} . q/' ) ) { / |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub assemble_tmpl_else { |
109
|
0
|
|
|
0
|
0
|
0
|
perl => "} else {" |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub assemble_tmpl_end { |
113
|
6
|
|
|
6
|
0
|
12
|
perl => "}" |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
###################################################################### |
117
|
|
|
|
|
|
|
|
118
|
1
|
|
|
1
|
|
4
|
use vars qw( %Filters ); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
162
|
|
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub defaults { |
121
|
11
|
|
|
11
|
0
|
23
|
(shift)->NEXT('defaults'), filters => \%Filters, |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
# Output filtering |
125
|
|
|
|
|
|
|
$Filters{1} = $Filters{html} = \&HTML::Entities::encode |
126
|
|
|
|
|
|
|
if eval { require HTML::Entities}; |
127
|
|
|
|
|
|
|
$Filters{url} = \&URI::Escape::uri_escape if eval { require URI::Escape }; |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
# $result = $mason->filter( @filters, $content ); |
130
|
|
|
|
|
|
|
sub filter { |
131
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
132
|
0
|
|
|
|
|
|
my $content = pop; |
133
|
|
|
|
|
|
|
|
134
|
0
|
|
|
|
|
|
foreach my $filter ( @_ ) { |
135
|
|
|
|
|
|
|
my $function = ( ref $filter eq 'CODE' ) ? $filter : |
136
|
0
|
0
|
0
|
|
|
|
$self->{filters}{ $filter } || |
137
|
|
|
|
|
|
|
$self->croak_msg("No definition for a filter named '$filter'" ); |
138
|
0
|
|
|
|
|
|
$content = &$function($content) |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
$content |
141
|
0
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
###################################################################### |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
1; |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
__END__ |