Tmojo.pm

filter: Statementstmt 65.4% 292 / 446 Branchbran 56.8% 150 / 264 Conditioncond 70.3% 19 / 27 MC/DCmcdc 42.8% 6 / 14 Subroutinesub 81.8% 18 / 22 Podpod 0.0% 0 / 12 totaltotal 61.7% 485 / 785 CC 147 SCAR 47.5
CC
147
Cov
63%
compile_template
263.3
compile_lite_template
210.0
parse_template
56.2
CRAP 115.2
↑ Summary Tmojo/HttpArgParser.pm »
1 ###########################################################################
2 # Copyright 2003, 2004 Lab-01 LLC <http://lab-01.com/>
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16 # Tmojo(tm) is a trademark of Lab-01 LLC.
17 ###########################################################################
18  
19 package HTML::Tmojo;
20  
21 5 our $VERSION = '0.300';
22  
23 =head1 NAME
24  
25 HTML::Tmojo - Dynamic Text Generation Engine
26  
27 =head1 SYNOPSIS
28  
29 my $tmojo = HTML::Tmojo->new(
30 template_dir => '/location/of/templates',
31 cache_dir => '/place/to/save/compiled/templates',
32 );
33
34 my $result = $tmojo->call('my_template.tmojo', arg1 => 1, arg2 => 3);
35
36 # HONESTLY, THIS SYNOPSIS DOESN'T COVER NEARLY ENOUGH.
37 # GO READ TMOJO IN A NUTSHELL
38  
39 =head1 ABSTRACT
40  
41 Tmojo is used for generating dynamic text documents.
42 While it is particularly suited to generating HTML
43 and XML documents, it can be used effectively to
44 produce any text output, including dynamically
45 generated source code.
46  
47 =head1 AUTHOR
48  
49 Will Conant <will@willconant.com>
50  
51 =cut
52  
53 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 5 ▶use Symbol qw(delete_package);
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 5 ▶use HTML::Tmojo::TemplateLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 5 our %memory_cache;
60  
61 13 ▶sub new {
Subroutine new: called CC=3 SCAR=11.1
Pod undocumented
62 13 my ($class, %args) = @_;
63  
64 13 ▶ if (defined $args{template_dir}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $args{'template_dir'}) { } 5 8 13
elsif (not defined $args{'template_loader'}) { } 0 8 8
65 5 $args{template_loader} = HTML::Tmojo::TemplateLoader->new($args{template_dir}, $args{tmojo_lite});
66 5 delete $args{template_dir};
67 }
68 elsif (not defined $args{template_loader}) {
69 0 $args{template_loader} = HTML::Tmojo::TemplateLoader->new($ENV{TMOJO_TEMPLATE_DIR}, $args{tmojo_lite});
70 }
71
72 %args = (
73 cache_dir => $ENV{TMOJO_CACHE_DIR},
74 13 context_path => '',
75
76 %args,
77 );
78
79 13 $args{cache_dir} =~ s/\/$//;
80
81 13 my $self = {
82 %args
83 };
84
85 13 return bless $self, $class;
86 }
87  
88 8 ▶sub call {
Subroutine call: called CC=1 SCAR=0.0
Pod undocumented
89 8 my ($self, $template_id, %args) = @_;
90 8 return $self->call_with_container($template_id, undef, %args);
91 }
92  
93 8 ▶sub call_with_container {
Subroutine call_with_container: called CC=5 SCAR=16.5
Pod undocumented
94 8 my ($self, $template_id, $container_override_id, %args) = @_;
95
96 8 my $result = eval {
97
98 8 my $current_package = $self->get_template_class($template_id);
99 8 my $current_template = $current_package->new(\%args);
100
101 # WE HAVE TO KEEP TRACK OF WHICH CONTAINERS HAVE BEEN USED,
102 # SO THAT USERS CAN'T CREATE AN INFINITE CONTAINER LOOP
103 8 my %used_containers = (
104 $self->normalize_template_id($template_id) => 1,
105 );
106
107 8 for (;;) {
108 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
109
110 9 my $contextual_tmojo = ${$current_package . '::Tmojo'};
111
112 9 my $container_id;
113 9 ▶ if (defined $container_override_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $container_override_id) { } 0 9 9
114 0 $container_id = $container_override_id;
115 0 $container_override_id = undef;
116 }
117 else {
118 9 $container_id = ${$current_package . '::TMOJO_CONTAINER'};
119 }
120
121 9 ▶ if (defined $container_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $container_id) { } 1 8 9
122 # NORMALIZE THE CONTAINER ID FOR GOOD MEASURE
123 1 $container_id = $contextual_tmojo->normalize_template_id($container_id);
124
125 # CHECK TO MAKE SURE THAT THE CONTAINER HASN'T ALREADY BEEN USED
126 1 ▶ if (defined $used_containers{$container_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $used_containers{$container_id}) 0 1 1
127 0 die "circular container reference, $container_id already used (this will cause an infinite loop)";
128 }
129
130 # PUT IT IN THE USED LIST
131 1 $used_containers{$container_id} = 1;
132
133 # MOVE ON UP
134 1 $current_package = $contextual_tmojo->get_template_class($container_id);
135 1 $current_template = $current_package->new(\%args, $current_template);
136 }
137 else {
138 8 return $current_template->main();
139 }
140 }
141
142 };
143 8 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 8 8
144 0 $self->report_error($@);
145 }
146
147 8 return $result;
148 }
149  
150 0 ▶sub prepare {
Subroutine prepare: not called CC=1 SCAR=6.9
Pod undocumented
151 0 my ($self, $template_id, %args) = @_;
152
153 0 my $package = $self->get_template_class($template_id);
154 0 my $template = $package->new(\%args);
155
156 0 return $template;
157 }
158  
159 0 ▶sub template_exists {
Subroutine template_exists: not called CC=1 SCAR=6.9
Pod undocumented
160 0 my ($self, $template_id) = @_;
161
162 0 $template_id = $self->normalize_template_id($template_id);
163 0 return $self->{template_loader}->template_exists($template_id);
164 }
165  
166 0 ▶sub report_error {
Subroutine report_error: not called CC=6 SCAR=37.4
Pod undocumented
167 0 my ($self, $error) = @_;
168
169 0 my $err = (split(/\n/, $error))[0];
170 0 ▶ if ($err =~ /at ([^\s]+) line\s+(\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err =~ /at ([^\s]+) line\s+(\d+)/) 0 0 0
171 0 my $file_name = $1;
172 0 my $line_number = $2;
173
174 0 my $template_id;
175
176 0 open FH, "$file_name.lines";
177 0 local $/ = "\n"; # THIS CAN GET EXTRA SCREWED UP IN MOD_PERL
178
179 0 my $cur_line = 1;
180 0 ▶ while (my $line = <FH>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline FH)) 0 0 0
181 0 ▶ if ($line =~ /^###TMOJO_TEMPLATE_ID: (.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^###TMOJO_TEMPLATE_ID: (.+)$/) 0 0 0
182 0 $template_id = $1;
183 0 chomp $template_id;
184 }
185
186 0 ▶ if ($cur_line == $line_number) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur_line == $line_number) 0 0 0
187 0 ▶ if ($line =~ /###TMOJO_LINE: (\d+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /###TMOJO_LINE: (\d+)$/) 0 0 0
188 0 die "Error at $template_id line $1.\n$@";
189 }
190 }
191
192 0 $cur_line += 1;
193 }
194 0 close FH;
195 }
196
197 0 die $error;
198 }
199  
200 8 ▶sub parse_template {
Subroutine parse_template: called CC=48 SCAR=40.3
Pod undocumented
201 8 my ($source) = @_;
202
203 8 my @parsed;
204
205 8 my $tag_open = "<:";
206 8 my $tag_close = ":>";
207 8 my $tag_line = ":";
208
209 8 my $tag_open_r;
210 my $tag_close_r;
211 8 my $tag_line_r;
212
213 12 ▶ my $make_regexes = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
214 12 $tag_open_r = $tag_open;
215 12 $tag_close_r = $tag_close;
216 12 $tag_line_r = $tag_line;
217
218 12 $tag_open_r =~ s/([\[\]\{\}\(\)\$\@\^\\\|\?\*\+])/\\$1/g;
219 12 $tag_close_r =~ s/([\[\]\{\}\(\)\$\@\^\\\|\?\*\+])/\\$1/g;
220 12 $tag_line_r =~ s/([\[\]\{\}\(\)\$\@\^\\\|\?\*\+])/\\$1/g;
221 8 };
222
223 73 ▶ my $count_newlines = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
224 73 my $count = 0;
225 73 my $pos = 0;
226
227 73 ▶ while ($pos > -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos > -1) 127 73 200
228 127 $pos = index($_[0], "\n", $pos);
229 127 ▶ if ($pos > -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos > -1) 54 73 127
230 54 $pos += 1;
231 54 $count += 1;
232 }
233 }
234
235 73 return $count;
236 8 };
237
238 8 $make_regexes->();
239
240 8 my $keywords = "GLOBAL|INIT|METHOD|PERL|MERGE|CAPTURE|FILTER|REGEX|NOP|TAG_STYLE";
241 8 my %crush_defaults = (
242 'GLOBAL' => [0, 0],
243 '/GLOBAL' => [0, 2],
244
245 'INIT' => [0, 0],
246 '/INIT' => [0, 2],
247
248 'METHOD' => [0, 2],
249 '/METHOD' => [2, 2],
250
251 'PERL' => [1, 0],
252 '/PERL' => [0, 0],
253
254 'MERGE' => [0, 0],
255
256 'CAPTURE' => [1, 2],
257 '/CAPTURE' => [2, 0],
258
259 'FILTER' => [0, 0],
260 '/FILTER' => [0, 0],
261
262 'REGEX' => [0, 0],
263 '/REGEX' => [0, 0],
264
265 'TAG_STYLE' => [1, 0],
266
267 'NOP' => [0, 0],
268 );
269
270 8 my $current_line = 1;
271  
272 8 ▶ while ($source ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($source ne "") 45 8 53
273
274 # SNAG THE NEXT TAG
275 # -------------------
276
277 45 my $found_tag = 0;
278 45 my $tag_notation;
279 my $pre_tag_text;
280  
281 45 ▶ if (scalar(@parsed) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @parsed == 0) 8 37 45
282 8 ▶ if ($source =~ s/^([ \t]*)$tag_line_r//s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source =~ s/^([ \t]*)$tag_line_r//s) 1 7 8
283 1 $found_tag = 1;
284 1 $tag_notation = 'line';
285 1 $pre_tag_text = $1;
286 }
287 }
288
289 45 ▶ unless ($found_tag == 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found_tag == 1) 44 1 45
290 44 ▶ if ($source =~ s/^(.*?)($tag_open_r|(\n[ \t]*)$tag_line_r)//s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source =~ s/^(.*?)($tag_open_r|(\n[ \t]*)$tag_line_r)//s) 38 6 44
291 38 $found_tag = 1;
292
293 # DETERMINE IF THIS IS A LINE OR INLINE TAG
294 38 ▶ if ($2 eq $tag_open) {
Branch: 1 branch
BranchTrueFalseTotal
if ($2 eq $tag_open) { } 31 7 38
295 31 $tag_notation = 'inline';
296 }
297 else {
298 7 $tag_notation = 'line';
299 }
300
301 # DETERMINE THE PRE TAG TEXT
302 38 $pre_tag_text = $1;
303 38 ▶ if ($tag_notation eq 'line') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag_notation eq "line") 7 31 38
304 7 $pre_tag_text .= $3;
305 }
306 }
307 }
308
309 45 ▶ if ($found_tag == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($found_tag == 1) { } 39 6 45
elsif ($source ne '') { } 6 0 6
310
311 39 ▶ if ($pre_tag_text ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($pre_tag_text ne "") 34 5 39
312 # PUSH PLAIN TEXT ONTO THE PARSED RESULT
313 34 push @parsed, { type => 'TEXT', text => $pre_tag_text, source => $pre_tag_text, crush_before => 0, crush_after => 0, start_line => $current_line };
314
315 # COUNT THE NUMBER OF NEWLINES
316 34 $current_line += $count_newlines->($pre_tag_text);
317 }
318
319 # GRAB THE REST OF THE TAG
320 39 my $tag_source;
321 my $tag_inside;
322
323 39 ▶ if ($tag_notation eq 'inline') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag_notation eq 'inline') { } 31 8 39
324 31 $tag_source = $tag_line;
325
326 31 ▶ if ($source =~ s/^(.*?)$tag_close_r//s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source =~ s/^(.*?)$tag_close_r//s) { } 31 0 31
327 31 $tag_inside = $1;
328 31 $tag_source .= "$1$tag_close";
329 }
330 else {
331 0 die "expected '$tag_close'";
332 }
333 }
334 else {
335 8 $tag_source = $tag_open;
336
337 # GOBBLE UP THE REST OF THE LINE
338 8 $source =~ s/^([^\n]*)//;
339 8 $tag_inside = $1;
340 8 $tag_source .= $1;
341 }
342
343 # NOTCH UP THE LINES
344 39 $current_line += $count_newlines->($tag_source);
345
346 # PARSE THE TAG INSIDES
347
348 39 my %tag = (
349 source => $tag_source,
350 start_line => $current_line,
351 );
352
353 # LOOK FOR WHITESPACE CRUSHERS
354
355 39 ▶ if ($tag_notation eq 'inline') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag_notation eq "inline") 31 8 39
356 31 ▶ if ($tag_inside =~ s/^--//) {
Branch: 3 branches
BranchTrueFalseTotal
if ($tag_inside =~ s/^--//) { } 2 29 31
elsif ($tag_inside =~ s/^-//) { } 0 29 29
elsif ($tag_inside =~ s/^\+//) { } 0 29 29
357 2 $tag{crush_before} = 2;
358 }
359 elsif ($tag_inside =~ s/^-//) {
360 0 $tag{crush_before} = 1;
361 }
362 elsif ($tag_inside =~ s/^\+//) {
363 0 $tag{crush_before} = 0;
364 }
365
366 31 ▶ if ($tag_inside =~ s/--$//) {
Branch: 3 branches
BranchTrueFalseTotal
if ($tag_inside =~ s/--$//) { } 1 30 31
elsif ($tag_inside =~ s/-$//) { } 1 29 30
elsif ($tag_inside =~ s/\+$//) { } 0 29 29
367 1 $tag{crush_after} = 2;
368 }
369 elsif ($tag_inside =~ s/-$//) {
370 1 $tag{crush_after} = 1;
371 }
372 elsif ($tag_inside =~ s/\+$//) {
373 0 $tag{crush_after} = 0;
374 }
375 }
376
377 # FIGURE OUT THE TAG TYPE
378
379 39 ▶ if ($tag_inside =~ /^\s*$/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($tag_inside =~ /^\s*$/) { } 0 39 39
elsif ($tag_inside =~ s[^\s*(/?(?:$keywords))\s+][]) { } 18 21 39
elsif ($tag_notation eq 'inline') { } 13 8 21
380 0 $tag{type} = 'NOP';
381 }
382 elsif ($tag_inside =~ s/^\s*(\/?(?:$keywords))\s+//) {
383 18 $tag{type} = $1;
384 }
385 elsif ($tag_notation eq 'inline') {
386 # USE A LITTLE MAGIC TO SEE IF WE'VE GOT A STATEMENT OR AN EXPRESSION
387 13 ▶ if ($tag_inside =~ /^\s*(if|unless|while|until|for|foreach)\s+/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($tag_inside =~ /^\s*(if|unless|while|until|for|foreach)\s+/) { } 0 13 13
elsif ($tag_inside =~ /^\s*\}?\s*(else|elsif|continue)\s+/) { } 0 13 13
elsif ($tag_inside =~ /^\s*\}\s*$/) { } 0 13 13
388 # THIS LOOKS LIKE A PERL STATEMENT
389 0 $tag{type} = 'PERL';
390 }
391 elsif ($tag_inside =~ /^\s*\}?\s*(else|elsif|continue)\s+/) {
392 # THIS LOOKS LIKE A PERL STATEMENT
393 0 $tag{type} = 'PERL';
394 }
395 elsif ($tag_inside =~ /^\s*\}\s*$/) {
396 # THIS LOOKS LIKE A PERL STATEMENT
397 0 $tag{type} = 'PERL';
398 }
399 else {
400 # MUST BE A PERL EXPRESSION
401 13 $tag{type} = 'MERGE';
402 }
403 }
404 else {
405 8 $tag{type} = 'PERL';
406 }
407
408 # PUT WHAT'S LEFT IN THE TAG TEXT
409
410 39 $tag_inside =~ s/(^\s+|\s+$)//g;
411 39 $tag{text} = $tag_inside;
412
413 # SET DEFAULT CRUSHING
414
415 39 ▶ if (not defined $tag{crush_before}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tag{"crush_before"}) 37 2 39
416 37 $tag{crush_before} = $crush_defaults{$tag{type}}[0];
417 }
418
419 39 ▶ if (not defined $tag{crush_after}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tag{"crush_after"}) 37 2 39
420 37 $tag{crush_after} = $crush_defaults{$tag{type}}[1];
421 }
422
423
424 # HANDLE FIRST-PASS TAGS
425 # ----------------------
426 39 ▶ if ($tag{type} eq 'TAG_STYLE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag{"type"} eq "TAG_STYLE") 4 35 39
427 4 ▶ if ($tag{text} eq 'default') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag{'text'} eq 'default') { } 1 3 4
428 1 ($tag_open, $tag_close, $tag_line) = ('<:', ':>', ':');
429 }
430 else {
431 3 ($tag_open, $tag_close, $tag_line) = split /\s+/, $tag{text};
432 }
433
434 4 ▶ if ($tag_open eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag_open eq "") 0 4 4
435 0 die "invalid open tag marker";
436 }
437
438 4 ▶ if ($tag_close eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag_close eq "") 0 4 4
439 0 die "invalid close tag marker";
440 }
441
442 4 ▶ if ($tag_line eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag_line eq "") 0 4 4
443 0 die "invalid line tag marker";
444 }
445
446 4 ▶ if ($tag_line eq $tag_open or $tag_line eq $tag_close) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag_line eq $tag_open or $tag_line eq $tag_close) 0 4 4
Condition33%1/3
$tag_line eq $tag_open or $tag_line eq $tag_close
l!l&&r!l&&!r
004
MC/DC0%0/2
$tag_line eq $tag_open or $tag_line eq $tag_close
$tag_line eq $tag_open $tag_line eq $tag_close
Truth table33%1/3
A or B
A= $tag_line eq $tag_openB= $tag_line eq $tag_close
ABresult
1X1
011
000
447 0 die "line tag marker must not be the same as either the open tag marker or close tag marker";
448 }
449
450 4 $make_regexes->();
451 }
452
453 # PUSH THE TAG ONTO THE RESULT
454
455 39 push @parsed, \%tag;
456 }
457 elsif ($source ne '') {
458 6 push @parsed, { type => 'TEXT', text => $source, source => $source, crush_before => 0, crush_after => 0, start_line => $current_line };
459 6 $source = '';
460 }
461 }
462
463 # RUN THROUGH AGAIN AND CRUSH WHITESPACE
464 8 ▶ for (my $i = 0; $i < scalar(@parsed); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @parsed) 79 8 87
465 79 ▶ if ($parsed[$i]{crush_before} == 1 and $i > 0 and $parsed[$i-1]{type} eq 'TEXT') {
Branch: 2 branches
BranchTrueFalseTotal
if ($parsed[$i]{'crush_before'} == 1 and $i > 0 and $parsed[$i - 1]{'type'} eq 'TEXT') { } 11 68 79
elsif ($parsed[$i]{'crush_before'} == 2 and $i > 0 and $parsed[$i - 1]{'type'} eq 'TEXT') { } 6 124 130
Condition83%5/6
$parsed[$i]{"crush_before"} == 1 and $i > 0 and $parsed[$i - 1]{"type"} eq "TEXT"
!ll&&!rl&&r
68011
$parsed[$i]{"crush_before"} == 1 and $i > 0
!ll&&!rl&&r
65311
MC/DC66%2/3
$parsed[$i]{"crush_before"} == 1 and $i > 0 and $parsed[$i - 1]{"type"} eq "TEXT"
$parsed[$i]{"crush_before"} == 1 $i > 0 $parsed[$i - 1]{"type"} eq "TEXT"
Truth table75%3/4
A and B and C
A= $parsed[$i]{"crush_before"} == 1B= $i > 0C= $parsed[$i - 1]{"type"} eq "TEXT"
ABCresult
0XX0
10X0
1100
1111
466 11 $parsed[$i-1]{text} =~ s/\n?[ \t]*$//;
467 }
468 ▶ elsif ($parsed[$i]{crush_before} == 2 and $i > 0 and $parsed[$i-1]{type} eq 'TEXT') {
Condition66%4/6
$parsed[$i]{"crush_before"} == 2 and $i > 0 and $parsed[$i - 1]{"type"} eq "TEXT"
!ll&&!rl&&r
6206
$parsed[$i]{"crush_before"} == 2 and $i > 0
!ll&&!rl&&r
6206
MC/DC33%1/3
$parsed[$i]{"crush_before"} == 2 and $i > 0 and $parsed[$i - 1]{"type"} eq "TEXT"
$parsed[$i]{"crush_before"} == 2 $i > 0 $parsed[$i - 1]{"type"} eq "TEXT"
Truth table50%2/4
A and B and C
A= $parsed[$i]{"crush_before"} == 2B= $i > 0C= $parsed[$i - 1]{"type"} eq "TEXT"
ABCresult
0XX0
10X0
1100
1111
469 6 $parsed[$i-1]{text} =~ s/\s+$//;
470 }
471
472 79 ▶ if ($parsed[$i]{crush_after} == 1 and $i < (scalar(@parsed)-1) and $parsed[$i+1]{type} eq 'TEXT') {
Branch: 2 branches
BranchTrueFalseTotal
if ($parsed[$i]{'crush_after'} == 1 and $i < scalar(@parsed) - 1 and $parsed[$i + 1]{'type'} eq 'TEXT') { } 1 78 79
elsif ($parsed[$i]{'crush_after'} == 2 and $i < scalar(@parsed) - 1 and $parsed[$i + 1]{'type'} eq 'TEXT') { } 9 138 147
Condition66%4/6
$parsed[$i]{"crush_after"} == 1 and $i < scalar(@parsed) - 1 and $parsed[$i + 1]{"type"} eq "TEXT"
!ll&&!rl&&r
7801
$parsed[$i]{"crush_after"} == 1 and $i < scalar(@parsed) - 1
!ll&&!rl&&r
7801
MC/DC33%1/3
$parsed[$i]{"crush_after"} == 1 and $i < scalar(@parsed) - 1 and $parsed[$i + 1]{"type"} eq "TEXT"
$parsed[$i]{"crush_after"} == 1 $i < scalar(@parsed) - 1 $parsed[$i + 1]{"type"} eq "TEXT"
Truth table50%2/4
A and B and C
A= $parsed[$i]{"crush_after"} == 1B= $i < scalar(@parsed) - 1C= $parsed[$i + 1]{"type"} eq "TEXT"
ABCresult
0XX0
10X0
1100
1111
473 1 $parsed[$i+1]{text} =~ s/^[ \t]*\n?//;
474 }
475 ▶ elsif ($parsed[$i]{crush_after} == 2 and $i < (scalar(@parsed)-1) and $parsed[$i+1]{type} eq 'TEXT') {
Condition83%5/6
$parsed[$i]{"crush_after"} == 2 and $i < scalar(@parsed) - 1 and $parsed[$i + 1]{"type"} eq "TEXT"
!ll&&!rl&&r
6909
$parsed[$i]{"crush_after"} == 2 and $i < scalar(@parsed) - 1
!ll&&!rl&&r
6819
MC/DC66%2/3
$parsed[$i]{"crush_after"} == 2 and $i < scalar(@parsed) - 1 and $parsed[$i + 1]{"type"} eq "TEXT"
$parsed[$i]{"crush_after"} == 2 $i < scalar(@parsed) - 1 $parsed[$i + 1]{"type"} eq "TEXT"
Truth table75%3/4
A and B and C
A= $parsed[$i]{"crush_after"} == 2B= $i < scalar(@parsed) - 1C= $parsed[$i + 1]{"type"} eq "TEXT"
ABCresult
0XX0
10X0
1100
1111
476 9 $parsed[$i+1]{text} =~ s/^\s+//;
477 }
478 }
479
480 # AND WE'RE DONE
481 8 return \@parsed;
482 }
483  
484 8 ▶sub compile_template {
Subroutine compile_template: called CC=49 SCAR=55.7
Pod undocumented
485 8 my ($source, $template_id, $package_name) = @_;
486
487 # ADJUST FOR SOURCE LINES
488 8 ▶ if (ref($source) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $source eq "ARRAY") 8 0 8
489 8 $source = join "", @$source;
490 }
491
492 # PARSE THE SOURCE INTO TAGS
493 8 my $tags = parse_template($source);
494
495 # INITIALIZE OUR PARSE VARIABLES
496 8 my $global_section = '';
497 8 my $init_section = '';
498
499 8 my %methods = (
500 main => '',
501 );
502
503 8 my $cur_method = 'main';
504
505 8 my @stack;
506 my @stack_details;
507 8 my @stack_lines;
508
509 # DEFINE A USEFUL LITTLE FUNCTION
510 2 ▶ my $format_perl = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
511 2 my ($source, $start_line) = @_;
512
513 2 my @lines = split /\n/, $source;
514
515 2 my $result;
516 2 my $cur_line = $start_line;
517 2 foreach my $line (@lines) {
518 4 $result .= "$line###TMOJO_LINE: $cur_line\n";
519 4 $cur_line += 1;
520 }
521
522 2 return $result;
523 8 };
524
525 # PARSE ALL OF THE TAGS
526 8 ▶ while (my $tag = shift @$tags) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tag = shift @$tags) 75 8 83
527
528 # TEXT TAG
529 # ---------------------------------
530
531 75 ▶ if ($tag->{type} eq 'TEXT') {
Branch: 13 branches
BranchTrueFalseTotal
if ($tag->{'type'} eq 'TEXT') { } 38 37 75
elsif ($tag->{'type'} eq 'GLOBAL') { } 1 36 37
elsif ($tag->{'type'} eq 'INIT') { } 0 36 36
elsif ($tag->{'type'} eq 'PERL') { } 11 25 36
elsif ($tag->{'type'} eq 'METHOD') { } 4 21 25
elsif ($tag->{'type'} eq '/METHOD') { } 4 17 21
elsif ($tag->{'type'} eq 'MERGE') { } 13 4 17
elsif ($tag->{'type'} eq 'CAPTURE') { } 0 4 4
elsif ($tag->{'type'} eq '/CAPTURE') { } 0 4 4
elsif ($tag->{'type'} eq 'FILTER') { } 0 4 4
elsif ($tag->{'type'} eq '/FILTER') { } 0 4 4
elsif ($tag->{'type'} eq 'REGEX') { } 0 4 4
elsif ($tag->{'type'} eq '/REGEX') { } 0 4 4
532 38 my $dumper = Data::Dumper->new([$tag->{text}]);
533 38 $dumper->Useqq(1);
534 38 $dumper->Indent(0);
535 38 $dumper->Terse(1);
536 38 my $literal = $dumper->Dump();
537
538 38 $methods{$cur_method} .= "\t\$Result .= $literal;\n";
539 }
540
541 # GLOBAL TAG
542 # ---------------------------------
543
544 elsif ($tag->{type} eq 'GLOBAL') {
545
546 1 ▶ if ($cur_method ne 'main') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur_method ne "main") 0 1 1
547 0 die "cannot declare METHOD here";
548 }
549
550 1 ▶ if ($global_section ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($global_section ne "") 0 1 1
551 0 die "attempting to redefine GLOBAL section";
552 }
553
554 1 my $source = '';
555 1 my $start_line;
556
557 1 ▶ while (my $tag = shift @$tags) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tag = shift @$tags) 2 0 2
558 2 ▶ if (not defined $tag) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tag) 0 2 2
559 0 die "missing /GLOBAL tag";
560 }
561
562 2 ▶ if ($tag->{type} eq '/GLOBAL') {
Branch: 2 branches
BranchTrueFalseTotal
if ($tag->{'type'} eq '/GLOBAL') { } 1 1 2
elsif ($tag->{'type'} ne 'TEXT') { } 0 1 1
563 1 last;
564 }
565 elsif ($tag->{type} ne 'TEXT') {
566 0 die "non-text tag in GLOBAL section in '$template_id' starting at line $tag->{start_line}";
567 }
568 else {
569 1 ▶ if (not defined $start_line) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $start_line) 1 0 1
570 1 $start_line = $tag->{start_line};
571 }
572 1 $source .= $tag->{source};
573 }
574 }
575
576 1 $global_section .= $format_perl->($source, $start_line);
577 }
578
579 # INIT TAG
580 # ---------------------------------
581
582 elsif ($tag->{type} eq 'INIT') {
583
584 0 ▶ if ($cur_method ne 'main') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur_method ne "main") 0 0 0
585 0 die "cannot declare METHOD here";
586 }
587
588 0 ▶ if ($init_section ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($init_section ne "") 0 0 0
589 0 die "attempting to redefine INIT section";
590 }
591
592 0 my $source = '';
593 0 my $start_line;
594
595 0 ▶ while (my $tag = shift @$tags) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tag = shift @$tags) 0 0 0
596 0 ▶ if (not defined $tag) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tag) 0 0 0
597 0 die "missing /INIT tag";
598 }
599
600 0 ▶ if ($tag->{type} eq '/INIT') {
Branch: 2 branches
BranchTrueFalseTotal
if ($tag->{'type'} eq '/INIT') { } 0 0 0
elsif ($tag->{'type'} ne 'TEXT') { } 0 0 0
601 0 last;
602 }
603 elsif ($tag->{type} ne 'TEXT') {
604 0 die "non-text tag in INIT section in '$template_id' starting at line $tag->{start_line}";
605 }
606 else {
607 0 ▶ if (not defined $start_line) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $start_line) 0 0 0
608 0 $start_line = $tag->{start_line};
609 }
610 0 $source .= $tag->{source};
611 }
612 }
613
614 0 $init_section .= $format_perl->($source, $start_line);
615 }
616
617 # PERL TAG
618 # ---------------------------------
619
620 elsif ($tag->{type} eq 'PERL') {
621
622 11 ▶ if ($tag->{text} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag->{'text'} ne '') { } 10 1 11
623 10 my @lines = split /\n/, $tag->{text};
624
625 10 my $cur_line = $tag->{start_line};
626 10 ▶ while ($_ = shift @lines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ = shift @lines) 11 10 21
627 11 $methods{$cur_method} .= "$_###TMOJO_LINE: $cur_line\n";
628 11 $cur_line += 1;
629 }
630 }
631 else {
632 1 my $source = '';
633 1 my $start_line;
634
635 1 ▶ while (my $tag = shift @$tags) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tag = shift @$tags) 2 0 2
636 2 ▶ if (not defined $tag) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tag) 0 2 2
637 0 die "missing /PERL tag";
638 }
639
640 2 ▶ if ($tag->{type} eq '/PERL') {
Branch: 2 branches
BranchTrueFalseTotal
if ($tag->{'type'} eq '/PERL') { } 1 1 2
elsif ($tag->{'type'} ne 'TEXT') { } 0 1 1
641 1 last;
642 }
643 elsif ($tag->{type} ne 'TEXT') {
644 0 die "non-text tag in PERL section in '$template_id' starting at line $tag->{start_line}";
645 }
646 else {
647 1 ▶ if (not defined $start_line) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $start_line) 1 0 1
648 1 $start_line = $tag->{start_line};
649 }
650 1 $source .= $tag->{source};
651 }
652 }
653
654 1 $methods{$cur_method} .= $format_perl->($source, $start_line);
655 }
656 }
657
658 # METHOD TAG
659 # ---------------------------------
660
661 elsif ($tag->{type} eq 'METHOD') {
662
663 4 ▶ if ($cur_method ne 'main') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur_method ne "main") 0 4 4
664 0 die "cannot declare METHOD here";
665 }
666
667 4 $cur_method = $tag->{text};
668 4 ▶ if ($cur_method !~ /^[a-zA-Z]\w*$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cur_method =~ /^[a-zA-Z]\w*$/) 0 4 4
669 0 die "illegal method name $cur_method";
670 }
671
672 4 ▶ if (defined $methods{$cur_method}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $methods{$cur_method}) 0 4 4
673 0 die "attempting to redefine METHOD $cur_method";
674 }
675 }
676
677 # /METHOD TAG
678 # ---------------------------------
679
680 elsif ($tag->{type} eq '/METHOD') {
681
682 4 ▶ if ($cur_method eq 'main') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur_method eq "main") 0 4 4
683 0 die "cannot end METHOD here";
684 }
685
686 4 $cur_method = 'main';
687 }
688
689 # MERGE TAG
690 # ---------------------------------
691
692 elsif ($tag->{type} eq 'MERGE') {
693
694 # FORMAT THE PERL
695 13 $methods{$cur_method} .= "\t\$Result .= (";
696
697 13 my @lines = split /\n/, $tag->{text};
698
699 13 my $cur_line = $tag->{start_line};
700 13 ▶ while ($_ = shift @lines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ = shift @lines) 13 13 26
701 13 $methods{$cur_method} .= $_;
702 13 ▶ if (@lines) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines) { } 0 13 13
703 0 $methods{$cur_method} .= "###TMOJO_LINE: $cur_line\n";
704 }
705 else {
706 13 $methods{$cur_method} .= "); ###TMOJO_LINE: $cur_line\n";
707 }
708
709 13 $cur_line += 1;
710 }
711 }
712
713 # CAPTURE TAG
714 # ---------------------------------
715
716 elsif ($tag->{type} eq 'CAPTURE') {
717
718 0 push @stack, 'CAPTURE';
719 0 push @stack_details, $tag->{text};
720 0 push @stack_lines, $tag->{start_line};
721
722 0 $methods{$cur_method} .= "\tpush(\@ResultStack, ''); local \*Result = \\\$ResultStack[-1];\n";
723 }
724
725 # /CAPTURE TAG
726 # ---------------------------------
727
728 elsif ($tag->{type} eq '/CAPTURE') {
729
730 0 ▶ if (pop(@stack) ne 'CAPTURE') {
Branch: 1 branch
BranchTrueFalseTotal
if (pop @stack ne "CAPTURE") 0 0 0
731 0 die "unexpected /CAPTURE tag>";
732 }
733
734 0 my $capture_lvalue = pop @stack_details;
735 0 my $capture_line = pop @stack_lines;
736
737 0 $methods{$cur_method} .= "\t$capture_lvalue = pop(\@ResultStack); local \*Result = \\\$ResultStack[-1];###TMOJO_LINE: $capture_line\n";
738 }
739
740 # FILTER TAG
741 # ---------------------------------
742
743 elsif ($tag->{type} eq 'FILTER') {
744
745 0 push @stack, 'FILTER';
746 0 push @stack_details, $tag->{text};
747 0 push @stack_lines, $tag->{start_line};
748
749 0 $methods{$cur_method} .= "\tpush(\@ResultStack, ''); local \*Result = \\\$ResultStack[-1];\n";
750 }
751
752 # /FILTER TAG
753 # ---------------------------------
754
755 elsif ($tag->{type} eq '/FILTER') {
756
757 0 ▶ if (pop(@stack) ne 'FILTER') {
Branch: 1 branch
BranchTrueFalseTotal
if (pop @stack ne "FILTER") 0 0 0
758 0 die "unexpected /FILTER tag>";
759 }
760
761 0 my $filter_code = pop @stack_details;
762 0 my $filter_line = pop @stack_lines;
763
764 0 $methods{$cur_method} .= "\t\$ResultStack[-2] .= ($filter_code); pop(\@ResultStack); local \*Result = \\\$ResultStack[-1];###TMOJO_LINE: $filter_line\n";
765 }
766
767 # REGEX TAG
768 # ---------------------------------
769
770 elsif ($tag->{type} eq 'REGEX') {
771
772 0 push @stack, 'REGEX';
773 0 push @stack_details, $tag->{text};
774 0 push @stack_lines, $tag->{start_line};
775
776 0 $methods{$cur_method} .= "\tpush(\@ResultStack, ''); local \*Result = \\\$ResultStack[-1];\n";
777 }
778
779 # /REGEX TAG
780 # ---------------------------------
781
782 elsif ($tag->{type} eq '/REGEX') {
783
784 0 ▶ if (pop(@stack) ne 'REGEX') {
Branch: 1 branch
BranchTrueFalseTotal
if (pop @stack ne "REGEX") 0 0 0
785 0 die "unexpected /REGEX tag>";
786 }
787
788 0 my $regex = pop @stack_details;
789 0 my $regex_line = pop @stack_lines;
790
791 0 $methods{$cur_method} .= "\t\$Result =~ $regex; \$ResultStack[-2] .= \$Result; pop(\@ResultStack); local \*Result = \\\$ResultStack[-1];###TMOJO_LINE: $regex_line\n";
792 }
793 }
794
795 # MAKE SURE OUR MODE IS COOL
796 8 ▶ if ($cur_method ne 'main') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur_method ne "main") 0 8 8
797 0 die "expected /METHOD tag";
798 }
799
800 8 ▶ if (@stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stack) 0 8 8
801 0 die "expected /$stack[-1] tag";
802 }
803
804 # NOW, WE CONSTRUCT THE ENTIRE PACKAGE
805 # --------------------------------------
806
807 8 my $template_compiled = qq{###TMOJO_TEMPLATE_ID: $template_id
808 package $package_name;
809  
810 use strict;
811  
812 our \$Tmojo;
813  
814 $global_section
815  
816 sub new {
817 my \$Self = {
818 args => \$_[1],
819 next => \$_[2],
820 vars => {},
821 };
822
823 bless \$Self, \$_[0];
824
825 # DEFINE THE IMPLICIT VARIABLES
826 my \$Next = \$Self->{next};
827 our \%Args; local \*Args = \$Self->{args};
828 our \%Vars; local \*Vars = \$Self->{vars};
829
830 # --- BEGIN USER CODE ---
831 $init_section
832 # --- END USER CODE ---
833
834 # RETURN THE VALUE
835 return \$Self;
836 }
837 };
838
839 8 foreach my $method (keys %methods) {
840 12 $template_compiled .= qq{
841 sub $method {
842 my \$Self = shift \@_;
843
844 # DEFINE THE IMPLICIT VARIABLES
845 my \$Next = \$Self->{next};
846 our \%Args; local \*Args = \$Self->{args};
847 our \%Vars; local \*Vars = \$Self->{vars};
848
849 my \@ResultStack = ('');
850 our \$Result; local \*Result = \\\$ResultStack[-1];
851
852
853 # --- BEGIN USER CODE ---
854 $methods{$method}
855 # --- END USER CODE ---
856
857 return \$Result;
858 }
859 };
860 }
861
862 8 $template_compiled .= "\n1;\n";
863
864 8 return $template_compiled;
865 }
866  
867  
868  
869  
870  
871 0 ▶sub compile_lite_template {
Subroutine compile_lite_template: not called CC=14 SCAR=53.5
Pod undocumented
872 0 my ($source, $template_id, $package_name) = @_;
873
874 # ADJUST FOR SOURCE LINES
875 0 ▶ if (ref($source) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $source eq "ARRAY") 0 0 0
876 0 $source = join "", @$source;
877 }
878
879 # PARSE THE SOURCE INTO TAGS
880 0 my $tags = parse_template($source);
881
882 0 my %methods = (
883 main => '',
884 );
885
886 0 my $cur_method = 'main';
887
888 # PARSE ALL OF THE TAGS
889 0 ▶ while (my $tag = shift @$tags) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tag = shift @$tags) 0 0 0
890
891 # TEXT TAG
892 # ---------------------------------
893
894 0 ▶ if ($tag->{type} eq 'TEXT') {
Branch: 4 branches
BranchTrueFalseTotal
if ($tag->{'type'} eq 'TEXT') { } 0 0 0
elsif ($tag->{'type'} eq 'METHOD') { } 0 0 0
elsif ($tag->{'type'} eq '/METHOD') { } 0 0 0
elsif ($tag->{'type'} eq 'MERGE') { } 0 0 0
895 0 my $dumper = Data::Dumper->new([$tag->{text}]);
896 0 $dumper->Useqq(1);
897 0 $dumper->Indent(0);
898 0 $dumper->Terse(1);
899 0 my $literal = $dumper->Dump();
900
901 0 $methods{$cur_method} .= "\t\$Result .= $literal;\n";
902 }
903
904 # METHOD TAG
905 # ---------------------------------
906
907 elsif ($tag->{type} eq 'METHOD') {
908
909 0 ▶ if ($cur_method ne 'main') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur_method ne "main") 0 0 0
910 0 die "cannot declare METHOD here";
911 }
912
913 0 $cur_method = $tag->{text};
914 0 ▶ if ($cur_method !~ /^[a-zA-Z]\w*$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cur_method =~ /^[a-zA-Z]\w*$/) 0 0 0
915 0 die "illegal method name $cur_method";
916 }
917
918 0 ▶ if ($methods{$cur_method} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($methods{$cur_method} ne "") 0 0 0
919 0 die "attempting to redefine METHOD $cur_method";
920 }
921 }
922
923 # /METHOD TAG
924 # ---------------------------------
925
926 elsif ($tag->{type} eq '/METHOD') {
927
928 0 ▶ if ($cur_method eq 'main') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur_method eq "main") 0 0 0
929 0 die "cannot end METHOD here";
930 }
931
932 0 $cur_method = 'main';
933 }
934
935 # MERGE TAG
936 # ---------------------------------
937
938 elsif ($tag->{type} eq 'MERGE') {
939
940 0 ▶ if ($tag->{text} =~ /^\$([\w\.]+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag->{'text'} =~ /^\$([\w\.]+)$/) { } 0 0 0
941 0 my $lookup = $1;
942 0 $lookup =~ s/\.(\w+)/}{$1/g;
943 0 $methods{$cur_method} .= "\t\$Result .= \$args->{$lookup};###TMOJO_LINE: $tag->{start_line}\n";
944 }
945 else {
946 0 die "malformed merge tag in $template_id on line $tag->{start_line}";
947 }
948 }
949 }
950
951 # MAKE SURE OUR MODE IS COOL
952 0 ▶ if ($cur_method ne 'main') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur_method ne "main") 0 0 0
953 0 die "expected /METHOD tag";
954 }
955
956 # NOW, WE CONSTRUCT THE ENTIRE PACKAGE
957 # --------------------------------------
958
959 0 my $template_compiled = qq{###TMOJO_TEMPLATE_ID: $template_id
960 package $package_name;
961  
962 use strict;
963  
964 our \$Tmojo;
965  
966 sub new {
967 my \$Self = {
968 args => \$_[1],
969 };
970
971 # RETURN THE VALUE
972 return bless \$Self, \$_[0];
973 }
974 };
975
976 0 foreach my $method (keys %methods) {
977 0 $template_compiled .= qq{
978 sub $method {
979 my \$Self = shift \@_;
980
981 my \$args = \$Self->{args};
982 if (\@_) {
983 \$args = { \@_ };
984 }
985
986 my \$Result = '';
987
988 # --- BEGIN USER CODE ---
989 $methods{$method}
990 # --- END USER CODE ---
991
992 return \$Result;
993 }
994 };
995 }
996
997 0 $template_compiled .= "\n1;\n";
998
999 0 return $template_compiled;
1000 }
1001  
1002 9 ▶sub get_template_class {
Subroutine get_template_class: called CC=17 SCAR=29.7
Pod undocumented
1003  
1004 9 my ($self, $template_id, $used_parents) = @_;
1005
1006 # NORMALIZE THE TEMPLATE_ID
1007 9 my $normalized_template_id = $self->normalize_template_id($template_id);
1008
1009 # GET THE PACKAGE NAME
1010 9 my $package_name = $self->{template_loader}->template_package_name($normalized_template_id);
1011
1012 # FIGURE OUT WHERE WE'D CACHE THIS THING
1013 9 my $template_compiled_fn = $self->get_template_compiled_fn($package_name);
1014
1015 # LOOK IN OUR CACHE TO SEE IF WE HAVE THE TEMPLATE
1016 9 my $cache_time_stamp = 0;
1017 9 ▶ if (-r $template_compiled_fn) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r $template_compiled_fn) 1 8 9
1018 1 $cache_time_stamp = (stat($template_compiled_fn))[9];
1019 }
1020
1021 # ATTEMPT TO LOAD THE TEMPLATE
1022 9 my ($template_lines, $tmojo_lite) = $self->{template_loader}->load_template($normalized_template_id, $cache_time_stamp);
1023
1024 # IF $template_lines CAME BACK AS A ZERO, THEN OUR CACHED VERSION IS STILL GOOD
1025 9 my $cache_level = 0;
1026 9 ▶ if ($template_lines == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($template_lines == 0) 1 8 9
1027 1 $cache_level = 1;
1028
1029 1 ▶ if (exists $memory_cache{$package_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $HTML::Tmojo::memory_cache{$package_name}) 1 0 1
1030 1 ▶ if ($cache_time_stamp <= $memory_cache{$package_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache_time_stamp <= $HTML::Tmojo::memory_cache{$package_name}) 1 0 1
1031 1 $cache_level = 2;
1032 }
1033 }
1034 }
1035
1036 # IF WE DON'T HAVE IT IN THE CACHE
1037 9 ▶ if ($cache_level == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache_level == 0) 8 1 9
1038
1039 # COMPILE THE TEMPLATE
1040 8 my $template_compiled;
1041 8 ▶ if ($tmojo_lite) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmojo_lite) { } 0 8 8
1042 0 $template_compiled = compile_lite_template($template_lines, $normalized_template_id, $package_name);
1043 }
1044 else {
1045 8 $template_compiled = compile_template($template_lines, $normalized_template_id, $package_name);
1046 }
1047
1048 # CACHE THE TEMPLATE
1049 # ------------------
1050 # IT TURNS OUT THAT YOU CAN'T GET AWAY WITH HAVING THE LINE
1051 # NUMBERS IN THE PERL CODE, BECAUSE IT SCREWS UP qq{} AND
1052 # OTHER NEATO THINGS
1053
1054 # SO, ALAS, NOW THAT WE'VE GONE TO THE TROUBLE OF ADDING THE
1055 # LINE NUMBERS, WE'RE GOING TO STRIP THEM AND PUT THEM IN
1056 # ANOTHER FILE... :(
1057 8 my @final_lines = split /\n/, $template_compiled;
1058
1059 8 ▶ open CODE_FH, ">$template_compiled_fn" or die "$! ($template_compiled_fn)";
Branch: 1 branch
BranchTrueFalseTotal
open CODE_FH, ">$template_compiled_fn" or die "$! ($template_compiled_fn)" 8 0 8
1060 8 ▶ open LINE_FH, ">$template_compiled_fn.lines" or die "$! ($template_compiled_fn.lines)";
Branch: 1 branch
BranchTrueFalseTotal
open LINE_FH, ">$template_compiled_fn.lines" or die "$! ($template_compiled_fn.lines)" 8 0 8
1061
1062 8 foreach my $line (@final_lines) {
1063 550 ▶ if ($line =~ /^(.*)(###TMOJO_(TEMPLATE_ID|LINE): .+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^(.*)(###TMOJO_(TEMPLATE_ID|LINE): .+)$/) { } 36 514 550
1064 36 print CODE_FH "$1\n";
1065 36 print LINE_FH "$2\n";
1066 }
1067 else {
1068 514 print CODE_FH "$line\n";
1069 514 print LINE_FH ".\n";
1070 }
1071 }
1072
1073 8 close CODE_FH;
1074 8 close LINE_FH;
1075 }
1076
1077 # IF IT'S NOT IN THE MEMORY CACHE
1078 9 ▶ if ($cache_level < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache_level < 2) 8 1 9
1079 # DELETE THE PACKAGE
1080 8 delete_package($package_name);
1081
1082 # PUT A CONTEXTUAL TMOJO OBJECT INTO THE PACKAGE
1083 {
1084 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1085 8 my $context_path = $normalized_template_id;
1086 8 $context_path =~ s{/[^/]+$}{};
1087 8 my $contextual_tmojo = HTML::Tmojo->new(%$self, context_path => $context_path);
1088 8 ${$package_name . '::Tmojo'} = $contextual_tmojo;
1089 }
1090
1091 # NOW WE DO THE FILE
1092 8 do $template_compiled_fn;
1093 8 ▶ die if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 8 8
1094
1095 # REMOVE THE TEMPLATE FROM %INC (BECAUSE StatINC HURTS)
1096 8 delete $INC{$template_compiled_fn};
1097
1098 # RECORD THE LAST TIME THAT THE PACKAGE WAS COMPILED
1099 8 $memory_cache{$package_name} = time();
1100 }
1101
1102 # MAKE SURE THAT LOAD TEMPLATE HAS BEEN CALLED ON THE PARENT TEMPLATES
1103 {
1104 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1105
1106 # MAKE SURE THAT WE DON'T HAVE AN INFINITE LOOP HERE
1107 9 ▶ if (defined $used_parents) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $used_parents) { } 0 9 9
1108 0 ▶ if ($used_parents->{$normalized_template_id} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($used_parents->{$normalized_template_id} == 1) 0 0 0
1109 0 die "circular parent reference, $normalized_template_id already used (this will cause an infinite loop)";
1110 }
1111 }
1112 else {
1113 9 $used_parents = {};
1114 }
1115
1116 9 $used_parents->{$normalized_template_id} = 1;
1117
1118 9 my @parents = @{$package_name . '::TMOJO_ISA'};
1119
1120 9 ▶ if (@parents) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parents) 0 9 9
1121 0 foreach (@parents) {
1122 0 my $contextual_tmojo = ${$package_name . '::Tmojo'};
1123 0 $_ = $contextual_tmojo->get_template_class($_, $used_parents);
1124 }
1125
1126 0 @{$package_name . '::ISA'} = @parents;
1127 }
1128 }
1129
1130 # RETURN THE PACKAGE NAME
1131 9 return $package_name;
1132 }
1133  
1134 18 ▶sub normalize_template_id {
Subroutine normalize_template_id: called CC=7 SCAR=30.7
Pod undocumented
1135 18 my ($self, $template_id) = @_;
1136
1137 # THIS IS WHERE THE MAGIC OF THE CONTEXT PATH IS RESOLVED
1138 18 ▶ if (substr($template_id, 0, 3) eq '../') {
Branch: 2 branches
BranchTrueFalseTotal
if (substr($template_id, 0, 3) eq '../') { } 0 18 18
elsif (substr($template_id, 0, 1) ne '/') { } 16 2 18
1139 0 my $context_path = $self->{context_path};
1140
1141 0 ▶ while (substr($template_id, 0, 3) eq '../') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($template_id, 0, 3) eq "../") 0 0 0
1142 0 $context_path =~ s{/[^/]*$}{};
1143 0 $template_id = substr($template_id, 3);
1144 }
1145
1146 0 $template_id = "$context_path/$template_id";
1147 }
1148 elsif (substr($template_id, 0, 1) ne '/') {
1149 16 $template_id = "$self->{context_path}/$template_id";
1150 }
1151
1152 # HANDLE UPWARD TRAVERSAL
1153 18 ▶ if (substr($template_id, -1, 1) eq '^') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($template_id, -1, 1) eq "^") 0 18 18
1154 0 $template_id = substr($template_id, 0, -1);
1155
1156 0 ▶ while (rindex($template_id, '/') > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rindex($template_id, "/") > 0) 0 0 0
1157 0 ▶ if ($self->{template_loader}->template_exists($template_id)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'template_loader'}->template_exists($template_id)) { } 0 0 0
1158 0 last;
1159 }
1160 else {
1161 0 $template_id =~ s{/[^/]+/([^/]+)$}{/$1};
1162 }
1163 }
1164 }
1165
1166 # NOW WE'VE GOT OUR NAME
1167 18 return $template_id;
1168 }
1169  
1170 9 ▶sub get_template_compiled_fn {
Subroutine get_template_compiled_fn: called CC=3 SCAR=11.0
Pod undocumented
1171 9 my ($self, $package_name) = @_;
1172
1173 # MAKE SURE ALL OF THE DIRECTORIES ARE THERE
1174 9 my @parts = split('::', $package_name);
1175 9 my $current_dir = $self->{cache_dir};
1176
1177 # GET RID OF THE LAST ONE
1178 9 my $last_part = pop @parts;
1179
1180 # MAKE ALL OF THE DIRECTORIES
1181 9 ▶ while (@parts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts) 45 9 54
1182 45 $current_dir .= '/' . shift(@parts);
1183 45 ▶ unless (-d $current_dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $current_dir) 25 20 45
1184 25 mkdir $current_dir;
1185 }
1186 }
1187
1188 9 my $compiled_fn = $current_dir . "/$last_part";
1189
1190 9 return $compiled_fn;
1191 }
1192  
1193 5 1;
↑ Summary Tmojo/HttpArgParser.pm »