line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Templ::Tag; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
23
|
|
4
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
27
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
4
|
use Carp qw(carp croak); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
51
|
|
7
|
1
|
|
|
1
|
|
4
|
use Data::Dumper; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
45
|
|
8
|
1
|
|
|
1
|
|
4
|
use Templ::Util qw(unquote); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
775
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
my $valid_chars = '`~!&^*_|;.,#?@$'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new { |
13
|
6
|
|
|
6
|
0
|
12
|
my $class = shift; |
14
|
6
|
0
|
33
|
|
|
18
|
if ( not defined $class || ref $class || $class !~ m/^(\w+\:\:)*\w+$/ ) { |
|
|
|
33
|
|
|
|
|
15
|
0
|
|
|
|
|
0
|
croak "Can only be called as __PACKAGE__->new"; |
16
|
|
|
|
|
|
|
} |
17
|
6
|
50
|
|
|
|
14
|
if ( $class eq 'Templ::Tag' ) { |
18
|
0
|
|
|
|
|
0
|
croak "Can't instantiate a Templ::Tag object, please use a subclass"; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
6
|
|
|
|
|
21
|
my $self = bless {@_}, $class; |
22
|
6
|
|
|
|
|
26
|
$self->check; |
23
|
|
|
|
|
|
|
|
24
|
6
|
|
|
|
|
19
|
return $self; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub check { |
28
|
6
|
|
|
6
|
0
|
8
|
my $self = shift; |
29
|
6
|
50
|
33
|
|
|
21
|
unless ( index( $valid_chars, $self->char ) |
30
|
|
|
|
|
|
|
&& length( $self->char ) == 1 ) |
31
|
|
|
|
|
|
|
{ |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# The above $self->char statement will croak if the char didn't get |
34
|
|
|
|
|
|
|
# set or was not defaulted. If we got into this block, then the char |
35
|
|
|
|
|
|
|
# wasn't a valid settable character |
36
|
0
|
|
|
|
|
0
|
croak "Invalid character in 'char' specification of tag object"; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub char { |
41
|
24
|
|
|
24
|
0
|
27
|
my $self = shift; |
42
|
24
|
50
|
|
|
|
70
|
if ( not defined $self->{'char'} ) { croak "No 'char' specification"; } |
|
0
|
|
|
|
|
0
|
|
43
|
24
|
|
|
|
|
79
|
return $self->{'char'}; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# Regex for matching the beginning of the tag, with optional |
47
|
|
|
|
|
|
|
# whitespace removal greediness |
48
|
|
|
|
|
|
|
# |
49
|
|
|
|
|
|
|
# + Don't trim preceding whitespace |
50
|
|
|
|
|
|
|
# - Trim all preceding whitespace |
51
|
|
|
|
|
|
|
# = Keep indentation |
52
|
|
|
|
|
|
|
# Default, trim tabs and space only if there's a preceding newline |
53
|
|
|
|
|
|
|
sub pre_rx { |
54
|
21
|
|
|
21
|
0
|
24
|
my $self = shift; |
55
|
21
|
100
|
|
|
|
59
|
if ( not defined $self->{'_pre_rx'} ) { |
56
|
6
|
|
|
|
|
18
|
my $char = $self->char; |
57
|
6
|
|
|
|
|
245
|
$self->{'_pre_rx'} |
58
|
|
|
|
|
|
|
= qr/(?:<\Q$char\E\+|\s*<\Q$char\E\-|[ \t]*<\Q$char\E\=|(?:(?:(?<=\r\n)|(?<=\n))[\t ]*)?<\Q$char\E)\s+/; |
59
|
|
|
|
|
|
|
} |
60
|
21
|
|
|
|
|
53
|
return $self->{'_pre_rx'}; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
# Regex for matching the end of the tag, with optional |
64
|
|
|
|
|
|
|
# whitespace removal greediness |
65
|
|
|
|
|
|
|
# |
66
|
|
|
|
|
|
|
# +?> Don't trim any trailing whitespace |
67
|
|
|
|
|
|
|
# -?> Trim all trailing whitespace |
68
|
|
|
|
|
|
|
# ?> Default, trim trailing whitespace including a newline, only if |
69
|
|
|
|
|
|
|
# there's a newline |
70
|
|
|
|
|
|
|
sub post_rx { |
71
|
21
|
|
|
21
|
0
|
27
|
my $self = shift; |
72
|
21
|
100
|
|
|
|
45
|
if ( not defined $self->{'_post_rx'} ) { |
73
|
6
|
|
|
|
|
13
|
my $char = $self->char; |
74
|
6
|
|
|
|
|
136
|
$self->{'_post_rx'} |
75
|
|
|
|
|
|
|
= qr/\s+(?:\+\Q$char\E>|\-\Q$char\E>\s*|\Q$char\E>(?:[\t ]*\r?\n)?)/; |
76
|
|
|
|
|
|
|
} |
77
|
21
|
|
|
|
|
44
|
return $self->{'_post_rx'}; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub perl { |
81
|
0
|
|
|
0
|
0
|
0
|
die "Cannot call ->perl() method of Templ::Tag (must be subclassed)"; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# For the given template input string, change any appearance of the tag |
85
|
|
|
|
|
|
|
# into the appropriate substitution |
86
|
|
|
|
|
|
|
sub process { |
87
|
21
|
|
|
21
|
0
|
36
|
my $self = shift; |
88
|
21
|
|
|
|
|
27
|
my $perl = shift; # Perl version of the template |
89
|
21
|
|
|
|
|
25
|
my $parser = shift; |
90
|
|
|
|
|
|
|
|
91
|
21
|
|
|
|
|
53
|
my $pre_rx = $self->pre_rx; |
92
|
21
|
|
|
|
|
53
|
my $post_rx = $self->post_rx; |
93
|
|
|
|
|
|
|
|
94
|
21
|
|
|
|
|
61
|
my $append = $parser->append; |
95
|
|
|
|
|
|
|
|
96
|
21
|
|
|
|
|
3618
|
$perl =~ s{ ($pre_rx) (.*?) ($post_rx) } |
97
|
|
|
|
|
|
|
{ |
98
|
18
|
|
|
|
|
37
|
my $pre = $1; |
99
|
18
|
|
|
|
|
46
|
my $content = unquote($2); |
100
|
18
|
|
|
|
|
25
|
my $indent = ''; |
101
|
18
|
50
|
|
|
|
48
|
if ($pre =~ m/^([ \t]*).*\=/) |
102
|
|
|
|
|
|
|
{ |
103
|
0
|
|
|
|
|
0
|
$indent = $1; |
104
|
|
|
|
|
|
|
} |
105
|
18
|
|
|
|
|
57
|
"';\n".$self->perl($content,$indent,$append)."\n$append'" |
106
|
|
|
|
|
|
|
}egsx; |
107
|
|
|
|
|
|
|
|
108
|
21
|
|
|
|
|
126
|
return $perl; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub dump { |
112
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
113
|
0
|
|
|
|
|
|
return Data::Dumper->Dump( [$self], ['tag'] ); |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
1; |