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
|
|
613
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
723
|
|
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
|
481
|
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
|
365
|
/\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
|
29
|
my $self = shift; |
42
|
15
|
|
|
|
|
29
|
my $args = "$_[0]"; |
43
|
15
|
100
|
|
|
|
46
|
return () unless length($args); |
44
|
9
|
100
|
|
|
|
35
|
return ( name => $args ) unless ( $args =~ /=/ ); |
45
|
5
|
|
|
|
|
8
|
my @tokens; |
46
|
5
|
|
|
|
|
13
|
until ( $args =~ /\G\z/gc ) { |
47
|
5
|
100
|
0
|
|
|
37
|
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
|
|
|
|
|
30
|
@tokens; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
###################################################################### |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub assemble_tmpl_var { |
60
|
3
|
|
|
3
|
0
|
6
|
my ($self, $args) = @_; |
61
|
|
|
|
|
|
|
|
62
|
3
|
|
|
|
|
10
|
my $output = "\$m->param( '$args->{name}' )"; |
63
|
3
|
50
|
|
|
|
8
|
if ( defined $args->{default} ) { |
64
|
0
|
|
|
|
|
0
|
$output = "local \$_ = $output; defined ? \$_ : '$args->{default}'" |
65
|
|
|
|
|
|
|
} |
66
|
3
|
50
|
|
|
|
6
|
if ( $args->{escape} ) { |
67
|
0
|
|
|
|
|
0
|
$output = "\$m->filter( $output, '$args->{escape}' )" |
68
|
|
|
|
|
|
|
} |
69
|
3
|
|
|
|
|
11
|
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
|
4
|
my ($self, $args) = @_; |
79
|
2
|
100
|
|
|
|
6
|
if ( ! $self->{loop_context_vars} ) { |
80
|
1
|
|
|
|
|
5
|
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
|
|
|
|
|
5
|
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
|
5
|
my ($self, $args) = @_; |
100
|
2
|
|
|
|
|
9
|
perl => q/if ( $m->param( '/ . $args->{name} . q/' ) ) { / |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub assemble_tmpl_unless { |
104
|
2
|
|
|
2
|
0
|
5
|
my ($self, $args) = @_; |
105
|
2
|
|
|
|
|
7
|
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
|
16
|
perl => "}" |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
###################################################################### |
117
|
|
|
|
|
|
|
|
118
|
1
|
|
|
1
|
|
26
|
use vars qw( %Filters ); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
243
|
|
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub defaults { |
121
|
11
|
|
|
11
|
0
|
30
|
(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__ |