lib/Jemplate.pm

filter: Statementstmt 77.7% 140 / 180 Branchbran 63.6% 84 / 132 Conditioncond 56.8% 25 / 44 MC/DCmcdc 25.0% 6 / 24 Subroutinesub 85.0% 17 / 20 Podpod 30.7% 4 / 13 totaltotal 66.8% 276 / 413 SCAR 57.7
CC
92
Cov
70%
get_options
62.7
runtime_source_code
37.2
compile_module
12.0
CRAP 321.8
« blib/lib/Jemplate/Directive.pm ↑ Summary blib/lib/Jemplate/Parser.pm »
1 package Jemplate;
2 51 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 51 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 51 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 51 ▶use Template 2.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 51 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 VERSION
9  
10 Version 0.25_2
11  
12 =cut
13
14 51 our $VERSION = '0.25_2';
15  
16 51 ▶use Jemplate::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 #-------------------------------------------------------------------------------
19 sub usage {
20 1 ▶ <<'...';
Subroutine usage: called CC=1 SCAR=0.0
Pod undocumented
21 Usage:
22  
23 jemplate --runtime [runtime-opt]
24  
25 jemplate --compile [compile-opt] <template-list>
26  
27 jemplate --runtime [runtime-opt] --compile [compile-opt] <template-list>
28  
29 jemplate --list <template-list>
30  
31 Where "--runtime" and "runtime-opt" can include:
32  
33 --runtime Equivalent to --ajax=ilinsky --json=json2
34 --runtime=standard
35  
36 --runtime=lite Same as --ajax=none --json=none
37 --runtime=jquery Same as --ajax=jquery --json=none
38 --runtime=yui Same as --ajax=yui --json=yui
39 --runtime=legacy Same as --ajax=gregory --json=json2
40  
41 --json By itself, equivalent to --json=json2
42 --json=json2 Include http://www.json.org/json2.js for parsing/stringifying
43 --json=yui Use YUI: YAHOO.lang.JSON (requires external YUI)
44 --json=none Doesn't provide any JSON functionality except a warning
45
46 --ajax By itself, equivalent to --ajax=xhr
47 --ajax=jquery Use jQuery for Ajax get and post (requires external jQuery)
48 --ajax=yui Use YUI: yui/connection/connection.js (requires external YUI)
49 --ajax=xhr Use XMLHttpRequest (will automatically use --xhr=ilinsky if --xhr is not set)
50 --ajax=none Doesn't provide any Ajax functionality except a warning
51  
52 --xhr By itself, equivalent to --xhr=ilinsky
53 --xhr=ilinsky Include http://code.google.com/p/xmlhttprequest/
54 --xhr=gregory Include http://www.scss.com.au/family/andrew/webdesign/xmlhttprequest/
55  
56 --xxx Include XXX and JJJ helper functions
57  
58 --compact Use the YUICompressor compacted version of the runtime
59  
60 Where "compile-opt" can include:
61  
62 --start-tag
63 --end-tag
64 --pre-chomp
65 --post-chomp
66 --trim
67 --any-case
68 --eval
69 --noeval
70 -s, --source
71  
72 For more information use:
73 perldoc jemplate
74 ...
75 }
76  
77 sub main {
78 29 ▶ my $class = shift;
Subroutine main: called CC=9 SCAR=24.0
Pod undocumented
79  
80 29 my @argv = @_;
81  
82 29 my ($template_options, $jemplate_options) = get_options(@argv);
83 28 my ($runtime, $compile, $list) = @$jemplate_options{qw/runtime compile list/};
84  
85 28 ▶ if ($runtime) {
Branch: 1 branch
BranchTrueFalseTotal
if ($runtime) 28 0 28
86 28 print runtime_source_code(@$jemplate_options{qw/runtime ajax json xhr xxx compact/});
87 28 ▶ return unless $compile;
Branch: 1 branch
BranchTrueFalseTotal
unless $compile 27 1 28
88 }
89  
90 1 my $templates = make_file_list(@argv);
91 1 ▶ print_usage_and_exit() unless @$templates;
Branch: 1 branch
BranchTrueFalseTotal
unless @$templates 0 1 1
92  
93 1 ▶ if ($list) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list) 0 1 1
94 0 foreach (@$templates) {
95 0 print STDOUT $_->{short} . "\n";
96 }
97 0 return;
98 }
99  
100 1 ▶ if ($compile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($compile) 1 0 1
101 1 my $jemplate = Jemplate->new(%$template_options);
102 1 print STDOUT $jemplate->_preamble;
103 1 foreach my $template (@$templates) {
104 1 my $content = slurp($template->{full});
105 1 ▶ if ($content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content) 1 0 1
106 print STDOUT $jemplate->compile_template_content(
107 $content,
108 $template->{short},
109 1 );
110 }
111 }
112 0 return;
113 }
114  
115 0 print_usage_and_exit();
116 }
117  
118 sub get_options {
119 29 ▶ local @ARGV = @_;
Subroutine get_options: called CC=32 SCAR=41.4
Pod undocumented
120  
121 29 my $runtime;
122 29 my $compile = 0;
123 29 my $list = 0;
124  
125 my $start_tag = exists $ENV{JEMPLATE_START_TAG}
126 ? $ENV{JEMPLATE_START_TAG}
127 29 ▶ : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $ENV{'JEMPLATE_START_TAG'} ? : 0 29 29
128 my $end_tag = exists $ENV{JEMPLATE_END_TAG}
129 ? $ENV{JEMPLATE_END_TAG}
130 29 ▶ : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $ENV{'JEMPLATE_END_TAG'} ? : 0 29 29
131 my $pre_chomp = exists $ENV{JEMPLATE_PRE_CHOMP}
132 ? $ENV{JEMPLATE_PRE_CHOMP}
133 29 ▶ : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $ENV{'JEMPLATE_PRE_CHOMP'} ? : 0 29 29
134 my $post_chomp = exists $ENV{JEMPLATE_POST_CHOMP}
135 ? $ENV{JEMPLATE_POST_CHOMP}
136 29 ▶ : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $ENV{'JEMPLATE_POST_CHOMP'} ? : 0 29 29
137 my $trim = exists $ENV{JEMPLATE_TRIM}
138 ? $ENV{JEMPLATE_TRIM}
139 29 ▶ : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $ENV{'JEMPLATE_TRIM'} ? : 0 29 29
140 my $anycase = exists $ENV{JEMPLATE_ANYCASE}
141 ? $ENV{JEMPLATE_ANYCASE}
142 29 ▶ : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $ENV{'JEMPLATE_ANYCASE'} ? : 0 29 29
143 my $eval_javascript = exists $ENV{JEMPLATE_EVAL_JAVASCRIPT}
144 ? $ENV{JEMPLATE_EVAL_JAVASCRIPT}
145 29 ▶ : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $ENV{'JEMPLATE_EVAL_JAVASCRIPT'} ? : 0 29 29
146  
147 29 my $source = 0;
148 29 my ($ajax, $json, $xxx, $xhr, $compact, $minify);
149  
150 29 my $help = 0;
151  
152 29 ▶ GetOptions(
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("compile|c", \$compile, "list|l", \$list, "runtime|r:s", \$runtime, "start-tag=s", \$start_tag, "end-tag=s", \$end_tag, "trim=s", \$trim, "pre-chomp", \$pre_chomp, "post-chomp", \$post_chomp, "any-case", \$anycase, "eval!", \$eval_javascript, "source|s", \$source, "ajax:s", \$ajax, "json:s", \$json, "xxx", \$xxx, "xhr:s", \$xhr, "compact", \$compact, "minify:s", \$minify, "help|?", \$help) or print_usage_and_exit() 0 29 29
153 "compile|c" => \$compile,
154 "list|l" => \$list,
155 "runtime|r:s" => \$runtime,
156  
157 "start-tag=s" => \$start_tag,
158 "end-tag=s" => \$end_tag,
159 "trim=s" => \$trim,
160 "pre-chomp" => \$pre_chomp,
161 "post-chomp" => \$post_chomp,
162 "any-case" => \$anycase,
163 "eval!" => \$eval_javascript,
164  
165 "source|s" => \$source,
166  
167 "ajax:s" => \$ajax,
168 "json:s" => \$json,
169 "xxx" => \$xxx,
170 "xhr:s" => \$xhr,
171  
172 "compact" => \$compact,
173 "minify:s" => \$minify,
174  
175 "help|?" => \$help,
176 ) or print_usage_and_exit();
177  
178 29 ▶ if ($help) {
Branch: 1 branch
BranchTrueFalseTotal
if ($help) 0 29 29
179 0 print_usage_and_exit();
180 }
181  
182 29 ▶ ($runtime, $ajax, $json, $xxx, $xhr, $minify) = map { defined $_ && ! length $_ ? 1 : $_ } ($runtime, $ajax, $json, $xxx, $xhr, $minify);
Branch: 1 branch
BranchTrueFalseTotal
defined $_ && !length($_) ? : 10 164 174
Condition100%3/3
defined $_ and not length $_
!ll&&!rl&&r
1234110
MC/DC100%2/2
defined $_ and not length $_
defined $_ not length $_
Truth table100%3/3
A and not(B)
A= defined $_B= not length $_
ABresult
0X0
100
111
183 29 ▶ $runtime = "standard" if $runtime && $runtime eq 1;
Branch: 1 branch
BranchTrueFalseTotal
if $runtime and $runtime eq 1 6 23 29
Condition100%3/3
$runtime and $runtime eq 1
!ll&&!rl&&r
1226
MC/DC100%2/2
$runtime and $runtime eq 1
$runtime $runtime eq 1
Truth table100%3/3
A and B
A= $runtimeB= $runtime eq 1
ABresult
0X0
100
111
184  
185 29 ▶ print_usage_and_exit("Don't understand '--runtime $runtime'") if defined $runtime && ! grep { $runtime =~ m/$_/ } qw/standard lite jquery yui legacy/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $runtime and not grep({$runtime =~ /$_/;} "standard", "lite", "jquery", "yui", "legacy") 0 29 29
Condition66%2/3
defined $runtime and not grep({$runtime =~ /$_/;} "standard", "lite", "jquery", "yui", "legacy")
!ll&&!rl&&r
1280
MC/DC0%0/2
defined $runtime and not grep({$runtime =~ /$_/;} "standard", "lite", "jquery", "yui", "legacy")
defined $runtime not grep({$runtime =~ /$_/;} "standard", "lite", "jquery", "yui", "legacy")
Truth table66%2/3
A and not(B)
A= defined $runtimeB= not grep({$runtime =~ /$_/;} "standard", "lite", "jquery", "yui", "legacy")
ABresult
0X0
100
111
186 29 ▶ print_usage_and_exit("Can't specify --list with a --runtime and/or the --compile option") if $list && ($runtime || $compile);
Branch: 1 branch
BranchTrueFalseTotal
if $list and $runtime || $compile 0 29 29
Condition16%1/6
$list and $runtime || $compile
!ll&&!rl&&r
2900
$runtime or $compile
l!l&&r!l&&!r
000
MC/DC0%0/3
$list and $runtime || $compile
$list $runtime $compile
Truth table25%1/4
A and B or C
A= $listB= $runtimeC= $compile
ABCresult
0XX0
1000
11X1
1011
187 29 ▶ print_usage_and_exit() unless $list || $runtime || $compile;
Branch: 1 branch
BranchTrueFalseTotal
unless $list or $runtime or $compile 1 28 29
Condition66%4/6
$list or $runtime or $compile
l!l&&r!l&&!r
2801
$list or $runtime
l!l&&r!l&&!r
0281
MC/DC33%1/3
$list or $runtime or $compile
$list $runtime $compile
Truth table50%2/4
A or B or C
A= $listB= $runtimeC= $compile
ABCresult
1XX1
01X1
0011
0000
188  
189 28 ▶ my $command =
Branch: 3 branches
BranchTrueFalseTotal
$runtime ? : 28 0 28
$compile ? : 0 0 0
$list ? : 0 0 0
190 $runtime ? 'runtime' :
191 $compile ? 'compile' :
192 $list ? 'list' :
193 print_usage_and_exit();
194  
195 28 my $options = {};
196 28 ▶ $options->{START_TAG} = $start_tag if defined $start_tag;
Branch: 1 branch
BranchTrueFalseTotal
if defined $start_tag 0 28 28
197 28 ▶ $options->{END_TAG} = $end_tag if defined $end_tag;
Branch: 1 branch
BranchTrueFalseTotal
if defined $end_tag 0 28 28
198 28 ▶ $options->{PRE_CHOMP} = $pre_chomp if defined $pre_chomp;
Branch: 1 branch
BranchTrueFalseTotal
if defined $pre_chomp 0 28 28
199 28 ▶ $options->{POST_CHOMP} = $post_chomp if defined $post_chomp;
Branch: 1 branch
BranchTrueFalseTotal
if defined $post_chomp 0 28 28
200 28 ▶ $options->{TRIM} = $trim if defined $trim;
Branch: 1 branch
BranchTrueFalseTotal
if defined $trim 0 28 28
201 28 ▶ $options->{ANYCASE} = $anycase if defined $anycase;
Branch: 1 branch
BranchTrueFalseTotal
if defined $anycase 0 28 28
202 28 ▶ $options->{EVAL_JAVASCRIPT} = $eval_javascript if defined $eval_javascript;
Branch: 1 branch
BranchTrueFalseTotal
if defined $eval_javascript 28 0 28
203  
204 return (
205 28 $options,
206 { compile => $compile, runtime => $runtime, list => $list,
207 source => $source,
208 ajax => $ajax, json => $json, xxx => $xxx, xhr => $xhr,
209 compact => $compact, minify => $minify },
210 );
211 }
212  
213  
214 sub slurp {
215 1 ▶ my $filepath = shift;
Subroutine slurp: called CC=2 SCAR=7.0
Pod undocumented
216 1 ▶ open(F, '<', $filepath) or die "Can't open '$filepath' for input:\n$!";
Branch: 1 branch
BranchTrueFalseTotal
open F, "<", $filepath or die "Can't open '${filepath}' for input:\n$!" 0 1 1
217 1 my $contents = do {local $/; <F>};
218 1 close(F);
219 1 return $contents;
220 }
221  
222 sub recurse_dir {
223 2 ▶ require File::Find::Rule;
Subroutine recurse_dir: called CC=3 SCAR=11.0
Pod undocumented
224  
225 2 my $dir = shift;
226 2 my @files;
227 2 foreach ( File::Find::Rule->file->in( $dir ) ) {
228 7 ▶ if ( m{/\.[^\.]+} ) {} # Skip ".hidden" files or directories
Branch: 1 branch
BranchTrueFalseTotal
if (m[/\.[^\.]+]) { } 4 3 7
229 else {
230 3 push @files, $_;
231 }
232 }
233 2 return @files;
234 }
235  
236 sub make_file_list {
237 1 ▶ my @args = @_;
Subroutine make_file_list: called CC=7 SCAR=21.8
Pod undocumented
238  
239 1 my @list;
240  
241 1 foreach my $arg (@args) {
242 3 ▶ unless (-e $arg) { next; } # file exists
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $arg) 2 1 3
243 1 ▶ unless (-s $arg or -d $arg) { next; } # file size > 0 or directory (for Win platform)
Branch: 1 branch
BranchTrueFalseTotal
unless (-s $arg or -d $arg) 0 1 1
Condition33%1/3
-s $arg or -d $arg
l!l&&r!l&&!r
100
MC/DC0%0/2
-s $arg or -d $arg
-s $arg -d $arg
Truth table33%1/3
A or B
A= -s $argB= -d $arg
ABresult
1X1
011
000
244  
245 1 ▶ if (-d $arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $arg) { } 1 0 1
246 1 foreach my $full ( recurse_dir($arg) ) {
247 2 $full =~ /$arg(\/|)(.*)/;
248 2 my $short = $2;
249 2 push(@list, {full=>$full, short=>$short} );
250 }
251 }
252 else {
253 0 my $full = $arg;
254 0 my $short = $full;
255 0 $short =~ s/.*[\/\\]//;
256 0 push(@list, {full=>$arg, short=>$short} );
257 }
258 }
259  
260 1 return [ sort { $a->{short} cmp $b->{short} } @list ];
261 }
262  
263 sub print_usage_and_exit {
264 1 ▶ print STDOUT join "\n", "", @_, "Aborting!", "\n" if @_;
Subroutine print_usage_and_exit: called CC=2 SCAR=7.1
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 1 1
265 1 print STDOUT usage();
266 1 exit;
267 }
268  
269 sub runtime_source_code {
270 28 ▶ require Jemplate::Runtime;
Subroutine runtime_source_code: called CC=34 SCAR=36.2
Pod undocumented
271 28 require Jemplate::Runtime::Compact;
272  
273 28 ▶ unshift @_, "standard" unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 28 28
274  
275 28 ▶ my ($runtime, $ajax, $json, $xhr, $xxx, $compact) = map { defined $_ ? lc $_ : "" } @_[0 .. 5];
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 51 117 168
276  
277 28 ▶ my $Jemplate_Runtime = $compact ? "Jemplate::Runtime::Compact" : "Jemplate::Runtime";
Branch: 1 branch
BranchTrueFalseTotal
$compact ? : 0 28 28
278  
279 28 ▶ if ($runtime eq "standard") {
Branch: 5 branches
BranchTrueFalseTotal
if ($runtime eq 'standard') { } 8 20 28
elsif ($runtime eq 'jquery') { } 2 18 20
elsif ($runtime eq 'yui') { } 5 13 18
elsif ($runtime eq 'legacy') { } 3 10 13
elsif ($runtime eq 'lite') { } 10 0 10
280 8 ▶ $ajax ||= "xhr";
Condition50%1/2
$ajax ||= "xhr"
l!l
08
MC/DC0%0/1
$ajax ||= "xhr"
$ajax
Truth table50%1/2
A
A= $ajax
Aresult
11
00
281 8 ▶ $json ||= "json2";
Condition50%1/2
$json ||= "json2"
l!l
08
MC/DC0%0/1
$json ||= "json2"
$json
Truth table50%1/2
A
A= $json
Aresult
11
00
282 8 ▶ $xhr ||= "ilinsky";
Condition50%1/2
$xhr ||= "ilinsky"
l!l
08
MC/DC0%0/1
$xhr ||= "ilinsky"
$xhr
Truth table50%1/2
A
A= $xhr
Aresult
11
00
283 }
284 elsif ($runtime eq "jquery") {
285 2 ▶ $ajax ||= "jquery";
Condition50%1/2
$ajax ||= "jquery"
l!l
02
MC/DC0%0/1
$ajax ||= "jquery"
$ajax
Truth table50%1/2
A
A= $ajax
Aresult
11
00
286 }
287 elsif ($runtime eq "yui") {
288 5 ▶ $ajax ||= "yui";
Condition50%1/2
$ajax ||= "yui"
l!l
05
MC/DC0%0/1
$ajax ||= "yui"
$ajax
Truth table50%1/2
A
A= $ajax
Aresult
11
00
289 5 ▶ $json ||= "yui";
Condition50%1/2
$json ||= "yui"
l!l
05
MC/DC0%0/1
$json ||= "yui"
$json
Truth table50%1/2
A
A= $json
Aresult
11
00
290 }
291 elsif ($runtime eq "legacy") {
292 3 ▶ $ajax ||= "xhr";
Condition50%1/2
$ajax ||= "xhr"
l!l
03
MC/DC0%0/1
$ajax ||= "xhr"
$ajax
Truth table50%1/2
A
A= $ajax
Aresult
11
00
293 3 ▶ $json ||= "json2";
Condition50%1/2
$json ||= "json2"
l!l
03
MC/DC0%0/1
$json ||= "json2"
$json
Truth table50%1/2
A
A= $json
Aresult
11
00
294 3 ▶ $xhr ||= "gregory";
Condition50%1/2
$xhr ||= "gregory"
l!l
03
MC/DC0%0/1
$xhr ||= "gregory"
$xhr
Truth table50%1/2
A
A= $xhr
Aresult
11
00
295 3 $xxx = 1;
296 }
297 elsif ($runtime eq "lite") {
298 }
299  
300 28 ▶ $ajax = "xhr" if $ajax eq 1;
Branch: 1 branch
BranchTrueFalseTotal
if $ajax eq 1 2 26 28
301 28 ▶ $xhr ||= 1 if $ajax eq "xhr";
Branch: 1 branch
BranchTrueFalseTotal
if $ajax eq "xhr" 17 11 28
Condition100%2/2
$xhr ||= 1
l!l
152
MC/DC100%1/1
$xhr ||= 1
$xhr
Truth table100%2/2
A
A= $xhr
Aresult
11
00
302 28 ▶ $json = "json2" if $json eq 1;
Branch: 1 branch
BranchTrueFalseTotal
if $json eq 1 2 26 28
303 28 ▶ $xhr = "ilinsky" if $xhr eq 1;
Branch: 1 branch
BranchTrueFalseTotal
if $xhr eq 1 2 26 28
304  
305 28 my @runtime;
306  
307 28 ▶ push @runtime, $Jemplate_Runtime->kernel if $runtime;
Branch: 1 branch
BranchTrueFalseTotal
if $runtime 28 0 28
308  
309 28 ▶ push @runtime, $Jemplate_Runtime->json2 if $json =~ m/^json2?$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $json =~ /^json2?$/i 16 12 28
310
311 28 ▶ push @runtime, $Jemplate_Runtime->ajax_xhr if $ajax eq "xhr";
Branch: 1 branch
BranchTrueFalseTotal
if $ajax eq "xhr" 17 11 28
312 28 ▶ push @runtime, $Jemplate_Runtime->ajax_jquery if $ajax eq "jquery";
Branch: 1 branch
BranchTrueFalseTotal
if $ajax eq "jquery" 3 25 28
313 28 ▶ push @runtime, $Jemplate_Runtime->ajax_yui if $ajax eq "yui";
Branch: 1 branch
BranchTrueFalseTotal
if $ajax eq "yui" 6 22 28
314  
315 28 ▶ push @runtime, $Jemplate_Runtime->json_json2 if $json =~ m/^json2?$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $json =~ /^json2?$/i 16 12 28
316 28 ▶ push @runtime, $Jemplate_Runtime->json_json2_internal if $json =~ m/^json2?[_-]?internal$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $json =~ /^json2?[_-]?internal$/i 0 28 28
317 28 ▶ push @runtime, $Jemplate_Runtime->json_yui if $json eq "yui";
Branch: 1 branch
BranchTrueFalseTotal
if $json eq "yui" 6 22 28
318  
319 28 ▶ push @runtime, $Jemplate_Runtime->xhr_ilinsky if $xhr eq "ilinsky";
Branch: 1 branch
BranchTrueFalseTotal
if $xhr eq "ilinsky" 14 14 28
320 28 ▶ push @runtime, $Jemplate_Runtime->xhr_gregory if $xhr eq "gregory";
Branch: 1 branch
BranchTrueFalseTotal
if $xhr eq "gregory" 6 22 28
321  
322 28 ▶ push @runtime, $Jemplate_Runtime->xxx if $xxx;
Branch: 1 branch
BranchTrueFalseTotal
if $xxx 5 23 28
323  
324 28 return join ";", @runtime;
325 }
326  
327 #-------------------------------------------------------------------------------
328  
329 sub new {
330 2 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
331 2 return bless { @_ }, $class;
332 }
333  
334 sub compile_module {
335 0 ▶ my ($self, $module_path, $template_file_paths) = @_;
Subroutine compile_module: not called CC=3 SCAR=24.8
Pod documented
336 0 ▶ my $result = $self->compile_template_files(@$template_file_paths)
Branch: 1 branch
BranchTrueFalseTotal
my $result = $self->compile_template_files(@$template_file_paths) or return 0 0 0
337 or return;
338 0 ▶ open MODULE, "> $module_path"
Branch: 1 branch
BranchTrueFalseTotal
open MODULE, "> $module_path" or die "Can't open '${module_path}' for output:\n$!" 0 0 0
339 or die "Can't open '$module_path' for output:\n$!";
340 0 print MODULE $result;
341 0 close MODULE;
342 0 return 1;
343 }
344  
345 sub compile_module_cached {
346 0 ▶ my ($self, $module_path, $template_file_paths) = @_;
Subroutine compile_module_cached: not called CC=2 SCAR=17.9
Pod documented
347 0 my $m = -M $module_path;
348 0 ▶ return 0 unless grep { -M($_) < $m } @$template_file_paths;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {-M $_ < $m;} @$template_file_paths 0 0 0
349 0 return $self->compile_module($module_path, $template_file_paths);
350 }
351  
352 sub compile_template_files {
353 0 ▶ my $self = shift;
Subroutine compile_template_files: not called CC=3 SCAR=24.8
Pod documented
354 0 my $output = $self->_preamble;
355 0 for my $filepath (@_) {
356 0 my $filename = $filepath;
357 0 $filename =~ s/.*[\/\\]//;
358 0 ▶ open FILE, $filepath
Branch: 1 branch
BranchTrueFalseTotal
open FILE, $filepath or die "Can't open '${filepath}' for input:\n$!" 0 0 0
359 or die "Can't open '$filepath' for input:\n$!";
360 0 my $template_input = do {local $/; <FILE>};
361 0 close FILE;
362 0 $output .=
363 $self->compile_template_content($template_input, $filename);
364 }
365 0 return $output;
366 }
367  
368 sub compile_template_content {
369 6 ▶ die "Invalid arguments in call to Jemplate->compile_template_content"
Subroutine compile_template_content: called CC=5 SCAR=23.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 3 0 6 6
370 unless @_ == 3;
371 6 my ($self, $template_content, $template_name) = @_;
372 6 ▶ my $parser = Jemplate::Parser->new( ref($self) ? %$self : () );
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 2 4 6
373 0 ▶ my $parse_tree = $parser->parse(
Branch: 1 branch
BranchTrueFalseTotal
my $parse_tree = $parser->parse($template_content, {"name", $template_name}) or die $parser->error 0 0 0
374 $template_content, {name => $template_name}
375 ) or die $parser->error;
376 my $output =
377 "Jemplate.templateMap['$template_name'] = " .
378 $parse_tree->{BLOCK} .
379 0 "\n";
380 0 for my $function_name (sort keys %{$parse_tree->{DEFBLOCKS}}) {
381 $output .=
382 "Jemplate.templateMap['$function_name'] = " .
383 0 $parse_tree->{DEFBLOCKS}{$function_name} .
384 "\n";
385 }
386 0 return $output;
387 }
388  
389 sub _preamble {
390 1 ▶ return <<'...';
Subroutine _preamble: called CC=1 SCAR=0.0
391 /*
392 This JavaScript code was generated by Jemplate, the JavaScript
393 Template Toolkit. Any changes made to this file will be lost the next
394 time the templates are compiled.
395  
396 Copyright 2006-2008 - Ingy döt Net - All rights reserved.
397 */
398  
399 if (typeof(Jemplate) == 'undefined')
400 throw('Jemplate.js must be loaded before any Jemplate template files');
401  
402 ...
403 }
404  
405 51 1;
406  
407 =head1 NAME
408  
409 Jemplate - JavaScript Templating with Template Toolkit
410  
411 =head1 SYNOPSIS
412  
413 var data = Ajax.get('url/data.json');
414 var elem = document.getElementById('some-div');
415 elem.innerHTML = Jemplate.process('my-template.html', data);
416  
417 or:
418  
419 var data = Ajax.get('url/data.json');
420 var elem = document.getElementById('some-div');
421 Jemplate.process('my-template.html', data, elem);
422  
423 or simply:
424  
425 Jemplate.process('my-template.html', 'url/data.json', '#some-div');
426  
427 or, with jQuery.js:
428  
429 jQuery.getJSON("url/data.json", function(data) {
430 Jemplate.process('my-template.html', data, '#some-div');
431 });
432  
433 From the commandline:
434  
435 jemplate --runtime --compile path/to/jemplate/directory/ > jemplate.js
436  
437 =head1 DESCRIPTION
438  
439 Jemplate is a templating framework for JavaScript that is built over
440 Perl's Template Toolkit (TT2).
441  
442 Jemplate parses TT2 templates using the TT2 Perl framework, but with a
443 twist. Instead of compiling the templates into Perl code, it compiles
444 them into JavaScript.
445  
446 Jemplate then provides a JavaScript runtime module for processing
447 the template code. Presto, we have full featured JavaScript
448 templating language!
449  
450 Combined with JSON and xmlHttpRequest, Jemplate provides a really simple
451 and powerful way to do Ajax stuff.
452  
453 =head1 HOWTO
454  
455 Jemplate comes with a command line tool call C<jemplate> that you use to
456 precompile your templates into a JavaScript file. For example if you have
457 a template directory called C<templates> that contains:
458  
459 > ls templates/
460 body.html
461 footer.html
462 header.html
463  
464 You might run this command:
465  
466 > jemplate --compile template/* > js/jemplates.js
467  
468 This will compile all the templates into one JavaScript file.
469  
470 You also need to generate the Jemplate runtime.
471  
472 > jemplate --runtime > js/Jemplate.js
473  
474 Now all you need to do is include these two files in your HTML:
475  
476 <script src="js/Jemplate.js" type="text/javascript"></script>
477 <script src="js/jemplates.js" type="text/javascript"></script>
478  
479 Now you have Jemplate support for these templates in your HTML document.
480  
481 =head1 PUBLIC API
482  
483 The Jemplate.js JavaScript runtime module has the following API method:
484  
485 =over
486  
487 =item Jemplate.process(template-name, data, target);
488  
489 The C<template-name> is a string like C<'body.html'> that is the name of
490 the top level template that you wish to process.
491  
492 The optional C<data> specififies the data object to be used by the
493 templates. It can be an object, a function or a url. If it is an object,
494 it is used directly. If it is a function, the function is called and the
495 returned object is used. If it is a url, an asynchronous <Ajax.get> is
496 performed. The result is expected to be a JSON string, which gets turned
497 into an object.
498  
499 The optional C<target> can be an HTMLElement reference, a function or a
500 string beginning with a C<#> char. If the target is omitted, the
501 template result is returned. If it is a function, the function is called
502 with the result. If it is a string, the string is used as an id to find
503 an HTMLElement.
504  
505 If an HTMLElement is used (by id or directly) then the innerHTML
506 property is set to the template processing result.
507  
508 =back
509  
510 The Jemplate.pm Perl module has the following public class methods,
511 although you won't likely need to use them directly. Normally, you just
512 use the C<jemplate> command line tool.
513  
514 =over
515  
516 =item Jemplate->compile_template_files(@template_file_paths);
517  
518 Take a list of template file paths and compile them into a module of
519 functions. Returns the text of the module.
520  
521 =item Jemplate->compile_template_content($content, $template_name);
522  
523 Compile one template whose content is in memory. You must provide a
524 unique template name. Returns the JavaScript text result of the
525 compilation.
526  
527 =item Jemplate->compile_module($module_path, \@template_file_paths);
528  
529 Similar to `compile_template_files`, but prints to result to the
530 $module_path. Returns 1 if successful, undef if error.
531  
532 =item Jemplate->compile_module_cached($module_path, \@template_file_paths);
533  
534 Similar to `compile_module`, but only compiles if one of the templates
535 is newer than the module. Returns 1 if sucessful compile, 0 if no
536 compile due to cache, undef if error.
537  
538 =back
539  
540 =head1 AJAX AND JSON METHODS
541  
542 Jemplate comes with builtin Ajax and JSON support.
543  
544 =over
545  
546 =item Ajax.get(url, [callback]);
547  
548 Does a GET operation to the url.
549  
550 If a callback is provided, the operation is asynchronous, and the data
551 is passed to the callback. Otherwise, the operation is synchronous and
552 the data is returned.
553  
554 =item Ajax.post(url, data, [callback]);
555  
556 Does a POST operation to the url.
557  
558 Same callback rules as C<get> apply.
559  
560 =item JSON.stringify(object);
561  
562 Return the JSON serialization of an object.
563  
564 =item JSON.parse(jsonString);
565  
566 Turns a JSON string into an object and returns the object.
567  
568 =back
569  
570 =head1 CURRENT SUPPORT
571  
572 The goal of Jemplate is to support all of the Template Toolkit features
573 that can possibly be supported.
574  
575 Jemplate now supports almost all the TT directives, including:
576  
577 * Plain text
578 * [% [GET] variable %]
579 * [% CALL variable %]
580 * [% [SET] variable = value %]
581 * [% DEFAULT variable = value ... %]
582 * [% INCLUDE [arguments] %]
583 * [% PROCESS [arguments] %]
584 * [% BLOCK name %]
585 * [% FILTER filter %] text... [% END %]
586 * [% JAVASCRIPT %] code... [% END %]
587 * [% WRAPPER template [variable = value ...] %]
588 * [% IF condition %]
589 * [% ELSIF condition %]
590 * [% ELSE %]
591 * [% SWITCH variable %]
592 * [% CASE [{value|DEFAULT}] %]
593 * [% FOR x = y %]
594 * [% WHILE expression %]
595 * [% RETURN %]
596 * [% THROW type message %]
597 * [% STOP %]
598 * [% NEXT %]
599 * [% LAST %]
600 * [% CLEAR %]
601 * [%# this is a comment %]
602 * [% MACRO name(param1, param2) BLOCK %] ... [% END %]
603  
604 ALL of the string virtual functions are supported.
605  
606 ALL of the array virtual functions are supported:
607  
608 ALL of the hash virtual functions are supported:
609  
610 MANY of the standard filters are implemented.
611  
612 The remaining features will be added very soon. See the DESIGN document
613 in the distro for a list of all features and their progress.
614  
615 =head1 BROWSER SUPPORT
616  
617 Tested successfully in:
618  
619 * Firefox Mac/Win32/Linux
620 * IE 6.0
621 * Safari
622 * Opera
623 * Konqueror
624  
625 All tests run 100% successful in the above browsers.
626  
627 =head1 DEVELOPMENT
628  
629 The bleeding edge code is available via Git at
630 git://github.com/ingydotnet/jemplate.git
631  
632 You can run the runtime tests directly from
633 http://svn.jemplate.net/repo/trunk/tests/run/index.html or from the
634 corresponding CPAN or JSAN directories.
635  
636 Jemplate development is being discussed at irc://irc.freenode.net/#jemplate
637  
638 If you want a committer bit, just ask ingy on the irc channel.
639  
640 =head1 CREDIT
641  
642 This module is only possible because of Andy Wardley's mighty Template
643 Toolkit. Thanks Andy. I will gladly give you half of any beers I
644 receive for this work. (As long as you are in the same room when I'm
645 drinking them ;)
646  
647 =head1 AUTHORS
648  
649 Ingy döt Net <ingy@cpan.org>
650  
651 (Note: I had to list myself first so that this line would go into META.yml)
652  
653 Jemplate is truly a community authored project:
654  
655 Ingy döt Net <ingy@cpan.org>
656  
657 Tatsuhiko Miyagawa <miyagawa@bulknews.net>
658  
659 Yann Kerherve <yannk@cpan.org>
660  
661 David Davis <xantus@xantus.org>
662  
663 Cory Bennett <coryb@corybennett.org>
664  
665 Cees Hek <ceeshek@gmail.com>
666  
667 Christian Hansen
668  
669 David A. Coffey <dacoffey@cogsmith.com>
670  
671 Robert Krimen <robertkrimen@gmail.com>
672  
673 Nickolay Platonov <nickolay8@gmail.com>
674  
675 =head1 COPYRIGHT
676  
677 Copyright (c) 2006-2008. Ingy döt Net.
678  
679 This program is free software; you can redistribute it and/or modify it
680 under the same terms as Perl itself.
681  
682 See L<http://www.perl.com/perl/misc/Artistic.html>
683  
684 =cut
« blib/lib/Jemplate/Directive.pm ↑ Summary blib/lib/Jemplate/Parser.pm »