File Coverage

blib/lib/Templ/Tag.pm
Criterion Covered Total %
statement 49 57 85.9
branch 8 14 57.1
condition 3 9 33.3
subroutine 11 13 84.6
pod 0 8 0.0
total 71 101 70.3


line stmt bran cond sub pod time code
1             package Templ::Tag;
2              
3 1     1   5 use strict;
  1         2  
  1         22  
4 1     1   4 use warnings;
  1         2  
  1         26  
5              
6 1     1   5 use Carp qw(carp croak);
  1         2  
  1         45  
7 1     1   4 use Data::Dumper;
  1         1  
  1         46  
8 1     1   4 use Templ::Util qw(unquote);
  1         2  
  1         767  
9              
10             my $valid_chars = '`~!&^*_|;.,#?@$';
11              
12             sub new {
13 6     6 0 11 my $class = shift;
14 6 0 33     19 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         20 my $self = bless {@_}, $class;
22 6         24 $self->check;
23              
24 6         21 return $self;
25             }
26              
27             sub check {
28 6     6 0 7 my $self = shift;
29 6 50 33     22 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 30 my $self = shift;
42 24 50       64 if ( not defined $self->{'char'} ) { croak "No 'char' specification"; }
  0         0  
43 24         107 return $self->{'char'};
44             }
45              
46             # Regex for matching the beginning of the tag, with optional
47             # whitespace removal greediness
48             #
49             #
50             #
51             #
52             #
53             sub pre_rx {
54 21     21 0 27 my $self = shift;
55 21 100       63 if ( not defined $self->{'_pre_rx'} ) {
56 6         16 my $char = $self->char;
57 6         248 $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         52 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 25 my $self = shift;
72 21 100       51 if ( not defined $self->{'_post_rx'} ) {
73 6         12 my $char = $self->char;
74 6         138 $self->{'_post_rx'}
75             = qr/\s+(?:\+\Q$char\E>|\-\Q$char\E>\s*|\Q$char\E>(?:[\t ]*\r?\n)?)/;
76             }
77 21         41 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 34 my $self = shift;
88 21         25 my $perl = shift; # Perl version of the template
89 21         25 my $parser = shift;
90              
91 21         54 my $pre_rx = $self->pre_rx;
92 21         55 my $post_rx = $self->post_rx;
93            
94 21         61 my $append = $parser->append;
95              
96 21         3473 $perl =~ s{ ($pre_rx) (.*?) ($post_rx) }
97             {
98 18         42 my $pre = $1;
99 18         45 my $content = unquote($2);
100 18         28 my $indent = '';
101 18 50       48 if ($pre =~ m/^([ \t]*).*\=/)
102             {
103 0         0 $indent = $1;
104             }
105 18         58 "';\n".$self->perl($content,$indent,$append)."\n$append'"
106             }egsx;
107              
108 21         132 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;