Hogan/Compiler.pm

filter: Statementstmt 84.1% 234 / 278 Branchbran 81.5% 75 / 92 Conditioncond 76.9% 40 / 52 MC/DCmcdc 62.0% 18 / 29 Subroutinesub 83.3% 35 / 42 Podpod 20.0% 5 / 25 totaltotal 78.5% 407 / 518 CC 73 SCAR 20.8
CC
73
Cov
83%
is_closer
20.0
build_tree
19.3
scan
16.0
CRAP 8.0
« Hogan/Template.pm ↑ Summary Hogan.pm »
1 package Text::Hogan::Compiler;
2 4 $Text::Hogan::Compiler::VERSION = '2.06';
3 4 ▶use Text::Hogan::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 4 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶use Ref::Util qw( is_arrayref );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Text::Trim 'trim';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 my $r_is_whitespace = qr/\S/;
13 4 my $r_quot = qr/"/;
14 4 my $r_newline = qr/\n/;
15 4 my $r_cr = qr/\r/;
16 4 my $r_slash = qr/\\/;
17  
18 4 my $linesep = "\u{2028}";
19 4 my $paragraphsep = "\u{2029}";
20 4 my $r_linesep = qr/\Q$linesep\E/;
21 4 my $r_paragraphsep = qr/\Q$paragraphsep\E/;
22  
23 4 my %tags = (
24 '#' => 1, '^' => 2, '<' => 3, '$' => 4,
25 '/' => 5, '!' => 6, '>' => 7, '=' => 8, '_v' => 9,
26 '{' => 10, '&' => 11, '_t' => 12
27 );
28  
29 4 my $Template = Text::Hogan::Template->new();
30  
31 130 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
32 130 my $class = shift;
33 130 return bless {}, $class;
34 }
35  
36 145 ▶sub scan {
Subroutine scan: called CC=16 SCAR=27.7
Pod documented
37 145 my ($self, $text_orig, $options) = @_;
38 145 my $text = [ split //, $text_orig ];
39  
40 145 my $len = scalar(@$text);
41 145 my ($IN_TEXT, $IN_TAG_TYPE, $IN_TAG) = (0, 1, 2);
42 145 my $state = $IN_TEXT;
43 145 my $tag_type = undef;
44 145 my $tag = undef;
45 145 my $buf = "";
46 145 my @tokens;
47 145 my $seen_tag = 0;
48 145 my $i = 0;
49 145 my $line_start = 0;
50 145 my ($otag, $ctag) = ('{{', '}}');
51  
52 598 ▶ my $add_buf = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
53 598 ▶ if (length $buf > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buf > 0) 337 261 598
54 337 push @tokens, { 'tag' => '_t', 'text' => $buf };
55 337 $buf = "";
56 }
57 145 };
58  
59 187 ▶ my $line_is_whitespace = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
60 187 my $is_all_whitespace = 1;
61 187 ▶ for (my $j = $line_start; $j < @tokens; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @tokens) 226 64 290
62 $is_all_whitespace =
63 ($tags{$tokens[$j]{'tag'}} < $tags{'_v'}) ||
64 226 ▶ ($tokens[$j]{'tag'} eq '_t' && $tokens[$j]{'text'} !~ $r_is_whitespace);
Condition100%6/6
$tags{$tokens[$j]{'tag'}} < $tags{'_v'} || $tokens[$j]{'tag'} eq '_t' && !($tokens[$j]{'text'} =~ /$r_is_whitespace/)
l!l&&r!l&&!r
6439123
$tokens[$j]{"tag"} eq "_t" and not $tokens[$j]{"text"} =~ /$r_is_whitespace/
!ll&&!rl&&r
2310039
MC/DC100%3/3
$tags{$tokens[$j]{'tag'}} < $tags{'_v'} || $tokens[$j]{'tag'} eq '_t' && !($tokens[$j]{'text'} =~ /$r_is_whitespace/)
$tags{$tokens[$j]{'tag'}} < $tags{'_v'} $tokens[$j]{"tag"} eq "_t" not $tokens[$j]{"text"} =~ /$r_is_whitespace/
Truth table100%4/4
A || B and not(C)
A= $tags{$tokens[$j]{'tag'}} < $tags{'_v'}B= $tokens[$j]{"tag"} eq "_t"C= not $tokens[$j]{"text"} =~ /$r_is_whitespace/
ABCresult
1XX1
0111
00X0
0100
65 226 ▶ if (!$is_all_whitespace) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_all_whitespace) 123 103 226
66 123 return 0;
67 }
68 }
69 64 return $is_all_whitespace;
70 145 };
71  
72 301 ▶ my $filter_line = sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
73 301 my ($have_seen_tag, $no_new_line) = @_;
74  
75 301 $add_buf->();
76  
77 301 ▶ if ($have_seen_tag && $line_is_whitespace->()) {
Branch: 2 branches
BranchTrueFalseTotal
if ($have_seen_tag and &$line_is_whitespace()) { } 64 237 301
elsif (not $no_new_line) { } 99 138 237
Condition100%3/3
$have_seen_tag and &$line_is_whitespace()
!ll&&!rl&&r
11412364
MC/DC100%2/2
$have_seen_tag and &$line_is_whitespace()
$have_seen_tag &$line_is_whitespace()
Truth table100%3/3
A and B
A= $have_seen_tagB= &$line_is_whitespace()
ABresult
0X0
100
111
78 64 ▶ for (my $j = $line_start, my $next; $j < @tokens; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @tokens) 65 64 129
79 65 ▶ if ($tokens[$j]{'text'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tokens[$j]{"text"}) 25 40 65
80 25 ▶ if (($next = $tokens[$j+1]) && $next->{'tag'} eq '>') {
Branch: 1 branch
BranchTrueFalseTotal
if ($next = $tokens[$j + 1] and $next->{"tag"} eq ">") 3 22 25
Condition100%3/3
$next = $tokens[$j + 1] and $next->{"tag"} eq ">"
!ll&&!rl&&r
10123
MC/DC100%2/2
$next = $tokens[$j + 1] and $next->{"tag"} eq ">"
$next = $tokens[$j + 1] $next->{"tag"} eq ">"
Truth table100%3/3
A and B
A= $next = $tokens[$j + 1]B= $next->{"tag"} eq ">"
ABresult
0X0
100
111
81 3 $next->{'indent'} = "".$tokens[$j]{'text'};
82 }
83 25 splice(@tokens,$j,1);
84 }
85 }
86 }
87 elsif (!$no_new_line) {
88 99 push @tokens, { 'tag' => "\n" };
89 }
90  
91 301 $seen_tag = 0;
92 301 $line_start = scalar @tokens;
93 145 };
94  
95 16 ▶ my $change_delimiters = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
96 16 my ($text_orig, $index) = @_;
97 16 my $text = join('', @$text_orig);
98  
99 16 my $close = '=' . $ctag;
100 16 my $close_index = index($text, $close, $index);
101 16 my $offset = index($text, '=', $index) + 1;
102 16 my @delimiters = (
103 split ' ', trim(
104 # WARNING
105 #
106 # JavaScript substring and Perl substring functions differ!
107 #
108 # JavaScript's length parameter always goes from the beginning
109 # of the string, whereas Perl's goes from the offset parameter!
110 #
111 # node> "{{=<% %>=}}".substring(3, 8)
112 # '<% %>'
113 #
114 # perl> substr("{{=<% %>=}}", 3, 8);
115 # <% %>=}}
116 #
117 #
118 substr($text, $offset, $close_index - $offset)
119 )
120 );
121  
122 16 $otag = $delimiters[0];
123 16 $ctag = $delimiters[-1];
124  
125 16 return $close_index + length($close) - 1;
126 145 };
127  
128 145 ▶ if ($options->{'delimiters'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"delimiters"}) 5 140 145
129 5 my @delimiters = split ' ', $options->{'delimiters'};
130 5 $otag = $delimiters[0];
131 5 $ctag = $delimiters[1];
132 }
133  
134 145 ▶ for (my $i = 0; $i < $len; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $len) 4088 145 4233
135 4088 ▶ if ($state eq $IN_TEXT) {
Branch: 2 branches
BranchTrueFalseTotal
if ($state eq $IN_TEXT) { } 1814 2274 4088
elsif ($state eq $IN_TAG_TYPE) { } 297 1977 2274
136 1814 ▶ if (tag_change($otag, $text, $i)) {
Branch: 1 branch
BranchTrueFalseTotal
if (tag_change($otag, $text, $i)) { } 297 1517 1814
137 297 --$i;
138 297 $add_buf->();
139 297 $state = $IN_TAG_TYPE;
140 }
141 else {
142 1517 ▶ if (char_at($text, $i) eq "\n") {
Branch: 1 branch
BranchTrueFalseTotal
if (char_at($text, $i) eq "\n") { } 156 1361 1517
143 156 $filter_line->($seen_tag);
144 }
145 else {
146 1361 $buf .= char_at($text, $i);
147 }
148 }
149 }
150 elsif ($state eq $IN_TAG_TYPE) {
151 297 $i += length($otag) - 1;
152 297 ▶ $i += 1 while($options->{'allow_whitespace_before_hashmark'} and (char_at($text, $i+1) eq ' '));
Branch: 1 branch
BranchTrueFalseTotal
$options->{"allow_whitespace_before_hashmark"} and char_at($text, $i + 1) eq " " and do { $i += 1 } 2 297 299
Condition100%3/3
$options->{"allow_whitespace_before_hashmark"} and char_at($text, $i + 1) eq " "
!ll&&!rl&&r
29522
MC/DC100%2/2
$options->{"allow_whitespace_before_hashmark"} and char_at($text, $i + 1) eq " "
$options->{"allow_whitespace_before_hashmark"} char_at($text, $i + 1) eq " "
Truth table100%3/3
A and B
A= $options->{"allow_whitespace_before_hashmark"}B= char_at($text, $i + 1) eq " "
ABresult
0X0
100
111
153 297 $tag = $tags{char_at($text,$i + 1)};
154 297 ▶ $tag_type = $tag ? char_at($text, $i + 1) : '_v';
Branch: 1 branch
BranchTrueFalseTotal
$tag ? : 204 93 297
155 297 ▶ if ($tag_type eq '=') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag_type eq '=') { } 16 281 297
156 16 $i = $change_delimiters->($text, $i);
157 16 $state = $IN_TEXT;
158 }
159 else {
160 281 ▶ if ($tag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag) 188 93 281
161 188 $i++;
162 }
163 281 $state = $IN_TAG;
164 }
165 297 $seen_tag = $i;
166 }
167 else {
168 1977 ▶ if (tag_change($ctag, $text, $i)) {
Branch: 1 branch
BranchTrueFalseTotal
if (tag_change($ctag, $text, $i)) { } 281 1696 1977
169 281 ▶ push @tokens, {
Branch: 1 branch
BranchTrueFalseTotal
$tag_type eq '/' ? : 69 212 281
170 'tag' => $tag_type,
171 'n' => trim($buf),
172 'otag' => $otag,
173 'ctag' => $ctag,
174 'i' => (($tag_type eq '/') ? $seen_tag - length($otag) : $i + length($ctag)),
175 };
176 281 $buf = "";
177 281 $i += length($ctag) - 1;
178 281 $state = $IN_TEXT;
179 281 ▶ if ($tag_type eq '{') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag_type eq "{") 12 269 281
180 12 ▶ if ($ctag eq '}}') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctag eq '}}') { } 12 0 12
181 12 $i++;
182 }
183 else {
184 0 clean_triple_stache($tokens[-1]);
185 }
186 }
187 }
188 else {
189 1696 $buf .= char_at($text, $i);
190 }
191 }
192 }
193  
194 145 $filter_line->($seen_tag, 1);
195  
196 145 return \@tokens;
197 }
198  
199 0 ▶sub clean_triple_stache {
Subroutine clean_triple_stache: not called CC=2 SCAR=17.9
Pod undocumented
200 0 my ($token) = @_;
201  
202 0 ▶ if (substr($token->{'n'}, length($token->{'n'}) - 1) eq '}') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($token->{"n"}, length($token->{"n"}) - 1) eq "}") 0 0 0
203 0 $token->{'n'} = substr($token->{'n'}, 0, length($token->{'n'}) - 1);
204 }
205  
206 0 return;
207 }
208  
209 3791 ▶sub tag_change {
Subroutine tag_change: called CC=4 SCAR=13.9
Pod undocumented
210 3791 my ($tag, $text, $index) = @_;
211  
212 3791 ▶ if (char_at($text, $index) ne char_at($tag, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (char_at($text, $index) ne char_at($tag, 0)) 3212 579 3791
213 3212 return 0;
214 }
215  
216 579 ▶ for (my $i = 1, my $l = length($tag); $i < $l; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $l) 553 578 1131
217 553 ▶ if (char_at($text, $index + $i) ne char_at($tag, $i)) {
Branch: 1 branch
BranchTrueFalseTotal
if (char_at($text, $index + $i) ne char_at($tag, $i)) 1 552 553
218 1 return 0;
219 }
220 }
221  
222 578 return 1;
223 }
224  
225 4 my %allowed_in_super = (
226 '_t' => 1,
227 "\n" => 1,
228 '$' => 1,
229 '/' => 1,
230 );
231  
232 214 ▶sub build_tree {
Subroutine build_tree: called CC=15 SCAR=29.6
Pod undocumented
233 214 my ($tokens, $kind, $stack, $custom_tags) = @_;
234 214 my (@instructions, $opener, $tail, $token);
235  
236 214 $tail = $stack->[-1];
237  
238 214 ▶ while (@$tokens > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$tokens > 0) 692 145 837
239 692 $token = shift @$tokens;
240  
241 692 ▶ if ($tail && ($tail->{'tag'} eq '<') && !$allowed_in_super{$token->{'tag'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tail and $tail->{"tag"} eq "<" and not $allowed_in_super{$token->{"tag"}}) 0 692 692
Condition50%3/6
$tail and $tail->{"tag"} eq "<" and not $allowed_in_super{$token->{"tag"}}
!ll&&!rl&&r
69200
$tail and $tail->{"tag"} eq "<"
!ll&&!rl&&r
4612310
MC/DC0%0/3
$tail and $tail->{"tag"} eq "<" and not $allowed_in_super{$token->{"tag"}}
$tail $tail->{"tag"} eq "<" not $allowed_in_super{$token->{"tag"}}
Truth table50%2/4
A and B and not(C)
A= $tailB= $tail->{"tag"} eq "<"C= not $allowed_in_super{$token->{"tag"}}
ABCresult
0XX0
10X0
1100
1111
242 0 die "Illegal content in < super tag.";
243 }
244  
245 692 ▶ if ($tags{$token->{'tag'}} && ($tags{$token->{'tag'}} <= $tags{'$'} || is_opener($token, $custom_tags))) {
Branch: 3 branches
BranchTrueFalseTotal
if ($tags{$token->{'tag'}} and $tags{$token->{'tag'}} <= $tags{'$'} || is_opener($token, $custom_tags)) { } 69 623 692
elsif ($token->{'tag'} eq '/') { } 69 554 623
elsif ($token->{'tag'} eq "\n") { } 99 455 554
Condition83%5/6
$tags{$token->{"tag"}} and $tags{$token->{"tag"}} <= $tags{"\$"} || is_opener($token, $custom_tags)
!ll&&!rl&&r
9952469
$tags{$token->{"tag"}} <= $tags{"\$"} or is_opener($token, $custom_tags)
l!l&&r!l&&!r
690524
MC/DC66%2/3
$tags{$token->{"tag"}} and $tags{$token->{"tag"}} <= $tags{"\$"} || is_opener($token, $custom_tags)
$tags{$token->{"tag"}} $tags{$token->{"tag"}} <= $tags{"\$"} is_opener($token, $custom_tags)
Truth table75%3/4
A and B or C
A= $tags{$token->{"tag"}}B= $tags{$token->{"tag"}} <= $tags{"\$"}C= is_opener($token, $custom_tags)
ABCresult
0XX0
1000
11X1
1011
246 69 push @$stack, $token;
247 69 $token->{'nodes'} = build_tree($tokens, $token->{'tag'}, $stack, $custom_tags);
248 }
249 elsif ($token->{'tag'} eq '/') {
250 69 ▶ if (!@$stack) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$stack) 0 69 69
251 0 die "Closing tag without opener: /$token->{'n'}";
252 }
253 69 $opener = pop @$stack;
254 69 ▶ if ($token->{'n'} ne $opener->{'n'} && !is_closer($token->{'n'}, $opener->{'n'}, $custom_tags)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->{"n"} ne $opener->{"n"} and not is_closer($token->{"n"}, $opener->{"n"}, $custom_tags)) 0 69 69
Condition33%1/3
$token->{"n"} ne $opener->{"n"} and not is_closer($token->{"n"}, $opener->{"n"}, $custom_tags)
!ll&&!rl&&r
6900
MC/DC0%0/2
$token->{"n"} ne $opener->{"n"} and not is_closer($token->{"n"}, $opener->{"n"}, $custom_tags)
$token->{"n"} ne $opener->{"n"} not is_closer($token->{"n"}, $opener->{"n"}, $custom_tags)
Truth table33%1/3
A and not(B)
A= $token->{"n"} ne $opener->{"n"}B= not is_closer($token->{"n"}, $opener->{"n"}, $custom_tags)
ABresult
0X0
100
111
255 0 die "Nesting error: $opener->{'n'} vs $token->{'n'}";
256 }
257 69 $opener->{'end'} = $token->{'i'};
258 69 return \@instructions;
259 }
260 elsif ($token->{'tag'} eq "\n") {
261 99 ▶ $token->{'last'} = (@$tokens == 0) || ($tokens->[0]{'tag'} eq "\n");
Condition66%2/3
@$tokens == 0 || $tokens->[0]{'tag'} eq "\n"
l!l&&r!l&&!r
35064
MC/DC50%1/2
@$tokens == 0 || $tokens->[0]{'tag'} eq "\n"
@$tokens == 0 $tokens->[0]{'tag'} eq "\n"
Truth table66%2/3
A || B
A= @$tokens == 0B= $tokens->[0]{'tag'} eq "\n"
ABresult
1X1
011
000
262 }
263  
264 623 push @instructions, $token;
265 }
266  
267 145 ▶ if (@$stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$stack) 0 145 145
268 0 die "Missing closing tag: ", pop(@$stack)->{'n'};
269 }
270  
271 145 return \@instructions;
272 }
273  
274 524 ▶sub is_opener {
Subroutine is_opener: called CC=3 SCAR=16.0
Pod undocumented
275 524 my ($token, $tags) = @_;
276  
277 524 ▶ for (my $i = 0, my $l = scalar(@$tags); $i < $l; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $l) 0 524 524
278 0 ▶ if ($tags->[$i]{'o'} eq $token->{'n'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tags->[$i]{"o"} eq $token->{"n"}) 0 0 0
279 0 $token->{'tag'} = '#';
280 0 return 1;
281 }
282 }
283  
284 524 return 0;
285 }
286  
287 0 ▶sub is_closer {
Subroutine is_closer: not called CC=4 SCAR=30.0
Pod undocumented
288 0 my ($close, $open, $tags) = @_;
289  
290 0 ▶ for (my $i = 0, my $l = scalar(@$tags); $i < $l; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $l) 0 0 0
291 0 ▶ if (($tags->[$i]{'c'} eq $close) && ($tags->[$i]{'o'} eq $open)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tags->[$i]{"c"} eq $close and $tags->[$i]{"o"} eq $open) 0 0 0
Condition0%0/3
$tags->[$i]{"c"} eq $close and $tags->[$i]{"o"} eq $open
!ll&&!rl&&r
000
MC/DC0%0/2
$tags->[$i]{"c"} eq $close and $tags->[$i]{"o"} eq $open
$tags->[$i]{"c"} eq $close $tags->[$i]{"o"} eq $open
Truth table0%0/3
A and B
A= $tags->[$i]{"c"} eq $closeB= $tags->[$i]{"o"} eq $open
ABresult
0X0
100
111
292 0 return 1;
293 }
294 }
295  
296 0 return 0;
297 }
298  
299 0 ▶sub stringify_substitutions {
Subroutine stringify_substitutions: not called CC=2 SCAR=17.9
Pod undocumented
300 0 my $obj = shift;
301  
302 0 my @items;
303 0 for my $key (sort keys %$obj) {
304 0 push @items, sprintf('"%s" => sub { my ($self,$c,$p,$t,$i) = @_; %s }', esc($key), $obj->{$key});
305 }
306  
307 0 return sprintf("{ %s }", join(', ', @items));
308 }
309  
310 0 ▶sub stringify_partials {
Subroutine stringify_partials: not called CC=2 SCAR=17.9
Pod undocumented
311 0 my $code_obj = shift;
312  
313 0 my @partials;
314 0 for my $key (sort keys %{ $code_obj->{'partials'} }) {
315 push @partials, sprintf('"%s" => { "name" => "%s", %s }', $key,
316 esc($code_obj->{'partials'}{$key}{'name'}),
317 0 stringify_partials($code_obj->{'partials'}{$key})
318 );
319 }
320  
321 return sprintf('"partials" => { %s }, "subs" => %s',
322 join(',', @partials),
323 0 stringify_substitutions($code_obj->{'subs'})
324 );
325 }
326  
327 0 ▶sub stringify {
Subroutine stringify: not called CC=1 SCAR=6.9
Pod undocumented
328 0 my ($self,$code_obj, $text, $options) = @_;
329 return sprintf('{ code => sub { my ($t,$c,$p,$i) = @_; %s }, %s }',
330 0 wrap_main($code_obj->{'code'}),
331 stringify_partials($code_obj)
332 );
333 }
334  
335 4 my $serial_no = 0;
336 145 ▶sub generate {
Subroutine generate: called CC=2 SCAR=7.1
Pod documented
337 145 my ($self, $tree, $text, $options) = @_;
338  
339 145 $serial_no = 0;
340  
341 145 my $context = { 'code' => "", 'subs' => {}, 'partials' => {} };
342 145 walk($tree, $context);
343  
344 145 ▶ if ($options->{'as_string'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"as_string"}) 0 145 145
345 0 return $self->stringify($context, $text, $options);
346 }
347  
348 145 return $self->make_template($context, $text, $options);
349 }
350  
351 145 ▶sub fuse {
Subroutine fuse: called CC=2 SCAR=6.9
Pod undocumented
352 145 my ($c) = @_;
353 145 ▶ 1 while $c =~ s/\);\$t->b\(/ . /g;
Branch: 1 branch
BranchTrueFalseTotal
$c =~ s/\);\$t->b\(/ . /g and do { '???' } 101 145 246
354 145 return $c;
355 }
356  
357 145 ▶sub wrap_main {
Subroutine wrap_main: called CC=1 SCAR=0.0
Pod undocumented
358 145 my ($code) = @_;
359 145 return fuse(sprintf('$t->b($i = $i || ""); %s return $t->fl();', $code));
360 }
361  
362 145 ▶sub make_template {
Subroutine make_template: called CC=1 SCAR=0.0
Pod undocumented
363 145 my $self = shift;
364 145 my ($code_obj, $text, $options) = @_;
365  
366 145 my $template = make_partials($code_obj);
367 145 $template->{'code'} = eval sprintf("sub { my (\$t, \$c, \$p, \$i) = \@_; %s }", wrap_main($code_obj->{'code'}));
368 145 return $Template->new($template, $text, $self, $options);
369 }
370  
371 160 ▶sub make_partials {
Subroutine make_partials: called CC=3 SCAR=11.0
Pod undocumented
372 160 my ($code_obj) = @_;
373  
374 160 my $key;
375 my $template = {
376 'subs' => {},
377 'partials' => $code_obj->{'partials'},
378 160 'name' => $code_obj->{'name'},
379 };
380  
381 160 for my $key (sort keys %{ $template->{'partials'} }) {
382 15 $template->{'partials'}{$key} = make_partials($template->{'partials'}{$key});
383 }
384  
385 160 for my $key (sort keys %{ $code_obj->{'subs'} }) {
386 0 $template->{'subs'}{$key} = eval "sub { my (\$t, \$c, \$p, \$t, \$i) = \@_; $code_obj->{subs}{$key}; }";
387 }
388  
389 160 return $template;
390 }
391  
392 511 ▶sub esc {
Subroutine esc: called CC=1 SCAR=0.0
Pod undocumented
393 511 my $s = shift;
394  
395 # standard from hogan.js
396 511 $s =~ s/$r_slash/\\\\/g;
397 511 $s =~ s/$r_quot/\\\"/g;
398 511 $s =~ s/$r_newline/\\n/g;
399 511 $s =~ s/$r_cr/\\r/g;
400 511 $s =~ s/$r_linesep/\\u2028/g;
401 511 $s =~ s/$r_paragraphsep/\\u2029/g;
402  
403 # specific for Text::Hogan / Perl
404 511 $s =~ s/\$/\\\$/g;
405 511 $s =~ s/\@/\\@/g;
406 511 $s =~ s/\%/\\%/g;
407  
408 511 return $s;
409 }
410  
411 13767 ▶sub char_at {
Subroutine char_at: called CC=2 SCAR=6.9
Pod undocumented
412 13767 my ($text, $index) = @_;
413 13767 ▶ if (is_arrayref($text)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_arrayref($text)) 9423 4344 13767
414 9423 return $text->[$index];
415 }
416 4344 return substr($text, $index, 1);
417 }
418  
419 184 ▶sub choose_method {
Subroutine choose_method: called CC=2 SCAR=6.9
Pod undocumented
420 184 my ($s) = @_;
421 184 ▶ return $s =~ m/[.]/ ? "d" : "f";
Branch: 1 branch
BranchTrueFalseTotal
$s =~ /[.]/ ? : 15 169 184
422 }
423  
424 15 ▶sub create_partial {
Subroutine create_partial: called CC=3 SCAR=11.0
Pod undocumented
425 15 my ($node, $context) = @_;
426  
427 15 ▶ my $prefix = "<" . ($context->{'prefix'} || "");
Condition50%1/2
$context->{'prefix'} || ''
l!l
015
MC/DC0%0/1
$context->{'prefix'} || ''
$context->{'prefix'}
Truth table50%1/2
A
A= $context->{'prefix'}
Aresult
11
00
428 15 my $sym = $prefix . $node->{'n'} . $serial_no++;
429 $context->{'partials'}{$sym} = {
430 15 'name' => $node->{'n'},
431 'partials' => {},
432 };
433 $context->{'code'} .= sprintf('$t->b($t->rp("%s",$c,$p,"%s"));',
434 esc($sym),
435 15 ▶ ($node->{'indent'} || "")
Condition100%2/2
$node->{'indent'} || ''
l!l
312
MC/DC100%1/1
$node->{'indent'} || ''
$node->{'indent'}
Truth table100%2/2
A
A= $node->{'indent'}
Aresult
11
00
436 );
437  
438 15 return $sym;
439 }
440  
441 my %codegen = (
442 44 ▶ '#' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
443 44 my ($node, $context) = @_;
444 $context->{'code'} .= sprintf('if($t->s($t->%s("%s",$c,$p,1),$c,$p,0,%s,%s,"%s %s")) { $t->rs($c,$p,sub { my ($c,$p,$t) = @_;',
445 choose_method($node->{'n'}),
446 esc($node->{'n'}),
447 44 @{$node}{qw/ i end otag ctag /},
448 );
449 44 walk($node->{'nodes'}, $context);
450 44 $context->{'code'} .= '}); pop @$c;}';
451 },
452 25 ▶ '^' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
453 25 my ($node, $context) = @_;
454 $context->{'code'} .= sprintf('if (!$t->s($t->%s("%s",$c,$p,1),$c,$p,1,0,0,"")){',
455 choose_method($node->{'n'}),
456 25 esc($node->{'n'})
457 );
458 25 walk($node->{'nodes'}, $context);
459 25 $context->{'code'} .= "};";
460 },
461 '>' => \&create_partial,
462 0 ▶ '<' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
463 0 my ($node, $context) = @_;
464 0 my $ctx = { 'partials' => {}, 'code' => "", 'subs' => {}, 'in_partial' => 1 };
465 0 walk($node->{'nodes'}, $ctx);
466 0 my $template = $context->{'partials'}{create_partial($node, $context)};
467 0 $template->{'subs'} = $ctx->{'subs'};
468 0 $template->{'partials'} = $ctx->{'partials'};
469 },
470 0 ▶ '$' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
471 0 my ($node, $context) = @_;
472 0 my $ctx = { 'subs' => {}, 'code' => "", 'partials' => $context->{'partials'}, 'prefix' => $node->{'n'} };
473 0 walk($node->{'nodes'}, $ctx);
474 0 $context->{'subs'}{$node->{'n'}} = $ctx->{'code'};
475 0 ▶ if (!$context->{'in_partial'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $context->{"in_partial"}) 0 0 0
476 $context->{'code'} += sprintf('$t->sub("%s",$c,$p,$i);',
477 0 esc($node->{'n'})
478 );
479 }
480 },
481 99 ▶ "\n" => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
482 99 my ($node, $context) = @_;
483 99 ▶ $context->{'code'} .= twrite(sprintf('"\n"%s', ($node->{'last'} ? "" : ' . $i')));
Branch: 1 branch
BranchTrueFalseTotal
$node->{'last'} ? : 35 64 99
484 },
485 93 ▶ '_v' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
486 93 my ($node, $context) = @_;
487 $context->{'code'} .= twrite(sprintf('$t->v($t->%s("%s",$c,$p,0))',
488 choose_method($node->{'n'}),
489 93 esc($node->{'n'})
490 ));
491 },
492 312 ▶ '_t' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
493 312 my ($node, $context) = @_;
494 312 $context->{'code'} .= twrite(sprintf('"%s"', esc($node->{'text'})));
495 },
496 4 '{' => \&triple_stache,
497 '&' => \&triple_stache,
498 );
499  
500 22 ▶sub triple_stache {
Subroutine triple_stache: called CC=1 SCAR=0.0
Pod undocumented
501 22 my ($node, $context) = @_;
502 $context->{'code'} .= sprintf('$t->b($t->t($t->%s("%s",$c,$p,0)));',
503 choose_method($node->{'n'}),
504 22 esc($node->{'n'})
505 );
506 }
507  
508 504 ▶sub twrite { sprintf '$t->b(%s);', @_ }
Subroutine twrite: called CC=1 SCAR=0.0
Pod undocumented
509  
510 214 ▶sub walk {
Subroutine walk: called CC=3 SCAR=11.0
Pod undocumented
511 214 my ($nodelist, $context) = @_;
512  
513 214 for my $node (@$nodelist) {
514 623 ▶ my $func = $codegen{$node->{'tag'}} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $func = $codegen{$node->{"tag"}} or next 610 13 623
515 610 $func->($node, $context);
516 }
517  
518 214 return $context;
519 }
520  
521 145 ▶sub parse {
Subroutine parse: called CC=3 SCAR=11.1
Pod documented
522 145 my ($self, $tokens, $text, $options) = @_;
523 145 ▶ $options ||= {};
Condition100%2/2
$options ||= {}
l!l
1441
MC/DC100%1/1
$options ||= {}
$options
Truth table100%2/2
A
A= $options
Aresult
11
00
524 145 ▶ return build_tree($tokens, "", [], $options->{'section_tags'} || []);
Condition50%1/2
$options->{'section_tags'} || []
l!l
0145
MC/DC0%0/1
$options->{'section_tags'} || []
$options->{'section_tags'}
Truth table50%1/2
A
A= $options->{'section_tags'}
Aresult
11
00
525 }
526  
527 4 my %cache;
528  
529 159 ▶sub cache_key {
Subroutine cache_key: called CC=3 SCAR=11.0
Pod undocumented
530 159 my ($text, $options) = @_;
531 159 ▶ return join('||', $text, !!$options->{'as_string'}, !!$options->{'numeric_string_as_string'}, !!$options->{'disable_lambda'}, ($options->{'delimiters'} || ""), ($options->{'allow_whitespace_before_hashmark'} || 0));
Condition100%4/4
$options->{'delimiters'} || ''
l!l
5154
$options->{'allow_whitespace_before_hashmark'} || 0
l!l
1158
MC/DC100%1/1
$options->{'delimiters'} || ''
$options->{'delimiters'}
MC/DC100%1/1
$options->{'allow_whitespace_before_hashmark'} || 0
$options->{'allow_whitespace_before_hashmark'}
Truth table100%2/2
A
A= $options->{'delimiters'}
Aresult
11
00
Truth table100%2/2
A
A= $options->{'allow_whitespace_before_hashmark'}
Aresult
11
00
532 }
533  
534 159 ▶sub compile {
Subroutine compile: called CC=5 SCAR=16.1
Pod documented
535 159 my ($self, $text, $options) = @_;
536 159 ▶ $options ||= {};
Condition100%2/2
$options ||= {}
l!l
32127
MC/DC100%1/1
$options ||= {}
$options
Truth table100%2/2
A
A= $options
Aresult
11
00
537  
538 159 ▶ $text //= "";
Condition100%2/2
$text //= ""
l!l
1581
MC/DC100%1/1
$text //= ""
$text
Truth table100%2/2
A
A= $text
Aresult
11
00
539  
540 159 my $key = cache_key($text, $options);
541 159 my $template = $cache{$key};
542  
543 159 ▶ if ($template) {
Branch: 1 branch
BranchTrueFalseTotal
if ($template) 15 144 159
544 15 my $partials = $template->{'partials'};
545 15 for my $name (sort keys %{ $template->{'partials'} }) {
546 2 delete $partials->{$name}{'instance'};
547 }
548 15 return $template;
549 }
550  
551 144 $template = $self->generate(
552 $self->parse(
553 $self->scan($text, $options), $text, $options
554 ), $text, $options
555 );
556  
557 144 return $cache{$key} = $template;
558 }
559  
560 4 1;
561  
562 __END__
« Hogan/Template.pm ↑ Summary Hogan.pm »