| 1 | package Text::MustacheTemplate::Compiler; | ||||||||||||||||||||||||||||||
| 2 | 13 | ▶ | use 5.022000; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 3 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 4 | 13 | ▶ | use warnings; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||
| 6 | 13 | ▶ | use B (); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 7 | 13 | ▶ | use List::Util qw/none/; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 8 | 13 | ▶ | use Carp qw/croak/; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||
| 10 | 13 | ▶ | use Text::MustacheTemplate::Lexer; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 11 | 13 | ▶ | use Text::MustacheTemplate::Parser qw/:syntaxes :variables :boxes :references/; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 12 | 13 | ▶ | use Text::MustacheTemplate::Evaluator qw/retrieve_variable evaluate_section_variable evaluate_section/; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 13 | 13 | ▶ | use Text::MustacheTemplate::HTML qw/escape_html/; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||
| 15 | 13 | ▶ | use constant { | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 16 | DEBUG => !!$ENV{PERL_TEXT_MUSTACHE_TEMPLATE_COMPILER_DEBUG}, | ||||||||||||||||||||||||||||||
| 17 | 13 | DISCARD_RESULT => '##DISCARD##', | |||||||||||||||||||||||||||||
| 18 | 13 | }; | |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||
| 20 | 13 | our @CONTEXT_HINT; # for optimize | |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||
| 22 | 13 | our $_PADDING; | |||||||||||||||||||||||||||||
| 23 | 13 | our $_PARENT; | |||||||||||||||||||||||||||||
| 24 | 13 | our $_DEFAULT_OPEN_DELIMITER; | |||||||||||||||||||||||||||||
| 25 | 13 | our $_DEFAULT_CLOSE_DELIMITER; | |||||||||||||||||||||||||||||
| 26 | 13 | our $_CURRENT_OPEN_DELIMITER; | |||||||||||||||||||||||||||||
| 27 | 13 | our $_CURRENT_CLOSE_DELIMITER; | |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||
| 29 | 527 | ▶ | sub compile { | ||||||||||||||||||||||||||||
|
Subroutine
compile: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 30 | 527 | my ($class, $ast) = @_; | |||||||||||||||||||||||||||||
| 31 | 527 | ▶ | die "Invalid AST: empty AST" unless @$ast; # uncoverable branch true | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||
| 33 | 527 | my @ast = @$ast; | |||||||||||||||||||||||||||||
| 34 | 527 | my $first_delimiter_syntax = shift @ast; | |||||||||||||||||||||||||||||
| 35 | 527 | my ($type, $open_delimiter, $close_delimiter) = @$first_delimiter_syntax; | |||||||||||||||||||||||||||||
| 36 | 527 | ▶ | if ($type != SYNTAX_DELIMITER) { # uncoverable branch true | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 37 | 0 | croak "Invalid AST: Delimiter should be first syntax"; # uncoverable statement | |||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||
| 40 | 527 | @ast = do { | |||||||||||||||||||||||||||||
| 41 | 527 | local $_DEFAULT_OPEN_DELIMITER = $open_delimiter; | |||||||||||||||||||||||||||||
| 42 | 527 | local $_DEFAULT_CLOSE_DELIMITER = $close_delimiter; | |||||||||||||||||||||||||||||
| 43 | 527 | _optimize(\@ast, 0); | |||||||||||||||||||||||||||||
| 44 | }; | ||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||
| 46 | # Optimize: empty | ||||||||||||||||||||||||||||||
| 47 | 527 | ▶ | return sub { '' } if @ast == 0; | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||
| 49 | # Optimize: raw text only | ||||||||||||||||||||||||||||||
| 50 | 526 | ▶ | if (@ast == 1 && $ast[0][0] == SYNTAX_RAW_TEXT) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@ast == 1 and $ast[0][0] == 0
MC/DC100%2/2
@ast == 1 and $ast[0][0] == 0
@ast == 1
$ast[0][0] == 0
Truth table100%3/3
A and B
A= @ast == 1B= $ast[0][0] == 0
| |||||||||||||||||||||||||||||||
| 51 | 134 | my (undef, $text) = @{ $ast[0] }; | |||||||||||||||||||||||||||||
| 52 | 134 | ▶ | if (!@CONTEXT_HINT && $text =~ /[\r\n](?!\z)/mano) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not @Text::MustacheTemplate::Compiler::CONTEXT_HINT and $text =~ /[\r\n](?!\z)/amno
MC/DC100%2/2
not @Text::MustacheTemplate::Compiler::CONTEXT_HINT and $text =~ /[\r\n](?!\z)/amno
not @Text::MustacheTemplate::Compiler::CONTEXT_HINT
$text =~ /[\r\n](?!\z)/amno
Truth table100%3/3
not(A) and B
A= not @Text::MustacheTemplate::Compiler::CONTEXT_HINTB= $text =~ /[\r\n](?!\z)/amno
| |||||||||||||||||||||||||||||||
| 53 | 17 | ▶ | return sub { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 54 | 17 | ▶ | defined $_PADDING ? $text =~ s/(\r\n?|\n)(?!\z)/${1}${_PADDING}/mgar : $text | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 55 | 17 | }; | |||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||
| 57 | 117 | ▶ | return sub { $text }; | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||
| 60 | 392 | my $code = eval { | |||||||||||||||||||||||||||||
| 61 | 392 | local $_PARENT; | |||||||||||||||||||||||||||||
| 62 | 392 | local $_DEFAULT_OPEN_DELIMITER = $open_delimiter; | |||||||||||||||||||||||||||||
| 63 | 392 | local $_DEFAULT_CLOSE_DELIMITER = $close_delimiter; | |||||||||||||||||||||||||||||
| 64 | 392 | local $_CURRENT_OPEN_DELIMITER = $open_delimiter; | |||||||||||||||||||||||||||||
| 65 | 392 | local $_CURRENT_CLOSE_DELIMITER = $close_delimiter; | |||||||||||||||||||||||||||||
| 66 | 392 | _generate_code(\@ast, 4); | |||||||||||||||||||||||||||||
| 67 | }; | ||||||||||||||||||||||||||||||
| 68 | 392 | ▶ | die "Invalid AST: $@" if "$@"; # uncoverable branch true | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||
| 70 | # wrap to define function global variables | ||||||||||||||||||||||||||||||
| 71 | 392 | $code = <<__CODE__; | |||||||||||||||||||||||||||||
| 72 | do { | ||||||||||||||||||||||||||||||
| 73 | our (\%_BLOCKS, \@_CTX, \$_OPEN_DELIMITER, \$_CLOSE_DELIMITER); | ||||||||||||||||||||||||||||||
| 74 | my (\$_name, \$_tmp, \@_section); | ||||||||||||||||||||||||||||||
| 75 | $code | ||||||||||||||||||||||||||||||
| 76 | }; | ||||||||||||||||||||||||||||||
| 77 | __CODE__ | ||||||||||||||||||||||||||||||
| 78 | 392 | warn $code if DEBUG; # uncoverable branch true | |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||
| 80 | 392 | my $f = eval $code; | |||||||||||||||||||||||||||||
| 81 | 392 | ▶ | die $@ if $@; # uncoverable branch true | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 82 | 392 | return $f; | |||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||
| 85 | 865 | ▶ | sub _optimize { | ||||||||||||||||||||||||||||
|
Subroutine
_optimize: called CC=27 SCAR=33.0
| |||||||||||||||||||||||||||||||
| 86 | 865 | my ($ast, $depth) = @_; | |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||
| 88 | 865 | my $raw_text_syntax; | |||||||||||||||||||||||||||||
| 89 | my @optimized_ast; | ||||||||||||||||||||||||||||||
| 90 | 865 | for my $syntax (@$ast) { | |||||||||||||||||||||||||||||
| 91 | 2022 | ▶ | if ($syntax->[0] == SYNTAX_RAW_TEXT) { | ||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||
| 92 | 1155 | ▶ | if ($raw_text_syntax) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 93 | 135 | $raw_text_syntax->[1] .= $syntax->[1]; | |||||||||||||||||||||||||||||
| 94 | } else { | ||||||||||||||||||||||||||||||
| 95 | 1020 | $raw_text_syntax = $syntax; | |||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||
| 97 | } elsif ($syntax->[0] == SYNTAX_COMMENT) { | ||||||||||||||||||||||||||||||
| 98 | # ignore | ||||||||||||||||||||||||||||||
| 99 | } elsif ($syntax->[0] == SYNTAX_DELIMITER) { | ||||||||||||||||||||||||||||||
| 100 | # keep it and keep raw text syntax context both | ||||||||||||||||||||||||||||||
| 101 | 35 | push @optimized_ast => $syntax; | |||||||||||||||||||||||||||||
| 102 | } else { | ||||||||||||||||||||||||||||||
| 103 | 802 | ▶ | if (@CONTEXT_HINT && $depth == 0) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@Text::MustacheTemplate::Compiler::CONTEXT_HINT and $depth == 0
MC/DC100%2/2
@Text::MustacheTemplate::Compiler::CONTEXT_HINT and $depth == 0
@Text::MustacheTemplate::Compiler::CONTEXT_HINT
$depth == 0
Truth table100%3/3
A and B
A= @Text::MustacheTemplate::Compiler::CONTEXT_HINTB= $depth == 0
| |||||||||||||||||||||||||||||||
| 104 | 209 | ▶ | if ($syntax->[0] == SYNTAX_VARIABLE) { | ||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 105 | 68 | my (undef, $type, $name) = @$syntax; | |||||||||||||||||||||||||||||
| 106 | 68 | local our $_OPEN_DELIMITER = $_DEFAULT_OPEN_DELIMITER; | |||||||||||||||||||||||||||||
| 107 | 68 | local our $_CLOSE_DELIMITER = $_DEFAULT_CLOSE_DELIMITER; | |||||||||||||||||||||||||||||
| 108 | 68 | local our @_CTX = @CONTEXT_HINT; | |||||||||||||||||||||||||||||
| 109 | 68 | ▶ | local $Text::MustacheTemplate::Evaluator::LAMBDA_RENDERER = \&_render_template_in_context if $Text::MustacheTemplate::LAMBDA_TEMPLATE_RENDERING; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||
| 111 | 68 | ▶ | my $value = $name eq '.' ? $_CTX[-1] : retrieve_variable(\@_CTX, split /\./ano, $name); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 112 | 68 | ▶ | next unless $value; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 113 | 60 | ▶ | if ($type == VARIABLE_HTML_ESCAPE) { # uncoverable branch false count:2 | ||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 114 | 40 | $value = escape_html($value); | |||||||||||||||||||||||||||||
| 115 | } elsif ($type == VARIABLE_RAW) { | ||||||||||||||||||||||||||||||
| 116 | # nothing to do | ||||||||||||||||||||||||||||||
| 117 | } else { | ||||||||||||||||||||||||||||||
| 118 | 0 | die "Unknown variable type: $type"; # uncoverable statement | |||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||
| 121 | 60 | ▶ | if ($raw_text_syntax) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 122 | 54 | $raw_text_syntax->[1] .= $value; | |||||||||||||||||||||||||||||
| 123 | } else { | ||||||||||||||||||||||||||||||
| 124 | 6 | $raw_text_syntax = [SYNTAX_RAW_TEXT, $value]; | |||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||
| 126 | 60 | next; | |||||||||||||||||||||||||||||
| 127 | } elsif ($syntax->[0] == SYNTAX_BOX) { | ||||||||||||||||||||||||||||||
| 128 | 107 | my (undef, $type, $name) = @$syntax; | |||||||||||||||||||||||||||||
| 129 | 107 | ▶ | if ($type == BOX_SECTION) { | ||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 130 | 55 | local our $_OPEN_DELIMITER = $_DEFAULT_OPEN_DELIMITER; | |||||||||||||||||||||||||||||
| 131 | 55 | local our $_CLOSE_DELIMITER = $_DEFAULT_CLOSE_DELIMITER; | |||||||||||||||||||||||||||||
| 132 | 55 | local our @_CTX = @CONTEXT_HINT; | |||||||||||||||||||||||||||||
| 133 | 55 | ▶ | local $Text::MustacheTemplate::Evaluator::LAMBDA_RENDERER = \&_render_template_in_context if $Text::MustacheTemplate::LAMBDA_TEMPLATE_RENDERING; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 134 | 55 | ▶ | next unless $name eq '.' ? evaluate_section($_CTX[-1]) : evaluate_section_variable(\@_CTX, split /\./ano, $name); | ||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 135 | } elsif ($type == BOX_INVERTED_SECTION) { | ||||||||||||||||||||||||||||||
| 136 | 28 | local our $_OPEN_DELIMITER = $_DEFAULT_OPEN_DELIMITER; | |||||||||||||||||||||||||||||
| 137 | 28 | local our $_CLOSE_DELIMITER = $_DEFAULT_CLOSE_DELIMITER; | |||||||||||||||||||||||||||||
| 138 | 28 | local our @_CTX = @CONTEXT_HINT; | |||||||||||||||||||||||||||||
| 139 | 28 | ▶ | local $Text::MustacheTemplate::Evaluator::LAMBDA_RENDERER = \&_render_template_in_context if $Text::MustacheTemplate::LAMBDA_TEMPLATE_RENDERING; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 140 | 28 | ▶ | next if $name eq '.' ? evaluate_section($_CTX[-1]) : evaluate_section_variable(\@_CTX, split /\./ano, $name); | ||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||
| 145 | 720 | ▶ | if ($raw_text_syntax) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 146 | 387 | push @optimized_ast => $raw_text_syntax; | |||||||||||||||||||||||||||||
| 147 | 387 | $raw_text_syntax = undef; | |||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||
| 149 | 720 | ▶ | if ($syntax->[0] == SYNTAX_BOX) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 150 | 338 | my @children = _optimize($syntax->[-1], $depth+1); | |||||||||||||||||||||||||||||
| 151 | 338 | $syntax = [@$syntax]; # shallow copy | |||||||||||||||||||||||||||||
| 152 | 338 | $syntax->[-1] = \@children; | |||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||
| 154 | 720 | push @optimized_ast => $syntax; | |||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||
| 157 | 865 | ▶ | if ($raw_text_syntax) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 158 | 639 | push @optimized_ast => $raw_text_syntax; | |||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||
| 160 | 865 | return @optimized_ast; | |||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||
| 163 | 430 | ▶ | sub _generate_code { | ||||||||||||||||||||||||||||
|
Subroutine
_generate_code: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||
| 164 | 430 | my ($ast, $indent) = @_; | |||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||
| 166 | 430 | my $initial_text = ''; | |||||||||||||||||||||||||||||
| 167 | # Optimize: remove first raw text and fill initial text if no contains new lines | ||||||||||||||||||||||||||||||
| 168 | 430 | ▶ | if ($ast->[0]->[0] == SYNTAX_RAW_TEXT && $ast->[0]->[1] !~ /[\r\n]/mano) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$ast->[0][0] == 0 and not $ast->[0][1] =~ /[\r\n]/amno
MC/DC100%2/2
$ast->[0][0] == 0 and not $ast->[0][1] =~ /[\r\n]/amno
$ast->[0][0] == 0
not $ast->[0][1] =~ /[\r\n]/amno
Truth table100%3/3
A and not(B)
A= $ast->[0][0] == 0B= not $ast->[0][1] =~ /[\r\n]/amno
| |||||||||||||||||||||||||||||||
| 169 | 244 | my (undef, $text) = @{ shift @$ast }; | |||||||||||||||||||||||||||||
| 170 | 244 | $initial_text = $text; | |||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||
| 173 | 430 | my $initial_text_perl = B::perlstring($initial_text); | |||||||||||||||||||||||||||||
| 174 | 430 | my $default_open_delimiter_perl = B::perlstring($_DEFAULT_OPEN_DELIMITER); | |||||||||||||||||||||||||||||
| 175 | 430 | my $default_close_delimiter_perl = B::perlstring($_DEFAULT_CLOSE_DELIMITER); | |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||
| 177 | 430 | my $code = ''; | |||||||||||||||||||||||||||||
| 178 | 430 | $code .= (' ' x $indent)."sub {\n"; | |||||||||||||||||||||||||||||
| 179 | 430 | $code .= (' ' x $indent)." local \@_CTX = \@_;\n"; | |||||||||||||||||||||||||||||
| 180 | 430 | $code .= (' ' x $indent)." local (\$_OPEN_DELIMITER, \$_CLOSE_DELIMITER) = ($default_open_delimiter_perl, $default_close_delimiter_perl);\n"; | |||||||||||||||||||||||||||||
| 181 | 430 | ▶ | $code .= (' ' x $indent)." local \$Text::MustacheTemplate::Evaluator::LAMBDA_RENDERER = \\&_render_template_in_context;\n" if $indent == 4; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 182 | 430 | $code .= "\n"; | |||||||||||||||||||||||||||||
| 183 | 430 | $code .= (' ' x $indent)." my \$_result = $initial_text_perl;\n"; | |||||||||||||||||||||||||||||
| 184 | 430 | $code .= _generate_body($ast, $indent+4, '$_result'); | |||||||||||||||||||||||||||||
| 185 | 430 | $code .= (' ' x $indent)." return \$_result;\n"; | |||||||||||||||||||||||||||||
| 186 | 430 | $code .= (' ' x $indent)."};\n"; | |||||||||||||||||||||||||||||
| 187 | 430 | return $code; | |||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||
| 190 | 730 | ▶ | sub _generate_body { | ||||||||||||||||||||||||||||
|
Subroutine
_generate_body: called CC=18 SCAR=28.9
| |||||||||||||||||||||||||||||||
| 191 | 730 | my ($ast, $indent, $result) = @_; | |||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||
| 193 | 730 | my $code = ''; | |||||||||||||||||||||||||||||
| 194 | 730 | for my $i (keys @$ast) { | |||||||||||||||||||||||||||||
| 195 | 1403 | my $syntax = $ast->[$i]; | |||||||||||||||||||||||||||||
| 196 | 1403 | my ($type) = @$syntax; | |||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||
| 198 | # uncoverable branch true count:6 | ||||||||||||||||||||||||||||||
| 199 | # uncoverable branch false count:5..6 | ||||||||||||||||||||||||||||||
| 200 | 1403 | ▶ | if ($type == SYNTAX_RAW_TEXT) { | ||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||
| 201 | 648 | my (undef, $text) = @$syntax; | |||||||||||||||||||||||||||||
| 202 | 648 | ▶ | next if $result eq DISCARD_RESULT; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 203 | 637 | ▶ | if ($i == $#{$ast} ? $text =~ /[\r\n](?!\z)/mano : $text =~ /[\r\n]/mano) { | ||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 204 | 109 | my $regex = '(\r\n?|\n)'; | |||||||||||||||||||||||||||||
| 205 | 109 | ▶ | $regex .= '(?!\z)' if $i == $#{$ast}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 206 | 109 | $code .= (' ' x $indent).'$_tmp = '.B::perlstring($text).";\n"; | |||||||||||||||||||||||||||||
| 207 | 109 | $code .= (' ' x $indent)."\$_tmp =~ s/$regex/\${1}\${_PADDING}/mag if defined \$_PADDING;\n"; | |||||||||||||||||||||||||||||
| 208 | 109 | $code .= (' ' x $indent)."$result .= \$_tmp;\n"; | |||||||||||||||||||||||||||||
| 209 | } else { | ||||||||||||||||||||||||||||||
| 210 | 528 | $code .= (' ' x $indent).$result.' .= '.B::perlstring($text).";\n"; | |||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||
| 212 | } elsif ($type == SYNTAX_VARIABLE) { | ||||||||||||||||||||||||||||||
| 213 | 303 | $code .= _generate_variable($syntax, $indent, $result); | |||||||||||||||||||||||||||||
| 214 | } elsif ($type == SYNTAX_DELIMITER) { | ||||||||||||||||||||||||||||||
| 215 | 35 | my (undef, $open_delimiter, $close_delimiter) = @$syntax; | |||||||||||||||||||||||||||||
| 216 | 35 | ($_CURRENT_OPEN_DELIMITER, $_CURRENT_CLOSE_DELIMITER) = ($open_delimiter, $close_delimiter); | |||||||||||||||||||||||||||||
| 217 | } elsif ($type == SYNTAX_BOX) { | ||||||||||||||||||||||||||||||
| 218 | 338 | $code .= _generate_box($syntax, $indent, $result); | |||||||||||||||||||||||||||||
| 219 | } elsif ($type == SYNTAX_PARTIAL) { | ||||||||||||||||||||||||||||||
| 220 | 79 | my (undef, $reference, $name, $padding) = @$syntax; | |||||||||||||||||||||||||||||
| 221 | 79 | ▶ | $padding = B::perlstring($padding) if $padding; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||
| 223 | # uncoverable branch false count:2 | ||||||||||||||||||||||||||||||
| 224 | 79 | ▶ | my $retriever = $reference == REFERENCE_DYNAMIC ? ($name eq '.' ? '$_CTX[-1]' : 'retrieve_variable(\@_CTX, '.(join ', ', map B::perlstring($_), split /\./ano, $name).')') | ||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||
| 225 | : $reference == REFERENCE_STATIC ? B::perlstring($name) | ||||||||||||||||||||||||||||||
| 226 | : die "Unknown reference: $reference"; | ||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||
| 228 | 79 | $code .= (' ' x $indent)."\$_name = $retriever;\n"; | |||||||||||||||||||||||||||||
| 229 | 79 | $code .= (' ' x $indent)."$result .= do {\n"; | |||||||||||||||||||||||||||||
| 230 | 79 | ▶ | $code .= (' ' x $indent)." local \$_PADDING;\n" unless $padding; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 231 | 79 | ▶ | $code .= (' ' x $indent)." local \$_PADDING = $padding;\n" if $padding; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 232 | 79 | $code .= (' ' x $indent)." \$Text::MustacheTemplate::REFERENCES{\$_name}->(\@_CTX);\n"; | |||||||||||||||||||||||||||||
| 233 | 79 | $code .= (' ' x $indent)."} if exists \$Text::MustacheTemplate::REFERENCES{\$_name};\n"; | |||||||||||||||||||||||||||||
| 234 | } elsif ($type == SYNTAX_COMMENT) { | ||||||||||||||||||||||||||||||
| 235 | # ignore | ||||||||||||||||||||||||||||||
| 236 | } else { | ||||||||||||||||||||||||||||||
| 237 | 0 | die "Unknown syntax: $type"; # uncoverable statement | |||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||
| 240 | 730 | return $code; | |||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||
| 243 | 303 | ▶ | sub _generate_variable { | ||||||||||||||||||||||||||||
|
Subroutine
_generate_variable: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||
| 244 | 303 | my ($syntax, $indent, $result) = @_; | |||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||
| 246 | 303 | my (undef, $type, $name) = @$syntax; | |||||||||||||||||||||||||||||
| 247 | 303 | ▶ | if ($type == VARIABLE_HTML_ESCAPE) { # uncoverable branch false count:2 | ||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 248 | 264 | ▶ | my $retriever = $name eq '.' ? '$_CTX[-1]' : 'retrieve_variable(\@_CTX, '.(join ', ', map B::perlstring($_), split /\./ano, $name).')'; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 249 | 264 | return (' ' x $indent)."$result .= escape_html($retriever // '');\n"; | |||||||||||||||||||||||||||||
| 250 | } elsif ($type == VARIABLE_RAW) { | ||||||||||||||||||||||||||||||
| 251 | 39 | ▶ | my $retriever = $name eq '.' ? '$_CTX[-1]' : 'retrieve_variable(\@_CTX, '.(join ', ', map B::perlstring($_), split /\./ano, $name).')'; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 252 | 39 | return (' ' x $indent)."$result .= $retriever // '';\n"; | |||||||||||||||||||||||||||||
| 253 | } else { | ||||||||||||||||||||||||||||||
| 254 | 0 | die "Unknown variable: $type"; # uncoverable statement | |||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||
| 258 | 338 | ▶ | sub _generate_box { | ||||||||||||||||||||||||||||
|
Subroutine
_generate_box: called CC=15 SCAR=27.1
| |||||||||||||||||||||||||||||||
| 259 | 338 | my ($syntax, $indent, $result) = @_; | |||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||
| 261 | 338 | my (undef, $type) = @$syntax; | |||||||||||||||||||||||||||||
| 262 | 338 | ▶ | if ($type == BOX_SECTION) { # uncoverable branch false count:4 | ||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||
| 263 | 135 | my (undef, undef, $name, $inner_template, $children) = @$syntax; | |||||||||||||||||||||||||||||
| 264 | 135 | ▶ | my $no_lambda = @CONTEXT_HINT && !$Text::MustacheTemplate::LAMBDA_TEMPLATE_RENDERING; | ||||||||||||||||||||||||||||
|
Condition100%3/3
@Text::MustacheTemplate::Compiler::CONTEXT_HINT && !$Text::MustacheTemplate::LAMBDA_TEMPLATE_RENDERING
MC/DC100%2/2
@Text::MustacheTemplate::Compiler::CONTEXT_HINT && !$Text::MustacheTemplate::LAMBDA_TEMPLATE_RENDERING
@Text::MustacheTemplate::Compiler::CONTEXT_HINT
!$Text::MustacheTemplate::LAMBDA_TEMPLATE_RENDERING
Truth table100%3/3
A && not(B)
A= @Text::MustacheTemplate::Compiler::CONTEXT_HINTB= !$Text::MustacheTemplate::LAMBDA_TEMPLATE_RENDERING
| |||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||
| 266 | 135 | ▶ | my $inner_code = _generate_body($children, $no_lambda ? $indent+4 : $indent+8, $result); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 267 | 135 | ▶ | my $evaluator = $name eq '.' | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 268 | ? 'evaluate_section($_CTX[-1])' | ||||||||||||||||||||||||||||||
| 269 | : 'evaluate_section_variable(\@_CTX, '.(join ', ', map B::perlstring($_), split /\./ano, $name).')'; | ||||||||||||||||||||||||||||||
| 270 | 135 | ▶ | if ($no_lambda) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 271 | 51 | ▶ | $evaluator = 'evaluate_section($_CTX[-2]) 'if $name eq '.'; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 272 | 51 | my $code = (' ' x $indent)."push \@_CTX => {};\n"; | |||||||||||||||||||||||||||||
| 273 | 51 | $code .= (' ' x $indent)."for my \$ctx ($evaluator) {\n"; | |||||||||||||||||||||||||||||
| 274 | 51 | $code .= (' ' x $indent)." \$_CTX[-1] = \$ctx;\n"; | |||||||||||||||||||||||||||||
| 275 | 51 | $code .= $inner_code; | |||||||||||||||||||||||||||||
| 276 | 51 | $code .= (' ' x $indent)."}\n"; | |||||||||||||||||||||||||||||
| 277 | 51 | $code .= (' ' x $indent)."pop \@_CTX;\n"; | |||||||||||||||||||||||||||||
| 278 | 51 | return $code; | |||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||
| 281 | 84 | my ($open_delimiter, $close_delimiter) = map B::perlstring($_), ($_CURRENT_OPEN_DELIMITER, $_CURRENT_CLOSE_DELIMITER); | |||||||||||||||||||||||||||||
| 282 | 84 | $inner_template = B::perlstring($inner_template); | |||||||||||||||||||||||||||||
| 283 | 84 | my $code = (' ' x $indent)."\@_section = $evaluator;\n"; | |||||||||||||||||||||||||||||
| 284 | 84 | $code .= (' ' x $indent)."if (\$Text::MustacheTemplate::LAMBDA_TEMPLATE_RENDERING && \@_section == 1 && ref \$_section[0] eq 'CODE') {\n"; | |||||||||||||||||||||||||||||
| 285 | 84 | $code .= (' ' x $indent)." my \$code = \$_section[0];\n"; | |||||||||||||||||||||||||||||
| 286 | 84 | $code .= (' ' x $indent)." \$_tmp = \$code->($inner_template);\n"; | |||||||||||||||||||||||||||||
| 287 | 84 | $code .= (' ' x $indent)." local (\$_OPEN_DELIMITER, \$_CLOSE_DELIMITER) = ($open_delimiter, $close_delimiter);\n"; | |||||||||||||||||||||||||||||
| 288 | 84 | $code .= (' ' x $indent)." $result .= _render_template_in_context(\$_tmp);\n"; | |||||||||||||||||||||||||||||
| 289 | 84 | $code .= (' ' x $indent)."} else {\n"; | |||||||||||||||||||||||||||||
| 290 | 84 | $code .= (' ' x $indent)." my \@section = \@_section;\n"; # copy to avoid rewrite same varialbe in recurse | |||||||||||||||||||||||||||||
| 291 | 84 | $code .= (' ' x $indent)." push \@_CTX => {};\n"; | |||||||||||||||||||||||||||||
| 292 | 84 | $code .= (' ' x $indent)." for my \$ctx (\@section) {\n"; | |||||||||||||||||||||||||||||
| 293 | 84 | $code .= (' ' x $indent)." \$_CTX[-1] = \$ctx;\n"; | |||||||||||||||||||||||||||||
| 294 | 84 | $code .= $inner_code; | |||||||||||||||||||||||||||||
| 295 | 84 | $code .= (' ' x $indent)." }\n"; | |||||||||||||||||||||||||||||
| 296 | 84 | $code .= (' ' x $indent)." pop \@_CTX;\n"; | |||||||||||||||||||||||||||||
| 297 | 84 | $code .= (' ' x $indent)."}\n"; | |||||||||||||||||||||||||||||
| 298 | 84 | return $code; | |||||||||||||||||||||||||||||
| 299 | } elsif ($type == BOX_INVERTED_SECTION) { | ||||||||||||||||||||||||||||||
| 300 | 55 | my (undef, undef, $name, $children) = @$syntax; | |||||||||||||||||||||||||||||
| 301 | 55 | ▶ | my $evaluator = $name eq '.' | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 302 | ? 'evaluate_section($_CTX[-1])' | ||||||||||||||||||||||||||||||
| 303 | : 'evaluate_section_variable(\@_CTX, '.(join ', ', map B::perlstring($_), split /\./ano, $name).')'; | ||||||||||||||||||||||||||||||
| 304 | 55 | my $code = (' ' x $indent)."if (!$evaluator) {\n"; | |||||||||||||||||||||||||||||
| 305 | 55 | $code .= _generate_body($children, $indent+4, $result); | |||||||||||||||||||||||||||||
| 306 | 55 | $code .= (' ' x $indent)."}\n"; | |||||||||||||||||||||||||||||
| 307 | 55 | return $code; | |||||||||||||||||||||||||||||
| 308 | } elsif ($type == BOX_BLOCK) { | ||||||||||||||||||||||||||||||
| 309 | 98 | my (undef, undef, $name, $children) = @$syntax; | |||||||||||||||||||||||||||||
| 310 | 98 | $name = B::perlstring($name); | |||||||||||||||||||||||||||||
| 311 | 98 | ▶ | unless ($_PARENT) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 312 | 60 | my $code = (' ' x $indent)."if (exists \$_BLOCKS{$name}) {\n"; | |||||||||||||||||||||||||||||
| 313 | 60 | $code .= (' ' x $indent)." $result .= \$_BLOCKS{$name}->(\@_CTX);\n"; | |||||||||||||||||||||||||||||
| 314 | 60 | $code .= (' ' x $indent)."} else {\n"; | |||||||||||||||||||||||||||||
| 315 | 60 | $code .= _generate_body($children, $indent+4, $result); | |||||||||||||||||||||||||||||
| 316 | 60 | $code .= (' ' x $indent)."}\n"; | |||||||||||||||||||||||||||||
| 317 | 60 | return $code; | |||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||
| 320 | 38 | my ($open_delimiter, $close_delimiter) = ($_CURRENT_OPEN_DELIMITER, $_CURRENT_CLOSE_DELIMITER); | |||||||||||||||||||||||||||||
| 321 | 38 | my $sub_code = _generate_code($children, $indent+4); | |||||||||||||||||||||||||||||
| 322 | 38 | $sub_code = substr $sub_code, $indent+4; # remove first indent | |||||||||||||||||||||||||||||
| 323 | 38 | my $code = (' ' x $indent)."unless (exists \$_BLOCKS{$name}) {\n"; | |||||||||||||||||||||||||||||
| 324 | 38 | $code .= (' ' x $indent)." \$_BLOCKS{$name} = $sub_code"; | |||||||||||||||||||||||||||||
| 325 | 38 | $code .= (' ' x $indent)."}\n"; | |||||||||||||||||||||||||||||
| 326 | 38 | return $code; | |||||||||||||||||||||||||||||
| 327 | } elsif ($type == BOX_PARENT) { | ||||||||||||||||||||||||||||||
| 328 | 50 | local $_PARENT = $syntax; | |||||||||||||||||||||||||||||
| 329 | 50 | my (undef, undef, $reference, $name, $children) = @$syntax; | |||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||
| 331 | # uncoverable branch false count:2 | ||||||||||||||||||||||||||||||
| 332 | 50 | ▶ | my $retriever = $reference == REFERENCE_DYNAMIC ? ($name eq '.' ? '$_CTX[-1]' : 'retrieve_variable(\@_CTX, '.(join ', ', map B::perlstring($_), split /\./ano, $name).')') | ||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||
| 333 | : $reference == REFERENCE_STATIC ? B::perlstring($name) | ||||||||||||||||||||||||||||||
| 334 | : die "Unknown reference: $type"; | ||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||
| 336 | 50 | my $code = (' ' x $indent)."{\n"; | |||||||||||||||||||||||||||||
| 337 | 50 | $code .= (' ' x $indent)." \$_name = $retriever;\n"; | |||||||||||||||||||||||||||||
| 338 | 50 | $code .= (' ' x $indent)." my \$_parent = \$Text::MustacheTemplate::REFERENCES{\$_name} or croak \"Unknown parent template: \$_name\";\n"; | |||||||||||||||||||||||||||||
| 339 | 50 | $code .= (' ' x $indent)." local \%_BLOCKS = \%_BLOCKS;\n"; | |||||||||||||||||||||||||||||
| 340 | 50 | $code .= _generate_body($children, $indent+4, DISCARD_RESULT); | |||||||||||||||||||||||||||||
| 341 | 50 | $code .= (' ' x $indent)." $result .= do {\n"; | |||||||||||||||||||||||||||||
| 342 | 50 | $code .= (' ' x $indent)." local \$_PADDING;\n"; | |||||||||||||||||||||||||||||
| 343 | 50 | $code .= (' ' x $indent)." \$_parent->(\@_CTX);\n"; | |||||||||||||||||||||||||||||
| 344 | 50 | $code .= (' ' x $indent)." };\n"; | |||||||||||||||||||||||||||||
| 345 | 50 | $code .= (' ' x $indent)."}\n"; | |||||||||||||||||||||||||||||
| 346 | 50 | return $code; | |||||||||||||||||||||||||||||
| 347 | } else { | ||||||||||||||||||||||||||||||
| 348 | 0 | die "Unknown box: $type"; # uncoverable statement | |||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||
| 352 | 31 | ▶ | sub _render_template_in_context { | ||||||||||||||||||||||||||||
|
Subroutine
_render_template_in_context: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 353 | 31 | my $source = shift; | |||||||||||||||||||||||||||||
| 354 | 31 | our ($_OPEN_DELIMITER, $_CLOSE_DELIMITER); | |||||||||||||||||||||||||||||
| 355 | 31 | ▶ | if ($source !~ /(?:\Q$_OPEN_DELIMITER\E|\Q$_CLOSE_DELIMITER\E)/man) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 356 | 18 | return $source; | |||||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||
| 359 | 13 | local $_PADDING; | |||||||||||||||||||||||||||||
| 360 | 13 | local $Text::MustacheTemplate::Lexer::OPEN_DELIMITER = $_OPEN_DELIMITER; | |||||||||||||||||||||||||||||
| 361 | 13 | local $Text::MustacheTemplate::Lexer::CLOSE_DELIMITER = $_CLOSE_DELIMITER; | |||||||||||||||||||||||||||||
| 362 | 13 | my @tokens = Text::MustacheTemplate::Lexer->tokenize($source); | |||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||
| 364 | 13 | local $Text::MustacheTemplate::Parser::SOURCE = $source; | |||||||||||||||||||||||||||||
| 365 | 13 | my $ast = Text::MustacheTemplate::Parser->parse(@tokens); | |||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||
| 367 | 13 | local @CONTEXT_HINT = our @_CTX; | |||||||||||||||||||||||||||||
| 368 | 13 | my $template = __PACKAGE__->compile($ast); | |||||||||||||||||||||||||||||
| 369 | 13 | return $template->(@_CTX); | |||||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||
| 372 | 13 | 1; | |||||||||||||||||||||||||||||
| 373 | __END__ | ||||||||||||||||||||||||||||||