Positron/Template.pm

filter: Statementstmt 95.0% 268 / 282 Branchbran 86.9% 113 / 130 Conditioncond 72.2% 52 / 72 MC/DCmcdc 52.5% 21 / 40 Subroutinesub 96.1% 25 / 26 Podpod 0.0% 0 / 4 totaltotal 86.4% 479 / 554 CC 100 SCAR 21.8
CC
100
Cov
89%
_process_wrap
13.7
_process_text
13.0
_process_switch
12.0
CRAP 8.8
« Positron/Expression.pm ↑ Summary Positron/Environment.pm »
1 package Positron::Template;
2 12 our $VERSION = 'v0.1.3'; # VERSION
3  
4 =head1 NAME
5  
6 Positron::Template - a DOM based templating system
7  
8 =head1 VERSION
9  
10 version v0.1.3
11  
12 =head1 SYNOPSIS
13  
14 use Positron::Template;
15  
16 my $template = Positron::Template->new();
17  
18 my $dom = create_dom_tree();
19 my $data = { foo => 'bar', baz => [ 1, 2, 3 ] };
20 my $result = $template->process($dom, $data);
21  
22 =cut
23  
24 12 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 12 ▶use Positron::Environment;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 12 ▶use Positron::Expression;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 12 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 11 ▶sub new {
Subroutine new: called CC=6 SCAR=21.8
Pod undocumented
34 11 my ($class, %options) = @_;
35 my $self = {
36 opener => '{',
37 closer => '}',
38 dom => $options{dom} // undef,
39 environment => Positron::Environment->new($options{env} // undef, { immutable => 1 }) // undef,
40 11 ▶ handler => $options{handler} || _handler_for($options{dom}) // undef,
Condition45%5/11
$options{"dom"} // undef
l!l
011
"Positron::Environment"->new($options{"env"} // undef, {"immutable", 1}) // undef
l!l
110
$options{"env"} // undef
l!l
011
($options{"handler"} || _handler_for($options{"dom"})) // undef
l!l
011
$options{"handler"} or _handler_for($options{"dom"})
l!l&&r!l&&!r
0011
MC/DC0%0/1
$options{"dom"} // undef
$options{"dom"}
MC/DC0%0/1
"Positron::Environment"->new($options{"env"} // undef, {"immutable", 1}) // undef
"Positron::Environment"->new($options{"env"} // undef, {"immutable", 1})
MC/DC0%0/1
$options{"env"} // undef
$options{"env"}
MC/DC0%0/2
($options{"handler"} || _handler_for($options{"dom"})) // undef
$options{"handler"} _handler_for($options{"dom"})
Truth table50%1/2
A
A= $options{"dom"}
Aresult
11
00
Truth table50%1/2
A
A= "Positron::Environment"->new($options{"env"} // undef, {"immutable", 1})
Aresult
11
00
Truth table50%1/2
A
A= $options{"env"}
Aresult
11
00
Truth table33%1/3
A or B
A= $options{"handler"}B= _handler_for($options{"dom"})
ABresult
1X1
011
000
41 include_paths => ['.'],
42 };
43 11 return bless ($self, $class);
44 }
45  
46 # Stop writing these until need is shown
47 0 ▶sub dom {
Subroutine dom: not called CC=2 SCAR=17.9
Pod undocumented
48 0 my ($self, $dom) = @_;
49 0 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
50 0 return $self->{'dom'};
51 } else {
52 0 $self->{'dom'} = $dom;
53 }
54 }
55  
56 2 ▶sub add_include_paths {
Subroutine add_include_paths: called CC=1 SCAR=0.0
Pod undocumented
57 2 my ($self, @paths) = @_;
58 2 push @{$self->{'include_paths'}}, @paths;
59 }
60  
61  
62 141 ▶sub process {
Subroutine process: called CC=9 SCAR=24.8
Pod undocumented
63 141 my ($self, $dom, $data) = @_;
64 # TODO: what if only one is passed? -> $self attributes
65 # What if a HashRef is passed? -> new Environment object
66 141 ▶ if (ref($dom) eq 'HASH' or blessed($dom) and $dom->isa('Positron::Environment')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $dom eq "HASH" or builtin::blessed($dom) and $dom->isa("Positron::Environment")) 0 141 141
Condition33%2/6
ref $dom eq "HASH" or builtin::blessed($dom) and $dom->isa("Positron::Environment")
l!l&&r!l&&!r
00141
builtin::blessed($dom) and $dom->isa("Positron::Environment")
!ll&&!rl&&r
14100
MC/DC0%0/3
ref $dom eq "HASH" or builtin::blessed($dom) and $dom->isa("Positron::Environment")
ref $dom eq "HASH" builtin::blessed($dom) $dom->isa("Positron::Environment")
Truth table25%1/4
A or B and C
A= ref $dom eq "HASH"B= builtin::blessed($dom)C= $dom->isa("Positron::Environment")
ABCresult
1XX1
0111
00X0
0100
67 0 $data = $dom;
68 0 $dom = $self->{'dom'};
69 }
70 141 ▶ if (not $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 0 141 141
71 0 $data = $self->{'environment'};
72 }
73 141 ▶ if (ref($data) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq "HASH") 141 0 141
74 141 $data = Positron::Environment->new($data);
75 }
76  
77 141 ▶ if (not ref($dom)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $dom) 23 118 141
78 23 return $self->_process_text($dom, $data);
79 }
80 # Real DOM -> List of nodes
81
82 118 my @nodes = ();
83  
84 118 ▶ $self->{'handler'} //= _handler_for($dom);
Condition66%2/3
$self->{"handler"} //= _handler_for($dom)
l!l&&r!l&&!r
10990
MC/DC0%0/2
$self->{"handler"} //= _handler_for($dom)
$self->{"handler"} _handler_for($dom)
Truth table66%2/3
A //= B
A= $self->{"handler"}B= _handler_for($dom)
ABresult
1X1
011
000
85 118 @nodes = $self->_process_element($dom, $data);
86 # DESTROY Handler?
87  
88 # Many people know that they will get a single node here.
89 # May as well not force them to unpack a list.
90 114 ▶ return (wantarray) ? @nodes : $nodes[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 109 5 114
91 }
92  
93 146 ▶sub _process_text {
Subroutine _process_text: called CC=13 SCAR=25.7
94 146 my ($self, $string, $environment, $with_quants) = @_;
95 146 my $string_finder = $self->_make_finder('$');
96 146 my $last_changing_quant = undef;
97 146 my $did_change = undef;
98 # First $ sigils; the quantifier chomps whitespace around it.
99 146 $string =~ s{
100 (\s*)
101 $string_finder
102 (\s*)
103 }{
104 81 my ($ws_before, $sigil, $quant, $expr, $ws_after) = ($1, $2, $3, $4, $5);
105 81 ▶ my $replacement = Positron::Expression::evaluate($expr, $environment) // '';
Condition100%2/2
Positron::Expression::evaluate($expr, $environment) // ""
l!l
774
MC/DC100%1/1
Positron::Expression::evaluate($expr, $environment) // ""
Positron::Expression::evaluate($expr, $environment)
Truth table100%2/2
A
A= Positron::Expression::evaluate($expr, $environment)
Aresult
11
00
106 81 ▶ if ($quant eq '-') {
Branch: 2 branches
BranchTrueFalseTotal
if ($quant eq '-') { } 3 78 81
elsif ($quant eq '*') { } 3 75 78
107 3 $ws_before = '';
108 3 $ws_after = '';
109 } elsif ($quant eq '*') {
110 3 ▶ $ws_before &&= ' ';
Condition100%2/2
$ws_before &&= " "
!ll
12
MC/DC100%1/1
$ws_before &&= " "
$ws_before
Truth table100%2/2
A
A= $ws_before
Aresult
00
11
111 3 ▶ $ws_after &&= ' ';
Condition50%1/2
$ws_after &&= " "
!ll
03
MC/DC0%0/1
$ws_after &&= " "
$ws_after
Truth table50%1/2
A
A= $ws_after
Aresult
00
11
112 3 ▶ if ($replacement eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($replacement eq "") 1 2 3
113 1 $ws_before = ' '; $ws_after = '';
114 }
115 }
116 81 $last_changing_quant = $quant;
117 81 $did_change = 1;
118 81 "$ws_before$replacement$ws_after";
119 }xmseg;
120 # Next comments; the quantifier chomps whitespace around it.
121 146 my $comment_finder = $self->_make_finder('#');
122 146 $string =~ s{
123 (\s*)
124 $comment_finder
125 (\s*)
126 }{
127 12 my ($ws_before, $sigil, $quant, $comment, $ws_after) = ($1, $2, $3, $4, $5);
128 12 ▶ if ($quant eq '-') {
Branch: 2 branches
BranchTrueFalseTotal
if ($quant eq '-') { } 1 11 12
elsif ($quant eq '*') { } 2 9 11
129 1 $ws_before = '';
130 1 $ws_after = '';
131 } elsif ($quant eq '*') {
132 2 $ws_before = ' ';
133 2 $ws_after = '';
134 }
135 12 $last_changing_quant = $quant;
136 12 $did_change = 1;
137 12 "$ws_before$ws_after";
138 }xmseg;
139 # Next voider; the quantifier chomps whitespace around it.
140 146 my $voider_finder = $self->_make_finder('~');
141 146 $string =~ s{
142 (\s*)
143 $voider_finder
144 (\s*)
145 }{
146 10 my ($ws_before, $sigil, $quant, undef, $ws_after) = ($1, $2, $3, $4, $5);
147 10 ▶ if ($quant eq '-') {
Branch: 2 branches
BranchTrueFalseTotal
if ($quant eq '-') { } 1 9 10
elsif ($quant eq '*') { } 1 8 9
148 1 $ws_before = '';
149 1 $ws_after = '';
150 } elsif ($quant eq '*') {
151 1 $ws_before = ' ';
152 1 $ws_after = ' ';
153 1 ▶ if ("$ws_before$ws_after" =~ m{\A \s+ \z}xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ("$ws_before$ws_after" =~ /\A \s+ \z/msx) 1 0 1
154 1 $ws_before = ' '; $ws_after = '';
155 }
156 }
157 10 $last_changing_quant = $quant;
158 10 $did_change = 1;
159 10 "$ws_before$ws_after";
160 }xmseg;
161 146 ▶ return $with_quants? ($string, $did_change, $last_changing_quant) : $string;
Branch: 1 branch
BranchTrueFalseTotal
$with_quants ? : 21 125 146
162 }
163  
164 525 ▶sub _process_element {
Subroutine _process_element: called CC=10 SCAR=23.0
165 525 my ($self, $node, $environment) = @_;
166 525 my $handler = $self->{'handler'};
167  
168 525 ▶ if (not ref($node)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $node) 102 423 525
169 102 return $self->_process_text($node, $environment);
170 }
171  
172 # check for assignments
173 # create a modified environment if some are detected
174 # proceed as normal
175  
176 # Evaluate structure sigils
177 423 my ($sigil, $quant, $tail) = $self->_get_structure_sigil($node);
178  
179 # Have sigil, evaluate
180 423 ▶ $sigil //= ''; # for 'eq'
Condition100%2/2
$sigil //= ""
l!l
204219
MC/DC100%1/1
$sigil //= ""
$sigil
Truth table100%2/2
A
A= $sigil
Aresult
11
00
181 423 ▶ if ($sigil eq '@') {
Branch: 7 branches
BranchTrueFalseTotal
if ($sigil eq '@') { } 31 392 423
elsif ($sigil ~~ ['?', '!']) { } 23 369 392
elsif ($sigil eq '|') { } 75 294 369
elsif ($sigil eq '/') { } 4 290 294
elsif ($sigil ~~ ['.', ',']) { } 12 278 290
elsif ($sigil ~~ [':', ';']) { } 50 228 278
elsif ($sigil eq '^') { } 9 219 228
182 31 return $self->_process_loop($node, $environment, $sigil, $quant, $tail);
183 } elsif ($sigil ~~ ['?', '!']) {
184 23 return $self->_process_condition($node, $environment, $sigil, $quant, $tail);
185 } elsif ($sigil eq '|') {
186 75 return $self->_process_switch($node, $environment, $sigil, $quant, $tail);
187 } elsif ($sigil eq '/') {
188 4 return $self->_process_structure_comment($node, $environment, $sigil, $quant, $tail);
189 } elsif ($sigil ~~ ['.', ',']) {
190 12 return $self->_process_include($node, $environment, $sigil, $quant, $tail);
191 } elsif ($sigil ~~ [':', ';']) {
192 50 return $self->_process_wrap($node, $environment, $sigil, $quant, $tail);
193 } elsif ($sigil eq '^') {
194 9 return $self->_process_function($node, $environment, $sigil, $quant, $tail);
195 } else {
196 219 my $new_node = $handler->shallow_clone($node);
197 219 $handler->push_contents( $new_node, map { $self->_process_element($_, $environment) } $handler->list_contents($node));
198 215 $self->_remove_structure_sigils($new_node);
199 #$self->resolve_hash_attr($new_node, $environment);
200 215 $self->_resolve_text_attr($new_node, $environment);
201 215 return $new_node;
202 }
203 # String ones
204 0 return $node;
205 }
206  
207 31 ▶sub _process_loop {
Subroutine _process_loop: called CC=8 SCAR=20.8
208 31 my ($self, $node, $environment, $sigil, $quant, $tail) = @_;
209 31 my $handler = $self->{'handler'};
210 # TODO: true() and coercion!
211 31 ▶ my $loop = Positron::Expression::evaluate($tail, $environment) || [];
Condition50%1/2
Positron::Expression::evaluate($tail, $environment) || []
l!l
310
MC/DC0%0/1
Positron::Expression::evaluate($tail, $environment) || []
Positron::Expression::evaluate($tail, $environment)
Truth table50%1/2
A
A= Positron::Expression::evaluate($tail, $environment)
Aresult
11
00
212 31 ▶ if (not @$loop) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$loop) 8 23 31
213 # keep if we should, else nothing
214 8 ▶ return ($quant eq '+') ? ($self->_clone_and_resolve($node, $environment)) : ();
Branch: 1 branch
BranchTrueFalseTotal
$quant eq '+' ? : 3 5 8
215 }
216 # else have loop
217 23 my @contents;
218 23 foreach my $row (@$loop) {
219 46 my $env = Positron::Environment->new($row, {parent => $environment});
220 46 my @row_contents = map { $self->_process_element( $_, $env) } $handler->list_contents($node);
221 46 ▶ push @contents, ($quant eq '*') ? ($self->_clone_and_resolve($node, $env, @row_contents)) : @row_contents;
Branch: 1 branch
BranchTrueFalseTotal
$quant eq '*' ? : 2 44 46
222 }
223 23 ▶ if ($quant ne '-' and $quant ne '*') { # remove this in any case
Branch: 1 branch
BranchTrueFalseTotal
if ($quant ne "-" and $quant ne "*") 21 2 23
Condition100%3/3
$quant ne "-" and $quant ne "*"
!ll&&!rl&&r
1121
MC/DC100%2/2
$quant ne "-" and $quant ne "*"
$quant ne "-" $quant ne "*"
Truth table100%3/3
A and B
A= $quant ne "-"B= $quant ne "*"
ABresult
0X0
100
111
224 21 return ($self->_clone_and_resolve($node, $environment, @contents));
225 }
226 2 return @contents;
227 }
228  
229 23 ▶sub _process_condition {
Subroutine _process_condition: called CC=7 SCAR=19.5
230 23 my ($self, $node, $environment, $sigil, $quant, $tail) = @_;
231 23 my $handler = $self->{'handler'};
232 23 my $truth = Positron::Expression::true(Positron::Expression::evaluate($tail, $environment));
233 23 ▶ if ($sigil eq '!') {$truth = not $truth;}
Branch: 1 branch
BranchTrueFalseTotal
if ($sigil eq "!") 9 14 23
234 23 ▶ my $keep = ($truth and $quant ne '-' or $quant eq '+');
Condition100%6/6
$truth && $quant ne '-' || $quant eq '+'
l!l&&r!l&&!r
8312
$truth and $quant ne "-"
!ll&&!rl&&r
1328
MC/DC100%3/3
$truth && $quant ne '-' || $quant eq '+'
$truth $quant ne "-" $quant eq '+'
Truth table80%4/5
A and B || C
A= $truthB= $quant ne "-"C= $quant eq '+'
ABCresult
11X1
0X11
1011
0X00
1000
235 23 my @contents = ();
236 23 ▶ if ($truth or $quant eq '*') {
Branch: 1 branch
BranchTrueFalseTotal
if ($truth or $quant eq "*") 12 11 23
Condition100%3/3
$truth or $quant eq "*"
l!l&&r!l&&!r
10211
MC/DC100%2/2
$truth or $quant eq "*"
$truth $quant eq "*"
Truth table100%3/3
A or B
A= $truthB= $quant eq "*"
ABresult
1X1
011
000
237 12 @contents = map { $self->_process_element($_, $environment) } $handler->list_contents($node);
238 }
239 23 ▶ return ($keep) ? ($self->_clone_and_resolve($node, $environment, @contents)) : @contents;
Branch: 1 branch
BranchTrueFalseTotal
$keep ? : 11 12 23
240 }
241  
242 75 ▶sub _process_switch {
Subroutine _process_switch: called CC=12 SCAR=24.9
243 75 my ($self, $node, $environment, $sigil, $quant, $tail) = @_;
244 75 my $handler = $self->{'handler'};
245 75 my $truth;
246 75 ▶ if ($tail =~ m{ \A : \s+ (.*) }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tail =~ / \A : \s+ (.*) /msx) { } 15 60 75
247 # "switch/given": setter
248 15 my $expr = $1;
249 15 ▶ my $goal = Positron::Expression::evaluate($expr, $environment) // ''; # always defined
Condition100%2/2
Positron::Expression::evaluate($expr, $environment) // ""
l!l
123
MC/DC100%1/1
Positron::Expression::evaluate($expr, $environment) // ""
Positron::Expression::evaluate($expr, $environment)
Truth table100%2/2
A
A= Positron::Expression::evaluate($expr, $environment)
Aresult
11
00
250 15 $environment = Positron::Environment->new({'|' => $goal}, { parent => $environment, immutable => 0});
251 15 $truth = 1; # counts as a true condition for quantifiers
252 } else {
253 # "case/when": getter
254 60 my $goal = $environment->get('|');
255 60 ▶ if (defined ($goal)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $goal) { } 52 8 60
256 # not consumed yet
257 52 ▶ my $test = Positron::Expression::evaluate($tail, $environment) // '';
Condition100%2/2
Positron::Expression::evaluate($tail, $environment) // ""
l!l
3814
MC/DC100%1/1
Positron::Expression::evaluate($tail, $environment) // ""
Positron::Expression::evaluate($tail, $environment)
Truth table100%2/2
A
A= Positron::Expression::evaluate($tail, $environment)
Aresult
11
00
258 # "truth" as in a condition
259 # To trigger the default, the $tail expression must be "empty", not the result!
260 52 ▶ $truth = ($goal eq $test or $tail =~ m{ \A \s* \z }xms);
Condition100%3/3
$goal eq $test || $tail =~ / \A \s* \z /msx
l!l&&r!l&&!r
9637
MC/DC100%2/2
$goal eq $test || $tail =~ / \A \s* \z /msx
$goal eq $test $tail =~ / \A \s* \z /msx
Truth table100%3/3
A || B
A= $goal eq $testB= $tail =~ / \A \s* \z /msx
ABresult
1X1
011
000
261 52 ▶ if ($truth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($truth) 15 37 52
262 # remember the match for defaults
263 15 $environment->set('|', undef); # Don't delete, because of parent
264 }
265 } else {
266 # already consumed, or never saw a '|:'
267 8 $truth = 0;
268 }
269 }
270 # Keep and contents (see condition)
271 75 my @contents = ();
272 75 ▶ my $keep = ($truth and $quant ne '-' or $quant eq '+');
Condition66%4/6
$truth && $quant ne '-' || $quant eq '+'
l!l&&r!l&&!r
30045
$truth and $quant ne "-"
!ll&&!rl&&r
45030
MC/DC33%1/3
$truth && $quant ne '-' || $quant eq '+'
$truth $quant ne "-" $quant eq '+'
Truth table40%2/5
A and B || C
A= $truthB= $quant ne "-"C= $quant eq '+'
ABCresult
11X1
0X11
1011
0X00
1000
273 75 ▶ if ($truth or $quant eq '*') {
Branch: 1 branch
BranchTrueFalseTotal
if ($truth or $quant eq "*") 30 45 75
Condition66%2/3
$truth or $quant eq "*"
l!l&&r!l&&!r
30045
MC/DC50%1/2
$truth or $quant eq "*"
$truth $quant eq "*"
Truth table66%2/3
A or B
A= $truthB= $quant eq "*"
ABresult
1X1
011
000
274 30 @contents = map { $self->_process_element($_, $environment) } $handler->list_contents($node);
275 }
276 75 ▶ return ($keep) ? ($self->_clone_and_resolve($node, $environment, @contents)) : @contents;
Branch: 1 branch
BranchTrueFalseTotal
$keep ? : 30 45 75
277 }
278  
279 4 ▶sub _process_structure_comment {
Subroutine _process_structure_comment: called CC=3 SCAR=11.0
280 4 my ($self, $node, $environment, $sigil, $quant, $tail) = @_;
281 4 my $handler = $self->{'handler'};
282 # basically an always-false condition
283 # we could reuse our $keep and @contents code here, but this is probably
284 # more readable:
285 4 ▶ if ($quant eq '+') {
Branch: 2 branches
BranchTrueFalseTotal
if ($quant eq '+') { } 1 3 4
elsif ($quant eq '*') { } 1 2 3
286 # keep node, not contents
287 1 return $self->_clone_and_resolve($node, $environment);
288 } elsif ($quant eq '*') {
289 # keep contents, not node
290 1 return map { $self->_process_element($_, $environment) } $handler->list_contents($node);
291 } else {
292 2 return; # nothing
293 }
294 }
295  
296 12 ▶sub _process_include {
Subroutine _process_include: called CC=10 SCAR=23.2
297 12 my ($self, $node, $environment, $sigil, $quant, $tail) = @_;
298 12 my $handler = $self->{'handler'};
299  
300 12 my @contents = ();
301  
302 12 ▶ if ($sigil eq '.') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sigil eq '.') { } 6 6 12
303 # from file
304 6 my $filename = Positron::Expression::evaluate($tail, $environment);
305 6 my $filepath = undef;
306 6 foreach my $include_path (@{$self->{'include_paths'}}) {
307 12 ▶ if (-r $include_path . $filename) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r $include_path . $filename) 5 7 12
308 5 $filepath = $include_path . $filename;
309 }
310 }
311 6 ▶ if (not defined $filepath) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $filepath) 1 5 6
312 1 croak "Could not find $filename (from $tail) for inclusion";
313 }
314  
315  
316 # automatically die if we can't read this
317 5 @contents = $handler->parse_file($filepath);
318 4 @contents = map { $self->_process_element($_, $environment) } @contents;
319 } else {
320 # from env
321 6 my $env_contents = Positron::Expression::evaluate($tail, $environment);
322 6 ▶ if ($env_contents) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env_contents) { } 6 0 6
323 6 ▶ if (ref($env_contents) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $env_contents eq 'ARRAY') { } 6 0 6
324 # special case: can't allow ['a', 'text'], must be [['a', 'text']], sorry
325 6 ▶ if ($handler->isa('Positron::Handler::ArrayRef') and not ref($env_contents->[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handler->isa('Positron::Handler::ArrayRef') and not ref $env_contents->[0]) { } 4 2 6
Condition66%2/3
$handler->isa("Positron::Handler::ArrayRef") and not ref $env_contents->[0]
!ll&&!rl&&r
024
MC/DC50%1/2
$handler->isa("Positron::Handler::ArrayRef") and not ref $env_contents->[0]
$handler->isa("Positron::Handler::ArrayRef") not ref $env_contents->[0]
Truth table66%2/3
A and not(B)
A= $handler->isa("Positron::Handler::ArrayRef")B= not ref $env_contents->[0]
ABresult
0X0
100
111
326 4 @contents = ($env_contents);
327 } else {
328 2 @contents = @$env_contents;
329 }
330 } else {
331 0 @contents = ($env_contents);
332 }
333 } else {
334 # warn?
335 0 @contents = ();
336 }
337 }
338  
339 10 my $keep = ($quant eq '+');
340 10 ▶ return ($keep) ? ($self->_clone_and_resolve($node, $environment, @contents)) : @contents;
Branch: 1 branch
BranchTrueFalseTotal
$keep ? : 5 5 10
341 }
342  
343 # TODO: Refactor. Either extract the parts that are common between _include and
344 # _wrap, or just push them both together in one function.
345 50 ▶sub _process_wrap {
Subroutine _process_wrap: called CC=13 SCAR=26.1
346 50 my ($self, $node, $environment, $sigil, $quant, $tail) = @_;
347 50 my $handler = $self->{'handler'};
348  
349 50 my @contents = ();
350  
351 50 ▶ if ($tail =~ m{ \S }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tail =~ / \S /msx) { } 26 24 50
352  
353 26 my @wrapping_contents;
354 26 ▶ if ($sigil eq ':') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sigil eq ':') { } 14 12 26
355 # filename, read that and include "us".
356 14 my $filename = Positron::Expression::evaluate($tail, $environment);
357 14 my $filepath = undef;
358 14 foreach my $include_path (@{$self->{'include_paths'}}) {
359 28 ▶ if (-r $include_path . $filename) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r $include_path . $filename) 13 15 28
360 13 $filepath = $include_path . $filename;
361 }
362 }
363 14 ▶ if (not defined $filepath) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $filepath) 1 13 14
364 1 croak "Could not find $filename (from $tail) for wrapping";
365 }
366 # automatically die if we can't read this
367 13 @wrapping_contents = $handler->parse_file($filepath);
368 } else {
369 # from env
370 12 my $env_contents = Positron::Expression::evaluate($tail, $environment);
371 12 ▶ if ($env_contents) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env_contents) { } 12 0 12
372 12 ▶ if (ref($env_contents) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $env_contents eq 'ARRAY') { } 12 0 12
373 # special case: can't allow ['a', 'text'], must be [['a', 'text']], sorry
374 12 ▶ if ($handler->isa('Positron::Handler::ArrayRef') and not ref($env_contents->[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handler->isa('Positron::Handler::ArrayRef') and not ref $env_contents->[0]) { } 12 0 12
Condition33%1/3
$handler->isa("Positron::Handler::ArrayRef") and not ref $env_contents->[0]
!ll&&!rl&&r
0012
MC/DC0%0/2
$handler->isa("Positron::Handler::ArrayRef") and not ref $env_contents->[0]
$handler->isa("Positron::Handler::ArrayRef") not ref $env_contents->[0]
Truth table33%1/3
A and not(B)
A= $handler->isa("Positron::Handler::ArrayRef")B= not ref $env_contents->[0]
ABresult
0X0
100
111
375 12 @wrapping_contents = ($env_contents);
376 } else {
377 0 @wrapping_contents = @$env_contents;
378 }
379 } else {
380 0 @wrapping_contents = ($env_contents);
381 }
382 } else {
383 # warn?
384 0 @wrapping_contents = ();
385 }
386 }
387  
388 # TODO: resolve later? We'd need to clone $node and remove structure sigils to defeat recursion
389 # Resolve now; also allows clone_and_resolve to clear sigils to defeat recursion
390 24 @contents = map { $self->_process_element($_, $environment) } $handler->list_contents($node);
391 # only quant-less versions pass the parent
392 24 ▶ my @passed_nodes = $quant ? @contents : $self->_clone_and_resolve($node, $environment, @contents);
Branch: 1 branch
BranchTrueFalseTotal
$quant ? : 16 8 24
393  
394 24 $environment = Positron::Environment->new({':' => [ @passed_nodes ]}, { parent => $environment, immutable => 0});
395  
396 24 @contents = @wrapping_contents;
397 24 @contents = map { $self->_process_element($_, $environment) } @contents;
398  
399 } else {
400 # inclusion marker
401 24 ▶ my $passed_nodes = $environment->get(':') || [];
Condition50%1/2
$environment->get(':') || []
l!l
240
MC/DC0%0/1
$environment->get(':') || []
$environment->get(':')
Truth table50%1/2
A
A= $environment->get(':')
Aresult
11
00
402 # On error, just kill all (warn, maybe?)
403 # Remember: already resolved!
404 24 @contents = @$passed_nodes;
405 }
406  
407 # this works for both cases!
408 48 my $keep = ($quant eq '+');
409 48 ▶ return ($keep) ? ($self->_clone_and_resolve($node, $environment, @contents)) : @contents;
Branch: 1 branch
BranchTrueFalseTotal
$keep ? : 20 28 48
410 }
411  
412 9 ▶sub _process_function {
Subroutine _process_function: called CC=4 SCAR=13.9
413 9 my ($self, $node, $environment, $sigil, $quant, $tail) = @_;
414 9 my $handler = $self->{'handler'};
415 9 my $function = Positron::Expression::evaluate($tail, $environment);
416  
417 # The "self" node can be part of the arguments (quant ''), receive the results of the function
418 # (quant '+'), or be dropped altogether (quant '-').
419 9 my $keep = ($quant eq '+');
420 9 ▶ my $pass_self = (not $keep and $quant ne '-');
Condition100%3/3
!$keep && $quant ne '-'
!ll&&!rl&&r
333
MC/DC100%2/2
!$keep && $quant ne '-'
!$keep $quant ne '-'
Truth table100%3/3
not(A) && B
A= !$keepB= $quant ne '-'
ABresult
0X0
100
111
421  
422 9 my @contents = ();
423 # Need these in any case:
424 9 @contents = map { $self->_process_element($_, $environment) } $handler->list_contents($node);
425 9 ▶ if ($pass_self) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pass_self) 3 6 9
426 # we could also need to pass "self".
427 3 @contents = $self->_clone_and_resolve($node, $environment, @contents);
428 }
429  
430 9 @contents = $function->(@contents);
431 9 ▶ return ($keep) ? ($self->_clone_and_resolve($node, $environment, @contents)) : @contents;
Branch: 1 branch
BranchTrueFalseTotal
$keep ? : 3 6 9
432 }
433  
434  
435 1186 ▶sub _make_finder {
Subroutine _make_finder: called CC=2 SCAR=6.9
436 1186 my ($self, $sigils) = @_;
437 # What to do on empty sigils? Need to find during development!
438 1186 ▶ die "Empty sigil list!" unless $sigils;
Branch: 1 branch
BranchTrueFalseTotal
unless $sigils 1 1185 1186
439 1185 my ($opener, $closer) = ($self->{opener}, $self->{closer});
440 1185 my ($eopener, $ecloser) = ("\\$opener","\\$closer");
441 1185 my ($esigils) = join('', map { "\\$_" } split(qr{}, $sigils));
442 1185 return qr{
443 $eopener
444 ( [$esigils] )
445 ( [-+*]? )
446 ( [^$ecloser]* )
447 $ecloser
448 }xms;
449 }
450  
451 # Handlers for:
452 # scalar string, no handler
453 # HTML::Element
454 # XML::LibXML
455 # ArrayRef Handler
456 # TODO: an extensible mechanism
457 20 ▶sub _handler_for {
Subroutine _handler_for: called CC=5 SCAR=19.4
458 20 my ($dom) = @_;
459 20 ▶ return unless ref($dom); # Text at most, needs no handler
Branch: 1 branch
BranchTrueFalseTotal
unless ref $dom 11 9 20
460 9 ▶ if (ref($dom) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $dom eq 'ARRAY') { } 9 0 9
elsif (my $package = builtin::blessed($dom)) { } 0 0 0
461 9 require Positron::Handler::ArrayRef;
462 9 return Positron::Handler::ArrayRef->new();
463 } elsif (my $package = blessed($dom)) {
464 0 ▶ eval "require Positron::Handler::$package; 1;" or croak "Could not load handler for $package";
Branch: 1 branch
BranchTrueFalseTotal
eval "require Positron::Handler::$package; 1;" or croak("Could not load handler for $package") 0 0 0
465 }
466 }
467  
468 423 ▶sub _get_structure_sigil {
Subroutine _get_structure_sigil: called CC=3 SCAR=11.0
469 423 my ($self, $node) = @_;
470 423 my $handler = $self->{'handler'};
471 423 my $structure_finder = $self->_make_finder('@?!/.:,;|^');
472 423 foreach my $attr ($handler->list_attributes($node)) {
473 225 my $value = $handler->get_attribute($node, $attr);
474 225 ▶ if ($value =~ m{ $structure_finder }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ / $structure_finder /msx) 204 21 225
475 204 return ($1, $2, $3);
476 }
477 }
478 219 return; # Has none
479 }
480  
481 322 ▶sub _remove_structure_sigils {
Subroutine _remove_structure_sigils: called CC=4 SCAR=13.9
482 322 my ($self, $node) = @_;
483 322 my $handler = $self->{'handler'};
484 # NOTE: we remove '=' here as well, even though it's not a structure sigil!
485 322 my $structure_finder = $self->_make_finder('@?!/.:,;=|^');
486 322 foreach my $attr ($handler->list_attributes($node)) {
487 129 my $value = $handler->get_attribute($node, $attr);
488 129 my $did_change = ($value =~ s{ $structure_finder }{}xmsg);
489 129 ▶ if ($did_change) {
Branch: 1 branch
BranchTrueFalseTotal
if ($did_change) 108 21 129
490 # We removed something from this attribute -> delete it if empty
491 108 ▶ if ($value eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq "") 108 0 108
492 108 $handler->set_attribute($node, $attr, undef);
493 }
494 }
495 }
496 322 return; # void?
497 }
498  
499 107 ▶sub _clone_and_resolve {
Subroutine _clone_and_resolve: called CC=1 SCAR=0.0
500 107 my ($self, $node, $environment, @contents) = @_;
501 107 my $handler = $self->{'handler'};
502 107 my $clone = $handler->shallow_clone($node);
503 107 $self->_remove_structure_sigils($clone);
504 107 $self->_resolve_text_attr($clone, $environment);
505 107 $handler->push_contents($clone, @contents);
506 107 return $clone;
507 }
508  
509 322 ▶sub _resolve_text_attr {
Subroutine _resolve_text_attr: called CC=5 SCAR=16.1
510 322 my ($self, $node, $environment) = @_;
511 322 my $handler = $self->{'handler'};
512 322 foreach my $attr ($handler->list_attributes($node)) {
513 21 my ($value, $did_change, $last_changing_quant) = $self->_process_text($handler->get_attribute($node, $attr), $environment, 1);
514 21 ▶ if ($did_change) {
Branch: 1 branch
BranchTrueFalseTotal
if ($did_change) 15 6 21
515 15 ▶ if ($value eq '' and not $last_changing_quant eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq "" and not $last_changing_quant eq "+") 4 11 15
Condition100%3/3
$value eq "" and not $last_changing_quant eq "+"
!ll&&!rl&&r
1014
MC/DC100%2/2
$value eq "" and not $last_changing_quant eq "+"
$value eq "" not $last_changing_quant eq "+"
Truth table100%3/3
A and not(B)
A= $value eq ""B= not $last_changing_quant eq "+"
ABresult
0X0
100
111
516 # We removed somethin from this attribute -> delete it if empty, unless the last sigil says otherwise
517 4 $value = undef;
518 }
519 15 $handler->set_attribute($node, $attr, $value);
520 }
521 }
522 322 return;
523 }
524 12 1;
525  
526 __END__
« Positron/Expression.pm ↑ Summary Positron/Environment.pm »