| 1 | package Templ::Tag; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Carp qw(carp croak); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Templ::Util qw(unquote); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | my $valid_chars = '`~!&^*_|;.,#?@$'; | |||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 6 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=19.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 6 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 14 | 6 | ▶ | if ( not defined $class || ref $class || $class !~ m/^(\w+\:\:)*\w+$/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
defined $class or ref $class or not $class =~ /^(\w+\:\:)*\w+$/
defined $class or ref $class
MC/DC0%0/3
defined $class or ref $class or not $class =~ /^(\w+\:\:)*\w+$/
defined $class
ref $class
not $class =~ /^(\w+\:\:)*\w+$/
Truth table25%1/4
A or B or not(C)
A= defined $classB= ref $classC= not $class =~ /^(\w+\:\:)*\w+$/
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 0 | croak "Can only be called as __PACKAGE__->new"; | |||||||||||||||||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||||||||||||||||
| 17 | 6 | ▶ | if ( $class eq 'Templ::Tag' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 0 | croak "Can't instantiate a Templ::Tag object, please use a subclass"; | |||||||||||||||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 6 | my $self = bless {@_}, $class; | |||||||||||||||||||||||||||||||||||||||||
| 22 | 6 | $self->check; | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 6 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 6 | ▶ | sub check { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
check: called CC=3 SCAR=14.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 6 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 29 | 6 | ▶ | unless ( index( $valid_chars, $self->char ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
index $valid_chars, $self->char and length $self->char == 1
MC/DC0%0/2
index $valid_chars, $self->char and length $self->char == 1
index $valid_chars, $self->char
length $self->char == 1
Truth table33%1/3
A and B
A= index $valid_chars, $self->charB= length $self->char == 1
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | croak "Invalid character in 'char' specification of tag object"; | |||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 24 | ▶ | sub char { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
char: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 24 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | if ( not defined $self->{'char'} ) { croak "No 'char' specification"; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 24 | 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 | 21 | ▶ | sub pre_rx { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pre_rx: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 21 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 55 | 21 | ▶ | if ( not defined $self->{'_pre_rx'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 6 | my $char = $self->char; | |||||||||||||||||||||||||||||||||||||||||
| 57 | 6 | $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 | 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 | 21 | ▶ | sub post_rx { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
post_rx: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 21 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 72 | 21 | ▶ | if ( not defined $self->{'_post_rx'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 6 | my $char = $self->char; | |||||||||||||||||||||||||||||||||||||||||
| 74 | 6 | $self->{'_post_rx'} | |||||||||||||||||||||||||||||||||||||||||
| 75 | = qr/\s+(?:\+\Q$char\E>|\-\Q$char\E>\s*|\Q$char\E>(?:[\t ]*\r?\n)?)/; | ||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 21 | return $self->{'_post_rx'}; | |||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | sub perl { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
perl: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 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 | 21 | ▶ | sub process { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
process: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 21 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 88 | 21 | my $perl = shift; # Perl version of the template | |||||||||||||||||||||||||||||||||||||||||
| 89 | 21 | my $parser = shift; | |||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 21 | my $pre_rx = $self->pre_rx; | |||||||||||||||||||||||||||||||||||||||||
| 92 | 21 | my $post_rx = $self->post_rx; | |||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 21 | my $append = $parser->append; | |||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 21 | $perl =~ s{ ($pre_rx) (.*?) ($post_rx) } | |||||||||||||||||||||||||||||||||||||||||
| 97 | { | ||||||||||||||||||||||||||||||||||||||||||
| 98 | 18 | my $pre = $1; | |||||||||||||||||||||||||||||||||||||||||
| 99 | 18 | my $content = unquote($2); | |||||||||||||||||||||||||||||||||||||||||
| 100 | 18 | my $indent = ''; | |||||||||||||||||||||||||||||||||||||||||
| 101 | 18 | ▶ | if ($pre =~ m/^([ \t]*).*\=/) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | { | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | $indent = $1; | |||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 18 | "';\n".$self->perl($content,$indent,$append)."\n$append'" | |||||||||||||||||||||||||||||||||||||||||
| 106 | }egsx; | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | 21 | return $perl; | |||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | sub dump { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
dump: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | return Data::Dumper->Dump( [$self], ['tag'] ); | |||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||