blib/lib/HCKit/Template.pm

filter: Statementstmt 10.6% 47 / 442 Branchbran 0.0% 0 / 176 Conditioncond 2.2% 1 / 44 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 11.6% 7 / 60 Podpod 16.3% 8 / 49 totaltotal 7.9% 63 / 796 CC 116 SCAR 43.5
CC
116
Cov
7%
parse_rule
420.0
process_tokens
110.0
fun_foreach
90.0
CRAP 77.1
↑ Summary
1 package HCKit::Template;
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 require Exporter;
7 1 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 @ISA = qw(Exporter);
9  
10 # Items to export into callers namespace by default. Note: do not export
11 # names by default without a very good reason. Use EXPORT_OK instead.
12 # Do not simply export all your public functions/methods/constants.
13  
14 # This allows declaration use HCKit::Template ':all';
15 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
16 # will save memory.
17 1 %EXPORT_TAGS = ();
18 1 @EXPORT_OK = ();
19 1 @EXPORT = ();
20  
21 1 $VERSION = '0.02';
22  
23 package HCKit::Template::Rule;
24  
25 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
26 0 my ($class, $body) = @_;
27 0 my $ref = {};
28 0 $ref->{body} = $body;
29 0 $ref->{args} = [];
30 0 ▶ return bless $ref, ref($class)||$class;
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
31 }
32  
33 0 ▶sub body {
Subroutine body: not called CC=1 SCAR=6.9
34 0 my ($self) = @_;
35 0 return $self->{body};
36 }
37  
38 0 ▶sub append {
Subroutine append: not called CC=1 SCAR=6.9
39 0 my ($self, $newbody) = @_;
40 0 $self->{body} .= $newbody;
41 }
42  
43 0 ▶sub set_args {
Subroutine set_args: not called CC=1 SCAR=6.9
44 0 my ($self, @arg) = @_;
45 0 $self->{args} = \@arg;
46 }
47  
48 0 ▶sub get_args {
Subroutine get_args: not called CC=1 SCAR=6.9
49 0 my ($self) = @_;
50 0 return @{$self->{args}};
51 }
52  
53 package HCKit::Template;
54  
55 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
56 1 my $class = shift;
57 1 my $ref = {};
58 1 $ref->{env} = {};
59 1 $ref->{fun} = {};
60 1 $ref->{op} = {};
61 1 $ref->{delayed} = {}; # names of delayed pre-precess fun
62 1 setup($ref);
63 1 ▶ bless $ref, ref($class)||$class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
64 }
65  
66 0 ▶sub rewrite_file {
Subroutine rewrite_file: not called CC=1 SCAR=6.9
Pod documented
67 0 my ($self, $file) = @_;
68 0 return $self->_rewrite_file($file, $self->{env});
69 }
70  
71 0 ▶sub rewrite_string {
Subroutine rewrite_string: not called CC=1 SCAR=6.9
Pod documented
72 0 my ($self, $string) = @_;
73 0 return $self->_rewrite_string($string, $self->{env});
74 }
75  
76 0 ▶sub process_file {
Subroutine process_file: not called CC=2 SCAR=17.9
Pod documented
77 0 my ($self, @file) = @_;
78 0 foreach my $f (@file){
79 0 $self->_include_file($f, $self->{env});
80 }
81 }
82  
83 0 ▶sub get_var {
Subroutine get_var: not called CC=1 SCAR=6.9
Pod documented
84 0 my ($self, $key) = @_;
85 0 return $self->{env}{$key};
86 }
87  
88 0 ▶sub set_var {
Subroutine set_var: not called CC=1 SCAR=6.9
Pod documented
89 0 my ($self, $key, $val) = @_;
90 0 $self->{env}{$key} = $val;
91 }
92  
93 # utilities ###########################################################
94  
95 0 ▶sub add_delayed {
Subroutine add_delayed: not called CC=1 SCAR=6.9
Pod undocumented
96 0 my ($self, $name) = @_;
97 0 $self->{delayed}->{$name} = 1;
98 }
99  
100 0 ▶sub is_delayed {
Subroutine is_delayed: not called CC=1 SCAR=6.9
Pod undocumented
101 0 my ($self, $name) = @_;
102 0 return $self->{delayed}->{$name};
103 }
104  
105 0 ▶sub _rewrite_file {
Subroutine _rewrite_file: not called CC=1 SCAR=6.9
106 0 my ($self, $file, $env) = @_;
107 0 my ($tmpl, $rule, $data) = read_src($file);
108 0 $self->parse_rule($rule, $env);
109 0 $self->parse_data($data, $env);
110 0 return $self->rewrite($tmpl, $env);
111 }
112  
113 0 ▶sub _include_file {
Subroutine _include_file: not called CC=1 SCAR=6.9
114 0 my ($self, $file, $env) = @_;
115 0 my ($tmpl, $rule, $data) = read_src($file);
116 0 $self->parse_rule($rule, $env);
117 0 $self->parse_data($data, $env);
118 }
119  
120 0 ▶sub _rewrite_string {
Subroutine _rewrite_string: not called CC=1 SCAR=6.9
121 0 my ($self, $string, $env) = @_;
122 0 my ($tmpl, $rule, $data) = read_src_from_string($string);
123 0 $self->parse_rule($rule, $env);
124 0 $self->parse_data($data, $env);
125 0 return $self->rewrite($tmpl, $env);
126 }
127  
128 0 ▶sub read_src {
Subroutine read_src: not called CC=5 SCAR=34.0
Pod undocumented
129 0 my ($file) = @_;
130 0 local *FILE;
131 0 my @x; # ($tmpl, $rule, $data);
132 0 my $mode = 0;
133 0 ▶ open(FILE, $file) || die "can't open $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, $file or die "can't open ${file}: $!" 0 0 0
134 0 ▶ while(<FILE>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FILE)) 0 0 0
135 0 ▶ if( index($_, '---RULE---') >= 0 ){
Branch: 2 branches
BranchTrueFalseTotal
if (index($_, '---RULE---') != -1) { } 0 0 0
elsif (index($_, '---DATA---') != -1) { } 0 0 0
136 0 $mode = 1;
137 }
138 elsif( index($_, '---DATA---') >= 0 ){
139 0 $mode = 2;
140 }
141 else{
142 0 $x[$mode] .= $_;
143 }
144 }
145 0 close(FILE);
146 0 return @x;
147 }
148  
149 0 ▶sub read_src_from_string {
Subroutine read_src_from_string: not called CC=4 SCAR=30.0
Pod undocumented
150 0 my ($str) = @_;
151 0 my @lines = split /\n/, $str;
152 0 my @x; # ($tmpl, $rule, $data);
153 0 my $mode = 0;
154 0 foreach (@lines){
155 0 ▶ if( index($_, '---RULE---') >= 0 ){
Branch: 2 branches
BranchTrueFalseTotal
if (index($_, '---RULE---') != -1) { } 0 0 0
elsif (index($_, '---DATA---') != -1) { } 0 0 0
156 0 $mode = 1;
157 }
158 elsif( index($_, '---DATA---') >= 0 ){
159 0 $mode = 2;
160 }
161 else{
162 0 $x[$mode] .= $_;
163 }
164 }
165 0 return @x;
166 }
167  
168 0 ▶sub parse_rule {
Subroutine parse_rule: not called CC=20 SCAR=60.4
Pod undocumented
169 0 my ($self, $rule, $env) = @_;
170 0 ▶ $rule ||= "";
Condition0%0/2
$rule ||= ""
l!l
00
MC/DC0%0/1
$rule ||= ""
$rule
Truth table0%0/2
A
A= $rule
Aresult
11
00
171 0 ▶ while( $rule =~
Branch: 1 branch
BranchTrueFalseTotal
if ($rule =~ m[ <([\w-]+)(\s[^>]+)?>(.*?)</\1> | <\*(.*?)\*> | (<\*--.*?--\*>) ]gsx) 0 0 0
172 /
173 <([\w-]+)(\s[^>]+)?>(.*?)<\/\1> |
174 <\*(.*?)\*> |
175 (<\*--.*?--\*>)
176 /gsx ){
177 0 my ($sym, $opt, $val, $cmd, $comm) = ($1,$2,$3,$4,$5);
178 0 ▶ if( $comm ){ next }
Branch: 1 branch
BranchTrueFalseTotal
if ($comm) 0 0 0
179 0 ▶ if( $cmd ){
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd) 0 0 0
180 0 ▶ if( $cmd =~ /^\?(.*?)\?$/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd =~ /^\?(.*?)\?$/) 0 0 0
181 0 my $instr = trim($1);
182 0 ▶ if( $instr =~ /^include\s+(\S+)$/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($instr =~ /^include\s+(\S+)$/) 0 0 0
183 0 $self->_include_file($1, $env);
184 0 next;
185 }
186 0 die "invalid instruction $cmd";
187 }
188 0 $self->eval_var_raw($cmd, $env);
189 0 next;
190 }
191 0 my $append = 0;
192 0 my @args;
193 0 ▶ $opt = "" if !defined($opt);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt 0 0 0
194 0 foreach(split " ", $opt){
195 0 ▶ if( $_ eq 'append' || $_ eq '+' ){
Branch: 5 branches
BranchTrueFalseTotal
if ($_ eq 'append' or $_ eq '+') { } 0 0 0
elsif ($_ eq 'trim') { } 0 0 0
elsif ($_ eq 'chomp') { } 0 0 0
elsif ($_ eq 'remove-white') { } 0 0 0
elsif (/args=(.*)/) { } 0 0 0
Condition0%0/3
$_ eq "append" or $_ eq "+"
l!l&&r!l&&!r
000
MC/DC0%0/2
$_ eq "append" or $_ eq "+"
$_ eq "append" $_ eq "+"
Truth table0%0/3
A or B
A= $_ eq "append"B= $_ eq "+"
ABresult
1X1
011
000
196 0 $append = 1;
197 }
198 elsif( $_ eq 'trim' ){
199 0 $val = trim($val);
200 }
201 elsif( $_ eq 'chomp' ){
202 0 $val =~ s/\s+$//;
203 }
204 elsif( $_ eq 'remove-white' ){
205 0 $val = trim($val);
206 0 $val =~ s/>\s+</></g;
207 }
208 elsif( /args=(.*)/ ){
209 0 @args = split /,/, $1;
210 }
211 }
212 0 my $prev = $env->{$sym};
213 0 ▶ if( $prev ){
Branch: 1 branch
BranchTrueFalseTotal
if ($prev) { } 0 0 0
214 0 ▶ ref($prev) eq "HCKit::Template::Rule"
Branch: 1 branch
BranchTrueFalseTotal
ref $prev eq "HCKit::Template::Rule" or die "$sym already defined" 0 0 0
215 || die "$sym already defined";
216 0 ▶ if( $append ){
Branch: 1 branch
BranchTrueFalseTotal
if ($append) { } 0 0 0
217 0 ▶ if( @args ){
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 0 0 0
218 0 die "cannot specify args in appending rule";
219 }
220 0 $prev->append($val);
221 }
222 else{
223 0 $env->{$sym} = HCKit::Template::Rule->new($val);
224 }
225 }
226 else{
227 0 $env->{$sym} = HCKit::Template::Rule->new($val);
228 0 ▶ if( @args ){
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 0 0 0
229 0 $env->{$sym}->set_args(@args);
230 }
231 }
232 }
233 }
234  
235 0 ▶sub parse_data {
Subroutine parse_data: not called CC=7 SCAR=40.3
Pod undocumented
236 0 my ($self, $data, $env) = @_;
237 0 ▶ $data ||= "";
Condition0%0/2
$data ||= ""
l!l
00
MC/DC0%0/1
$data ||= ""
$data
Truth table0%0/2
A
A= $data
Aresult
11
00
238 0 ▶ while( $data =~
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ m[<([\w-]+)>(.*?)</\1>|<\*(.*?)\*>|(<--.*?-->)]gs) 0 0 0
239 /<([\w-]+)>(.*?)<\/\1>|<\*(.*?)\*>|(<--.*?-->)/gs ){
240 0 my ($sym, $body, $cmd, $comm) = ($1, $2, trim($3), $4);
241 0 ▶ if( $comm ){ next }
Branch: 1 branch
BranchTrueFalseTotal
if ($comm) 0 0 0
242 0 ▶ if( $sym ){
Branch: 1 branch
BranchTrueFalseTotal
if ($sym) { } 0 0 0
243 0 my $val = $self->parse_data_body($body, $env);
244 0 extend_data($sym, $val, $env);
245 }
246 else{
247 0 ▶ if( $cmd =~ /^\?(.*?)\?$/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd =~ /^\?(.*?)\?$/) 0 0 0
248 0 my $instr = trim($1);
249 0 ▶ if( $instr =~ /^include\s+(\S+)$/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($instr =~ /^include\s+(\S+)$/) 0 0 0
250 0 $self->_include_file($1, $env);
251 0 next;
252 }
253 0 die "invalid instruction $cmd";
254 }
255 0 $self->eval_var_raw($cmd, $env);
256 }
257 }
258 }
259  
260 0 ▶sub parse_data_body {
Subroutine parse_data_body: not called CC=8 SCAR=42.8
Pod undocumented
261 0 my ($self, $body, $env) = @_;
262 0 my %hash;
263 my $text;
264 0 my $last = 0;
265 0 ▶ while( $body =~
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ m((<([\w-]+)>(.*?)</\2> | <\*(.*?)\*> | (<!\[\[CDATA\[.*\]\]>) ))gsx) 0 0 0
266 /(<([\w-]+)>(.*?)<\/\2> |
267 <\*(.*?)\*> |
268 (<!\[\[CDATA\[.*\]\]>)
269 )/gsx ){
270 0 my ($match, $sym, $val, $cmd, $cdata) = ($1,$2,$3,$4,$5);
271 0 my $len = length($match);
272 0 my $pre = substr($body, $last, pos($body)-$len-$last);
273 0 $text .= $pre;
274 0 $last = pos($body);
275 0 ▶ if( $sym ){
Branch: 2 branches
BranchTrueFalseTotal
if ($sym) { } 0 0 0
elsif ($cdata) { } 0 0 0
276 0 my $sub = $self->parse_data_body($val, $env);
277 0 extend_data($sym, $sub, \%hash);
278 }
279 elsif( $cdata ){
280 0 $cdata =~ s/^<!\[\[CDATA\[//;
281 0 $cdata =~ s/\]\]>$//;
282 0 $text .= $cdata;
283 }
284 else{
285 0 my ($key, $aux) =
286 $cmd =~ /\s*([\w:.-]+)\s*(.*)/;
287 0 my $val = $self->eval_var($key, $aux, $env);
288 0 ▶ if( ref($val) eq "HASH" ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq 'HASH') { } 0 0 0
289 0 ▶ while( my ($sym, $sub) = each %$val ){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$val) 0 0 0
290 0 extend_data($sym, $sub, \%hash);
291 }
292 }
293 0 else{ $text .= $val }
294 }
295 }
296 0 ▶ if( $last < length($body) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($last < length $body) 0 0 0
297 0 $text .= substr($body, $last);
298 }
299 0 ▶ return %hash ? \%hash : $text;
Branch: 1 branch
BranchTrueFalseTotal
%hash ? : 0 0 0
300 }
301  
302 0 ▶sub extend_data {
Subroutine extend_data: not called CC=3 SCAR=24.8
Pod undocumented
303 0 my ($key, $val, $env) = @_;
304 0 ▶ if( defined($env->{$key}) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $env->{$key}) { } 0 0 0
305 0 ▶ if( ref($env->{$key}) eq 'ARRAY' ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $env->{$key} eq 'ARRAY') { } 0 0 0
306 0 push @{$env->{$key}}, $val;
307 }
308 else{
309 0 $env->{$key} = [$env->{$key}, $val];
310 }
311 }
312 else{
313 0 $env->{$key} = $val;
314 }
315 }
316  
317 0 ▶sub rewrite {
Subroutine rewrite: not called CC=7 SCAR=40.3
Pod undocumented
318 0 my ($self, $tmpl, $env) = @_;
319 0 ▶ $tmpl = "" if !defined($tmpl);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tmpl 0 0 0
320 0 my $last = 0;
321 0 my $output = "";
322 0 ▶ while( $tmpl =~
Branch: 1 branch
BranchTrueFalseTotal
if ($tmpl =~ m[( <\*\s*([\w:.-]+)\s*(.*?)\*> | <\&\s*([\w:.-]+(?:\@\w+)?)\s*(.*?)\&>(.*?)<\&\s*/\4\s*\&> | <\{\s*([\w:.-]+(?:\@\w+)?)\s*(.*?)\}>(.*?)<\{\s*/\7\s*\}> )]gsx) 0 0 0
323 /(
324 <\*\s*([\w:.-]+)\s*(.*?)\*> |
325 <\&\s*([\w:.-]+(?:@\w+)?)\s*(.*?)\&>(.*?)<\&\s*\/\4\s*\&> |
326 <\{\s*([\w:.-]+(?:@\w+)?)\s*(.*?)\}>(.*?)<\{\s*\/\7\s*\}>
327 )/gxs ){
328 0 my ($match, $var, $varaux, $fun, $funaux, $funarg,
329 $loop, $loopaux, $loopbody) =
330 ($1,$2,$3,$4,$5,$6,$7,$8,$9);
331 0 my $len = length($match);
332 0 my $pre = substr($tmpl, $last, pos($tmpl)-$len-$last);
333 0 $output .= $pre;
334 0 $last = pos($tmpl);
335 0 ▶ if( $var ){
Branch: 3 branches
BranchTrueFalseTotal
if ($var) { } 0 0 0
elsif ($fun) { } 0 0 0
elsif ($loop) { } 0 0 0
336 0 $output .= $self->eval_var($var, $varaux, $env);
337 }
338 elsif( $fun ){
339 0 $fun =~ s/@.*//;
340 0 $output .= $self->eval_fun($fun, $funaux, $funarg, $env);
341 }
342 elsif( $loop ){
343 0 $loop =~ s/@.*//;
344 0 $output .= $self->eval_block($loop, $loopaux, $loopbody, $env);
345 }
346 }
347 0 ▶ if( $last < length($tmpl) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($last < length $tmpl) 0 0 0
348 0 $output .= substr($tmpl, $last);
349 }
350 0 return $output;
351 }
352  
353 0 ▶sub eval_var_raw {
Subroutine eval_var_raw: not called CC=1 SCAR=6.9
Pod undocumented
354 0 my ($self, $text, $env) = @_;
355 0 my ($key, $aux) = $text =~ /\s*([\w:.-]+)\s*(.*)/;
356 0 return $self->eval_var($key, $aux, $env);
357 }
358  
359 0 ▶sub eval_var {
Subroutine eval_var: not called CC=1 SCAR=6.9
Pod undocumented
360 0 my ($self, $key, $aux, $env) = @_;
361 0 $self->eval_construct($key, $aux, { __NEXT__ => $env });
362 }
363  
364 0 ▶sub eval_fun {
Subroutine eval_fun: not called CC=1 SCAR=6.9
Pod undocumented
365 0 my ($self, $key, $aux, $funarg, $env) = @_;
366 0 my $newenv = { __NEXT__ => $env };
367 0 $self->parse_funarg($funarg, $newenv);
368 0 $self->eval_construct($key, $aux, $newenv);
369 }
370  
371 0 ▶sub eval_block {
Subroutine eval_block: not called CC=1 SCAR=6.9
Pod undocumented
372 0 my ($self, $key, $aux, $body, $env) = @_;
373 0 my $newenv = { __NEXT__ => $env, __BODY__ => $body };
374 0 $self->eval_construct($key, $aux, $newenv);
375 }
376  
377 0 ▶sub eval_construct {
Subroutine eval_construct: not called CC=2 SCAR=17.9
Pod undocumented
378 0 my ($self, $key, $aux, $env) = @_;
379 0 my ($pre, $post) = parse_aux($aux);
380 0 my $stack = [];
381 0 ▶ if( $self->is_delayed($key) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_delayed($key)) { } 0 0 0
382 0 $stack = $pre;
383 }
384 else{
385 0 $self->process_tokens($stack, $env, @$pre);
386 }
387 0 my $val = $self->eval_with_fun($key, $stack, $env);
388 0 $stack = [$val];
389 0 $self->process_tokens($stack, $env, @$post);
390 0 return $stack->[0];
391 }
392  
393 0 ▶sub eval_data_construct {
Subroutine eval_data_construct: not called CC=2 SCAR=17.9
Pod undocumented
394 0 my ($self, $key, $aux, $env) = @_;
395 0 my ($pre, $post) = parse_aux($aux);
396 0 my $stack = [];
397 0 ▶ if( $self->is_delayed($key) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_delayed($key)) { } 0 0 0
398 0 $stack = $pre;
399 }
400 else{
401 0 $self->process_tokens($stack, $env, @$pre);
402 }
403 0 my $val = $self->eval_with_fun($key, $stack, $env);
404 0 $stack = [$val];
405 0 $self->process_tokens($stack, $env, @$post);
406 0 return $stack->[0];
407 }
408  
409 0 ▶sub process_para {
Subroutine process_para: not called CC=4 SCAR=30.0
Pod undocumented
410 0 my ($rule, $stack, $env) = @_;
411 0 my @args = $rule->get_args();
412 0 ▶ if( @args ){
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 0 0 0
413 0 foreach my $p (@args){
414 0 ▶ if( exists $env->{$p} ){ next }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $env->{$p}) 0 0 0
415 0 $env->{$p} = shift @$stack;
416 }
417 }
418 }
419  
420 0 ▶sub parse_aux {
Subroutine parse_aux: not called CC=4 SCAR=30.0
Pod undocumented
421 0 my ($aux) = @_;
422 0 ▶ $aux = "" if !defined($aux);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $aux 0 0 0
423 0 my $pre = [];
424 0 my $post = [];
425 0 my $ref = $pre;
426 0 ▶ while( $aux =~
Branch: 1 branch
BranchTrueFalseTotal
if ($aux =~ /( [\w:-]+=\"[^\"]*\" | [\w:-]+=\'[^\']*\' | [\w:-]+=[^\'\"\;\s]+ | \"[^\"]*\" | \'[^\']*\' | [^\s=;]+ | ; )/gsx) 0 0 0
427 /(
428 [\w:-]+=\"[^\"]*\" |
429 [\w:-]+=\'[^\']*\' |
430 [\w:-]+=[^\'\"\;\s]+ |
431 \"[^\"]*\" |
432 \'[^\']*\' |
433 [^\s=;]+ |
434 ;
435 )/gsx ){
436 0 ▶ if( $1 eq ';' ){ $ref = $post; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq ";") 0 0 0
437 0 push @$ref, $1;
438 }
439 0 return ($pre, $post);
440 }
441  
442 0 ▶sub parse_funarg {
Subroutine parse_funarg: not called CC=3 SCAR=24.8
Pod undocumented
443 0 my ($self, $funarg, $env) = @_;
444 0 ▶ $funarg ||= "";
Condition0%0/2
$funarg ||= ""
l!l
00
MC/DC0%0/1
$funarg ||= ""
$funarg
Truth table0%0/2
A
A= $funarg
Aresult
11
00
445 0 ▶ while( $funarg =~ /<([\w-]+)>(.*?)<\/\1>/gs ){
Branch: 1 branch
BranchTrueFalseTotal
if ($funarg =~ m[<([\w-]+)>(.*?)</\1>]gs) 0 0 0
446 0 my ($key, $val) = ($1, $2);
447 0 $env->{$key} = $self->rewrite($val, $env);
448 }
449 }
450  
451 0 ▶sub process_tokens {
Subroutine process_tokens: not called CC=10 SCAR=47.0
Pod undocumented
452 0 my ($self, $stack, $env, @tok) = @_;
453 0 my $i;
454 0 ▶ for($i=0;$i<=$#tok;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#tok) 0 0 0
455 0 my $t = $tok[$i];
456 0 ▶ if( $t =~ /^([\w:-]+)=\"([^\"]*)\"$/ ){
Branch: 8 branches
BranchTrueFalseTotal
if ($t =~ /^([\w:-]+)=\"([^\"]*)\"$/) { } 0 0 0
elsif ($t =~ /^([\w:-]+)+=\'([^\']*)\'$/) { } 0 0 0
elsif ($t =~ /^([\w:-]+)=([^\'\"\;\s]+)$/) { } 0 0 0
elsif ($t =~ /^\"(.*)\"$/) { } 0 0 0
elsif ($t =~ /^\'(.*)\'$/) { } 0 0 0
elsif ($t eq 'as') { } 0 0 0
elsif ($t =~ /^(\d+|\d+\.\d+)$/) { } 0 0 0
elsif ($t =~ /^(\d+)\.\.(\d+)$/) { } 0 0 0
457 0 $env->{$1} = $2;
458 }
459 elsif( $t =~ /^([\w:-]+)+=\'([^\']*)\'$/ ){
460 0 $env->{$1} = $2;
461 }
462 elsif( $t =~ /^([\w:-]+)=([^\'\"\;\s]+)$/ ){
463 0 $env->{$1} = var_lookup($2, $env);
464 }
465 elsif( $t =~ /^\"(.*)\"$/ ){
466 0 push @$stack, $1;
467 }
468 elsif( $t =~ /^\'(.*)\'$/ ){
469 0 push @$stack, $1;
470 }
471 elsif( $t eq "as" ){
472 0 push @$stack, $tok[++$i];
473 }
474 elsif( $t =~ /^(\d+|\d+\.\d+)$/ ){
475 0 push @$stack, $1;
476 }
477 elsif( $t =~ /^(\d+)\.\.(\d+)$/ ){
478 0 push @$stack, [$1..$2];
479 }
480 else{
481 0 $self->eval_with_op($t, $stack, $env);
482 }
483 }
484 }
485  
486 0 ▶sub lookup {
Subroutine lookup: not called CC=3 SCAR=24.8
Pod documented
487 0 my ($sym, $env) = @_;
488 0 ▶ while( $env ){
Branch: 1 branch
BranchTrueFalseTotal
if ($env) 0 0 0
489 0 ▶ if( defined($env->{$sym}) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $env->{$sym}) 0 0 0
490 0 return $env->{$sym};
491 }
492 0 $env = $env->{'__NEXT__'};
493 }
494 0 return undef;
495 }
496  
497 0 ▶sub var_lookup {
Subroutine var_lookup: not called CC=3 SCAR=24.8
Pod undocumented
498 0 my ($var, $env) = @_;
499 0 my @tok = split /\./, $var;
500 0 my $first = shift @tok;
501 0 my $val = lookup($first, $env);
502 0 foreach my $i (@tok){
503 0 ▶ unless( ref($val) eq 'HASH' ){
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $val eq "HASH") 0 0 0
504 0 return "";
505 }
506 0 $val = $val->{$i};
507 }
508 0 return $val;
509 }
510  
511 0 ▶sub eval_with_op {
Subroutine eval_with_op: not called CC=4 SCAR=30.0
Pod undocumented
512 0 my ($self, $expr, $stack, $env) = @_;
513 0 my $val = var_lookup($expr, $env);
514 0 ▶ if( defined($val) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) { } 0 0 0
515 0 ▶ unless( ref($val) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $val) 0 0 0
516 0 $val = $self->rewrite($val, $env);
517 }
518 0 push @$stack, $val;
519 }
520 else{
521 0 my $op = $self->{op}->{$expr};
522 0 ▶ if( $op ){
Branch: 1 branch
BranchTrueFalseTotal
if ($op) { } 0 0 0
523 0 &{$op}($self, $stack, $env);
524 }
525 else{
526 0 push @$stack, "";
527 }
528 }
529 }
530  
531 0 ▶sub eval_with_fun {
Subroutine eval_with_fun: not called CC=5 SCAR=34.0
Pod undocumented
532 0 my ($self, $expr, $stack, $env) = @_;
533 0 my $val = var_lookup($expr, $env);
534 0 ▶ if( defined($val) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) { } 0 0 0
535 0 ▶ if( ref($val) eq "HCKit::Template::Rule" ){
Branch: 2 branches
BranchTrueFalseTotal
if (ref $val eq 'HCKit::Template::Rule') { } 0 0 0
elsif (ref $val) { } 0 0 0
536 0 process_para($val, $stack, $env);
537 0 return $self->rewrite($val->body(), $env);
538 }
539 elsif( ref($val) ){
540 0 return $val;
541 }
542 else{
543 0 return $val;
544 }
545 }
546 else{
547 0 my $fun = $self->{fun}->{$expr};
548 0 ▶ if( $fun ){
Branch: 1 branch
BranchTrueFalseTotal
if ($fun) 0 0 0
549 0 return &{$fun}($self, $stack, $env);
550 }
551 }
552 0 return "";
553 }
554  
555 # fun ###############################################################
556  
557 # fun_foreach
558 # stack: LIST [IDENT]
559 # switches:
560 # foreach:sep=SEP
561 # foreach:toggle=INIT
562  
563 0 ▶sub fun_foreach {
Subroutine fun_foreach: not called CC=9 SCAR=45.0
Pod undocumented
564 0 my ($self, $stack, $env) = @_;
565 0 my $body = $env->{__BODY__};
566 0 my ($ident, $list);
567 0 my $top = pop @$stack;
568  
569 0 ▶ if( ref($top) eq "ARRAY" ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $top eq 'ARRAY') { } 0 0 0
570 0 $ident = "iter";
571 0 $list = $top;
572 }
573 else{
574 0 $ident = $top;
575 0 $list = pop @$stack;
576 }
577 0 my $output = "";
578 0 my $join = $env->{'foreach:sep'};
579 0 my $n = 0;
580 0 my $toggle = 0;
581 0 ▶ if( defined($env->{'foreach:toggle'}) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $env->{"foreach:toggle"}) 0 0 0
582 0 $toggle = 1;
583 0 $env->{toggle} = $env->{'foreach:toggle'};
584 }
585 0 ▶ unless( ref($list) eq "ARRAY" ){
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $list eq "ARRAY") 0 0 0
586 0 $list = [$list];
587 }
588 0 foreach my $e (@$list){
589 0 ▶ if( $join && $n++ > 0 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($join and $n++ > 0) 0 0 0
Condition0%0/3
$join and $n++ > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$join and $n++ > 0
$join $n++ > 0
Truth table0%0/3
A and B
A= $joinB= $n++ > 0
ABresult
0X0
100
111
590 0 $output .= $join;
591 }
592 0 $env->{$ident} = $e;
593 0 ▶ if( $toggle ){
Branch: 1 branch
BranchTrueFalseTotal
if ($toggle) 0 0 0
594 0 ▶ $env->{toggle} = $env->{toggle} ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$env->{'toggle'} ? : 0 0 0
595 }
596 0 my $tmp = $self->rewrite($body, $env);
597 0 $output .= $tmp;
598 }
599 0 return $output;
600 }
601  
602 0 ▶sub fun_if {
Subroutine fun_if: not called CC=2 SCAR=17.9
Pod undocumented
603 0 my ($self, $stack, $env) = @_;
604 0 my ($test) = @$stack;
605 0 ▶ if( $test ){
Branch: 1 branch
BranchTrueFalseTotal
if ($test) { } 0 0 0
606 0 my $body = $env->{__BODY__};
607 0 return $self->rewrite($body, $env);
608 }
609 0 else{ return ""; }
610 }
611  
612 0 ▶sub fun_include {
Subroutine fun_include: not called CC=2 SCAR=17.9
Pod undocumented
613 0 my ($self, $stack, $env) = @_;
614 0 my $prev_env = $env->{__NEXT__};
615 0 foreach my $f (@$stack){
616 0 $self->_include_file($f, $prev_env);
617 }
618 0 return "";
619 }
620  
621 0 ▶sub fun_set {
Subroutine fun_set: not called CC=2 SCAR=17.9
Pod undocumented
622 0 my ($self, $stack, $env) = @_; # <* set ident val *>
623 0 my $key = shift @$stack;
624 0 ▶ $key =~ /^[\w.-]+$/ || die "invalid identifier in set: $key";
Branch: 1 branch
BranchTrueFalseTotal
$key =~ /^[\w.-]+$/ or die "invalid identifier in set: $key" 0 0 0
625 0 my $ns = [];
626 0 $self->process_tokens($ns, $env, @$stack);
627 0 my $prev = $env->{__NEXT__};
628 0 $prev->{$key} = $ns->[0];
629 0 return "";
630 }
631  
632 0 ▶sub fun_default {
Subroutine fun_default: not called CC=3 SCAR=24.8
Pod undocumented
633 0 my ($self, $stack, $env) = @_; # <* default ident val *>
634 0 my $key = shift @$stack;
635 0 ▶ $key =~ /^[\w-]+$/ || die "invalid identifier in set: $key";
Branch: 1 branch
BranchTrueFalseTotal
$key =~ /^[\w-]+$/ or die "invalid identifier in set: $key" 0 0 0
636 0 my $bind = lookup($key, $env);
637 0 ▶ if( defined $bind ){ return }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bind) 0 0 0
638 0 my $ns = [];
639 0 $self->process_tokens($ns, $env, @$stack);
640 0 my $prev = $env->{__NEXT__};
641 0 $prev->{$key} = $ns->[0];
642 0 return "";
643 }
644  
645 1 ▶sub setup_fun {
Subroutine setup_fun: called CC=1 SCAR=0.0
Pod undocumented
646 1 my ($ref) = @_;
647 1 my $env = $ref->{fun};
648 1 $env->{foreach} = \&fun_foreach;
649 1 $env->{if} = \&fun_if;
650 1 $env->{include} = \&fun_include;
651 1 $env->{set} = \&fun_set;
652 1 $ref->{delayed}->{set} = 1;
653 1 $env->{default} = \&fun_default;
654 1 $ref->{delayed}->{default} = 1;
655 }
656  
657 0 ▶sub op_trim {
Subroutine op_trim: not called CC=1 SCAR=6.9
Pod undocumented
658 0 my ($self, $stack, $env) = @_;
659 0 my $s = pop @$stack;
660 0 push @$stack, trim($s);
661 }
662  
663 0 ▶sub op_list_remove_last {
Subroutine op_list_remove_last: not called CC=1 SCAR=6.9
Pod undocumented
664 0 my ($self, $stack, $env) = @_;
665 0 my $orig = pop @$stack;
666 0 my @list = @$orig;
667 0 pop @list;
668 0 push @$stack, \@list;
669 }
670  
671 0 ▶sub op_list_last {
Subroutine op_list_last: not called CC=1 SCAR=6.9
Pod undocumented
672 0 my ($self, $stack, $env) = @_;
673 0 my $orig = pop @$stack;
674 0 my $last = pop @$orig;
675 0 push @$stack, $last;
676 }
677  
678 0 ▶sub op_not {
Subroutine op_not: not called CC=2 SCAR=17.9
Pod undocumented
679 0 my ($self, $stack, $env) = @_;
680 0 my $arg = pop @$stack;
681 0 ▶ push @$stack, ((!$arg) ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
!$arg ? : 0 0 0
682 }
683  
684 0 ▶sub op_eq {
Subroutine op_eq: not called CC=2 SCAR=17.9
Pod undocumented
685 0 my ($self, $stack, $env) = @_;
686 0 my $right = pop @$stack;
687 0 my $left = pop @$stack;
688 0 ▶ push @$stack, (($left eq $right) ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$left eq $right ? : 0 0 0
689 }
690  
691 0 ▶sub op_or {
Subroutine op_or: not called CC=5 SCAR=34.0
Pod undocumented
692 0 my ($self, $stack, $env) = @_;
693 0 ▶ my $right = pop @$stack || 0;
Condition0%0/2
pop @$stack || 0
l!l
00
MC/DC0%0/1
pop @$stack || 0
pop @$stack
Truth table0%0/2
A
A= pop @$stack
Aresult
11
00
694 0 ▶ my $left = pop @$stack || 0;
Condition0%0/2
pop @$stack || 0
l!l
00
MC/DC0%0/1
pop @$stack || 0
pop @$stack
Truth table0%0/2
A
A= pop @$stack
Aresult
11
00
695 0 ▶ push @$stack, (($left || $right) ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$left || $right ? : 0 0 0
Condition0%0/3
$left or $right
l!l&&r!l&&!r
000
MC/DC0%0/2
$left or $right
$left $right
Truth table0%0/3
A or B
A= $leftB= $right
ABresult
1X1
011
000
696 }
697  
698 0 ▶sub op_and {
Subroutine op_and: not called CC=5 SCAR=34.0
Pod undocumented
699 0 my ($self, $stack, $env) = @_;
700 0 ▶ my $right = pop @$stack || 0;
Condition0%0/2
pop @$stack || 0
l!l
00
MC/DC0%0/1
pop @$stack || 0
pop @$stack
Truth table0%0/2
A
A= pop @$stack
Aresult
11
00
701 0 ▶ my $left = pop @$stack || 0;
Condition0%0/2
pop @$stack || 0
l!l
00
MC/DC0%0/1
pop @$stack || 0
pop @$stack
Truth table0%0/2
A
A= pop @$stack
Aresult
11
00
702 0 ▶ push @$stack, (($left && $right) ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$left && $right ? : 0 0 0
Condition0%0/3
$left and $right
!ll&&!rl&&r
000
MC/DC0%0/2
$left and $right
$left $right
Truth table0%0/3
A and B
A= $leftB= $right
ABresult
0X0
100
111
703 }
704  
705 0 ▶sub op_concat {
Subroutine op_concat: not called CC=1 SCAR=6.9
Pod undocumented
706 0 my ($self, $stack, $env) = @_;
707 0 my $right = pop @$stack;
708 0 my $left = pop @$stack;
709 0 push @$stack, ($left . $right);
710 }
711  
712 0 ▶sub op_lookup {
Subroutine op_lookup: not called CC=7 SCAR=40.3
Pod undocumented
713 0 my ($self, $stack, $env) = @_;
714 0 ▶ my $attr = pop @$stack || return "";
Condition0%0/2
pop @$stack || (return '')
l!l
00
MC/DC0%0/1
pop @$stack || (return '')
pop @$stack
Truth table0%0/2
A
A= pop @$stack
Aresult
11
00
715 0 ▶ my $hash = pop @$stack || return "";
Condition0%0/2
pop @$stack || (return '')
l!l
00
MC/DC0%0/1
pop @$stack || (return '')
pop @$stack
Truth table0%0/2
A
A= pop @$stack
Aresult
11
00
716 0 foreach my $a (split /\./, $attr){
717 0 ▶ if( $hash && ref($hash) eq "HASH" && exists($hash->{$a}) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($hash and ref $hash eq 'HASH' and exists $hash->{$a}) { } 0 0 0
Condition0%0/6
$hash and ref $hash eq "HASH" and exists $hash->{$a}
!ll&&!rl&&r
000
$hash and ref $hash eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
$hash and ref $hash eq "HASH" and exists $hash->{$a}
$hash ref $hash eq "HASH" exists $hash->{$a}
Truth table0%0/4
A and B and C
A= $hashB= ref $hash eq "HASH"C= exists $hash->{$a}
ABCresult
0XX0
10X0
1100
1111
718 0 $hash = $hash->{$a};
719 }
720 0 else{ return "" }
721 }
722 0 push @$stack, $hash;
723 }
724  
725 1 ▶sub setup_op {
Subroutine setup_op: called CC=1 SCAR=0.0
Pod undocumented
726 1 my ($self) = @_;
727 1 my $env = $self->{op};
728 1 $env->{trim} = \&op_trim;
729 1 $env->{'list-remove-last'} = \&op_list_remove_last;
730 1 $env->{'list-last'} = \&op_list_last;
731 1 $env->{'not'} = \&op_not;
732 1 $env->{'eq'} = \&op_eq;
733 1 $env->{'or'} = \&op_or;
734 1 $env->{'and'} = \&op_and;
735 1 $env->{'lookup'} = \&op_lookup;
736 1 $env->{'concat'} = \&op_concat;
737 }
738  
739 1 ▶sub setup {
Subroutine setup: called CC=1 SCAR=0.0
Pod undocumented
740 1 my ($ref) = @_;
741 1 setup_fun($ref);
742 1 setup_op($ref);
743 }
744  
745 # utilities ##########################################################
746  
747 0 ▶sub file_content {
Subroutine file_content: not called CC=2 SCAR=17.9
Pod undocumented
748 0 my ($file) = @_;
749 0 local *FILE;
750 0 local $/;
751 0 $/ = undef;
752 0 ▶ open(FILE, $file) || die "can't open $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, $file or die "can't open ${file}: $!" 0 0 0
753 0 my $c = <FILE>;
754 0 close(FILE);
755 0 return $c;
756 }
757  
758 0 ▶sub trim {
Subroutine trim: not called CC=2 SCAR=17.9
Pod documented
759 0 my ($str) = @_;
760 0 ▶ $str ||= "";
Condition0%0/2
$str ||= ""
l!l
00
MC/DC0%0/1
$str ||= ""
$str
Truth table0%0/2
A
A= $str
Aresult
11
00
761 0 $str =~ s/^\s+//;
762 0 $str =~ s/\s+$//;
763 0 return $str;
764 }
765  
766 0 ▶sub debug_env {
Subroutine debug_env: not called CC=5 SCAR=34.0
Pod undocumented
767 0 my ($env, $prefix) = @_;
768 0 ▶ if( ref($env) eq "ARRAY" ){
Branch: 2 branches
BranchTrueFalseTotal
if (ref $env eq 'ARRAY') { } 0 0 0
elsif (ref $env eq 'HASH') { } 0 0 0
769 0 foreach(@$env){
770 0 debug_env($_, $prefix);
771 }
772 }
773 elsif( ref($env) eq "HASH" ){
774 0 ▶ while( my($key, $val) = each %$env ){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$env) 0 0 0
775 0 print " " x $prefix, "<$key>\n";
776 0 debug_env($val, $prefix+2);
777 0 print "\n";
778 0 print " " x $prefix, "</$key>\n";
779 }
780 }
781 else{
782 0 print " " x $prefix, $env;
783 }
784 }
785  
786 1 1;
787 __END__
↑ Summary