| 1 | package Dancer2::Template::Simple; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Pure Perl 5 template engine for Dancer2 | ||||||||||||||||||||||||||||
| 3 | 2 | $Dancer2::Template::Simple::VERSION = '2.0.0'; | |||||||||||||||||||||||||||
| 4 | 2 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Dancer2::FileUtils 'read_file_content'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Ref::Util qw<is_arrayref is_coderef is_plain_hashref>; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | with 'Dancer2::Core::Role::Template'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | has start_tag => ( | ||||||||||||||||||||||||||||
| 11 | is => 'rw', | ||||||||||||||||||||||||||||
| 12 | 1 | ▶ | default => sub {'<%'}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 2 | ); | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | has stop_tag => ( | ||||||||||||||||||||||||||||
| 16 | is => 'rw', | ||||||||||||||||||||||||||||
| 17 | 1 | ▶ | default => sub {'%>'}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 2 | ); | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 21 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 22 | 1 | my $settings = $self->config; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | $settings->{$_} and $self->$_( $settings->{$_} ) | ||||||||||||||||||||||||||||
| 25 | 1 | ▶ | for qw/ start_tag stop_tag /; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 3 | ▶ | sub render { | ||||||||||||||||||||||||||
|
Subroutine
render: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 29 | 3 | my ( $self, $template, $tokens ) = @_; | |||||||||||||||||||||||||||
| 30 | 3 | my $content; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 3 | $content = read_file_content($template); | |||||||||||||||||||||||||||
| 33 | 1 | $content = $self->parse_branches( $content, $tokens ); | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 1 | return $content; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 1 | ▶ | sub parse_branches { | ||||||||||||||||||||||||||
|
Subroutine
parse_branches: called CC=13 SCAR=29.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 39 | 1 | my ( $self, $content, $tokens ) = @_; | |||||||||||||||||||||||||||
| 40 | 1 | my ( $start, $stop ) = ( $self->start_tag, $self->stop_tag ); | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 1 | my @buffer; | |||||||||||||||||||||||||||
| 43 | 1 | my $should_bufferize = 1; | |||||||||||||||||||||||||||
| 44 | 1 | my $bufferize_if_token = 0; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # $content =~ s/\Q${start}\E(\S)/${start} $1/sg; | ||||||||||||||||||||||||||||
| 47 | # $content =~ s/(\S)\Q${stop}\E/$1 ${stop}/sg; | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # we get here a list of tokens without the start/stop tags | ||||||||||||||||||||||||||||
| 50 | 1 | my @full = split( /\Q$start\E\s*(.*?)\s*\Q$stop\E/, $content ); | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # and here a list of tokens without variables | ||||||||||||||||||||||||||||
| 53 | 1 | my @flat = split( /\Q$start\E\s*.*?\s*\Q$stop\E/, $content ); | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # eg: for 'foo=<% var %>' | ||||||||||||||||||||||||||||
| 56 | # @full = ('foo=', 'var') | ||||||||||||||||||||||||||||
| 57 | # @flat = ('foo=') | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 1 | my $flat_index = 0; | |||||||||||||||||||||||||||
| 60 | 1 | my $full_index = 0; | |||||||||||||||||||||||||||
| 61 | 1 | for my $word (@full) { | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | # flat word, nothing to do | ||||||||||||||||||||||||||||
| 64 | 15 | ▶ | if ( defined $flat[$flat_index] | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $flat[$flat_index] and $flat[$flat_index] eq $full[$full_index]
MC/DC50%1/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 table66%2/3
A and B
A= defined $flat[$flat_index]B= $flat[$flat_index] eq $full[$full_index]
| |||||||||||||||||||||||||||||
| 65 | && ( $flat[$flat_index] eq $full[$full_index] ) ) | ||||||||||||||||||||||||||||
| 66 | { | ||||||||||||||||||||||||||||
| 67 | 8 | ▶ | push @buffer, $word if $should_bufferize; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 8 | $flat_index++; | |||||||||||||||||||||||||||
| 69 | 8 | $full_index++; | |||||||||||||||||||||||||||
| 70 | 8 | next; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 7 | my @to_parse = ($word); | |||||||||||||||||||||||||||
| 74 | 7 | ▶ | @to_parse = split( /\s+/, $word ) if $word =~ /\s+/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 7 | for my $w (@to_parse) { | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 7 | ▶ | if ( $w eq 'if' ) { | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 79 | 0 | $bufferize_if_token = 1; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | elsif ( $w eq 'else' ) { | ||||||||||||||||||||||||||||
| 82 | 0 | $should_bufferize = !$should_bufferize; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | elsif ( $w eq 'end' ) { | ||||||||||||||||||||||||||||
| 85 | 0 | $should_bufferize = 1; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | elsif ($bufferize_if_token) { | ||||||||||||||||||||||||||||
| 88 | 0 | my $bool = _find_value_from_token_name( $w, $tokens ); | |||||||||||||||||||||||||||
| 89 | 0 | ▶ | $should_bufferize = _interpolate_value($bool) ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 0 | $bufferize_if_token = 0; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | elsif ($should_bufferize) { | ||||||||||||||||||||||||||||
| 93 | 7 | my $val = | |||||||||||||||||||||||||||
| 94 | _interpolate_value( | ||||||||||||||||||||||||||||
| 95 | _find_value_from_token_name( $w, $tokens ) ); | ||||||||||||||||||||||||||||
| 96 | 7 | push @buffer, $val; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 7 | $full_index++; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 1 | return join "", @buffer; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 7 | ▶ | sub _find_value_from_token_name { | ||||||||||||||||||||||||||
|
Subroutine
_find_value_from_token_name: called CC=6 SCAR=26.2
| |||||||||||||||||||||||||||||
| 108 | 7 | my ( $key, $tokens ) = @_; | |||||||||||||||||||||||||||
| 109 | 7 | my $value = undef; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 7 | my @elements = split /\./, $key; | |||||||||||||||||||||||||||
| 112 | 7 | foreach my $e (@elements) { | |||||||||||||||||||||||||||
| 113 | 7 | ▶ | if ( not defined $value ) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 114 | 7 | $value = $tokens->{$e}; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | elsif ( is_plain_hashref($value) ) { | ||||||||||||||||||||||||||||
| 117 | 0 | $value = $value->{$e}; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | elsif ( ref($value) ) { | ||||||||||||||||||||||||||||
| 120 | 0 | local $@; | |||||||||||||||||||||||||||
| 121 | 0 | eval { $value = $value->$e }; | |||||||||||||||||||||||||||
| 122 | 0 | ▶ | $value = "" if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | 7 | return $value; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 7 | ▶ | sub _interpolate_value { | ||||||||||||||||||||||||||
|
Subroutine
_interpolate_value: called CC=5 SCAR=23.7
| |||||||||||||||||||||||||||||
| 129 | 7 | my ($value) = @_; | |||||||||||||||||||||||||||
| 130 | 7 | ▶ | if ( is_coderef($value) ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 131 | 0 | local $@; | |||||||||||||||||||||||||||
| 132 | 0 | eval { $value = $value->() }; | |||||||||||||||||||||||||||
| 133 | 0 | ▶ | $value = "" if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | elsif ( is_arrayref($value) ) { | ||||||||||||||||||||||||||||
| 136 | 0 | $value = "@{$value}"; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 7 | ▶ | $value = "" if not defined $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 7 | return $value; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 2 | 1; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | __END__ | ||||||||||||||||||||||||||||