| 1 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | package Text::ZTemplate; | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Cwd qw(abs_path); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Data::ZPath; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use File::Basename qw(dirname); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | our $VERSION = '0.001000'; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 11 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=11 SCAR=26.3
Pod
documented
| |||||||||||||||||||||||||||||
| 15 | 11 | my ( $class, %args ) = @_; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 11 | my $string = $args{string}; | |||||||||||||||||||||||||||
| 18 | 11 | my $file = $args{file}; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 11 | ▶ | if ( defined $string and defined $file ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $string and defined $file
MC/DC0%0/2
defined $string and defined $file
defined $string
defined $file
Truth table66%2/3
A and B
A= defined $stringB= defined $file
| |||||||||||||||||||||||||||||
| 21 | 0 | croak q{Specify only one of "string" or "file"}; | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | 11 | ▶ | if ( not defined $string and not defined $file ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $string or defined $file
MC/DC0%0/2
defined $string or defined $file
defined $string
defined $file
Truth table66%2/3
A or B
A= defined $stringB= defined $file
| |||||||||||||||||||||||||||||
| 24 | 0 | croak q{Missing template source: provide "string" or "file"}; | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 11 | ▶ | my $escape = defined $args{escape} ? lc $args{escape} : 'html'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 11 | ▶ | if ( $escape ne 'html' and $escape ne 'raw' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$escape ne "html" and $escape ne "raw"
MC/DC0%0/2
$escape ne "html" and $escape ne "raw"
$escape ne "html"
$escape ne "raw"
Truth table66%2/3
A and B
A= $escape ne "html"B= $escape ne "raw"
| |||||||||||||||||||||||||||||
| 29 | 0 | croak qq{Invalid escape mode "$escape"}; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 11 | ▶ | if ( defined $file ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 3 | ( $string, $file ) = _read_template_file($file); | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | my $tokens = _parse_template( | ||||||||||||||||||||||||||||
| 37 | template => $string // q{}, | ||||||||||||||||||||||||||||
| 38 | current_file => $file, | ||||||||||||||||||||||||||||
| 39 | seen_files => {}, | ||||||||||||||||||||||||||||
| 40 | 11 | ▶ | includes => exists $args{includes} ? $args{includes} : 1, | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$string // ""
MC/DC0%0/1
$string // ""
$string
Truth table50%1/2
A
A= $string
| |||||||||||||||||||||||||||||
| 41 | ); | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 9 | my $self = bless { | |||||||||||||||||||||||||||
| 44 | escape => $escape, | ||||||||||||||||||||||||||||
| 45 | tree => $tokens, | ||||||||||||||||||||||||||||
| 46 | }, $class; | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 9 | return $self; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 11 | ▶ | sub process { | ||||||||||||||||||||||||||
|
Subroutine
process: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | 11 | my ( $self, $data ) = @_; | |||||||||||||||||||||||||||
| 53 | 11 | ▶ | croak 'process() requires a data model' unless defined $data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | return _render_nodes( | ||||||||||||||||||||||||||||
| 56 | nodes => $self->{tree}, | ||||||||||||||||||||||||||||
| 57 | context => $data, | ||||||||||||||||||||||||||||
| 58 | default_escape => $self->{escape}, | ||||||||||||||||||||||||||||
| 59 | 11 | ); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 15 | ▶ | sub _parse_template { | ||||||||||||||||||||||||||
|
Subroutine
_parse_template: called CC=18 SCAR=29.4
| |||||||||||||||||||||||||||||
| 63 | 15 | my ( %args ) = @_; | |||||||||||||||||||||||||||
| 64 | 15 | my $template = $args{template}; | |||||||||||||||||||||||||||
| 65 | 15 | my $current_file = $args{current_file}; | |||||||||||||||||||||||||||
| 66 | 15 | ▶ | my $seen_files = $args{seen_files} || {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{'seen_files'} || {}
MC/DC0%0/1
$args{'seen_files'} || {}
$args{'seen_files'}
Truth table50%1/2
A
A= $args{'seen_files'}
| |||||||||||||||||||||||||||||
| 67 | 15 | my $includes = $args{includes}; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 15 | my $root = []; | |||||||||||||||||||||||||||
| 70 | 15 | my @stack = ({ | |||||||||||||||||||||||||||
| 71 | expr => undef, | ||||||||||||||||||||||||||||
| 72 | nodes => $root, | ||||||||||||||||||||||||||||
| 73 | }); | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 15 | my $pos = 0; | |||||||||||||||||||||||||||
| 76 | 15 | ▶ | while ( $template =~ /\G(.*?)\{\{/sgc ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 27 | my $text = $1; | |||||||||||||||||||||||||||
| 78 | 27 | ▶ | if ( length $text ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 12 | push @{$stack[-1]{nodes}}, { | |||||||||||||||||||||||||||
| 80 | type => 'text', | ||||||||||||||||||||||||||||
| 81 | text => $text, | ||||||||||||||||||||||||||||
| 82 | }; | ||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 27 | my $tag_start = pos($template) - 2; | |||||||||||||||||||||||||||
| 86 | 27 | ▶ | if ( $template !~ /\G(.*?)\}\}/sgc ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 0 | croak qq{Unterminated tag at character $tag_start}; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 27 | my $raw = $1; | |||||||||||||||||||||||||||
| 91 | 27 | my $trimmed = $raw; | |||||||||||||||||||||||||||
| 92 | 27 | $trimmed =~ s/^\s+//; | |||||||||||||||||||||||||||
| 93 | 27 | $trimmed =~ s/\s+\z//; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 27 | ▶ | if ( $trimmed =~ /^#(.*)\z/s ) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 96 | 5 | my $inner = $1; | |||||||||||||||||||||||||||
| 97 | 5 | $inner =~ s/^\s+//; | |||||||||||||||||||||||||||
| 98 | 5 | $inner =~ s/\s+\z//; | |||||||||||||||||||||||||||
| 99 | 5 | my $parsed = _parse_expression_spec($inner); | |||||||||||||||||||||||||||
| 100 | my $block = { | ||||||||||||||||||||||||||||
| 101 | type => 'block', | ||||||||||||||||||||||||||||
| 102 | expr_src => $parsed->{expr}, | ||||||||||||||||||||||||||||
| 103 | 5 | expr => Data::ZPath->new( $parsed->{expr} ), | |||||||||||||||||||||||||||
| 104 | nodes => [], | ||||||||||||||||||||||||||||
| 105 | }; | ||||||||||||||||||||||||||||
| 106 | 5 | push @{$stack[-1]{nodes}}, $block; | |||||||||||||||||||||||||||
| 107 | push @stack, { | ||||||||||||||||||||||||||||
| 108 | expr => $parsed->{expr}, | ||||||||||||||||||||||||||||
| 109 | nodes => $block->{nodes}, | ||||||||||||||||||||||||||||
| 110 | 5 | }; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | elsif ( $trimmed =~ m{^/(.*)\z}s ) { | ||||||||||||||||||||||||||||
| 113 | 5 | my $inner = $1; | |||||||||||||||||||||||||||
| 114 | 5 | $inner =~ s/^\s+//; | |||||||||||||||||||||||||||
| 115 | 5 | $inner =~ s/\s+\z//; | |||||||||||||||||||||||||||
| 116 | 5 | my $current = pop @stack; | |||||||||||||||||||||||||||
| 117 | 5 | ▶ | if ( not defined $current or not defined $current->{expr} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $current and defined $current->{"expr"}
MC/DC0%0/2
defined $current and defined $current->{"expr"}
defined $current
defined $current->{"expr"}
Truth table33%1/3
A and B
A= defined $currentB= defined $current->{"expr"}
| |||||||||||||||||||||||||||||
| 118 | 0 | croak qq{Mismatched close tag {{/$inner}}}; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | 5 | ▶ | if ( length $inner ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 2 | my $parsed = _parse_expression_spec($inner); | |||||||||||||||||||||||||||
| 122 | 2 | ▶ | if ( $current->{expr} ne $parsed->{expr} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 0 | croak qq{Mismatched close tag {{/$inner}} for {{$current->{expr}}}}; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | elsif ( length $trimmed ) { | ||||||||||||||||||||||||||||
| 128 | 17 | ▶ | if ( $trimmed =~ /^>(.*)\z/s ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 6 | ▶ | croak 'Template includes are disabled' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | unless $includes; | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 5 | my $include_path = $1; | |||||||||||||||||||||||||||
| 133 | 5 | $include_path =~ s/^\s+//; | |||||||||||||||||||||||||||
| 134 | 5 | $include_path =~ s/\s+\z//; | |||||||||||||||||||||||||||
| 135 | 5 | ▶ | croak 'Empty include path in template tag' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | unless length $include_path; | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 5 | my $resolved_file = _resolve_include_path( | |||||||||||||||||||||||||||
| 139 | include_path => $include_path, | ||||||||||||||||||||||||||||
| 140 | current_file => $current_file, | ||||||||||||||||||||||||||||
| 141 | ); | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 5 | my $key = _canonical_path($resolved_file); | |||||||||||||||||||||||||||
| 144 | 5 | ▶ | if ( $seen_files->{$key} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 1 | croak qq{Circular include detected for "$resolved_file"}; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 4 | $seen_files->{$key} = 1; | |||||||||||||||||||||||||||
| 149 | 4 | my ( $include_text, $include_file ) = | |||||||||||||||||||||||||||
| 150 | _read_template_file($resolved_file); | ||||||||||||||||||||||||||||
| 151 | 4 | my $include_nodes = _parse_template( | |||||||||||||||||||||||||||
| 152 | template => $include_text, | ||||||||||||||||||||||||||||
| 153 | current_file => $include_file, | ||||||||||||||||||||||||||||
| 154 | seen_files => $seen_files, | ||||||||||||||||||||||||||||
| 155 | includes => $includes, | ||||||||||||||||||||||||||||
| 156 | ); | ||||||||||||||||||||||||||||
| 157 | 2 | delete $seen_files->{$key}; | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 2 | push @{$stack[-1]{nodes}}, @$include_nodes; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | else { | ||||||||||||||||||||||||||||
| 162 | 11 | my $parsed = _parse_expression_spec($trimmed); | |||||||||||||||||||||||||||
| 163 | 11 | push @{$stack[-1]{nodes}}, { | |||||||||||||||||||||||||||
| 164 | type => 'expr', | ||||||||||||||||||||||||||||
| 165 | expr_src => $parsed->{expr}, | ||||||||||||||||||||||||||||
| 166 | escape => $parsed->{escape}, | ||||||||||||||||||||||||||||
| 167 | 11 | expr => Data::ZPath->new( $parsed->{expr} ), | |||||||||||||||||||||||||||
| 168 | }; | ||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 23 | $pos = pos($template); | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 11 | my $tail = substr( $template, $pos ); | |||||||||||||||||||||||||||
| 176 | 11 | ▶ | if ( length $tail ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | 4 | push @{$stack[-1]{nodes}}, { | |||||||||||||||||||||||||||
| 178 | type => 'text', | ||||||||||||||||||||||||||||
| 179 | text => $tail, | ||||||||||||||||||||||||||||
| 180 | }; | ||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 11 | ▶ | if ( @stack > 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 0 | my $missing = $stack[-1]{expr}; | |||||||||||||||||||||||||||
| 185 | 0 | croak qq{Missing close tag for {{$missing}}}; | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 11 | return $root; | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 7 | ▶ | sub _read_template_file { | ||||||||||||||||||||||||||
|
Subroutine
_read_template_file: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 193 | 7 | my ( $file ) = @_; | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 7 | ▶ | open my $fh, '<:encoding(UTF-8)', $file | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | or croak qq{Unable to read template file "$file": $!}; | ||||||||||||||||||||||||||||
| 197 | 7 | local $/; | |||||||||||||||||||||||||||
| 198 | 7 | my $text = <$fh>; | |||||||||||||||||||||||||||
| 199 | 7 | close $fh; | |||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 7 | my $canonical = _canonical_path($file); | |||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 7 | return ( $text, $canonical ); | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 5 | ▶ | sub _resolve_include_path { | ||||||||||||||||||||||||||
|
Subroutine
_resolve_include_path: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||
| 207 | 5 | my ( %args ) = @_; | |||||||||||||||||||||||||||
| 208 | 5 | my $include_path = $args{include_path}; | |||||||||||||||||||||||||||
| 209 | 5 | my $current_file = $args{current_file}; | |||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | 5 | ▶ | if ( File::Spec->file_name_is_absolute($include_path) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 212 | 0 | return $include_path; | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 5 | ▶ | croak qq{Relative include path "$include_path" requires file-based template source} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 216 | unless defined $current_file; | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | 5 | my $base_dir = dirname($current_file); | |||||||||||||||||||||||||||
| 219 | 5 | my $resolved = File::Spec->catfile( $base_dir, $include_path ); | |||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 5 | return $resolved; | |||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | 12 | ▶ | sub _canonical_path { | ||||||||||||||||||||||||||
|
Subroutine
_canonical_path: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 225 | 12 | my ( $path ) = @_; | |||||||||||||||||||||||||||
| 226 | 12 | my $abs = abs_path($path); | |||||||||||||||||||||||||||
| 227 | 12 | ▶ | return defined $abs ? $abs : File::Spec->rel2abs($path); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 18 | ▶ | sub _parse_expression_spec { | ||||||||||||||||||||||||||
|
Subroutine
_parse_expression_spec: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||
| 231 | 18 | my ( $raw ) = @_; | |||||||||||||||||||||||||||
| 232 | 18 | my $expr = $raw; | |||||||||||||||||||||||||||
| 233 | 18 | my $escape; | |||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | 18 | my $split = _find_escape_separator($raw); | |||||||||||||||||||||||||||
| 236 | 18 | ▶ | if ( defined $split ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 237 | 2 | my ( $lhs, $rhs ) = @$split; | |||||||||||||||||||||||||||
| 238 | 2 | ▶ | if ( $rhs eq 'html' or $rhs eq 'raw' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$rhs eq "html" or $rhs eq "raw"
MC/DC0%0/2
$rhs eq "html" or $rhs eq "raw"
$rhs eq "html"
$rhs eq "raw"
Truth table66%2/3
A or B
A= $rhs eq "html"B= $rhs eq "raw"
| |||||||||||||||||||||||||||||
| 239 | 2 | $expr = $lhs; | |||||||||||||||||||||||||||
| 240 | 2 | $escape = $rhs; | |||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | 18 | $expr =~ s/^\s+//; | |||||||||||||||||||||||||||
| 245 | 18 | $expr =~ s/\s+\z//; | |||||||||||||||||||||||||||
| 246 | 18 | ▶ | croak 'Empty expression in template tag' unless length $expr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | return { | ||||||||||||||||||||||||||||
| 249 | 18 | expr => $expr, | |||||||||||||||||||||||||||
| 250 | escape => $escape, | ||||||||||||||||||||||||||||
| 251 | }; | ||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | 18 | ▶ | sub _find_escape_separator { | ||||||||||||||||||||||||||
|
Subroutine
_find_escape_separator: called CC=9 SCAR=26.9
| |||||||||||||||||||||||||||||
| 255 | 18 | my ( $text ) = @_; | |||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | 18 | my $quote = q{}; | |||||||||||||||||||||||||||
| 258 | 18 | ▶ | for ( my $i = 0; $i < length $text; $i++ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 259 | 194 | my $ch = substr( $text, $i, 1 ); | |||||||||||||||||||||||||||
| 260 | 194 | ▶ | if ( $quote ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 261 | 0 | ▶ | if ( $ch eq '\\' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 262 | 0 | $i++; | |||||||||||||||||||||||||||
| 263 | 0 | next; | |||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||
| 265 | 0 | ▶ | if ( $ch eq $quote ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 266 | 0 | $quote = q{}; | |||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||
| 268 | 0 | next; | |||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | 194 | ▶ | if ( $ch eq q{"} or $ch eq q{'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$ch eq "\"" or $ch eq "'"
MC/DC0%0/2
$ch eq "\"" or $ch eq "'"
$ch eq "\""
$ch eq "'"
Truth table33%1/3
A or B
A= $ch eq "\""B= $ch eq "'"
| |||||||||||||||||||||||||||||
| 272 | 0 | $quote = $ch; | |||||||||||||||||||||||||||
| 273 | 0 | next; | |||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | 194 | ▶ | next unless $ch eq ':'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 277 | 2 | ▶ | next unless substr( $text, $i, 2 ) eq '::'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | 2 | my $lhs = substr( $text, 0, $i ); | |||||||||||||||||||||||||||
| 280 | 2 | my $rhs = substr( $text, $i + 2 ); | |||||||||||||||||||||||||||
| 281 | 2 | $lhs =~ s/\s+\z//; | |||||||||||||||||||||||||||
| 282 | 2 | $rhs =~ s/^\s+//; | |||||||||||||||||||||||||||
| 283 | 2 | $rhs =~ s/\s+\z//; | |||||||||||||||||||||||||||
| 284 | 2 | $rhs = lc $rhs; | |||||||||||||||||||||||||||
| 285 | 2 | return [ $lhs, $rhs ]; | |||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | 16 | return undef; | |||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | 18 | ▶ | sub _render_nodes { | ||||||||||||||||||||||||||
|
Subroutine
_render_nodes: called CC=11 SCAR=24.0
| |||||||||||||||||||||||||||||
| 292 | 18 | my ( %args ) = @_; | |||||||||||||||||||||||||||
| 293 | 18 | my $nodes = $args{nodes}; | |||||||||||||||||||||||||||
| 294 | 18 | my $context = $args{context}; | |||||||||||||||||||||||||||
| 295 | 18 | my $default_escape = $args{default_escape}; | |||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | 18 | my $out = q{}; | |||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | 18 | for my $node ( @$nodes ) { | |||||||||||||||||||||||||||
| 300 | 40 | ▶ | if ( $node->{type} eq 'text' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 301 | 20 | $out .= $node->{text}; | |||||||||||||||||||||||||||
| 302 | 20 | next; | |||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | 20 | ▶ | if ( $node->{type} eq 'expr' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 306 | 14 | my @vals = $node->{expr}->evaluate( $context ); | |||||||||||||||||||||||||||
| 307 | 14 | my $buf = q{}; | |||||||||||||||||||||||||||
| 308 | 14 | for my $val ( @vals ) { | |||||||||||||||||||||||||||
| 309 | 14 | my $sv = $val->string_value; | |||||||||||||||||||||||||||
| 310 | 14 | ▶ | next unless defined $sv; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 311 | 14 | $buf .= $sv; | |||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | my $escape = defined $node->{escape} | ||||||||||||||||||||||||||||
| 315 | ? $node->{escape} | ||||||||||||||||||||||||||||
| 316 | 14 | ▶ | : $default_escape; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 317 | 14 | $out .= _escape( $buf, $escape ); | |||||||||||||||||||||||||||
| 318 | 14 | next; | |||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | 6 | ▶ | if ( $node->{type} eq 'block' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 322 | 6 | my @vals = $node->{expr}->evaluate( $context ); | |||||||||||||||||||||||||||
| 323 | 6 | for my $val ( @vals ) { | |||||||||||||||||||||||||||
| 324 | 8 | ▶ | next unless _truthy( $val ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 325 | 7 | ▶ | my $inner_context = defined $val->id ? $val : $context; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 326 | $out .= _render_nodes( | ||||||||||||||||||||||||||||
| 327 | nodes => $node->{nodes}, | ||||||||||||||||||||||||||||
| 328 | 7 | context => $inner_context, | |||||||||||||||||||||||||||
| 329 | default_escape => $default_escape, | ||||||||||||||||||||||||||||
| 330 | ); | ||||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||
| 332 | 6 | next; | |||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | 18 | return $out; | |||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | 8 | ▶ | sub _truthy { | ||||||||||||||||||||||||||
|
Subroutine
_truthy: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 340 | 8 | my ( $node ) = @_; | |||||||||||||||||||||||||||
| 341 | 8 | ▶ | return !!0 unless $node; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 342 | 8 | return !!$node->primitive_value; | |||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | 14 | ▶ | sub _escape { | ||||||||||||||||||||||||||
|
Subroutine
_escape: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 346 | 14 | my ( $value, $mode ) = @_; | |||||||||||||||||||||||||||
| 347 | 14 | ▶ | return q{} unless defined $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | 14 | ▶ | if ( $mode eq 'raw' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 350 | 12 | return $value; | |||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | 2 | ▶ | if ( $mode eq 'html' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 354 | 2 | $value =~ s/\&/\&/g; | |||||||||||||||||||||||||||
| 355 | 2 | $value =~ s/\</\</g; | |||||||||||||||||||||||||||
| 356 | 2 | $value =~ s/\>/\>/g; | |||||||||||||||||||||||||||
| 357 | 2 | $value =~ s/"/\"/g; | |||||||||||||||||||||||||||
| 358 | 2 | $value =~ s/'/\'/g; | |||||||||||||||||||||||||||
| 359 | 2 | return $value; | |||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | 0 | croak qq{Unknown escape mode "$mode"}; | |||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | 1 | 1; | |||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | __END__ | ||||||||||||||||||||||||||||