blib/lib/Jemplate/Directive.pm untested

filter: Statementstmt 0.0% 0 / 217 Branchbran 0.0% 0 / 68 Conditioncond 0.0% 0 / 29 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 41 Podpod 0.0% 0 / 36 totaltotal 0.0% 0 / 391 SCAR 70.8
CC
34
Cov
0%
ident
30.0
assign
20.0
filter
20.0
CRAP 1190.0
1 package Jemplate::Directive;
2 use strict;
3 use warnings;
4  
5 our $OUTPUT = 'output +=';
6 our $WHILE_MAX = 1000;
7  
8 # parser state variable
9 # only true when inside JAVASCRIPT blocks
10 our $INJAVASCRIPT = 0;
11  
12 sub template {
13 my ($class, $block) = @_;
14  
15 return "function() { return ''; }" unless $block =~ /\S/;
16  
17 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 my $val = shift;
39 return $val unless
40 my ( $from, $to ) = $val =~ m/\s*\[\s*(\S+)\s*\.\.\s*(\S+)\s*\]/;
41  
42 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+$/;
43  
44 return join '', '[', join( ',', $from .. $to ), ']';
45 }
46  
47 #------------------------------------------------------------------------
48 # textblock($text)
49 #------------------------------------------------------------------------
50  
51 sub textblock {
52 my ($class, $text) = @_;
53 return $text if $INJAVASCRIPT;
54 return "$OUTPUT " . $class->text($text) . ';';
55 }
56  
57 #------------------------------------------------------------------------
58 # text($text)
59 #------------------------------------------------------------------------
60  
61 sub text {
62 my ($class, $text) = @_;
63 for ($text) {
64 s/([\'\\])/\\$1/g;
65 s/\n/\\n/g;
66 s/\r/\\r/g;
67 }
68 return "'" . $text . "'";
69 }
70  
71 #------------------------------------------------------------------------
72 # ident(\@ident) foo.bar(baz)
73 #------------------------------------------------------------------------
74  
75 sub ident {
76 my ($class, $ident) = @_;
77 return "''" unless @$ident;
78 my $ns;
79  
80 # does the first element of the identifier have a NAMESPACE
81 # handler defined?
82 if (ref $class && @$ident > 2 && ($ns = $class->{ NAMESPACE })) {
83 my $key = $ident->[0];
84 $key =~ s/^'(.+)'$/$1/s;
85 if ($ns = $ns->{ $key }) {
86 return $ns->ident($ident);
87 }
88 }
89  
90 if (scalar @$ident <= 2 && ! $ident->[1]) {
91 $ident = $ident->[0];
92 }
93 else {
94 $ident = '[' . join(', ', @$ident) . ']';
95 }
96 return "stash.get($ident)";
97 }
98  
99  
100 #------------------------------------------------------------------------
101 # assign(\@ident, $value, $default) foo = bar
102 #------------------------------------------------------------------------
103  
104 sub assign {
105 my ($class, $var, $val, $default) = @_;
106  
107 if (ref $var) {
108 if (scalar @$var == 2 && ! $var->[1]) {
109 $var = $var->[0];
110 }
111 else {
112 $var = '[' . join(', ', @$var) . ']';
113 }
114 }
115 $val = _attempt_range_expand_val $val;
116 $val .= ', 1' if $default;
117 return "stash.set($var, $val)";
118 }
119  
120  
121 #------------------------------------------------------------------------
122 # args(\@args) foo, bar, baz = qux
123 #------------------------------------------------------------------------
124  
125 sub args {
126 my ($class, $args) = @_;
127 my $hash = shift @$args;
128 push(@$args, '{ ' . join(', ', @$hash) . ' }')
129 if @$hash;
130  
131 return '[]' unless @$args;
132 return '[ ' . join(', ', @$args) . ' ]';
133 }
134  
135  
136 #------------------------------------------------------------------------
137 # filenames(\@names)
138 #------------------------------------------------------------------------
139
140 sub filenames {
141 my ($class, $names) = @_;
142 if (@$names > 1) {
143 $names = '[ ' . join(', ', @$names) . ' ]';
144 }
145 else {
146 $names = shift @$names;
147 }
148 return $names;
149 }
150
151
152 #------------------------------------------------------------------------
153 # get($expr) [% foo %]
154 #------------------------------------------------------------------------
155  
156 sub get {
157 my ($class, $expr) = @_;
158 return "$OUTPUT $expr;";
159 }
160  
161 sub block {
162 my ($class, $block) = @_;
163 return join "\n", map {
164 s/^#(?=line \d+)/\/\//gm;
165 $_;
166 } @{ $block || [] };
167 }
168  
169 #------------------------------------------------------------------------
170 # call($expr) [% CALL bar %]
171 #------------------------------------------------------------------------
172  
173 sub call {
174 my ($class, $expr) = @_;
175 $expr .= ';';
176 return $expr;
177 }
178  
179  
180 #------------------------------------------------------------------------
181 # set(\@setlist) [% foo = bar, baz = qux %]
182 #------------------------------------------------------------------------
183  
184 sub set {
185 my ($class, $setlist) = @_;
186 my $output;
187 while (my ($var, $val) = splice(@$setlist, 0, 2)) {
188 $output .= $class->assign($var, $val) . ";\n";
189 }
190 chomp $output;
191 return $output;
192 }
193  
194  
195 #------------------------------------------------------------------------
196 # default(\@setlist) [% DEFAULT foo = bar, baz = qux %]
197 #------------------------------------------------------------------------
198  
199 sub default {
200 my ($class, $setlist) = @_;
201 my $output;
202 while (my ($var, $val) = splice(@$setlist, 0, 2)) {
203 $output .= &assign($class, $var, $val, 1) . ";\n";
204 }
205 chomp $output;
206 return $output;
207 }
208  
209  
210 #------------------------------------------------------------------------
211 # include(\@nameargs) [% INCLUDE template foo = bar %]
212 # # => [ [ $file, ... ], \@args ]
213 #------------------------------------------------------------------------
214  
215 sub include {
216 my ($class, $nameargs) = @_;
217 my ($file, $args) = @$nameargs;
218 my $hash = shift @$args;
219 $file = $class->filenames($file);
220 $file .= @$hash ? ', { ' . join(', ', @$hash) . ' }' : '';
221 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 my ($class, $nameargs) = @_;
232 my ($file, $args) = @$nameargs;
233 my $hash = shift @$args;
234 $file = $class->filenames($file);
235 $file .= @$hash ? ', { ' . join(', ', @$hash) . ' }' : '';
236 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 my ($class, $expr, $block, $else) = @_;
250 my @else = $else ? @$else : ();
251 $else = pop @else;
252  
253 my $output = "if ($expr) {\n$block\n}\n";
254
255 foreach my $elsif (@else) {
256 ($expr, $block) = @$elsif;
257 $output .= "else if ($expr) {\n$block\n}\n";
258 }
259 if (defined $else) {
260 $output .= "else {\n$else\n}\n";
261 }
262  
263 return $output;
264 }
265  
266 #------------------------------------------------------------------------
267 # foreach($target, $list, $args, $block) [% FOREACH x = [ foo bar ] %]
268 # ...
269 # [% END %]
270 #------------------------------------------------------------------------
271  
272 sub foreach {
273 my ($class, $target, $list, $args, $block) = @_;
274 $args = shift @$args;
275 $args = @$args ? ', { ' . join(', ', @$args) . ' }' : '';
276  
277 my ($loop_save, $loop_set, $loop_restore, $setiter);
278 if ($target) {
279 $loop_save =
280 'try { oldloop = ' . $class->ident(["'loop'"]) . ' } finally {}';
281 $loop_set = "stash.data['$target'] = value";
282 $loop_restore = "stash.set('loop', oldloop)";
283 }
284 else {
285 die "XXX - Not supported yet";
286 $loop_save = 'stash = context.localise()';
287 $loop_set =
288 "stash.get(['import', [value]]) if typeof(value) == 'object'";
289 $loop_restore = 'stash = context.delocalise()';
290 }
291  
292 $list = _attempt_range_expand_val $list;
293  
294 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 return <<EOF;
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 my ($class, $nameargs, $block) = @_;
345 my ($file, $args) = @$nameargs;
346 my $hash = shift @$args;
347  
348 s/ => /: / for @$hash;
349 return $class->multi_wrapper($file, $hash, $block)
350 if @$file > 1;
351 $file = shift @$file;
352 push(@$hash, "'content': output");
353 $file .= @$hash ? ', { ' . join(', ', @$hash) . ' }' : '';
354  
355 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 my ($class, $file, $hash, $block) = @_;
368  
369 push(@$hash, "'content': output");
370 $hash = @$hash ? ', { ' . join(', ', @$hash) . ' }' : '';
371  
372 $file = join(', ', reverse @$file);
373 # print STDERR "multi wrapper: $file\n";
374  
375 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 my ($class, $expr, $block) = @_;
399
400 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 my ( $class, $javascript ) = @_;
419 return $javascript;
420 }
421  
422 sub no_javascript {
423 my ( $class ) = @_;
424 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 my ($class, $expr, $case) = @_;
436 my @case = @$case;
437 my ($match, $block, $default);
438 my $caseblock = '';
439  
440 $default = pop @case;
441  
442 foreach $case (@case) {
443 $match = $case->[0];
444 $block = $case->[1];
445 # $block = pad($block, 1) if $PRETTY;
446 $caseblock .= <<EOF;
447 case $match:
448 $block
449 break;
450  
451 EOF
452 }
453  
454 if (defined $default) {
455 $caseblock .= <<EOF;
456 default:
457 $default
458 break;
459 EOF
460 }
461 # $caseblock = pad($caseblock, 2) if $PRETTY;
462  
463 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 my ($class, $nameargs) = @_;
480 my ($type, $args) = @$nameargs;
481 my $hash = shift(@$args);
482 my $info = shift(@$args);
483 $type = shift @$type;
484  
485 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 return "output = '';";
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 return 'break;';
508 }
509
510 #------------------------------------------------------------------------
511 # return() [% RETURN %]
512 #------------------------------------------------------------------------
513  
514 sub return {
515 return "return output;"
516 }
517  
518  
519 #------------------------------------------------------------------------
520 # stop() [% STOP %]
521 #------------------------------------------------------------------------
522  
523 sub stop {
524 return "throw('Jemplate.STOP\\n' + output);";
525 }
526  
527  
528 #------------------------------------------------------------------------
529 # use(\@lnameargs) [% USE alias = plugin(args) %]
530 # # => [ [$file, ...], \@args, $alias ]
531 #------------------------------------------------------------------------
532  
533 sub use {
534 my ($class, $lnameargs) = @_;
535 my ($file, $args, $alias) = @$lnameargs;
536 $file = shift @$file; # same production rule as INCLUDE
537 $alias ||= $file;
538 $args = &args($class, $args);
539 $file .= ", $args" if $args;
540 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 my ($class, $lnameargs) = @_;
552 my ($file, $args, $alias) = @$lnameargs;
553 $file = shift @$file; # same production rule as INCLUDE
554 $alias ||= $file;
555 $args = &args($class, $args);
556 # $file .= ", $args" if $args;
557 $file =~ s/'|"//g;
558 return "// RAW\n"
559 . "stash.set($alias, $file);";
560 }
561  
562  
563 #------------------------------------------------------------------------
564 # stubs() [% STOP %]
565 #------------------------------------------------------------------------
566  
567 sub filter {
568 my ($class, $lnameargs, $block) = @_;
569 my ($name, $args, $alias) = @$lnameargs;
570 $name = shift @$name;
571 $args = &args($class, $args);
572 $args = $args ? "$args, $alias" : ", null, $alias"
573 if $alias;
574 $name .= ", $args" if $args;
575 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 my $class = shift;
590 if ( @_ && ref($_[0]) ) {
591 return join( " + ", @{$_[0]} );
592 }
593 return "throw('QUOTED called with unknown arguments in Jemplate');";
594 }
595  
596 #------------------------------------------------------------------------
597 # macro($name, $block, \@args)
598 #------------------------------------------------------------------------
599  
600 sub macro {
601 my ($class, $ident, $block, $args) = @_;
602  
603 if ($args) {
604 $args = join(';', map { "args['$_'] = fargs.shift()" } @$args);
605  
606 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 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 my ($class, $name, $block) = @_;
670  
671 if (ref $name) {
672 if (scalar @$name == 2 && ! $name->[1]) {
673 $name = $name->[0];
674 }
675 else {
676 $name = '[' . join(', ', @$name) . ']';
677 }
678 }
679  
680 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 return; # Comment out this line to get callback traces
694 no strict 'refs';
695 my $pkg = __PACKAGE__ . '::';
696 my $stash = \ %$pkg;
697 use strict 'refs';
698 for my $name (keys %$stash) {
699 my $glob = $stash->{$name};
700 if (*$glob{CODE}) {
701 my $code = *$glob{CODE};
702 no warnings 'redefine';
703 $stash->{$name} = sub {
704 warn "Calling $name(@_)\n";
705 &$code(@_);
706 };
707 }
708 }
709 }
710  
711
712 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