lib/Jemplate/Directive.pm

filter: Statementstmt 9.3% 20 / 214 Branchbran 0.0% 0 / 72 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 14.2% 6 / 42 Podpod 0.0% 0 / 34 totaltotal 6.4% 26 / 406 SCAR 76.9
CC
51
Cov
6%
ident
72.0
assign
30.0
_attempt_range_expand_val
20.0
CRAP 2184.6
« blib/lib/Jemplate.pm ↑ Summary blib/lib/Jemplate/Directive.pm »
1 package Jemplate::Directive;
2 51 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 51 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 51 our $OUTPUT = 'output +=';
6 51 our $WHILE_MAX = 1000;
7  
8 # parser state variable
9 # only true when inside JAVASCRIPT blocks
10 51 our $INJAVASCRIPT = 0;
11  
12 sub template {
13 0 ▶ my ($class, $block) = @_;
Subroutine template: not called CC=2 SCAR=17.9
Pod undocumented
14  
15 0 ▶ return "function() { return ''; }" unless $block =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $block =~ /\S/ 0 0 0
16  
17 0 return <<"...";
18 function(context) {
19 if (! context) throw('Jemplate function called without context\\n');
20 var stash = context.stash;
21 var output = '';
22  
23 try {
24 $block
25 }
26 catch(e) {
27 var error = context.set_error(e, output);
28 throw(error);
29 }
30  
31 return output;
32 }
33 ...
34 }
35
36 # Try to do 1 .. 10 expansions
37 sub _attempt_range_expand_val ($) {
38 0 ▶ my $val = shift;
Subroutine _attempt_range_expand_val: not called CC=4 SCAR=30.0
39 0 ▶ return $val unless
Branch: 1 branch
BranchTrueFalseTotal
unless () = $val =~ /\s*\[\s*(\S+)\s*\.\.\s*(\S+)\s*\]/ 0 0 0
40 my ( $from, $to ) = $val =~ m/\s*\[\s*(\S+)\s*\.\.\s*(\S+)\s*\]/;
41  
42 0 ▶ die "Range expansion is current supported for positive/negative integer values only (e.g. [ 1 .. 10 ])\nCannot expand: $val" unless $from =~ m/^-?\d+$/ && $to =~ m/^-?\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $from =~ /^-?\d+$/ and $to =~ /^-?\d+$/ 0 0 0
Condition0%0/3
$from =~ /^-?\d+$/ and $to =~ /^-?\d+$/
!ll&&!rl&&r
000
MC/DC0%0/2
$from =~ /^-?\d+$/ and $to =~ /^-?\d+$/
$from =~ /^-?\d+$/ $to =~ /^-?\d+$/
Truth table0%0/3
A and B
A= $from =~ /^-?\d+$/B= $to =~ /^-?\d+$/
ABresult
0X0
100
111
43  
44 0 return join '', '[', join( ',', $from .. $to ), ']';
45 }
46  
47 #------------------------------------------------------------------------
48 # textblock($text)
49 #------------------------------------------------------------------------
50  
51 sub textblock {
52 0 ▶ my ($class, $text) = @_;
Subroutine textblock: not called CC=2 SCAR=17.9
Pod undocumented
53 0 ▶ return $text if $INJAVASCRIPT;
Branch: 1 branch
BranchTrueFalseTotal
if $Jemplate::Directive::INJAVASCRIPT 0 0 0
54 0 return "$OUTPUT " . $class->text($text) . ';';
55 }
56  
57 #------------------------------------------------------------------------
58 # text($text)
59 #------------------------------------------------------------------------
60  
61 sub text {
62 0 ▶ my ($class, $text) = @_;
Subroutine text: not called CC=2 SCAR=17.9
Pod undocumented
63 0 for ($text) {
64 0 s/([\'\\])/\\$1/g;
65 0 s/\n/\\n/g;
66 0 s/\r/\\r/g;
67 }
68 0 return "'" . $text . "'";
69 }
70  
71 #------------------------------------------------------------------------
72 # ident(\@ident) foo.bar(baz)
73 #------------------------------------------------------------------------
74  
75 sub ident {
76 0 ▶ my ($class, $ident) = @_;
Subroutine ident: not called CC=8 SCAR=42.8
Pod undocumented
77 0 ▶ return "''" unless @$ident;
Branch: 1 branch
BranchTrueFalseTotal
unless @$ident 0 0 0
78 0 my $ns;
79  
80 # does the first element of the identifier have a NAMESPACE
81 # handler defined?
82 0 ▶ if (ref $class && @$ident > 2 && ($ns = $class->{ NAMESPACE })) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class and @$ident > 2 and $ns = $class->{"NAMESPACE"}) 0 0 0
Condition0%0/6
ref $class and @$ident > 2 and $ns = $class->{"NAMESPACE"}
!ll&&!rl&&r
000
ref $class and @$ident > 2
!ll&&!rl&&r
000
MC/DC0%0/3
ref $class and @$ident > 2 and $ns = $class->{"NAMESPACE"}
ref $class @$ident > 2 $ns = $class->{"NAMESPACE"}
Truth table0%0/4
A and B and C
A= ref $classB= @$ident > 2C= $ns = $class->{"NAMESPACE"}
ABCresult
0XX0
10X0
1100
1111
83 0 my $key = $ident->[0];
84 0 $key =~ s/^'(.+)'$/$1/s;
85 0 ▶ if ($ns = $ns->{ $key }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ns = $ns->{$key}) 0 0 0
86 0 return $ns->ident($ident);
87 }
88 }
89  
90 0 ▶ if (scalar @$ident <= 2 && ! $ident->[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$ident <= 2 and not $ident->[1]) { } 0 0 0
Condition0%0/3
scalar @$ident <= 2 and not $ident->[1]
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @$ident <= 2 and not $ident->[1]
scalar @$ident <= 2 not $ident->[1]
Truth table0%0/3
A and not(B)
A= scalar @$ident <= 2B= not $ident->[1]
ABresult
0X0
100
111
91 0 $ident = $ident->[0];
92 }
93 else {
94 0 $ident = '[' . join(', ', @$ident) . ']';
95 }
96 0 return "stash.get($ident)";
97 }
98  
99  
100 #------------------------------------------------------------------------
101 # assign(\@ident, $value, $default) foo = bar
102 #------------------------------------------------------------------------
103  
104 sub assign {
105 0 ▶ my ($class, $var, $val, $default) = @_;
Subroutine assign: not called CC=5 SCAR=34.0
Pod undocumented
106  
107 0 ▶ if (ref $var) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $var) 0 0 0
108 0 ▶ if (scalar @$var == 2 && ! $var->[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$var == 2 and not $var->[1]) { } 0 0 0
Condition0%0/3
scalar @$var == 2 and not $var->[1]
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @$var == 2 and not $var->[1]
scalar @$var == 2 not $var->[1]
Truth table0%0/3
A and not(B)
A= scalar @$var == 2B= not $var->[1]
ABresult
0X0
100
111
109 0 $var = $var->[0];
110 }
111 else {
112 0 $var = '[' . join(', ', @$var) . ']';
113 }
114 }
115 0 $val = _attempt_range_expand_val $val;
116 0 ▶ $val .= ', 1' if $default;
Branch: 1 branch
BranchTrueFalseTotal
if $default 0 0 0
117 0 return "stash.set($var, $val)";
118 }
119  
120  
121 #------------------------------------------------------------------------
122 # args(\@args) foo, bar, baz = qux
123 #------------------------------------------------------------------------
124  
125 sub args {
126 0 ▶ my ($class, $args) = @_;
Subroutine args: not called CC=3 SCAR=24.8
Pod undocumented
127 0 my $hash = shift @$args;
128 0 ▶ push(@$args, '{ ' . join(', ', @$hash) . ' }')
Branch: 1 branch
BranchTrueFalseTotal
if @$hash 0 0 0
129 if @$hash;
130  
131 0 ▶ return '[]' unless @$args;
Branch: 1 branch
BranchTrueFalseTotal
unless @$args 0 0 0
132 0 return '[ ' . join(', ', @$args) . ' ]';
133 }
134  
135  
136 #------------------------------------------------------------------------
137 # filenames(\@names)
138 #------------------------------------------------------------------------
139
140 sub filenames {
141 0 ▶ my ($class, $names) = @_;
Subroutine filenames: not called CC=2 SCAR=17.9
Pod undocumented
142 0 ▶ if (@$names > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$names > 1) { } 0 0 0
143 0 $names = '[ ' . join(', ', @$names) . ' ]';
144 }
145 else {
146 0 $names = shift @$names;
147 }
148 0 return $names;
149 }
150
151
152 #------------------------------------------------------------------------
153 # get($expr) [% foo %]
154 #------------------------------------------------------------------------
155  
156 sub get {
157 0 ▶ my ($class, $expr) = @_;
Subroutine get: not called CC=1 SCAR=6.9
Pod undocumented
158 0 return "$OUTPUT $expr;";
159 }
160  
161 sub block {
162 0 ▶ my ($class, $block) = @_;
Subroutine block: not called CC=2 SCAR=17.9
Pod undocumented
163 return join "\n", map {
164 0 s/^#(?=line \d+)/\/\//gm;
165 0 $_;
166 0 ▶ } @{ $block || [] };
Branch: 1 branch
BranchTrueFalseTotal
$block or [] 0 0 0
167 }
168  
169 #------------------------------------------------------------------------
170 # call($expr) [% CALL bar %]
171 #------------------------------------------------------------------------
172  
173 sub call {
174 0 ▶ my ($class, $expr) = @_;
Subroutine call: not called CC=1 SCAR=6.9
Pod undocumented
175 0 $expr .= ';';
176 0 return $expr;
177 }
178  
179  
180 #------------------------------------------------------------------------
181 # set(\@setlist) [% foo = bar, baz = qux %]
182 #------------------------------------------------------------------------
183  
184 sub set {
185 0 ▶ my ($class, $setlist) = @_;
Subroutine set: not called CC=2 SCAR=17.9
Pod undocumented
186 0 my $output;
187 0 ▶ while (my ($var, $val) = splice(@$setlist, 0, 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@$setlist, 0, 2)) 0 0 0
188 0 $output .= $class->assign($var, $val) . ";\n";
189 }
190 0 chomp $output;
191 0 return $output;
192 }
193  
194  
195 #------------------------------------------------------------------------
196 # default(\@setlist) [% DEFAULT foo = bar, baz = qux %]
197 #------------------------------------------------------------------------
198  
199 sub default {
200 0 ▶ my ($class, $setlist) = @_;
Subroutine default: not called CC=2 SCAR=17.9
Pod undocumented
201 0 my $output;
202 0 ▶ while (my ($var, $val) = splice(@$setlist, 0, 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@$setlist, 0, 2)) 0 0 0
203 0 $output .= &assign($class, $var, $val, 1) . ";\n";
204 }
205 0 chomp $output;
206 0 return $output;
207 }
208  
209  
210 #------------------------------------------------------------------------
211 # include(\@nameargs) [% INCLUDE template foo = bar %]
212 # # => [ [ $file, ... ], \@args ]
213 #------------------------------------------------------------------------
214  
215 sub include {
216 0 ▶ my ($class, $nameargs) = @_;
Subroutine include: not called CC=2 SCAR=17.9
Pod undocumented
217 0 my ($file, $args) = @$nameargs;
218 0 my $hash = shift @$args;
219 0 $file = $class->filenames($file);
220 0 ▶ $file .= @$hash ? ', { ' . join(', ', @$hash) . ' }' : '';
Branch: 1 branch
BranchTrueFalseTotal
@$hash ? : 0 0 0
221 0 return "$OUTPUT context.include($file);";
222 }
223
224
225 #------------------------------------------------------------------------
226 # process(\@nameargs) [% PROCESS template foo = bar %]
227 # # => [ [ $file, ... ], \@args ]
228 #------------------------------------------------------------------------
229  
230 sub process {
231 0 ▶ my ($class, $nameargs) = @_;
Subroutine process: not called CC=2 SCAR=17.9
Pod undocumented
232 0 my ($file, $args) = @$nameargs;
233 0 my $hash = shift @$args;
234 0 $file = $class->filenames($file);
235 0 ▶ $file .= @$hash ? ', { ' . join(', ', @$hash) . ' }' : '';
Branch: 1 branch
BranchTrueFalseTotal
@$hash ? : 0 0 0
236 0 return "$OUTPUT context.process($file);";
237 }
238
239
240 #------------------------------------------------------------------------
241 # if($expr, $block, $else) [% IF foo < bar %]
242 # ...
243 # [% ELSE %]
244 # ...
245 # [% END %]
246 #------------------------------------------------------------------------
247
248 sub if {
249 0 ▶ my ($class, $expr, $block, $else) = @_;
Subroutine if: not called CC=4 SCAR=30.0
Pod undocumented
250 0 ▶ my @else = $else ? @$else : ();
Branch: 1 branch
BranchTrueFalseTotal
$else ? : 0 0 0
251 0 $else = pop @else;
252  
253 0 my $output = "if ($expr) {\n$block\n}\n";
254
255 0 foreach my $elsif (@else) {
256 0 ($expr, $block) = @$elsif;
257 0 $output .= "else if ($expr) {\n$block\n}\n";
258 }
259 0 ▶ if (defined $else) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $else) 0 0 0
260 0 $output .= "else {\n$else\n}\n";
261 }
262  
263 0 return $output;
264 }
265  
266 #------------------------------------------------------------------------
267 # foreach($target, $list, $args, $block) [% FOREACH x = [ foo bar ] %]
268 # ...
269 # [% END %]
270 #------------------------------------------------------------------------
271  
272 sub foreach {
273 0 ▶ my ($class, $target, $list, $args, $block) = @_;
Subroutine foreach: not called CC=3 SCAR=24.8
Pod undocumented
274 0 $args = shift @$args;
275 0 ▶ $args = @$args ? ', { ' . join(', ', @$args) . ' }' : '';
Branch: 1 branch
BranchTrueFalseTotal
@$args ? : 0 0 0
276  
277 0 my ($loop_save, $loop_set, $loop_restore, $setiter);
278 0 ▶ if ($target) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target) { } 0 0 0
279 0 $loop_save =
280 'try { oldloop = ' . $class->ident(["'loop'"]) . ' } finally {}';
281 0 $loop_set = "stash.data['$target'] = value";
282 0 $loop_restore = "stash.set('loop', oldloop)";
283 }
284 else {
285 0 die "XXX - Not supported yet";
286 0 $loop_save = 'stash = context.localise()';
287 0 $loop_set =
288 "stash.get(['import', [value]]) if typeof(value) == 'object'";
289 0 $loop_restore = 'stash = context.delocalise()';
290 }
291  
292 0 $list = _attempt_range_expand_val $list;
293  
294 0 return <<EOF;
295  
296 // FOREACH
297 (function() {
298 var list = $list;
299 list = new Jemplate.Iterator(list);
300 var retval = list.get_first();
301 var value = retval[0];
302 var done = retval[1];
303 var oldloop;
304 $loop_save
305 stash.set('loop', list);
306 try {
307 while (! done) {
308 $loop_set;
309 $block;
310 retval = list.get_next();
311 value = retval[0];
312 done = retval[1];
313 }
314 }
315 catch(e) {
316 throw(context.set_error(e, output));
317 }
318 $loop_restore;
319 })();
320 EOF
321 }
322  
323  
324 #------------------------------------------------------------------------
325 # next() [% NEXT %]
326 #
327 # Next iteration of a FOREACH loop (experimental)
328 #------------------------------------------------------------------------
329  
330 sub next {
331 0 ▶ return <<EOF;
Subroutine next: not called CC=1 SCAR=6.9
Pod undocumented
332 retval = list.get_next();
333 value = retval[0];
334 done = retval[1];
335 continue;
336 EOF
337 }
338  
339 #------------------------------------------------------------------------
340 # wrapper(\@nameargs, $block) [% WRAPPER template foo = bar %]
341 # # => [ [$file,...], \@args ]
342 #------------------------------------------------------------------------
343 sub wrapper {
344 0 ▶ my ($class, $nameargs, $block) = @_;
Subroutine wrapper: not called CC=4 SCAR=30.0
Pod undocumented
345 0 my ($file, $args) = @$nameargs;
346 0 my $hash = shift @$args;
347  
348 0 s/ => /: / for @$hash;
349 0 ▶ return $class->multi_wrapper($file, $hash, $block)
Branch: 1 branch
BranchTrueFalseTotal
if @$file > 1 0 0 0
350 if @$file > 1;
351 0 $file = shift @$file;
352 0 push(@$hash, "'content': output");
353 0 ▶ $file .= @$hash ? ', { ' . join(', ', @$hash) . ' }' : '';
Branch: 1 branch
BranchTrueFalseTotal
@$hash ? : 0 0 0
354  
355 0 return <<EOF;
356  
357 // WRAPPER
358 $OUTPUT (function() {
359 var output = '';
360 $block;
361 return context.include($file);
362 })();
363 EOF
364 }
365  
366 sub multi_wrapper {
367 0 ▶ my ($class, $file, $hash, $block) = @_;
Subroutine multi_wrapper: not called CC=2 SCAR=17.9
Pod undocumented
368  
369 0 push(@$hash, "'content': output");
370 0 ▶ $hash = @$hash ? ', { ' . join(', ', @$hash) . ' }' : '';
Branch: 1 branch
BranchTrueFalseTotal
@$hash ? : 0 0 0
371  
372 0 $file = join(', ', reverse @$file);
373 # print STDERR "multi wrapper: $file\n";
374  
375 0 return <<EOF;
376  
377 // WRAPPER
378 $OUTPUT (function() {
379 var output = '';
380 $block;
381 var files = new Array($file);
382 for (var i = 0; i < files.length; i++) {
383 output = context.include(files[i]$hash);
384 }
385 return output;
386 })();
387 EOF
388 }
389  
390  
391 #------------------------------------------------------------------------
392 # while($expr, $block) [% WHILE x < 10 %]
393 # ...
394 # [% END %]
395 #------------------------------------------------------------------------
396  
397 sub while {
398 0 ▶ my ($class, $expr, $block) = @_;
Subroutine while: not called CC=1 SCAR=6.9
Pod undocumented
399
400 0 return <<EOF;
401
402 // WHILE
403 var failsafe = $WHILE_MAX;
404 while (--failsafe && ($expr)) {
405 $block
406 }
407 if (! failsafe)
408 throw("WHILE loop terminated (> $WHILE_MAX iterations)\\n")
409 EOF
410 }
411  
412 #------------------------------------------------------------------------
413 # javascript($script) [% JAVASCRIPT %]
414 # ...
415 # [% END %]
416 #------------------------------------------------------------------------
417 sub javascript {
418 0 ▶ my ( $class, $javascript ) = @_;
Subroutine javascript: not called CC=1 SCAR=6.9
Pod undocumented
419 0 return $javascript;
420 }
421  
422 sub no_javascript {
423 0 ▶ my ( $class ) = @_;
Subroutine no_javascript: not called CC=1 SCAR=6.9
Pod undocumented
424 0 die "EVAL_JAVASCRIPT has not been enabled, cannot process [% JAVASCRIPT %] blocks";
425 }
426  
427 #------------------------------------------------------------------------
428 # switch($expr, \@case) [% SWITCH %]
429 # [% CASE foo %]
430 # ...
431 # [% END %]
432 #------------------------------------------------------------------------
433  
434 sub switch {
435 0 ▶ my ($class, $expr, $case) = @_;
Subroutine switch: not called CC=3 SCAR=24.8
Pod undocumented
436 0 my @case = @$case;
437 0 my ($match, $block, $default);
438 0 my $caseblock = '';
439  
440 0 $default = pop @case;
441  
442 0 foreach $case (@case) {
443 0 $match = $case->[0];
444 0 $block = $case->[1];
445 # $block = pad($block, 1) if $PRETTY;
446 0 $caseblock .= <<EOF;
447 case $match:
448 $block
449 break;
450  
451 EOF
452 }
453  
454 0 ▶ if (defined $default) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $default) 0 0 0
455 0 $caseblock .= <<EOF;
456 default:
457 $default
458 break;
459 EOF
460 }
461 # $caseblock = pad($caseblock, 2) if $PRETTY;
462  
463 0 return <<EOF;
464  
465 switch($expr) {
466 $caseblock
467 }
468  
469 EOF
470 }
471  
472  
473 #------------------------------------------------------------------------
474 # throw(\@nameargs) [% THROW foo "bar error" %]
475 # # => [ [$type], \@args ]
476 #------------------------------------------------------------------------
477  
478 sub throw {
479 0 ▶ my ($class, $nameargs) = @_;
Subroutine throw: not called CC=1 SCAR=6.9
Pod undocumented
480 0 my ($type, $args) = @$nameargs;
481 0 my $hash = shift(@$args);
482 0 my $info = shift(@$args);
483 0 $type = shift @$type;
484  
485 0 return qq{throw([$type, $info]);};
486 }
487  
488  
489 #------------------------------------------------------------------------
490 # clear() [% CLEAR %]
491 #
492 # NOTE: this is redundant, being hard-coded (for now) into Parser.yp
493 #------------------------------------------------------------------------
494  
495 sub clear {
496 0 ▶ return "output = '';";
Subroutine clear: not called CC=1 SCAR=6.9
Pod undocumented
497 }
498  
499  
500 #------------------------------------------------------------------------
501 # break() [% BREAK %]
502 #
503 # NOTE: this is redundant, being hard-coded (for now) into Parser.yp
504 #------------------------------------------------------------------------
505  
506 sub break {
507 0 ▶ return 'break;';
Subroutine break: not called CC=1 SCAR=6.9
Pod undocumented
508 }
509
510 #------------------------------------------------------------------------
511 # return() [% RETURN %]
512 #------------------------------------------------------------------------
513  
514 sub return {
515 0 ▶ return "return output;"
Subroutine return: not called CC=1 SCAR=6.9
Pod undocumented
516 }
517  
518  
519 #------------------------------------------------------------------------
520 # stop() [% STOP %]
521 #------------------------------------------------------------------------
522  
523 sub stop {
524 0 ▶ return "throw('Jemplate.STOP\\n' + output);";
Subroutine stop: not called CC=1 SCAR=6.9
Pod undocumented
525 }
526  
527  
528 #------------------------------------------------------------------------
529 # use(\@lnameargs) [% USE alias = plugin(args) %]
530 # # => [ [$file, ...], \@args, $alias ]
531 #------------------------------------------------------------------------
532  
533 sub use {
534 0 ▶ my ($class, $lnameargs) = @_;
Subroutine use: not called CC=3 SCAR=24.8
Pod undocumented
535 0 my ($file, $args, $alias) = @$lnameargs;
536 0 $file = shift @$file; # same production rule as INCLUDE
537 0 ▶ $alias ||= $file;
Condition0%0/3
$alias ||= $file
l!l&&r!l&&!r
000
MC/DC0%0/2
$alias ||= $file
$alias $file
Truth table0%0/3
A ||= B
A= $aliasB= $file
ABresult
1X1
011
000
538 0 $args = &args($class, $args);
539 0 ▶ $file .= ", $args" if $args;
Branch: 1 branch
BranchTrueFalseTotal
if $args 0 0 0
540 0 return "// USE\n"
541 . "stash.set($alias, context.plugin($file));";
542 }
543  
544  
545 #------------------------------------------------------------------------
546 # raw(\@lnameargs) [% RAW alias = plugin(args) %]
547 # # => [ [$file, ...], \@args, $alias ]
548 #------------------------------------------------------------------------
549  
550 sub raw {
551 0 ▶ my ($class, $lnameargs) = @_;
Subroutine raw: not called CC=2 SCAR=17.9
Pod undocumented
552 0 my ($file, $args, $alias) = @$lnameargs;
553 0 $file = shift @$file; # same production rule as INCLUDE
554 0 ▶ $alias ||= $file;
Condition0%0/3
$alias ||= $file
l!l&&r!l&&!r
000
MC/DC0%0/2
$alias ||= $file
$alias $file
Truth table0%0/3
A ||= B
A= $aliasB= $file
ABresult
1X1
011
000
555 0 $args = &args($class, $args);
556 # $file .= ", $args" if $args;
557 0 $file =~ s/'|"//g;
558 0 return "// RAW\n"
559 . "stash.set($alias, $file);";
560 }
561  
562  
563 #------------------------------------------------------------------------
564 # stubs() [% STOP %]
565 #------------------------------------------------------------------------
566  
567 sub filter {
568 0 ▶ my ($class, $lnameargs, $block) = @_;
Subroutine filter: not called CC=4 SCAR=30.0
Pod undocumented
569 0 my ($name, $args, $alias) = @$lnameargs;
570 0 $name = shift @$name;
571 0 $args = &args($class, $args);
572 0 ▶ $args = $args ? "$args, $alias" : ", null, $alias"
Branch: 2 branches
BranchTrueFalseTotal
if $alias 0 0 0
$args ? : 0 0 0
573 if $alias;
574 0 ▶ $name .= ", $args" if $args;
Branch: 1 branch
BranchTrueFalseTotal
if $args 0 0 0
575 0 return <<EOF;
576  
577 // FILTER
578 $OUTPUT (function() {
579 var output = '';
580  
581 $block
582  
583 return context.filter(output, $name);
584 })();
585 EOF
586 }
587  
588 sub quoted {
589 0 ▶ my $class = shift;
Subroutine quoted: not called CC=3 SCAR=24.8
Pod undocumented
590 0 ▶ if ( @_ && ref($_[0]) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and ref $_[0]) 0 0 0
Condition0%0/3
@_ and ref $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and ref $_[0]
@_ ref $_[0]
Truth table0%0/3
A and B
A= @_B= ref $_[0]
ABresult
0X0
100
111
591 0 return join( " + ", @{$_[0]} );
592 }
593 0 return "throw('QUOTED called with unknown arguments in Jemplate');";
594 }
595  
596 #------------------------------------------------------------------------
597 # macro($name, $block, \@args)
598 #------------------------------------------------------------------------
599  
600 sub macro {
601 0 ▶ my ($class, $ident, $block, $args) = @_;
Subroutine macro: not called CC=2 SCAR=17.9
Pod undocumented
602  
603 0 ▶ if ($args) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args) { } 0 0 0
604 0 $args = join(';', map { "args['$_'] = fargs.shift()" } @$args);
605  
606 0 return <<EOF;
607  
608 //MACRO
609 stash.set('$ident', function () {
610 var output = '';
611 var args = {};
612 var fargs = Array.prototype.slice.call(arguments);
613 $args;
614 args.arguments = Array.prototype.slice.call(arguments);
615  
616 var params = fargs.shift() || {};
617  
618 for (var key in params) {
619 args[key] = params[key];
620 }
621  
622 context.stash.clone(args);
623 try {
624 $block
625 }
626 catch(e) {
627 var error = context.set_error(e, output);
628 throw(error);
629 }
630  
631 context.stash.declone();
632 return output;
633 });
634  
635 EOF
636  
637 }
638 else {
639 0 return <<EOF;
640  
641 //MACRO
642  
643 stash.set('$ident', function () {
644 var output = '';
645 var args = {};
646
647 var fargs = Array.prototype.slice.call(arguments);
648 args.arguments = Array.prototype.slice.call(arguments);
649
650 if (typeof arguments[0] == 'object') args = arguments[0];
651
652 context.stash.clone(args);
653 try {
654 $block
655 }
656 catch(e) {
657 var error = context.set_error(e, output);
658 throw(error);
659 }
660  
661 context.stash.declone();
662 return output;});
663  
664 EOF
665 }
666 }
667  
668 sub capture {
669 0 ▶ my ($class, $name, $block) = @_;
Subroutine capture: not called CC=4 SCAR=30.0
Pod undocumented
670  
671 0 ▶ if (ref $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $name) 0 0 0
672 0 ▶ if (scalar @$name == 2 && ! $name->[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$name == 2 and not $name->[1]) { } 0 0 0
Condition0%0/3
scalar @$name == 2 and not $name->[1]
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @$name == 2 and not $name->[1]
scalar @$name == 2 not $name->[1]
Truth table0%0/3
A and not(B)
A= scalar @$name == 2B= not $name->[1]
ABresult
0X0
100
111
673 0 $name = $name->[0];
674 }
675 else {
676 0 $name = '[' . join(', ', @$name) . ']';
677 }
678 }
679  
680 0 return <<EOF;
681  
682 // CAPTURE
683 (function() {
684 var output = '';
685 $block
686 stash.set($name, output);
687 })();
688 EOF
689  
690 }
691  
692 BEGIN {
693 51 ▶ return; # Comment out this line to get callback traces
Subroutine BEGIN: called CC=3 SCAR=14.6
694 51 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
695 0 my $pkg = __PACKAGE__ . '::';
696 0 my $stash = \ %$pkg;
697 51 ▶ use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
698 0 for my $name (keys %$stash) {
699 0 my $glob = $stash->{$name};
700 0 ▶ if (*$glob{CODE}) {
Branch: 1 branch
BranchTrueFalseTotal
if (*$glob{"CODE"}) 0 0 0
701 0 my $code = *$glob{CODE};
702 51 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
703 $stash->{$name} = sub {
704 0 ▶ warn "Calling $name(@_)\n";
Subroutine __ANON__: not called CC=1 SCAR=6.9
705 0 &$code(@_);
706 0 };
707 }
708 }
709 }
710  
711
712 51 1;
713  
714 =head1 NAME
715  
716 Jemplate::Directive - Jemplate Code Generating Backend
717  
718 =head1 SYNOPSIS
719  
720 use Jemplate::Directive;
721  
722 =head1 DESCRIPTION
723  
724 Jemplate::Directive is the analog to Template::Directive, which is the
725 module that produces that actual code that templates turn into. The
726 Jemplate version obviously produces JavaScript code rather than Perl.
727 Other than that the two modules are almost exactly the same.
728  
729 =head1 BUGS
730  
731 Unfortunately, some of the code generation seems to happen before
732 Jemplate::Directive gets control. So it currently has heuristical code
733 to rejigger Perl code snippets into JavaScript. This processing needs to
734 happen upstream once I get more clarity on how Template::Toolkit works.
735  
736 =head1 AUTHOR
737  
738 Ingy döt Net <ingy@cpan.org>
739  
740 =head1 COPYRIGHT
741  
742 Copyright (c) 2006-2008. Ingy döt Net. All rights reserved.
743  
744 This program is free software; you can redistribute it and/or modify it
745 under the same terms as Perl itself.
746  
747 See L<http://www.perl.com/perl/misc/Artistic.html>
748  
749 =cut
« blib/lib/Jemplate.pm ↑ Summary blib/lib/Jemplate/Directive.pm »