lib/OODoc/Template.pm

filter: Statementstmt 94.9% 208 / 219 Branchbran 80.1% 133 / 166 Conditioncond 56.0% 28 / 50 MC/DCmcdc 30.0% 9 / 30 Subroutinesub 96.1% 25 / 26 Podpod 92.8% 13 / 14 totaltotal 82.3% 416 / 505 SCAR 49.8
CC
106
Cov
85%
process
31.1
valueFor
14.7
includeTemplate
13.4
CRAP 145.2
« blib/lib/OODoc/Template.pm ↑ Summary
1 # Copyrights 2003,2007-2013 by [Mark Overmeer].
2 # For other contributors see ChangeLog.
3 # See the manual pages for details on the licensing terms.
4 # Pod stripped from pm file by OODoc 2.00.
5 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 package OODoc::Template;
9 10 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 $VERSION = '0.16';
11  
12  
13 10 ▶use Log::Report 'oodoc-template';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 10 ▶use IO::File ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 10 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 10 ▶use Data::Dumper qw(Dumper);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 10 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 10 my @default_markers = ('<!--{', '}-->', '<!--{/', '}-->');
21  
22  
23 sub new(@)
24 11 ▶{ my ($class, %args) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
25 11 (bless {}, $class)->init(\%args);
26 }
27  
28 sub init($)
29 11 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=6 SCAR=18.4
Pod undocumented
30  
31 11 $self->{cached} = {};
32 11 $self->{macros} = {};
33  
34 11 my $s = $self; weaken $s; # avoid circular ref
35 11 ▶ $args->{template} ||= sub { $s->includeTemplate(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.6
Condition33%1/3
$args->{"template"} ||= sub { $s->includeTemplate(@_); }
l!l&&r!l&&!r
0110
MC/DC0%0/2
$args->{"template"} ||= sub { $s->includeTemplate(@_); }
$args->{"template"} sub { $s->includeTemplate(@_); }
Truth table33%1/3
A ||= B
A= $args->{"template"}B= sub { $s->includeTemplate(@_); }
ABresult
1X1
011
000
36 11 ▶ $args->{macro} ||= sub { $s->defineMacro(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.6
Condition33%1/3
$args->{"macro"} ||= sub { $s->defineMacro(@_); }
l!l&&r!l&&!r
0110
MC/DC0%0/2
$args->{"macro"} ||= sub { $s->defineMacro(@_); }
$args->{"macro"} sub { $s->defineMacro(@_); }
Truth table33%1/3
A ||= B
A= $args->{"macro"}B= sub { $s->defineMacro(@_); }
ABresult
1X1
011
000
37  
38 11 ▶ $args->{search} ||= '.';
Condition100%2/2
$args->{"search"} ||= "."
l!l
110
MC/DC100%1/1
$args->{"search"} ||= "."
$args->{"search"}
Truth table100%2/2
A
A= $args->{"search"}
Aresult
11
00
39 11 ▶ $args->{markers} ||= \@default_markers;
Condition100%2/2
$args->{"markers"} ||= \@default_markers
l!l
29
MC/DC100%1/1
$args->{"markers"} ||= \@default_markers
$args->{"markers"}
Truth table100%2/2
A
A= $args->{"markers"}
Aresult
11
00
40 11 ▶ $args->{define} ||= sub { shift; (1, @_) };
Subroutine __ANON__: called CC=1 SCAR=0.4
Condition33%1/3
$args->{"define"} ||= sub { shift(); 1, @_; }
l!l&&r!l&&!r
0110
MC/DC0%0/2
$args->{"define"} ||= sub { shift(); 1, @_; }
$args->{"define"} sub { shift(); 1, @_; }
Truth table33%1/3
A ||= B
A= $args->{"define"}B= sub { shift(); 1, @_; }
ABresult
1X1
011
000
41  
42 11 $self->pushValues($args);
43 11 $self;
44 }
45  
46  
47 sub process($)
48 111 ▶{ my ($self, $templ) = (shift, shift);
Subroutine process: called CC=31 SCAR=34.4
Pod documented
49  
50 111 ▶ my $values = @_==1 ? shift : @_ ? {@_} : {};
Branch: 2 branches
BranchTrueFalseTotal
@_ == 1 ? : 30 81 111
@_ ? : 30 51 81
51  
52 111 ▶ my $tree # parse with real copy
Branch: 2 branches
BranchTrueFalseTotal
ref $templ eq 'SCALAR' ? : 1 110 111
ref $templ eq 'ARRAY' ? : 6 104 110
53 = ref $templ eq 'SCALAR' ? $self->parseTemplate($$templ)
54 : ref $templ eq 'ARRAY' ? $templ
55 : $self->parseTemplate("$templ");
56  
57 111 ▶ defined $tree
Branch: 1 branch
BranchTrueFalseTotal
defined $tree or return () 0 111 111
58 or return ();
59  
60 111 ▶ $self->pushValues($values)
Branch: 1 branch
BranchTrueFalseTotal
if keys %$values 54 57 111
61 if keys %$values;
62  
63 111 my @output;
64 111 foreach my $node (@$tree)
65 337 ▶ { unless(ref $node)
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $node) 224 113 337
66 224 { push @output, $node;
67 224 next;
68 }
69
70 113 my ($tag, $attr, $then, $else) = @$node;
71  
72 113 my %attrs;
73 113 ▶ while(my($k, $v) = each %$attr)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$attr) 53 113 166
74 { $attrs{$k} = ref $v ne 'ARRAY' ? $v
75 5 : @$v==1 ? scalar $self->valueFor(@{$v->[0]})
76 : join '',
77 53 ▶ map {ref $_ eq 'ARRAY' ? scalar $self->valueFor(@$_) : $_}
Branch: 3 branches
BranchTrueFalseTotal
ref $v ne 'ARRAY' ? : 41 12 53
@$v == 1 ? : 5 7 12
ref $_ eq 'ARRAY' ? : 11 6 17
78 @$v;
79 }
80  
81 113 (my $value, my $attrs, $then, $else)
82 = $self->valueFor($tag, \%attrs, $then, $else);
83  
84 113 ▶ unless(defined $then || defined $else)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $then or defined $else) 74 39 113
Condition100%3/3
defined $then or defined $else
l!l&&r!l&&!r
34574
MC/DC100%2/2
defined $then or defined $else
defined $then defined $else
Truth table100%3/3
A or B
A= defined $thenB= defined $else
ABresult
1X1
011
000
85 74 ▶ { defined $value
Branch: 1 branch
BranchTrueFalseTotal
defined $value or next 14 60 74
86 or next;
87  
88 60 ▶ ref $value ne 'ARRAY' && ref $value ne 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
ref $value ne "ARRAY" and ref $value ne "HASH" or &error(&__x("value for {tag} is {value}, must be single", "tag", $tag, "value", $value)) 0 60 60
Condition33%1/3
ref $value ne "ARRAY" and ref $value ne "HASH"
!ll&&!rl&&r
0060
MC/DC0%0/2
ref $value ne "ARRAY" and ref $value ne "HASH"
ref $value ne "ARRAY" ref $value ne "HASH"
Truth table33%1/3
A and B
A= ref $value ne "ARRAY"B= ref $value ne "HASH"
ABresult
0X0
100
111
89 or error __x"value for {tag} is {value}, must be single"
90 , tag => $tag, value => $value;
91  
92 60 push @output, $value;
93 60 next;
94 }
95  
96 39 ▶ my $take_else
Condition100%6/6
!defined($value) || ref $value eq 'ARRAY' && @$value == 0
l!l&&r!l&&!r
11523
ref $value eq "ARRAY" and @$value == 0
!ll&&!rl&&r
11125
MC/DC100%3/3
!defined($value) || ref $value eq 'ARRAY' && @$value == 0
!defined($value) ref $value eq "ARRAY" @$value == 0
Truth table100%4/4
not(A) || B and C
A= !defined($value)B= ref $value eq "ARRAY"C= @$value == 0
ABCresult
1XX1
0111
00X0
0100
97 = !defined $value || (ref $value eq 'ARRAY' && @$value==0);
98  
99 39 ▶ my $container = $take_else ? $else : $then;
Branch: 1 branch
BranchTrueFalseTotal
$take_else ? : 16 23 39
100  
101 39 ▶ defined $container
Branch: 1 branch
BranchTrueFalseTotal
defined $container or next 12 27 39
102 or next;
103  
104 27 ▶ $self->pushValues($attrs) if keys %$attrs;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$attrs 3 24 27
105  
106 27 ▶ if($take_else)
Branch: 3 branches
BranchTrueFalseTotal
if ($take_else) { } 6 21 27
elsif (ref $value eq 'HASH') { } 3 18 21
elsif (ref $value eq 'ARRAY') { } 11 7 18
107 6 { my ($nest_out, $nest_tree) = $self->process($container);
108 6 push @output, $nest_out;
109 6 $node->[3] = $nest_tree;
110 }
111 elsif(ref $value eq 'HASH')
112 3 { my ($nest_out, $nest_tree) = $self->process($container, $value);
113 3 push @output, $nest_out;
114 3 $node->[2] = $nest_tree;
115 }
116 elsif(ref $value eq 'ARRAY')
117 11 { foreach my $data (@$value)
118 16 { my ($nest_out, $nest_tree) = $self->process($container, $data);
119 16 push @output, $nest_out;
120 16 $node->[2] = $nest_tree;
121 }
122 }
123 else
124 7 { my ($nest_out, $nest_tree) = $self->process($container);
125 7 push @output, $nest_out;
126 7 $node->[2] = $nest_tree;
127 }
128  
129 27 ▶ $self->popValues if keys %$attrs;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$attrs 3 24 27
130 }
131
132 111 ▶ $self->popValues if keys %$values;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$values 54 57 111
133  
134 111 ▶ wantarray ? (join('', @output), $tree) # LIST context
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 104 7 111
defined wantarray ? : 6 1 7
135 : defined wantarray ? join('', @output) # SCALAR context
136 : print @output; # VOID context
137 }
138  
139  
140 sub processFile($;@)
141 6 ▶{ my ($self, $filename) = (shift, shift);
Subroutine processFile: called CC=10 SCAR=23.9
Pod documented
142  
143 6 ▶ my $values = @_==1 ? shift : {@_};
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 6 0 6
144 6 ▶ $values->{source} ||= $filename;
Condition66%2/3
$values->{"source"} ||= $filename
l!l&&r!l&&!r
150
MC/DC0%0/2
$values->{"source"} ||= $filename
$values->{"source"} $filename
Truth table66%2/3
A ||= B
A= $values->{"source"}B= $filename
ABresult
1X1
011
000
145  
146 6 my $cache = $self->{cached};
147  
148 6 my ($output, $tree, $template);
149 6 ▶ if(exists $cache->{$filename})
Branch: 2 branches
BranchTrueFalseTotal
if (exists $cache->{$filename}) { } 4 2 6
elsif ($template = $self->loadFile($filename)) { } 1 1 2
150 4 { $tree = $cache->{$filename};
151 4 ▶ $output = $self->process($tree, $values)
Branch: 1 branch
BranchTrueFalseTotal
if defined $tree 4 0 4
152 if defined $tree;
153 }
154 elsif($template = $self->loadFile($filename))
155 1 { ($output, $tree) = $self->process($template, $values);
156 1 $cache->{$filename} = $tree;
157 }
158 else
159 1 { $tree = $cache->{$filename} = undef;
160 }
161  
162 6 ▶ defined $tree || defined wantarray
Branch: 1 branch
BranchTrueFalseTotal
defined $tree or defined wantarray or &error(&__x("cannot find template file {fn}", "fn", $filename)) 0 6 6
Condition66%2/3
defined $tree or defined wantarray
l!l&&r!l&&!r
510
MC/DC0%0/2
defined $tree or defined wantarray
defined $tree defined wantarray
Truth table66%2/3
A or B
A= defined $treeB= defined wantarray
ABresult
1X1
011
000
163 or error __x"cannot find template file {fn}", fn => $filename;
164  
165 6 ▶ wantarray ? ($output, $tree) # LIST context
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 0 6 6
defined wantarray ? : 6 0 6
166 : defined wantarray ? $output # SCALAR context
167 : print $output; # VOID context
168 }
169  
170  
171 sub defineMacro($$$$)
172 1 ▶{ my ($self, $tag, $attrs, $then, $else) = @_;
Subroutine defineMacro: called CC=3 SCAR=11.1
Pod documented
173 my $name = delete $attrs->{name}
174 1 ▶ or error __x"macro requires a name";
Branch: 1 branch
BranchTrueFalseTotal
my $name = delete $attrs->{"name"} or &error(&__x("macro requires a name")) 0 1 1
175  
176 1 ▶ defined $else
Branch: 1 branch
BranchTrueFalseTotal
defined $else and &error(&__x("macros cannot have an else part ({macro})", "macro", $name)) 0 1 1
177 and error __x"macros cannot have an else part ({macro})",macro => $name;
178  
179 1 my %attrs = %$attrs; # for closure
180 1 $attrs{markers} = $self->valueFor('markers');
181  
182 $self->{macros}{$name} =
183 2 ▶ sub { my ($tag, $at) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
184 2 $self->process($then, +{%attrs, %$at});
185 1 };
186  
187 1 ();
188
189 }
190  
191  
192 sub valueFor($;$$$)
193 235 ▶{ my ($self, $tag, $attrs, $then, $else) = @_;
Subroutine valueFor: called CC=12 SCAR=26.9
Pod documented
194  
195 #warn "Looking for $tag";
196 #warn Dumper $self->{values};
197 235 ▶ for(my $set = $self->{values}; defined $set; $set = $set->{NEXT})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $set) 283 20 303
198 283 { my $v = $set->{$tag};
199  
200 283 ▶ if(defined $v)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v) 211 72 283
201 { # HASH defines container
202 # ARRAY defines container loop
203 # object or other things can be stored as well, but may get
204 # stringified.
205 211 ▶ return wantarray ? ($v, $attrs, $then, $else) : $v
Branch: 2 branches
BranchTrueFalseTotal
if ref $v ne "CODE" 178 33 211
wantarray ? : 60 118 178
206 if ref $v ne 'CODE';
207  
208 return wantarray
209 33 ▶ ? $v->($tag, $attrs, $then, $else)
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 29 4 33
210 : ($v->($tag, $attrs, $then, $else))[0]
211 }
212  
213 return wantarray ? (undef, $attrs, $then, $else) : undef
214 72 ▶ if exists $set->{$tag};
Branch: 2 branches
BranchTrueFalseTotal
if exists $set->{$tag} 4 68 72
wantarray ? : 4 0 4
215  
216 68 my $code = $set->{DYNAMIC};
217 68 ▶ if(defined $code)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $code) 0 68 68
218 0 { my ($value, @other) = $code->($tag, $attrs, $then, $else);
219 0 ▶ return wantarray ? ($value, @other) : $value
Branch: 2 branches
BranchTrueFalseTotal
if defined $value 0 0 0
wantarray ? : 0 0 0
220 if defined $value;
221 # and continue the search otherwise
222 }
223 }
224  
225 20 ▶ wantarray ? (undef, $attrs, $then, $else) : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 20 0 20
226 }
227  
228  
229 sub allValuesFor($;$$$)
230 2 ▶{ my ($self, $tag, $attrs, $then, $else) = @_;
Subroutine allValuesFor: called CC=7 SCAR=21.5
Pod documented
231 2 my @values;
232  
233 2 ▶ for(my $set = $self->{values}; defined $set; $set = $set->{NEXT})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $set) 4 2 6
234 {
235 4 ▶ if(defined(my $v = $set->{$tag}))
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $v = $set->{$tag})) 2 2 4
236 2 ▶ { my $t = ref $v eq 'CODE' ? $v->($tag, $attrs, $then, $else) : $v;
Branch: 1 branch
BranchTrueFalseTotal
ref $v eq 'CODE' ? : 0 2 2
237 2 ▶ push @values, $t if defined $t;
Branch: 1 branch
BranchTrueFalseTotal
if defined $t 2 0 2
238 }
239  
240 4 ▶ if(defined(my $code = $set->{DYNAMIC}))
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $code = $set->{"DYNAMIC"})) 0 4 4
241 0 { my $t = $code->($tag, $attrs, $then, $else);
242 0 ▶ push @values, $t if defined $t;
Branch: 1 branch
BranchTrueFalseTotal
if defined $t 0 0 0
243 }
244 }
245  
246 2 @values;
247 }
248  
249  
250 sub pushValues($)
251 68 ▶{ my ($self, $attrs) = @_;
Subroutine pushValues: called CC=8 SCAR=20.8
Pod documented
252  
253 68 ▶ if(my $markers = $attrs->{markers})
Branch: 1 branch
BranchTrueFalseTotal
if (my $markers = $attrs->{"markers"}) 14 54 68
254 { my @markers = ref $markers eq 'ARRAY' ? @$markers
255 14 ▶ : map {s/\\\,//g; $_} split /(?!<\\)\,\s*/, $markers;
Branch: 1 branch
BranchTrueFalseTotal
ref $markers eq 'ARRAY' ? : 13 1 14
256  
257 14 ▶ push @markers, $markers[0] . '/'
Branch: 1 branch
BranchTrueFalseTotal
if @markers == 2 2 12 14
258 if @markers==2;
259  
260 14 ▶ push @markers, $markers[1]
Branch: 1 branch
BranchTrueFalseTotal
if @markers == 3 2 12 14
261 if @markers==3;
262  
263 $attrs->{markers}
264 14 ▶ = [ map { ref $_ eq 'Regexp' ? $_ : qr/\Q$_/ } @markers ];
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'Regexp' ? : 8 48 56
265 }
266  
267 68 ▶ if(my $search = $attrs->{search})
Branch: 1 branch
BranchTrueFalseTotal
if (my $search = $attrs->{"search"}) 11 57 68
268 11 ▶ { $attrs->{search} = [ split /\:/, $search ]
Branch: 1 branch
BranchTrueFalseTotal
if ref $search ne "ARRAY" 11 0 11
269 if ref $search ne 'ARRAY';
270 }
271  
272 68 $self->{values} = { %$attrs, NEXT => $self->{values} };
273 }
274  
275  
276 sub popValues()
277 57 ▶{ my $self = shift;
Subroutine popValues: called CC=1 SCAR=0.0
Pod documented
278 57 $self->{values} = $self->{values}{NEXT};
279 }
280  
281  
282 sub includeTemplate($$$)
283 7 ▶{ my ($self, $tag, $attrs, $then, $else) = @_;
Subroutine includeTemplate: called CC=10 SCAR=25.9
Pod documented
284  
285 7 ▶ defined $then || defined $else
Branch: 1 branch
BranchTrueFalseTotal
defined $then || defined $else and &error(&__x("template is not a container")) 0 7 7
Condition33%1/3
defined $then or defined $else
l!l&&r!l&&!r
007
MC/DC0%0/2
defined $then or defined $else
defined $then defined $else
Truth table33%1/3
A or B
A= defined $thenB= defined $else
ABresult
1X1
011
000
286 and error __x"template is not a container";
287  
288 7 ▶ if(my $fn = $attrs->{file})
Branch: 1 branch
BranchTrueFalseTotal
if (my $fn = $attrs->{"file"}) 5 2 7
289 5 { my $output = $self->processFile($fn, $attrs);
290 $output = $self->processFile($attrs->{alt}, $attrs)
291 5 ▶ if !defined $output && $attrs->{alt};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $output and $attrs->{"alt"} 1 4 5
Condition66%2/3
not defined $output and $attrs->{"alt"}
!ll&&!rl&&r
401
MC/DC50%1/2
not defined $output and $attrs->{"alt"}
not defined $output $attrs->{"alt"}
Truth table66%2/3
not(A) and B
A= not defined $outputB= $attrs->{"alt"}
ABresult
0X0
100
111
292  
293 5 ▶ defined $output
Branch: 1 branch
BranchTrueFalseTotal
defined $output or &error(&__x("cannot find template file {fn}", "fn", $fn)) 0 5 5
294 or error __x"cannot find template file {fn}", fn => $fn;
295  
296 5 return ($output);
297 }
298  
299 2 ▶ if(my $name = $attrs->{macro})
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = $attrs->{"macro"}) 2 0 2
300 2 ▶ { my $macro = $self->{macros}{$name}
Branch: 1 branch
BranchTrueFalseTotal
my $macro = $self->{"macros"}{$name} or &error(&__x("cannot find macro {name}", "name", $name)) 0 2 2
301 or error __x"cannot find macro {name}", name => $name;
302  
303 2 return $macro->($tag, $attrs, $then, $else);
304 }
305  
306 0 ▶ error __x"file or macro attribute required for template in {source}"
Condition0%0/2
$self->valueFor('source') || '??'
l!l
00
MC/DC0%0/1
$self->valueFor('source') || '??'
$self->valueFor('source')
Truth table0%0/2
A
A= $self->valueFor('source')
Aresult
11
00
307 , source => $self->valueFor('source') || '??';
308 }
309  
310  
311 sub loadFile($)
312 2 ▶{ my ($self, $relfn) = @_;
Subroutine loadFile: called CC=9 SCAR=25.1
Pod documented
313 2 my $absfn;
314  
315 2 ▶ if(File::Spec->file_name_is_absolute($relfn))
Branch: 1 branch
BranchTrueFalseTotal
if ("File::Spec"->file_name_is_absolute($relfn)) 0 2 2
316 0 { my $fn = File::Spec->canonpath($relfn);
317 0 ▶ $absfn = $fn if -f $fn;
Branch: 1 branch
BranchTrueFalseTotal
if -f $fn 0 0 0
318 }
319  
320 2 ▶ unless($absfn)
Branch: 1 branch
BranchTrueFalseTotal
unless ($absfn) 2 0 2
321 2 { my @srcs = map { @$_ } $self->allValuesFor('search');
322 2 foreach my $dir (@srcs)
323 3 { $absfn = File::Spec->rel2abs($relfn, $dir);
324 3 ▶ last if -f $absfn;
Branch: 1 branch
BranchTrueFalseTotal
if -f $absfn 1 2 3
325 2 $absfn = undef;
326 }
327 }
328  
329 2 ▶ defined $absfn
Branch: 1 branch
BranchTrueFalseTotal
defined $absfn or return undef 1 1 2
330 or return undef;
331  
332 1 my $in = IO::File->new($absfn, 'r');
333 1 ▶ unless(defined $in)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $in) 0 1 1
334 0 ▶ { my $source = $self->valueFor('source') || '??';
Condition0%0/2
$self->valueFor('source') || '??'
l!l
00
MC/DC0%0/1
$self->valueFor('source') || '??'
$self->valueFor('source')
Truth table0%0/2
A
A= $self->valueFor('source')
Aresult
11
00
335 0 fault __x"Cannot read from {fn} in {file}", fn => $absfn, file=>$source;
336 }
337  
338 1 \(join '', $in->getlines); # auto-close in
339 }
340  
341  
342 sub parse($@)
343 0 ▶{ my ($self, $template) = (shift, shift);
Subroutine parse: not called CC=1 SCAR=6.9
Pod documented
344 0 $self->process(\$template, @_);
345 }
346  
347  
348 sub parseTemplate($)
349 105 ▶{ my ($self, $template) = @_;
Subroutine parseTemplate: called CC=7 SCAR=19.5
Pod documented
350  
351 105 ▶ defined $template
Branch: 1 branch
BranchTrueFalseTotal
defined $template or return undef 0 105 105
352 or return undef;
353  
354 105 my $markers = $self->valueFor('markers');
355  
356 # Remove white-space escapes
357 105 $template =~ s! \\ (?: \s* (?: \\ \s*)? \n)+
358 (?: \s* (?= $markers->[0] | $markers->[3] ))?
359 !!mgx;
360  
361 105 my @frags;
362  
363 # NOT_$tag supported for backwards compat
364 105 ▶ while( $template =~ s!^(.*?) # text before container
Branch: 1 branch
BranchTrueFalseTotal
if ($template =~ s/^(.*?) # text before container $markers->[0] \s* (?: IF \s* )? (NOT (?:_|\s+) )? ([\w.-]+) \s* # tag (.*?) \s* # attributes $markers->[1] //sx) 109 105 214
365 $markers->[0] \s*
366 (?: IF \s* )?
367 (NOT (?:_|\s+) )?
368 ([\w.-]+) \s* # tag
369 (.*?) \s* # attributes
370 $markers->[1]
371 !!xs
372 )
373 109 { push @frags, $1;
374 109 my ($not, $tag, $attr) = ($2, $3, $4);
375 109 my ($then, $else);
376  
377 109 ▶ if($template =~ s! (.*?) # contained
Branch: 1 branch
BranchTrueFalseTotal
if ($template =~ s/ (.*?) # contained ( $markers->[2] \s* \Q$tag\E \s* # "our" tag $markers->[3] ) //sx) 40 69 109
378 ( $markers->[2]
379 \s* \Q$tag\E \s* # "our" tag
380 $markers->[3]
381 )
382 !!xs)
383 40 { $then = $1;
384 40 my $endline = $2;
385 }
386  
387 109 ▶ if($not) { ($then, $else) = (undef, $then) }
Branch: 3 branches
BranchTrueFalseTotal
if ($not) { } 5 104 109
elsif (not defined $then) { } 69 35 104
elsif ($then =~ s/ $markers->[0] \s* ELSE (?:_|\s+) \Q$tag\E \s* $markers->[1] (.*) //sx) { } 5 30 35
388 elsif(!defined $then) { }
389 elsif($then =~ s! $markers->[0]
390 \s* ELSE (?:_|\s+)
391 \Q$tag\E \s*
392 $markers->[1]
393 (.*)
394 !!xs)
395 { # ELSE_$tag for backwards compat
396 5 $else = $1;
397 }
398  
399 109 push @frags, [$tag, $self->parseAttrs($attr), $then, $else];
400 }
401  
402 105 push @frags, $template;
403 105 \@frags;
404 }
405  
406  
407 sub parseAttrs($)
408 113 ▶{ my ($self, $string) = @_;
Subroutine parseAttrs: called CC=13 SCAR=25.9
Pod documented
409  
410 113 my %attrs;
411 113 ▶ while( $string =~
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ s/^\s* (?: '([^']+)' # attribute name (might be quoted) | "([^"]+)" | (\w+) ) \s* (?: \= \>? \s* # an optional value ( \"[^"]*\" # dquoted value | \'[^']*\' # squoted value | \$\{ [^}]+ \} # complex variable | [^\s,]+ # unquoted value ) )? \s* \,? # optionally separated by commas //sx) 57 113 170
412 s!^\s* (?: '([^']+)' # attribute name (might be quoted)
413 | "([^"]+)"
414 | (\w+)
415 )
416 \s* (?: \= \>? \s* # an optional value
417 ( \"[^"]*\" # dquoted value
418 | \'[^']*\' # squoted value
419 | \$\{ [^}]+ \} # complex variable
420 | [^\s,]+ # unquoted value
421 )
422 )?
423 \s* \,? # optionally separated by commas
424 !!xs)
425 57 ▶ { my ($k, $v) = ($1||$2||$3, $4);
Condition33%2/6
$1 || $2 || $3
l!l&&r!l&&!r
0570
$1 or $2
l!l&&r!l&&!r
0057
MC/DC0%0/3
$1 || $2 || $3
$1 $2 $3
Truth table25%1/4
A or B || C
A= $1B= $2C= $3
ABCresult
1XX1
01X1
0011
0000
426 57 ▶ unless(defined $v)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $v) 11 46 57
427 11 { $attrs{$k} = 1;
428 11 next;
429 }
430  
431 46 ▶ if($v =~ m/^\'(.*)\'$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($v =~ /^\'(.*)\'$/) 11 35 46
432 { # Single quoted parameter, no interpolation
433 11 $attrs{$k} = $1;
434 11 next;
435 }
436  
437 35 $v =~ s/^\"(.*)\"$/$1/;
438 35 my @v = split /( \$\{[^\}]+\} | \$\w+ )/x, $v;
439  
440 35 ▶ if(@v==1 && $v[0] !~ m/^\$/)
Branch: 1 branch
BranchTrueFalseTotal
if (@v == 1 and not $v[0] =~ /^\$/) 23 12 35
Condition66%2/3
@v == 1 and not $v[0] =~ /^\$/
!ll&&!rl&&r
12023
MC/DC50%1/2
@v == 1 and not $v[0] =~ /^\$/
@v == 1 not $v[0] =~ /^\$/
Truth table66%2/3
A and not(B)
A= @v == 1B= not $v[0] =~ /^\$/
ABresult
0X0
100
111
441 23 { $attrs{$k} = $v[0];
442 23 next;
443 }
444  
445 12 my @steps;
446 12 foreach (@v)
447 36 ▶ { if( m/^ (?: \$(\w+) | \$\{ (\w+) \s* \} ) $/x )
Branch: 2 branches
BranchTrueFalseTotal
if (/^ (?: \$(\w+) | \$\{ (\w+) \s* \} ) $/x) { } 12 24 36
elsif (/^ \$\{ (\w+) \s* ([^\}]+? \s* ) \} $/x) { } 4 20 24
448 12 { push @steps, [ $+ ];
449 }
450 elsif( m/^ \$\{ (\w+) \s* ([^\}]+? \s* ) \} $/x )
451 4 { push @steps, [ $1, $self->parseAttrs($2) ];
452 }
453 else
454 20 ▶ { push @steps, $_ if length $_;
Branch: 1 branch
BranchTrueFalseTotal
if length $_ 6 14 20
455 }
456 }
457  
458 12 $attrs{$k} = \@steps;
459 }
460  
461 113 ▶ error __x"attribute error in {tag}'", tag => $_[1]
Branch: 1 branch
BranchTrueFalseTotal
if length $string 0 113 113
462 if length $string;
463  
464 113 \%attrs;
465 }
466  
467  
468 10 1;
« blib/lib/OODoc/Template.pm ↑ Summary