blib/lib/Dancer/Template/Simple.pm

filter: Statementstmt 100% 93 / 93 Branchbran 86.3% 38 / 44 Conditioncond 100% 7 / 7 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 12 / 12 Podpod 66.6% 2 / 3 totaltotal 95.7% 156 / 163 SCAR 33.7
CC
29
Cov
96%
parse_branches
13.0
_find_value_from_token_name
6.0
init
5.1
CRAP 29.1
« lib/Dancer/Route/Cache.pm ↑ Summary blib/lib/Dancer/Template/Abstract.pm »
1 package Dancer::Template::Simple;
2 235 our $AUTHORITY = 'cpan:SUKRIA';
3 #ABSTRACT: pure Perl 5 template engine for Dancer
4 235 $Dancer::Template::Simple::VERSION = '1.3520';
5 235 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 235 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 235 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 235 ▶use base 'Dancer::Template::Abstract';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 235 Dancer::Template::Simple->attributes('start_tag', 'stop_tag');
11 235 ▶use Dancer::FileUtils 'read_file_content';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 235 ▶use Dancer::Exception qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 sub init {
15 288 ▶ my $self = shift;
Subroutine init: called CC=5 SCAR=16.2
Pod documented
16 288 my $settings = $self->config;
17  
18 288 ▶ my $start = $settings->{'start_tag'} || '<%';
Condition100%2/2
$settings->{'start_tag'} || '<%'
l!l
1287
MC/DC100%1/1
$settings->{'start_tag'} || '<%'
$settings->{'start_tag'}
Truth table100%2/2
A
A= $settings->{'start_tag'}
Aresult
11
00
19 288 ▶ my $stop = $settings->{'stop_tag'} || '%>';
Condition100%2/2
$settings->{'stop_tag'} || '%>'
l!l
1287
MC/DC100%1/1
$settings->{'stop_tag'} || '%>'
$settings->{'stop_tag'}
Truth table100%2/2
A
A= $settings->{'stop_tag'}
Aresult
11
00
20  
21 288 ▶ $self->start_tag($start) unless defined $self->start_tag;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->start_tag 288 0 288
22 288 ▶ $self->stop_tag($stop) unless defined $self->stop_tag;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->stop_tag 288 0 288
23 }
24  
25 sub render {
26 98 ▶ my ($self, $template, $tokens) = @_;
Subroutine render: called CC=1 SCAR=0.0
Pod documented
27 98 my $content;
28  
29 98 $content = _read_content_from_template($template);
30 97 $content = $self->parse_branches($content, $tokens);
31  
32 97 return $content;
33 }
34  
35 sub parse_branches {
36 97 ▶ my ($self, $content, $tokens) = @_;
Subroutine parse_branches: called CC=13 SCAR=25.7
Pod undocumented
37 97 my ($start, $stop) = ($self->start_tag, $self->stop_tag);
38  
39 97 my @buffer;
40 97 my $prefix = "";
41 97 my $should_bufferize = 1;
42 97 my $bufferize_if_token = 0;
43  
44 # $content =~ s/\Q${start}\E(\S)/${start} $1/sg;
45 # $content =~ s/(\S)\Q${stop}\E/$1 ${stop}/sg;
46  
47 # we get here a list of tokens without the start/stop tags
48 97 my @full = split(/\Q$start\E\s*(.*?)\s*\Q$stop\E/, $content);
49  
50 # and here a list of tokens without variables
51 97 my @flat = split(/\Q$start\E\s*.*?\s*\Q$stop\E/, $content);
52  
53 # eg: for 'foo=<% var %>'
54 # @full = ('foo=', 'var')
55 # @flat = ('foo=')
56  
57 97 my $flat_index = 0;
58 97 my $full_index = 0;
59 97 for my $word (@full) {
60  
61 # flat word, nothing to do
62 735 ▶ if (defined $flat[$flat_index]
Branch: 1 branch
BranchTrueFalseTotal
if (defined $flat[$flat_index] and $flat[$flat_index] eq $full[$full_index]) 414 321 735
Condition100%3/3
defined $flat[$flat_index] and $flat[$flat_index] eq $full[$full_index]
!ll&&!rl&&r
4317414
MC/DC100%2/2
defined $flat[$flat_index] and $flat[$flat_index] eq $full[$full_index]
defined $flat[$flat_index] $flat[$flat_index] eq $full[$full_index]
Truth table100%3/3
A and B
A= defined $flat[$flat_index]B= $flat[$flat_index] eq $full[$full_index]
ABresult
0X0
100
111
63 && ($flat[$flat_index] eq $full[$full_index]))
64 {
65 414 ▶ push @buffer, $word if $should_bufferize;
Branch: 1 branch
BranchTrueFalseTotal
if $should_bufferize 412 2 414
66 414 $flat_index++;
67 414 $full_index++;
68 414 next;
69 }
70  
71 321 my @to_parse = ($word);
72 321 ▶ @to_parse = split(/\s+/, $word) if $word =~ /\s+/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ /\s+/ 2 319 321
73  
74 321 for my $w (@to_parse) {
75  
76 323 ▶ if ($w eq 'if') {
Branch: 5 branches
BranchTrueFalseTotal
if ($w eq 'if') { } 2 321 323
elsif ($w eq 'else') { } 2 319 321
elsif ($w eq 'end') { } 2 317 319
elsif ($bufferize_if_token) { } 2 315 317
elsif ($should_bufferize) { } 315 0 315
77 2 $bufferize_if_token = 1;
78 }
79 elsif ($w eq 'else') {
80 2 $should_bufferize = !$should_bufferize;
81 }
82 elsif ($w eq 'end') {
83 2 $should_bufferize = 1;
84 }
85 elsif ($bufferize_if_token) {
86 2 my $bool = _find_value_from_token_name($w, $tokens);
87 2 ▶ $should_bufferize = _interpolate_value($bool) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
_interpolate_value($bool) ? : 1 1 2
88 2 $bufferize_if_token = 0;
89 }
90 elsif ($should_bufferize) {
91 315 my $val =
92 _interpolate_value(_find_value_from_token_name($w, $tokens));
93 315 push @buffer, $val;
94 }
95 }
96  
97 321 $full_index++;
98 }
99  
100 97 return join "", @buffer;
101 }
102  
103 # private
104  
105 sub _read_content_from_template {
106 98 ▶ my ($template) = @_;
Subroutine _read_content_from_template: called CC=4 SCAR=13.9
107 98 my $content = undef;
108  
109 98 ▶ if (ref($template)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $template) { } 46 52 98
110 46 $content = $$template;
111 }
112 else {
113 52 ▶ raise core_template => "'$template' is not a regular file"
Branch: 1 branch
BranchTrueFalseTotal
unless -f $template 1 51 52
114 unless -f $template;
115 51 $content = read_file_content($template);
116 51 ▶ raise core_template => "unable to read content for file $template"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content 0 51 51
117 if not defined $content;
118 }
119 97 return $content;
120 }
121  
122 sub _find_value_from_token_name {
123 317 ▶ my ($key, $tokens) = @_;
Subroutine _find_value_from_token_name: called CC=6 SCAR=17.9
124 317 my $value = undef;
125  
126 317 my @elements = split /\./, $key;
127 317 foreach my $e (@elements) {
128 348 ▶ if (not defined $value) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $value) { } 317 31 348
elsif (ref $value eq 'HASH') { } 20 11 31
elsif (ref $value) { } 11 0 11
129 317 $value = $tokens->{$e};
130 }
131 elsif (ref($value) eq 'HASH') {
132 20 $value = $value->{$e};
133 }
134 elsif (ref($value)) {
135 11 local $@;
136 11 eval { $value = $value->$e };
137 11 ▶ $value = "" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 10 11
138 }
139 }
140 317 return $value;
141 }
142  
143 sub _interpolate_value {
144 317 ▶ my ($value) = @_;
Subroutine _interpolate_value: called CC=5 SCAR=16.1
145 317 ▶ if (ref($value) eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'CODE') { } 2 315 317
elsif (ref $value eq 'ARRAY') { } 2 313 315
146 2 local $@;
147 2 eval { $value = $value->() };
148 2 ▶ $value = "" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
149 }
150 elsif (ref($value) eq 'ARRAY') {
151 2 $value = "@{$value}";
152 }
153  
154 317 ▶ $value = "" if not defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 8 309 317
155 317 return $value;
156 }
157  
158 235 1;
159  
160 __END__
« lib/Dancer/Route/Cache.pm ↑ Summary blib/lib/Dancer/Template/Abstract.pm »