| 1 | package Dancer::Template::Simple; | ||||||||||||||||||||||||||||
| 2 | 244 | our $AUTHORITY = 'cpan:SUKRIA'; | |||||||||||||||||||||||||||
| 3 | #ABSTRACT: pure Perl 5 template engine for Dancer | ||||||||||||||||||||||||||||
| 4 | 244 | $Dancer::Template::Simple::VERSION = '1.3522'; | |||||||||||||||||||||||||||
| 5 | 244 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 244 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 244 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 244 | ▶ | use base 'Dancer::Template::Abstract'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 244 | Dancer::Template::Simple->attributes('start_tag', 'stop_tag'); | |||||||||||||||||||||||||||
| 11 | 244 | ▶ | use Dancer::FileUtils 'read_file_content'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 244 | ▶ | use Dancer::Exception qw(:all); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 405 | ▶ | sub init { | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||
| 15 | 405 | my $self = shift; | |||||||||||||||||||||||||||
| 16 | 405 | my $settings = $self->config; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 405 | ▶ | my $start = $settings->{'start_tag'} || '<%'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$settings->{'start_tag'} || '<%'
MC/DC100%1/1
$settings->{'start_tag'} || '<%'
$settings->{'start_tag'}
Truth table100%2/2
A
A= $settings->{'start_tag'}
| |||||||||||||||||||||||||||||
| 19 | 405 | ▶ | my $stop = $settings->{'stop_tag'} || '%>'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$settings->{'stop_tag'} || '%>'
MC/DC100%1/1
$settings->{'stop_tag'} || '%>'
$settings->{'stop_tag'}
Truth table100%2/2
A
A= $settings->{'stop_tag'}
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 405 | ▶ | $self->start_tag($start) unless defined $self->start_tag; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 405 | ▶ | $self->stop_tag($stop) unless defined $self->stop_tag; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 206 | ▶ | sub render { | ||||||||||||||||||||||||||
|
Subroutine
render: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 26 | 206 | my ($self, $template, $tokens) = @_; | |||||||||||||||||||||||||||
| 27 | 206 | my $content; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 206 | $content = _read_content_from_template($template); | |||||||||||||||||||||||||||
| 30 | 205 | $content = $self->parse_branches($content, $tokens); | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 205 | return $content; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 205 | ▶ | sub parse_branches { | ||||||||||||||||||||||||||
|
Subroutine
parse_branches: called CC=13 SCAR=25.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 36 | 205 | my ($self, $content, $tokens) = @_; | |||||||||||||||||||||||||||
| 37 | 205 | my ($start, $stop) = ($self->start_tag, $self->stop_tag); | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 205 | my @buffer; | |||||||||||||||||||||||||||
| 40 | 205 | my $prefix = ""; | |||||||||||||||||||||||||||
| 41 | 205 | my $should_bufferize = 1; | |||||||||||||||||||||||||||
| 42 | 205 | 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 | 205 | my @full = split(/\Q$start\E\s*(.*?)\s*\Q$stop\E/, $content); | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # and here a list of tokens without variables | ||||||||||||||||||||||||||||
| 51 | 205 | 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 | 205 | my $flat_index = 0; | |||||||||||||||||||||||||||
| 58 | 205 | my $full_index = 0; | |||||||||||||||||||||||||||
| 59 | 205 | for my $word (@full) { | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # flat word, nothing to do | ||||||||||||||||||||||||||||
| 62 | 855 | ▶ | if (defined $flat[$flat_index] | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $flat[$flat_index] and $flat[$flat_index] eq $full[$full_index]
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]
| |||||||||||||||||||||||||||||
| 63 | && ($flat[$flat_index] eq $full[$full_index])) | ||||||||||||||||||||||||||||
| 64 | { | ||||||||||||||||||||||||||||
| 65 | 528 | ▶ | push @buffer, $word if $should_bufferize; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 528 | $flat_index++; | |||||||||||||||||||||||||||
| 67 | 528 | $full_index++; | |||||||||||||||||||||||||||
| 68 | 528 | next; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 327 | my @to_parse = ($word); | |||||||||||||||||||||||||||
| 72 | 327 | ▶ | @to_parse = split(/\s+/, $word) if $word =~ /\s+/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 327 | for my $w (@to_parse) { | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 329 | ▶ | if ($w eq 'if') { | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 88 | 2 | $bufferize_if_token = 0; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | elsif ($should_bufferize) { | ||||||||||||||||||||||||||||
| 91 | 321 | my $val = | |||||||||||||||||||||||||||
| 92 | _interpolate_value(_find_value_from_token_name($w, $tokens)); | ||||||||||||||||||||||||||||
| 93 | 321 | push @buffer, $val; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 327 | $full_index++; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 205 | return join "", @buffer; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | # private | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 206 | ▶ | sub _read_content_from_template { | ||||||||||||||||||||||||||
|
Subroutine
_read_content_from_template: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 106 | 206 | my ($template) = @_; | |||||||||||||||||||||||||||
| 107 | 206 | my $content = undef; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 206 | ▶ | if (ref($template)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 154 | $content = $$template; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | else { | ||||||||||||||||||||||||||||
| 113 | 52 | ▶ | raise core_template => "'$template' is not a regular file" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 117 | if not defined $content; | ||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | 205 | return $content; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 323 | ▶ | sub _find_value_from_token_name { | ||||||||||||||||||||||||||
|
Subroutine
_find_value_from_token_name: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 123 | 323 | my ($key, $tokens) = @_; | |||||||||||||||||||||||||||
| 124 | 323 | my $value = undef; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 323 | my @elements = split /\./, $key; | |||||||||||||||||||||||||||
| 127 | 323 | foreach my $e (@elements) { | |||||||||||||||||||||||||||
| 128 | 354 | ▶ | if (not defined $value) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 129 | 323 | $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
| |||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | 323 | return $value; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 323 | ▶ | sub _interpolate_value { | ||||||||||||||||||||||||||
|
Subroutine
_interpolate_value: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 144 | 323 | my ($value) = @_; | |||||||||||||||||||||||||||
| 145 | 323 | ▶ | if (ref($value) eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 146 | 2 | local $@; | |||||||||||||||||||||||||||
| 147 | 2 | eval { $value = $value->() }; | |||||||||||||||||||||||||||
| 148 | 2 | ▶ | $value = "" if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | elsif (ref($value) eq 'ARRAY') { | ||||||||||||||||||||||||||||
| 151 | 2 | $value = "@{$value}"; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 323 | ▶ | $value = "" if not defined $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 323 | return $value; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 244 | 1; | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | __END__ | ||||||||||||||||||||||||||||