Venus/Template.pm

filter: Statementstmt 80.7% 88 / 109 Branchbran 65.6% 21 / 32 Conditioncond 64.7% 11 / 17 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 75.0% 15 / 20 Podpod 10.0% 1 / 10 totaltotal 71.0% 140 / 197 CC 23 SCAR 16.8
CC
23
Cov
76%
mappable
12.1
render_if_not
12.0
render_blocks
4.1
CRAP 5.4
« Venus/Match.pm ↑ Summary Venus/Json.pm »
1 package Venus::Template;
2  
3 2 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Venus::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 base 'Venus::Kind::Utility';
11  
12 2 with 'Venus::Role::Accessible';
13 2 with 'Venus::Role::Explainable';
14  
15 use overload (
16 '""' => 'explain',
17 0 ▶ '.' => sub{$_[0]->render . "$_[1]"},
Subroutine __ANON__: not called CC=1 SCAR=6.9
18 0 ▶ 'eq' => sub{$_[0]->render eq "$_[1]"},
Subroutine __ANON__: not called CC=1 SCAR=6.9
19 0 ▶ 'ne' => sub{$_[0]->render ne "$_[1]"},
Subroutine __ANON__: not called CC=1 SCAR=6.9
20 0 ▶ 'qr' => sub{qr/@{[quotemeta($_[0]->render)]}/},
Subroutine __ANON__: not called CC=1 SCAR=6.9
21 2 '~~' => 'explain',
22 fallback => 1,
23 2 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 # ATTRIBUTES
26  
27 2 attr 'markers';
28 2 attr 'variables';
29  
30 # BUILDERS
31  
32 sub build_self {
33 10 ▶ my ($self, $data) = @_;
Subroutine build_self: called CC=3 SCAR=11.4
Pod undocumented
34  
35 10 ▶ $self->markers([qr/\{\{/, qr/\}\}/]) if !defined $self->markers;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->markers 10 0 10
36 10 ▶ $self->variables({}) if !defined $self->variables;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->variables 10 0 10
37  
38 10 return $self;
39 }
40  
41 # METHODS
42  
43 sub default {
44 1 ▶ return '';
Subroutine default: called CC=1 SCAR=0.0
Pod undocumented
45 }
46  
47 sub explain {
48 5 ▶ my ($self) = @_;
Subroutine explain: called CC=1 SCAR=0.0
Pod undocumented
49  
50 5 return $self->render;
51 }
52  
53 sub mappable {
54 119 ▶ my ($self, $data) = @_;
Subroutine mappable: called CC=10 SCAR=25.0
Pod undocumented
55  
56 119 require Scalar::Util;
57 119 require Venus::Array;
58 119 require Venus::Hash;
59  
60 119 ▶ if (!$data) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 0 119 119
61 0 return Venus::Hash->new;
62 }
63 119 ▶ if (!Scalar::Util::blessed($data) && ref($data) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (not builtin::blessed($data) and ref $data eq "ARRAY") 3 116 119
Condition100%3/3
not builtin::blessed($data) and ref $data eq "ARRAY"
!ll&&!rl&&r
31853
MC/DC100%2/2
not builtin::blessed($data) and ref $data eq "ARRAY"
not builtin::blessed($data) ref $data eq "ARRAY"
Truth table100%3/3
not(A) and B
A= not builtin::blessed($data)B= ref $data eq "ARRAY"
ABresult
0X0
100
111
64 3 return Venus::Array->new($data);
65 }
66 116 ▶ if (!Scalar::Util::blessed($data) && ref($data) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (not builtin::blessed($data) and ref $data eq "HASH") 85 31 116
Condition66%2/3
not builtin::blessed($data) and ref $data eq "HASH"
!ll&&!rl&&r
31085
MC/DC50%1/2
not builtin::blessed($data) and ref $data eq "HASH"
not builtin::blessed($data) ref $data eq "HASH"
Truth table66%2/3
not(A) and B
A= not builtin::blessed($data)B= ref $data eq "HASH"
ABresult
0X0
100
111
67 85 return Venus::Hash->new($data);
68 }
69 31 ▶ if (!Scalar::Util::blessed($data) || (Scalar::Util::blessed($data)
Branch: 1 branch
BranchTrueFalseTotal
if (not builtin::blessed($data) or builtin::blessed($data) and not $data->isa('Venus::Array') || $data->isa('Venus::Hash')) { } 0 31 31
Condition44%4/9
not builtin::blessed($data) or builtin::blessed($data) and not $data->isa("Venus::Array") || $data->isa("Venus::Hash")
l!l&&r!l&&!r
0031
builtin::blessed($data) and not $data->isa("Venus::Array") || $data->isa("Venus::Hash")
!ll&&!rl&&r
0310
$data->isa('Venus::Array') || $data->isa('Venus::Hash')
l!l&&r!l&&!r
6250
MC/DC0%0/4
not builtin::blessed($data) or builtin::blessed($data) and not $data->isa("Venus::Array") || $data->isa("Venus::Hash")
not builtin::blessed($data) builtin::blessed($data) $data->isa('Venus::Array') $data->isa('Venus::Hash')
Truth table40%2/5
not(A) or B and not(C || D)
A= not builtin::blessed($data)B= builtin::blessed($data)C= $data->isa('Venus::Array')D= $data->isa('Venus::Hash')
ABCDresult
1XXX1
01001
00XX0
011X0
01010
70 && !($data->isa('Venus::Array') || $data->isa('Venus::Hash'))))
71 {
72 0 return Venus::Hash->new;
73 }
74 else {
75 31 return $data;
76 }
77 }
78  
79 sub render {
80 30 ▶ my ($self, $content, $variables) = @_;
Subroutine render: called CC=3 SCAR=11.0
Pod documented
81  
82 30 ▶ if (!defined $content) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $content) 14 16 30
83 14 $content = $self->get;
84 }
85  
86 30 ▶ if (!defined $variables) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $variables) { } 14 16 30
87 14 $variables = $self->variables;
88 }
89 else {
90 16 $variables = $self->mappable($self->variables)->merge(
91 $self->mappable($variables)->get
92 );
93 }
94  
95 30 $content =~ s/^\r?\n//;
96 30 $content =~ s/\r?\n\ *$//;
97  
98 30 $content = $self->render_blocks($content, $variables);
99  
100 30 $content = $self->render_tokens($content, $variables);
101  
102 30 return $content;
103 }
104  
105 sub render_blocks {
106 30 ▶ my ($self, $content, $variables) = @_;
Subroutine render_blocks: called CC=4 SCAR=14.0
Pod undocumented
107  
108 30 my ($stag, $etag) = @{$self->markers};
109  
110 30 my $path = qr/[a-z_][\w.]*/;
111  
112 30 my $regexp = qr{
113 $stag
114 \s*
115 (FOR|IF|IF\sNOT)
116 \s+
117 ($path)
118 \s*
119 $etag
120 (.+)
121 $stag
122 \s*
123 (END)
124 \s+
125 \2
126 \s*
127 $etag
128 }xis;
129  
130 30 $variables = $self->mappable($variables);
131  
132 30 $content =~ s{
133 $regexp
134 }{
135 12 my ($type, $path, $body) = ($1, $2, $3);
136 12 ▶ if (lc($type) eq 'if') {
Branch: 3 branches
BranchTrueFalseTotal
if (lc $type eq 'if') { } 9 3 12
elsif (lc $type eq 'if not') { } 0 3 3
elsif (lc $type eq 'for') { } 3 0 3
137 9 $self->render_if(
138 $body, $variables, !!scalar($variables->path($path)), $path
139 );
140 }
141 elsif (lc($type) eq 'if not') {
142 0 $self->render_if_not(
143 $body, $variables, !!scalar($variables->path($path)), $path
144 );
145 }
146 elsif (lc($type) eq 'for') {
147 3 $self->render_foreach(
148 $body, $self->mappable($variables->path($path))
149 );
150 }
151 }gsex;
152  
153 30 return $content;
154 }
155  
156 sub render_if {
157 9 ▶ my ($self, $context, $variables, $boolean, $path) = @_;
Subroutine render_if: called CC=3 SCAR=11.0
Pod undocumented
158  
159 9 my $mappable = $self->mappable($variables);
160  
161 9 my ($stag, $etag) = @{$self->markers};
162  
163 9 $path = quotemeta $path;
164  
165 9 my $regexp = qr{
166 $stag
167 \s*
168 ELSE
169 \s+
170 $path
171 \s*
172 $etag
173 }xis;
174  
175 9 my ($a, $b) = split /$regexp/, $context;
176  
177 9 ▶ if ($boolean) {
Branch: 1 branch
BranchTrueFalseTotal
if ($boolean) { } 5 4 9
178 5 return $self->render($a, $mappable);
179 }
180 else {
181 4 ▶ if ($b) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b) { } 2 2 4
182 2 return $self->render($b, $mappable);
183 }
184 else {
185 2 return '';
186 }
187 }
188 }
189  
190 sub render_if_not {
191 0 ▶ my ($self, $context, $variables, $boolean, $path) = @_;
Subroutine render_if_not: not called CC=3 SCAR=24.8
Pod undocumented
192  
193 0 my $mappable = $self->mappable($variables);
194  
195 0 my ($stag, $etag) = @{$self->markers};
196  
197 0 $path = quotemeta $path;
198  
199 0 my $regexp = qr{
200 $stag
201 \s*
202 ELSE
203 \s+
204 $path
205 \s*
206 $etag
207 }xis;
208  
209 0 my ($a, $b) = split /$regexp/, $context;
210  
211 0 ▶ if (!$boolean) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $boolean) { } 0 0 0
212 0 return $self->render($a, $mappable);
213 }
214 else {
215 0 ▶ if ($b) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b) { } 0 0 0
216 0 return $self->render($b, $mappable);
217 }
218 else {
219 0 return '';
220 }
221 }
222 }
223  
224 sub render_foreach {
225 3 ▶ my ($self, $context, $mappable) = @_;
Subroutine render_foreach: called CC=2 SCAR=7.1
Pod undocumented
226  
227 3 $mappable = $self->mappable($mappable);
228  
229 3 ▶ if (!$mappable->isa('Venus::Array')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mappable->isa("Venus::Array")) 0 3 3
230 0 return '';
231 }
232  
233 my @results = $self->mappable($mappable)->each(sub {
234 9 ▶ $self->render($context, $self->mappable($_));
Subroutine __ANON__: called CC=1 SCAR=0.0
235 3 });
236  
237 3 return join "\n", grep !!$_, @results;
238 }
239  
240 sub render_tokens {
241 30 ▶ my ($self, $content, $variables) = @_;
Subroutine render_tokens: called CC=2 SCAR=6.9
Pod undocumented
242  
243 30 my ($stag, $etag) = @{$self->markers};
244  
245 30 my $path = qr/[a-z_][\w.]*/;
246  
247 30 my $regexp = qr{
248 $stag
249 \s*
250 ($path)
251 \s*
252 $etag
253 }xi;
254  
255 30 $variables = $self->mappable($variables);
256  
257 30 $content =~ s{
258 $regexp
259 }{
260 27 ▶ scalar($variables->path($1)) // ''
Condition100%2/2
scalar $variables->path($1) // ""
l!l
234
MC/DC100%1/1
scalar $variables->path($1) // ""
scalar $variables->path($1)
Truth table100%2/2
A
A= scalar $variables->path($1)
Aresult
11
00
261 }gsex;
262  
263 30 return $content;
264 }
265  
266 2 1;
267  
268  
269  
270 =head1 NAME
271  
272 Venus::Template - Template Class
273  
274 =cut
275  
276 =head1 ABSTRACT
277  
278 Template Class for Perl 5
279  
280 =cut
281  
282 =head1 SYNOPSIS
283  
284 package main;
285  
286 use Venus::Template;
287  
288 my $template = Venus::Template->new(
289 'From: <{{ email }}>',
290 );
291  
292 # $template->render;
293  
294 # "From: <>"
295  
296 =cut
297  
298 =head1 DESCRIPTION
299  
300 This package provides a templating system, and methods for rendering templates
301 using simple markup and minimal control structures. The default opening and
302 closing markers, denoting a template token, block, or control structure, are
303 C<{{> and C<}}>. A token takes the form of C<{{ foo }}> or C<{{ foo.bar }}>. A
304 block takes the form of C<{{ for foo.bar }}> where C<foo.bar> represents any
305 valid path, resolvable by L<Venus::Array/path> or L<Venus::Hash/path>, which
306 returns an arrayref or L<Venus::Array> object, and must be followed by
307 C<{{ end foo }}>. Control structures take the form of C<{{ if foo }}> or
308 C<{{ if not foo }}>, may contain a nested C<{{ else foo }}> control structure,
309 and must be followed by C<{{ end foo }}>. Leading and trailing whitespace is
310 automatically removed from all replacements.
311  
312 =cut
313  
314 =head1 ATTRIBUTES
315  
316 This package has the following attributes:
317  
318 =cut
319  
320 =head2 variables
321  
322 variables(HashRef)
323  
324 This attribute is read-write, accepts C<(HashRef)> values, is optional, and defaults to C<{}>.
325  
326 =cut
327  
328 =head1 INHERITS
329  
330 This package inherits behaviors from:
331  
332 L<Venus::Kind::Utility>
333  
334 =cut
335  
336 =head1 INTEGRATES
337  
338 This package integrates behaviors from:
339  
340 L<Venus::Role::Accessible>
341  
342 L<Venus::Role::Explainable>
343  
344 =cut
345  
346 =head1 METHODS
347  
348 This package provides the following methods:
349  
350 =cut
351  
352 =head2 render
353  
354 The render method processes the template by replacing the tokens and control
355 structurs with the appropriate replacements and returns the result.
356  
357 =over 4
358  
359 =item render example 1
360  
361 # given: synopsis;
362  
363 my $result = $template->render;
364  
365 # "From: <>"
366  
367 =back
368  
369 =over 4
370  
371 =item render example 2
372  
373 # given: synopsis;
374  
375 $template->value(
376 'From: {{ if name }}{{ name }}{{ end name }} <{{ email }}>',
377 );
378  
379 $template->variables({
380 email => 'noreply@example.com',
381 });
382  
383 my $result = $template->render;
384  
385 # "From: <noreply@example.com>"
386  
387 =back
388  
389 =over 4
390  
391 =item render example 3
392  
393 # given: synopsis;
394  
395 $template->value(
396 'From: {{ if name }}{{ name }}{{ end name }} <{{ email }}>',
397 );
398  
399 $template->variables({
400 name => 'No-Reply',
401 email => 'noreply@example.com',
402 });
403  
404 my $result = $template->render;
405  
406 # "From: No-Reply <noreply@example.com>"
407  
408 =back
409  
410 =over 4
411  
412 =item render example 4
413  
414 package main;
415  
416 use Venus::Template;
417  
418 my $template = Venus::Template->new(q(
419 {{ for chat.messages }}
420 {{ user.name }}: {{ message }}
421 {{ end chat.messages }}
422 ));
423  
424 $template->variables({
425 chat => { messages => [
426 { user => { name => 'user1' }, message => 'ready?' },
427 { user => { name => 'user2' }, message => 'ready!' },
428 { user => { name => 'user1' }, message => 'lets begin!' },
429 ]}
430 });
431  
432 my $result = $template->render;
433  
434 # user1: ready?
435 # user2: ready!
436 # user1: lets begin!
437  
438 =back
439  
440 =over 4
441  
442 =item render example 5
443  
444 package main;
445  
446 use Venus::Template;
447  
448 my $template = Venus::Template->new(q(
449 {{ for chat.messages }}
450 {{ if user.legal }}
451 {{ user.name }} [18+]: {{ message }}
452 {{ else user.legal }}
453 {{ user.name }} [-18]: {{ message }}
454 {{ end user.legal }}
455 {{ end chat.messages }}
456 ));
457  
458 $template->variables({
459 chat => { messages => [
460 { user => { name => 'user1', legal => 1 }, message => 'ready?' },
461 { user => { name => 'user2', legal => 0 }, message => 'ready!' },
462 { user => { name => 'user1', legal => 1 }, message => 'lets begin!' },
463 ]}
464 });
465  
466 my $result = $template->render;
467  
468 # user1 [18+]: ready?
469 # user2 [-18]: ready!
470 # user1 [18+]: lets begin!
471  
472 =back
473  
474 =over 4
475  
476 =item render example 6
477  
478 package main;
479  
480 use Venus::Template;
481  
482 my $template = Venus::Template->new(q(
483 {{ for chat.messages }}
484 {{ if user.admin }}@{{ end user.admin }}{{ user.name }}: {{ message }}
485 {{ end chat.messages }}
486 ));
487  
488 $template->variables({
489 chat => { messages => [
490 { user => { name => 'user1', admin => 1 }, message => 'ready?' },
491 { user => { name => 'user2', admin => 0 }, message => 'ready!' },
492 { user => { name => 'user1', admin => 1 }, message => 'lets begin!' },
493 ]}
494 });
495  
496 my $result = $template->render;
497  
498 # @user1: ready?
499 # user2: ready!
500 # @user1: lets begin!
501  
502 =back
503  
504 =cut
505  
506 =head1 OPERATORS
507  
508 This package overloads the following operators:
509  
510 =cut
511  
512 =over 4
513  
514 =item operation: C<("")>
515  
516 This package overloads the C<""> operator.
517  
518 B<example 1>
519  
520 # given: synopsis;
521  
522 my $result = "$template";
523  
524 # "From: <>"
525  
526 =back
527  
528 =over 4
529  
530 =item operation: C<(~~)>
531  
532 This package overloads the C<~~> operator.
533  
534 B<example 1>
535  
536 # given: synopsis;
537  
538 my $result = $template ~~ 'From: <>';
539  
540 # 1
541  
542 =back
« Venus/Match.pm ↑ Summary Venus/Json.pm »