Template/LiquidX/Tidy/impl.pm

filter: Statementstmt 70.4% 143 / 203 Branchbran 58.3% 70 / 120 Conditioncond 40.4% 53 / 131 MC/DCmcdc 8.8% 6 / 68 Subroutinesub 100% 11 / 11 Podpod n/a totaltotal 53.1% 283 / 533 CC 109 SCAR 59.1
CC
109
Cov
59%
_tidy_make_string
642.4
_tidy_html
47.2
_tidy_list
18.4
CRAP 367.6
↑ Summary Template/LiquidX/Tidy.pm »
1 package Template::LiquidX::Tidy::impl;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 our $VERSION = '0.01';
8  
9 3 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 our @EXPORT_OK = qw(_tidy_list _tidy_make_string);
11 3 ▶use vars '%defaults';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 *defaults = \%Template::LiquidX::Tidy::defaults;
13  
14 # parser regex
15 3 our $PI_START = qr/\{%-?/;
16 3 our $PI_END = qr/-?%\}/;
17 3 our $LV_START = qr/\{\{/;
18  
19 # pseudo html indenter
20 # $html - fragment of html to chunk into lines
21 # $args - indenter configuration
22 # $level - current level
23 # $clevel - current level for debugging
24 # $stack - additional storage for
25 # open_tags /
26 # closed_tags /
27 # level at return time
28 # returns a list of [ level, fragment ] pairs and modifies $stack
29 ▶sub _tidy_html ($html, $args, $level, $clevel, $stack) {
Subroutine _tidy_html: called CC=22 SCAR=38.5
30 #print ((" " x $clevel ). "_tidy_html <<$html>> <<$level>>\n");
31 12 my $nl = $html =~ /\n/;
32 12 my @return;
33 12 my $last_line_start = 0;
34 12 my $start_level = $level;
35 12 my $level_change = '';
36 12 ▶ while ($html =~ m{(?: (?<cdata> <!\[CDATA\[(.*?)\]\]> )
Branch: 1 branch
BranchTrueFalseTotal
if ($html =~ m((?: (?<cdata> <!\[CDATA\[(.*?)\]\]> ) | (?<nl> \n ) | (?<close2> />) | <(?<close> /)? (?<tag> \w+ ) ) )gsx) 9 12 21
37 | (?<nl> \n )
38 | (?<close2> />)
39 | <(?<close> /)? (?<tag> \w+ )
40 ) }gsx) {
41 #use Data::Dumper; warn Dumper \%+;
42 9 ▶ if ($+{cdata}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($+{'cdata'}) { } 0 9 9
elsif (length $+{'nl'}) { } 3 6 9
43 # ignore
44 }
45 elsif (length $+{nl}) {
46 3 ▶ 1 while $level_change =~ s/\(\)//;
Branch: 1 branch
BranchTrueFalseTotal
$level_change =~ s/\(\)// and do { '???' } 0 3 3
47 push @return, [ $start_level - ($level_change =~ y/)//), (substr $html, $last_line_start, (pos $html) - $last_line_start),
48 +{ html => 1,
49 ($stack->{open_tags} ? (open => [$stack->{open_tags}->@*]) : ()),
50 3 ▶ ($stack->{closed_tags} ? (closed => [$stack->{closed_tags}->@*]) : ()), } ];
Branch: 2 branches
BranchTrueFalseTotal
$stack->{'open_tags'} ? : 3 0 3
$stack->{'closed_tags'} ? : 0 3 3
51 3 $last_line_start = pos($html);
52 3 $start_level = $level;
53 3 $level_change = '';
54 }
55 else {
56 6 ▶ my $tag = $+{tag} ? lc $+{tag} : '';
Branch: 1 branch
BranchTrueFalseTotal
$+{'tag'} ? : 6 0 6
57 6 ▶ if ($tag =~ /^(?| img | br | link | meta )$/ix) {
Branch: 3 branches
BranchTrueFalseTotal
if ($tag =~ /^(?| img | br | link | meta )$/xi) { } 0 6 6
elsif ($+{'close2'}) { } 0 6 6
elsif ($+{'close'}) { } 3 3 6
58 0 push $stack->{waiting}->@*, $tag;
59 }
60 elsif ($+{close2}) {
61 0 ▶ if ($stack->{waiting} && $stack->{waiting}->@*) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stack->{'waiting'} and @{$stack->{'waiting'}}) { } 0 0 0
Condition0%0/3
$stack->{"waiting"} and @{$stack->{"waiting"}}
!ll&&!rl&&r
000
MC/DC0%0/2
$stack->{"waiting"} and @{$stack->{"waiting"}}
$stack->{"waiting"} @{$stack->{"waiting"}}
Truth table0%0/3
A and B
A= $stack->{"waiting"}B= @{$stack->{"waiting"}}
ABresult
0X0
100
111
62 0 pop $stack->{waiting}->@*;
63 } else {
64 0 pop $stack->{open_tags}->@*;
65 0 $level_change .= ')';
66 0 $level--;
67 #print ((" " x $clevel ). " close2 <<$level_change>> <<$start_level>>\n");
68 }
69 }
70 elsif ($+{close}) {
71 3 ▶ if ($stack->{waiting} && $stack->{waiting}->@* && $stack->{waiting}[-1] eq $tag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stack->{'waiting'} and @{$stack->{'waiting'}} and $stack->{'waiting'}[-1] eq $tag) { } 0 3 3
Condition33%2/6
$stack->{"waiting"} and @{$stack->{"waiting"}} and $stack->{"waiting"}[-1] eq $tag
!ll&&!rl&&r
300
$stack->{"waiting"} and @{$stack->{"waiting"}}
!ll&&!rl&&r
030
MC/DC0%0/3
$stack->{"waiting"} and @{$stack->{"waiting"}} and $stack->{"waiting"}[-1] eq $tag
$stack->{"waiting"} @{$stack->{"waiting"}} $stack->{"waiting"}[-1] eq $tag
Truth table25%1/4
A and B and C
A= $stack->{"waiting"}B= @{$stack->{"waiting"}}C= $stack->{"waiting"}[-1] eq $tag
ABCresult
0XX0
10X0
1100
1111
72 0 pop $stack->{waiting}->@*;
73 }
74 else {
75 3 ▶ if ($stack->{open_tags} && $stack->{open_tags}[-1] eq $tag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stack->{'open_tags'} and $stack->{'open_tags'}[-1] eq $tag) { } 3 0 3
Condition33%1/3
$stack->{"open_tags"} and $stack->{"open_tags"}[-1] eq $tag
!ll&&!rl&&r
003
MC/DC0%0/2
$stack->{"open_tags"} and $stack->{"open_tags"}[-1] eq $tag
$stack->{"open_tags"} $stack->{"open_tags"}[-1] eq $tag
Truth table33%1/3
A and B
A= $stack->{"open_tags"}B= $stack->{"open_tags"}[-1] eq $tag
ABresult
0X0
100
111
76 3 pop $stack->{open_tags}->@*;
77 } else {
78 0 push $stack->{closed_tags}->@*, $tag;
79 }
80 3 $level_change .= ')';
81 3 $level--;
82 3 $stack->{waiting}->@* = ();
83 #print ((" " x $clevel ). " close<<$tag>> <<$level_change>> <<$start_level>>\n");
84 }
85 }
86 else {
87 3 push $stack->{open_tags}->@*, $tag;
88 3 $level_change .= '(';
89 3 $level++;
90 3 $stack->{waiting}->@* = ();
91 #print ((" " x $clevel ). " open<<$tag>> <<$level_change>> <<$start_level>>\n");
92 }
93 }
94 }
95 12 ▶ if ($last_line_start < length $html) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_line_start < length $html) 9 3 12
96 9 ▶ 1 while $level_change =~ s/\(\)//;
Branch: 1 branch
BranchTrueFalseTotal
$level_change =~ s/\(\)// and do { '???' } 0 9 9
97 push @return, [ $start_level - ($level_change =~ y/)//), (substr $html, $last_line_start),
98 +{ html => 1,
99 ($stack->{open_tags} ? (open => [$stack->{open_tags}->@*]) : ()),
100 9 ▶ ($stack->{closed_tags} ? (closed => [$stack->{closed_tags}->@*]) : ()), } ];
Branch: 2 branches
BranchTrueFalseTotal
$stack->{'open_tags'} ? : 9 0 9
$stack->{'closed_tags'} ? : 0 9 9
101 }
102 12 $stack->{level} = $level;
103 @return
104 12 }
105  
106 # liquid indenter part
107 # $obj - an object, subclass of Template::Liquid::Document
108 # $args - indenter configuration
109 # $level - current level
110 # $clevel - current level for debugging
111 # $stack - additional storage for the html indenter in particular
112 # returns a list of [ level, fragment ] pairs and modifies $stack
113 42 ▶sub _tidy_list ($obj, $args, $level, $clevel, $stack = {}) {
Subroutine _tidy_list: called CC=13 SCAR=29.1
114 42 my @return;
115 push @return, [ $level, $obj->{markup}, +{ markup => $obj } ]
116 42 ▶ if defined $obj->{markup};
Branch: 1 branch
BranchTrueFalseTotal
if defined $obj->{"markup"} 33 9 42
117  
118 42 my $nlevel = $level + 1;
119 42 for my $node ($obj->{nodelist}->@*) {
120 36 my @result;
121 36 ▶ if (ref $node) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node) { } 24 12 36
122 24 @result = _tidy_list($node, $args, $nlevel, $clevel + 1, $stack)
123 } else {
124 12 ▶ if ($args->{html} // $defaults{html}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'html'} // $Template::LiquidX::Tidy::impl::defaults{'html'}) { } 12 0 12
Condition33%1/3
$args->{"html"} // $Template::LiquidX::Tidy::impl::defaults{"html"}
l!l&&r!l&&!r
0120
MC/DC0%0/2
$args->{"html"} // $Template::LiquidX::Tidy::impl::defaults{"html"}
$args->{"html"} $Template::LiquidX::Tidy::impl::defaults{"html"}
Truth table33%1/3
A // B
A= $args->{"html"}B= $Template::LiquidX::Tidy::impl::defaults{"html"}
ABresult
1X1
011
000
125 12 @result = _tidy_html($node, $args, $nlevel, $clevel + 1, $stack);
126 12 ▶ if ($stack->{level}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stack->{"level"}) 12 0 12
127 12 $nlevel = delete $stack->{level};
128 }
129 } else {
130 0 @result = [ $nlevel, $node, +{ text => 1 } ]
131 }
132 }
133 36 push @return, @result;
134 }
135  
136 42 for my $block ($obj->{blocks}->@*) {
137 15 my @result;
138 15 ▶ if (ref $block) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $block) { } 15 0 15
139 15 @result = _tidy_list($block, $args, $level, $clevel + 1, $stack)
140 } else {
141 0 ▶ if ($args->{html} // $defaults{html}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'html'} // $Template::LiquidX::Tidy::impl::defaults{'html'}) { } 0 0 0
Condition0%0/3
$args->{"html"} // $Template::LiquidX::Tidy::impl::defaults{"html"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"html"} // $Template::LiquidX::Tidy::impl::defaults{"html"}
$args->{"html"} $Template::LiquidX::Tidy::impl::defaults{"html"}
Truth table0%0/3
A // B
A= $args->{"html"}B= $Template::LiquidX::Tidy::impl::defaults{"html"}
ABresult
1X1
011
000
142 0 @result = _tidy_html($block, $args, $level, $clevel + 1, $stack)
143 } else {
144 0 @result = [ $level, $block, +{ text => 1 } ]
145 }
146 }
147 15 push @return, @result;
148 }
149  
150 push @return, [ $level, $obj->{markup_2}, +{ markup_2 => $obj } ]
151 42 ▶ if defined $obj->{markup_2};
Branch: 1 branch
BranchTrueFalseTotal
if defined $obj->{"markup_2"} 6 36 42
152  
153 @return
154 42 }
155  
156 # check if the tag should always be linebroken
157 # $args->{force_nl} - config option to enable linebreaks
158 # $args->{force_nl_tags} - whitespace separated string with tags that should be linebroken
159 ▶sub _force_nl_tags ($args, $content) {
Subroutine _force_nl_tags: called CC=5 SCAR=16.6
160 12 ▶ my $force_nl = $args->{force_nl} // $defaults{force_nl};
Condition66%2/3
$args->{"force_nl"} // $Template::LiquidX::Tidy::impl::defaults{"force_nl"}
l!l&&r!l&&!r
804
MC/DC50%1/2
$args->{"force_nl"} // $Template::LiquidX::Tidy::impl::defaults{"force_nl"}
$args->{"force_nl"} $Template::LiquidX::Tidy::impl::defaults{"force_nl"}
Truth table66%2/3
A // B
A= $args->{"force_nl"}B= $Template::LiquidX::Tidy::impl::defaults{"force_nl"}
ABresult
1X1
011
000
161 12 ▶ my $force_nl_tags = $args->{force_nl_tags} // $defaults{force_nl_tags};
Condition33%1/3
$args->{"force_nl_tags"} // $Template::LiquidX::Tidy::impl::defaults{"force_nl_tags"}
l!l&&r!l&&!r
0120
MC/DC0%0/2
$args->{"force_nl_tags"} // $Template::LiquidX::Tidy::impl::defaults{"force_nl_tags"}
$args->{"force_nl_tags"} $Template::LiquidX::Tidy::impl::defaults{"force_nl_tags"}
Truth table33%1/3
A // B
A= $args->{"force_nl_tags"}B= $Template::LiquidX::Tidy::impl::defaults{"force_nl_tags"}
ABresult
1X1
011
000
162 12 ▶ return unless $force_nl;
Branch: 1 branch
BranchTrueFalseTotal
unless $force_nl 4 8 12
163 8 my @tags = split ' ', $force_nl_tags;
164 8 ▶ return 1 unless @tags;
Branch: 1 branch
BranchTrueFalseTotal
unless @tags 0 8 8
165 8 my $re = join '|', map { "\Q$_" } @tags;
166 8 return $content =~ /$PI_START\s*($re)/;
167 }
168  
169 # there is only whitespace before $list[$i] and a linebreak
170 ▶sub _ws_only_before ($i, @list) {
Subroutine _ws_only_before: called CC=4 SCAR=15.9
171 19 ▶ while ($i > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) 19 0 19
172 19 $i--;
173 # found newline, exit search
174 19 ▶ if ($list[$i]->[1] =~ /\n\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list[$i][1] =~ /\n\z/) 0 19 19
175 0 return 1;
176 }
177 # found non whitespace
178 19 ▶ if ($list[$i]->[1] =~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list[$i][1] =~ /\S/) 19 0 19
179 19 return;
180 }
181 }
182 0 return 1;
183 }
184  
185 # there is only whitespace after $list[$i] and a processing instruction
186 ▶sub _ws_only_after_beforepi ($i, @list) {
Subroutine _ws_only_after_beforepi: called CC=4 SCAR=13.9
187 27 ▶ while ($i < $#list) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#list) 24 3 27
188 24 $i++;
189 # found processing instruction, exit search
190 24 ▶ if ($list[$i]->[1] =~ /\{%-?/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list[$i][1] =~ /\{%-?/) 21 3 24
191 21 return 1;
192 }
193 # found non whitespace
194 3 ▶ if ($list[$i]->[1] =~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list[$i][1] =~ /\S/) 3 0 3
195 3 return;
196 }
197 }
198 3 return;
199 }
200  
201 # liquid indenter main routine
202 # $obj - the root element object, subclass of Template::Liquid::Document
203 # $args - indenter configuration
204 # @list - a list of [ level, string fragment ] pairs as produced by _tidy_html / _tidy_list
205 # returns the indented document string
206 ▶sub _tidy_make_string ($obj, $args, @list) {
Subroutine _tidy_make_string: called CC=66 SCAR=64.7
207 3 my $level_correct = -1;
208 #my $level_correct = 0;
209 #use Data::Dumper;
210 #print Dumper \@list;
211  
212 3 my $result = '';
213  
214 # config
215 3 ▶ my $indent = $args->{indent} // $defaults{indent};
Condition33%1/3
$args->{"indent"} // $Template::LiquidX::Tidy::impl::defaults{"indent"}
l!l&&r!l&&!r
030
MC/DC0%0/2
$args->{"indent"} // $Template::LiquidX::Tidy::impl::defaults{"indent"}
$args->{"indent"} $Template::LiquidX::Tidy::impl::defaults{"indent"}
Truth table33%1/3
A // B
A= $args->{"indent"}B= $Template::LiquidX::Tidy::impl::defaults{"indent"}
ABresult
1X1
011
000
216 3 ▶ my $short_if = $args->{short_if} // $defaults{short_if} // 0;
Condition40%2/5
($args->{"short_if"} // $Template::LiquidX::Tidy::impl::defaults{"short_if"}) // 0
l!l
30
$args->{"short_if"} // $Template::LiquidX::Tidy::impl::defaults{"short_if"}
l!l&&r!l&&!r
030
MC/DC0%0/2
($args->{"short_if"} // $Template::LiquidX::Tidy::impl::defaults{"short_if"}) // 0
$args->{"short_if"} $Template::LiquidX::Tidy::impl::defaults{"short_if"}
Truth table33%1/3
A // B
A= $args->{"short_if"}B= $Template::LiquidX::Tidy::impl::defaults{"short_if"}
ABresult
1X1
011
000
217 3 ▶ my $indent_html = $args->{html} // $defaults{html};
Condition33%1/3
$args->{"html"} // $Template::LiquidX::Tidy::impl::defaults{"html"}
l!l&&r!l&&!r
030
MC/DC0%0/2
$args->{"html"} // $Template::LiquidX::Tidy::impl::defaults{"html"}
$args->{"html"} $Template::LiquidX::Tidy::impl::defaults{"html"}
Truth table33%1/3
A // B
A= $args->{"html"}B= $Template::LiquidX::Tidy::impl::defaults{"html"}
ABresult
1X1
011
000
218  
219 # state
220 3 my $next_nl = 0; # next PI needs to have a newline because we just deleted one
221  
222 3 for my $i (0..$#list) {
223 # poke = previous line
224 51 ▶ my $poke_content = $i > 0 ? $list[$i - 1]->[1] : '';
Branch: 1 branch
BranchTrueFalseTotal
$i > 0 ? : 48 3 51
225  
226 51 my $it = $list[$i];
227 51 my $content = $it->[1];
228  
229 # peek = next line
230 51 ▶ my $peek_content = $i < $#list ? $list[$i + 1]->[1] : '';
Branch: 1 branch
BranchTrueFalseTotal
$i < $#list ? : 48 3 51
231  
232 51 my $level = $it->[0] + $level_correct;
233 51 ▶ my $peek_level = $i < $#list ? $list[$i + 1]->[0] + $level_correct : $level;
Branch: 1 branch
BranchTrueFalseTotal
$i < $#list ? : 48 3 51
234  
235 51 ▶ if ($content =~ /$PI_START/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /$Template::LiquidX::Tidy::impl::PI_START/) 24 27 51
236 # $result .= "\e[34mPI;$level\e[0m";
237 24 ▶ if (($content !~ /\n/ && !_force_nl_tags($args, $content))
Branch: 1 branch
BranchTrueFalseTotal
if (not $content =~ /\n/ || _force_nl_tags($args, $content) or $content =~ /$Template::LiquidX::Tidy::impl::PI_START\s*post_url\s/) 4 20 24
Condition83%5/6
not $content =~ /\n/ || _force_nl_tags($args, $content) or $content =~ /$Template::LiquidX::Tidy::impl::PI_START\s*post_url\s/
l!l&&r!l&&!r
4020
$content =~ /\n/ || _force_nl_tags($args, $content)
l!l&&r!l&&!r
1284
MC/DC66%2/3
not $content =~ /\n/ || _force_nl_tags($args, $content) or $content =~ /$Template::LiquidX::Tidy::impl::PI_START\s*post_url\s/
$content =~ /\n/ _force_nl_tags($args, $content) $content =~ /$Template::LiquidX::Tidy::impl::PI_START\s*post_url\s/
Truth table60%3/5
not(A || B) or C
A= $content =~ /\n/B= _force_nl_tags($args, $content)C= $content =~ /$Template::LiquidX::Tidy::impl::PI_START\s*post_url\s/
ABCresult
00X1
1X11
0111
1X00
0100
238 || $content =~ /$PI_START\s*post_url\s/) {
239 # $result .= "\e[35mA\e[0m";
240 # we stripped a newline, put it back
241 4 ▶ if ($next_nl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_nl) 0 4 4
242 0 $result .= "\n";
243 0 $next_nl = 0;
244 }
245  
246 # there was only whitespace before this tag (which we deleted)
247 # -> add indentation
248 4 ▶ if (_ws_only_before($i, @list)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_ws_only_before($i, @list)) 0 4 4
249 0 my $m1 = $content;
250 0 ▶ $m1 =~ s/^\s*+/' ' x ($indent * $level)/e
Branch: 1 branch
BranchTrueFalseTotal
if $indent_html 0 0 0
251 if $indent_html;
252 #$m1 =~ s/\n\K/' ' x ($indent * ($level + 1))/ge;
253  
254 0 $result .= $m1;
255 0 next;
256 }
257  
258 4 $result .= $content;
259 4 next;
260 }
261  
262 20 ▶ if ($short_if > 0 && !$next_nl && $content !~ /\n/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($short_if > 0 and not $next_nl and not $content =~ /\n/) 8 12 20
Condition50%3/6
$short_if > 0 and not $next_nl and not $content =~ /\n/
!ll&&!rl&&r
0128
$short_if > 0 and not $next_nl
!ll&&!rl&&r
0020
MC/DC33%1/3
$short_if > 0 and not $next_nl and not $content =~ /\n/
$short_if > 0 not $next_nl not $content =~ /\n/
Truth table50%2/4
A and not(B) and not(C)
A= $short_if > 0B= not $next_nlC= not $content =~ /\n/
ABCresult
0XX0
10X0
1100
1111
263 8 ▶ if ($content =~ /$PI_START\s*(if|unless)\s/ && (length $peek_content) <= $short_if
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /$Template::LiquidX::Tidy::impl::PI_START\s*(if|unless)\s/ and length $peek_content <= $short_if and $i + 2 <= $#list and $list[$i + 2][1] =~ /$Template::LiquidX::Tidy::impl::PI_START/) 0 8 8
Condition44%4/9
$content =~ /$Template::LiquidX::Tidy::impl::PI_START\s*(if|unless)\s/ and length $peek_content <= $short_if and $i + 2 <= $#list and $list[$i + 2][1] =~ /$Template::LiquidX::Tidy::impl::PI_START/
!ll&&!rl&&r
800
$content =~ /$Template::LiquidX::Tidy::impl::PI_START\s*(if|unless)\s/ and length $peek_content <= $short_if and $i + 2 <= $#list
!ll&&!rl&&r
800
$content =~ /$Template::LiquidX::Tidy::impl::PI_START\s*(if|unless)\s/ and length $peek_content <= $short_if
!ll&&!rl&&r
440
MC/DC0%0/4
$content =~ /$Template::LiquidX::Tidy::impl::PI_START\s*(if|unless)\s/ and length $peek_content <= $short_if and $i + 2 <= $#list and $list[$i + 2][1] =~ /$Template::LiquidX::Tidy::impl::PI_START/
$content =~ /$Template::LiquidX::Tidy::impl::PI_START\s*(if|unless)\s/ length $peek_content <= $short_if $i + 2 <= $#list $list[$i + 2][1] =~ /$Template::LiquidX::Tidy::impl::PI_START/
Truth table40%2/5
A and B and C and D
A= $content =~ /$Template::LiquidX::Tidy::impl::PI_START\s*(if|unless)\s/B= length $peek_content <= $short_ifC= $i + 2 <= $#listD= $list[$i + 2][1] =~ /$Template::LiquidX::Tidy::impl::PI_START/
ABCDresult
0XXX0
10XX0
110X0
11100
11111
264 && ($i + 2 <= $#list) && $list[$i + 2]->[1] =~ /$PI_START/) {
265 0 $result .= $content;
266 0 next;
267 }
268 8 ▶ if ($content =~ /$PI_START\s*end(if|unless)\s/ && (length $poke_content) <= $short_if
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /$Template::LiquidX::Tidy::impl::PI_START\s*end(if|unless)\s/ and length $poke_content <= $short_if and $i - 2 >= 0 and $list[$i - 2][1] =~ /$Template::LiquidX::Tidy::impl::PI_START\s*(\Q$1\E\s|elsif\s|else)/) 0 8 8
Condition33%3/9
$content =~ /$Template::LiquidX::Tidy::impl::PI_START\s*end(if|unless)\s/ and length $poke_content <= $short_if and $i - 2 >= 0 and $list[$i - 2][1] =~ /$Template::LiquidX::Tidy::impl::PI_START\s*(\Q$1\E\s|elsif\s|else)/
!ll&&!rl&&r
800
$content =~ /$Template::LiquidX::Tidy::impl::PI_START\s*end(if|unless)\s/ and length $poke_content <= $short_if and $i - 2 >= 0
!ll&&!rl&&r
800
$content =~ /$Template::LiquidX::Tidy::impl::PI_START\s*end(if|unless)\s/ and length $poke_content <= $short_if
!ll&&!rl&&r
800
MC/DC0%0/4
$content =~ /$Template::LiquidX::Tidy::impl::PI_START\s*end(if|unless)\s/ and length $poke_content <= $short_if and $i - 2 >= 0 and $list[$i - 2][1] =~ /$Template::LiquidX::Tidy::impl::PI_START\s*(\Q$1\E\s|elsif\s|else)/
$content =~ /$Template::LiquidX::Tidy::impl::PI_START\s*end(if|unless)\s/ length $poke_content <= $short_if $i - 2 >= 0 $list[$i - 2][1] =~ /$Template::LiquidX::Tidy::impl::PI_START\s*(\Q$1\E\s|elsif\s|else)/
Truth table20%1/5
A and B and C and D
A= $content =~ /$Template::LiquidX::Tidy::impl::PI_START\s*end(if|unless)\s/B= length $poke_content <= $short_ifC= $i - 2 >= 0D= $list[$i - 2][1] =~ /$Template::LiquidX::Tidy::impl::PI_START\s*(\Q$1\E\s|elsif\s|else)/
ABCDresult
0XXX0
10XX0
110X0
11100
11111
269 && ($i - 2 >= 0) && $list[$i - 2]->[1] =~ /$PI_START\s*(\Q$1\E\s|elsif\s|else)/) {
270 0 $result .= $content;
271 0 next;
272 }
273 }
274  
275 20 my $m1 = $content;
276 # normalise
277 20 $m1 =~ s/\s*($PI_END)/ $1/;
278 20 $m1 =~ s/($PI_START)\s*/$1 /;
279 # $result .= "[\e[33m$m1\e[0m]";
280 # $result .= "{\e[32m$poke_content\e[0m}";
281 20 my $line = 0;
282  
283 # previous line not empty
284 # -> we assume the {% goes to previous line
285 20 ▶ if ( ($poke_content =~ /^(.*)\z/m && length $1)
Branch: 2 branches
BranchTrueFalseTotal
if ($poke_content =~ /^(.*)\z/m and length $1 or $indent_html and $poke_content =~ />\s*\n\z/) { } 20 0 20
elsif (_ws_only_before($i, @list) or $next_nl) { } 0 0 0
Condition22%2/9
$poke_content =~ /^(.*)\z/m and length $1 or $indent_html and $poke_content =~ />\s*\n\z/
l!l&&r!l&&!r
2000
$poke_content =~ /^(.*)\z/m and length $1
!ll&&!rl&&r
0020
$indent_html and $poke_content =~ />\s*\n\z/
!ll&&!rl&&r
000
MC/DC0%0/4
$poke_content =~ /^(.*)\z/m and length $1 or $indent_html and $poke_content =~ />\s*\n\z/
$poke_content =~ /^(.*)\z/m length $1 $indent_html $poke_content =~ />\s*\n\z/
Truth table14%1/7
A and B or C and D
A= $poke_content =~ /^(.*)\z/mB= length $1C= $indent_htmlD= $poke_content =~ />\s*\n\z/
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
286 || ($indent_html && $poke_content =~ />\s*\n\z/) ) {
287 20 $m1 =~ s/$PI_START\K\s*/\n/;
288 }
289 # there was only whitespace before this tag (which we deleted)
290 # -> add indentation
291 ▶ elsif (_ws_only_before($i, @list) || $next_nl) {
Condition0%0/3
_ws_only_before($i, @list) or $next_nl
l!l&&r!l&&!r
000
MC/DC0%0/2
_ws_only_before($i, @list) or $next_nl
_ws_only_before($i, @list) $next_nl
Truth table0%0/3
A or B
A= _ws_only_before($i, @list)B= $next_nl
ABresult
1X1
011
000
292 # $result .= "[\e[33mwsonly before $i; next_nl:$next_nl\e[0m]";
293 # if ($indent_html && !$next_nl) {
294 # # we can indent the whole tag
295 # $m1 =~ s/^\s*+/' ' x ($indent * $level)/e;
296 # $line++;
297 # } else {
298 0 ▶ my $local_indent = $poke_content =~ /^(.*)\z/m ? length $1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$poke_content =~ /^(.*)\z/m ? : 0 0 0
299 0 my $required_indent = $indent * $level;
300 0 $required_indent -= $local_indent;
301 0 $required_indent -= length '{% ';
302 0 ▶ if ($required_indent >= 0 && !$next_nl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($required_indent >= 0 and not $next_nl) { } 0 0 0
Condition0%0/3
$required_indent >= 0 and not $next_nl
!ll&&!rl&&r
000
MC/DC0%0/2
$required_indent >= 0 and not $next_nl
$required_indent >= 0 not $next_nl
Truth table0%0/3
A and not(B)
A= $required_indent >= 0B= not $next_nl
ABresult
0X0
100
111
303 0 $m1 =~ s/$PI_START\K\s*/' ' x ($required_indent + 1)/e;
304 } else {
305 # force newline after {%
306 0 $m1 =~ s/$PI_START\K\s*/\n/;
307 }
308 # }
309 }
310 20 $next_nl = 0;
311 20 ▶ $m1 =~ s/\n\K(?!["'])[ \t]*/' ' x ($indent * ($level + ($line++ ? 1 : 0)))/ge;
Branch: 1 branch
BranchTrueFalseTotal
$line++ ? : 0 20 20
312 # $result .= "[\e[33m$m1\e[0m]";
313  
314 # next line is no processing instruction
315 # -> the %} should go to the next line, indented
316 20 ▶ if ($peek_content !~ /$PI_START/ && $peek_content !~ /^\n/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($peek_content =~ /$Template::LiquidX::Tidy::impl::PI_START/ or $peek_content =~ /^\n/) 17 3 20
Condition66%2/3
$peek_content =~ /$Template::LiquidX::Tidy::impl::PI_START/ or $peek_content =~ /^\n/
l!l&&r!l&&!r
3017
MC/DC50%1/2
$peek_content =~ /$Template::LiquidX::Tidy::impl::PI_START/ or $peek_content =~ /^\n/
$peek_content =~ /$Template::LiquidX::Tidy::impl::PI_START/ $peek_content =~ /^\n/
Truth table66%2/3
A or B
A= $peek_content =~ /$Template::LiquidX::Tidy::impl::PI_START/B= $peek_content =~ /^\n/
ABresult
1X1
011
000
317 17 $m1 =~ s/ ($PI_END)/"\n" . (' ' x ($indent * $peek_level)) . $1/e;
318 }
319  
320 # clean "blank" lines
321 20 $m1 =~ s/^[ \t]+(?=\n)//gm;
322  
323 # special case for the last command in file:
324 # the %} goes on its own line
325 20 ▶ if ($i == $#list || ($peek_content eq "\n"
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $#list or $peek_content eq "\n" and not grep({$_->[1] ne "\n";} @list[$i + 1 .. $#list])) 0 20 20
Condition33%2/6
$i == $#list or $peek_content eq "\n" and not grep({$_->[1] ne "\n";} @list[$i + 1 .. $#list])
l!l&&r!l&&!r
0020
$peek_content eq "\n" and not grep({$_->[1] ne "\n";} @list[$i + 1 .. $#list])
!ll&&!rl&&r
2000
MC/DC0%0/3
$i == $#list or $peek_content eq "\n" and not grep({$_->[1] ne "\n";} @list[$i + 1 .. $#list])
$i == $#list $peek_content eq "\n" not grep({$_->[1] ne "\n";} @list[$i + 1 .. $#list])
Truth table0%0/4
A or B and not(C)
A= $i == $#listB= $peek_content eq "\n"C= not grep({$_->[1] ne "\n";} @list[$i + 1 .. $#list])
ABCresult
1XX1
0111
00X0
0100
326 0 && !grep { $_->[1] ne "\n" } @list[ ($i + 1) .. $#list ]) ) {
327 0 $m1 =~ s/ ($PI_END)/\n$1/;
328 }
329  
330 # $result .= "\e[35mB\e[0m";
331 20 $result .= $m1;
332 20 next;
333 }
334  
335 # indent content that starts with {{ (liquid variables)
336 27 ▶ if ($content =~ /^\s*$LV_START/ && (_ws_only_before($i, @list)) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /^\s*$Template::LiquidX::Tidy::impl::LV_START/ and _ws_only_before($i, @list)) 0 27 27
Condition66%2/3
$content =~ /^\s*$Template::LiquidX::Tidy::impl::LV_START/ and _ws_only_before($i, @list)
!ll&&!rl&&r
12150
MC/DC0%0/2
$content =~ /^\s*$Template::LiquidX::Tidy::impl::LV_START/ and _ws_only_before($i, @list)
$content =~ /^\s*$Template::LiquidX::Tidy::impl::LV_START/ _ws_only_before($i, @list)
Truth table66%2/3
A and B
A= $content =~ /^\s*$Template::LiquidX::Tidy::impl::LV_START/B= _ws_only_before($i, @list)
ABresult
0X0
100
111
337 0 my $m1 = $content;
338 0 ▶ if ($indent_html) {
Branch: 1 branch
BranchTrueFalseTotal
if ($indent_html) 0 0 0
339 0 $m1 =~ s/^\s*+/' ' x ($indent * $level)/e;
340 }
341 0 $m1 =~ s/\n\K/' ' x ($indent * ($level + 1))/ge;
342  
343 0 $result .= $m1;
344 0 next;
345 }
346  
347 # indent content that start with < (html tag)
348 27 ▶ if ($indent_html && $content =~ /^\s*<[^!]/ && ($poke_content =~ /\n\z/) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($indent_html and $content =~ /^\s*<[^!]/ and $poke_content =~ /\n\z/) 0 27 27
Condition66%4/6
$indent_html and $content =~ /^\s*<[^!]/ and $poke_content =~ /\n\z/
!ll&&!rl&&r
2430
$indent_html and $content =~ /^\s*<[^!]/
!ll&&!rl&&r
0243
MC/DC0%0/3
$indent_html and $content =~ /^\s*<[^!]/ and $poke_content =~ /\n\z/
$indent_html $content =~ /^\s*<[^!]/ $poke_content =~ /\n\z/
Truth table50%2/4
A and B and C
A= $indent_htmlB= $content =~ /^\s*<[^!]/C= $poke_content =~ /\n\z/
ABCresult
0XX0
10X0
1100
1111
349 0 my $m1 = $content;
350 0 $m1 =~ s/^\s*+/' ' x ($indent * $level)/e;
351  
352 # next line is a processing instruction
353 # -> delete the last newline if the line ends with > (html tag end)
354 0 ▶ if (_ws_only_after_beforepi($i, @list)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_ws_only_after_beforepi($i, @list)) 0 0 0
355 0 $next_nl = $m1 =~ s/>\K\s*\n\z//;
356 }
357  
358 0 $result .= $m1;
359 0 next;
360 }
361  
362 # next line is a processing instruction
363 # -> delete the last newline if the line ends with > (html tag end)
364 27 ▶ if ($indent_html && _ws_only_after_beforepi($i, @list)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($indent_html and _ws_only_after_beforepi($i, @list)) 21 6 27
Condition66%2/3
$indent_html and _ws_only_after_beforepi($i, @list)
!ll&&!rl&&r
0621
MC/DC50%1/2
$indent_html and _ws_only_after_beforepi($i, @list)
$indent_html _ws_only_after_beforepi($i, @list)
Truth table66%2/3
A and B
A= $indent_htmlB= _ws_only_after_beforepi($i, @list)
ABresult
0X0
100
111
365 21 my $m1 = $content;
366 21 ▶ if ($m1 =~ s/(?<!-)>\K\s*\n\z//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m1 =~ s/(?<!-)>\K\s*\n\z//) 0 21 21
367 0 $result .= $m1;
368 0 $next_nl = 1;
369 0 next;
370 }
371  
372 # ignore plain newline if it follows a processing instruction
373 21 ▶ if ($m1 eq "\n" && $poke_content =~ /$PI_START/ && $poke_content !~ /$PI_START\s*capture/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m1 eq "\n" and $poke_content =~ /$Template::LiquidX::Tidy::impl::PI_START/ and not $poke_content =~ /$Template::LiquidX::Tidy::impl::PI_START\s*capture/) 0 21 21
Condition33%2/6
$m1 eq "\n" and $poke_content =~ /$Template::LiquidX::Tidy::impl::PI_START/ and not $poke_content =~ /$Template::LiquidX::Tidy::impl::PI_START\s*capture/
!ll&&!rl&&r
2100
$m1 eq "\n" and $poke_content =~ /$Template::LiquidX::Tidy::impl::PI_START/
!ll&&!rl&&r
2100
MC/DC0%0/3
$m1 eq "\n" and $poke_content =~ /$Template::LiquidX::Tidy::impl::PI_START/ and not $poke_content =~ /$Template::LiquidX::Tidy::impl::PI_START\s*capture/
$m1 eq "\n" $poke_content =~ /$Template::LiquidX::Tidy::impl::PI_START/ not $poke_content =~ /$Template::LiquidX::Tidy::impl::PI_START\s*capture/
Truth table25%1/4
A and B and not(C)
A= $m1 eq "\n"B= $poke_content =~ /$Template::LiquidX::Tidy::impl::PI_START/C= not $poke_content =~ /$Template::LiquidX::Tidy::impl::PI_START\s*capture/
ABCresult
0XX0
10X0
1100
1111
374 0 $next_nl = 1;
375 0 next;
376 }
377 }
378  
379 # ignore white space in front of liquid variables and liquid processing instructions
380 27 ▶ if ($indent_html
Branch: 1 branch
BranchTrueFalseTotal
if ($indent_html and $content =~ /^[ \t]+$/ and $peek_content =~ /$Template::LiquidX::Tidy::impl::LV_START/ || $peek_content =~ /$Template::LiquidX::Tidy::impl::PI_START/ and $poke_content =~ /\n\z/) 0 27 27
Condition25%3/12
$indent_html and $content =~ /^[ \t]+$/ and $peek_content =~ /$Template::LiquidX::Tidy::impl::LV_START/ || $peek_content =~ /$Template::LiquidX::Tidy::impl::PI_START/ and $poke_content =~ /\n\z/
!ll&&!rl&&r
2700
$indent_html and $content =~ /^[ \t]+$/ and $peek_content =~ /$Template::LiquidX::Tidy::impl::LV_START/ || $peek_content =~ /$Template::LiquidX::Tidy::impl::PI_START/
!ll&&!rl&&r
2700
$indent_html and $content =~ /^[ \t]+$/
!ll&&!rl&&r
0270
$peek_content =~ /$Template::LiquidX::Tidy::impl::LV_START/ or $peek_content =~ /$Template::LiquidX::Tidy::impl::PI_START/
l!l&&r!l&&!r
000
MC/DC0%0/5
$indent_html and $content =~ /^[ \t]+$/ and $peek_content =~ /$Template::LiquidX::Tidy::impl::LV_START/ || $peek_content =~ /$Template::LiquidX::Tidy::impl::PI_START/ and $poke_content =~ /\n\z/
$indent_html $content =~ /^[ \t]+$/ $peek_content =~ /$Template::LiquidX::Tidy::impl::LV_START/ $peek_content =~ /$Template::LiquidX::Tidy::impl::PI_START/ $poke_content =~ /\n\z/
Truth table14%1/7
A and B and C or D and E
A= $indent_htmlB= $content =~ /^[ \t]+$/C= $peek_content =~ /$Template::LiquidX::Tidy::impl::LV_START/D= $peek_content =~ /$Template::LiquidX::Tidy::impl::PI_START/E= $poke_content =~ /\n\z/
ABCDEresult
0XXXX0
10XXX0
1100X0
111X00
110100
111X11
110111
381 && $content =~ /^[ \t]+$/
382 && ($peek_content =~ /$LV_START/ || $peek_content =~ /$PI_START/)
383 && $poke_content =~ /\n\z/) {
384  
385 # skip this indentation, will be corrected when the next token is processed
386 0 next;
387 }
388  
389 # indent html text
390 27 ▶ if ($indent_html
Branch: 1 branch
BranchTrueFalseTotal
if ($indent_html and not $it->[2] && $it->[2]{"open"} && @{$it->[2]{"open"}} && $it->[2]{"open"}[-1] eq "script") 27 0 27
Condition66%8/12
$indent_html and not $it->[2] && $it->[2]{"open"} && @{$it->[2]{"open"}} && $it->[2]{"open"}[-1] eq "script"
!ll&&!rl&&r
0027
$it->[2] && $it->[2]{'open'} && @{$it->[2]{'open'}} && $it->[2]{'open'}[-1] eq 'script'
!ll&&!rl&&r
1890
$it->[2] and $it->[2]{"open"} and @{$it->[2]{"open"}}
!ll&&!rl&&r
1539
$it->[2] and $it->[2]{"open"}
!ll&&!rl&&r
01512
MC/DC0%0/5
$indent_html and not $it->[2] && $it->[2]{"open"} && @{$it->[2]{"open"}} && $it->[2]{"open"}[-1] eq "script"
$indent_html $it->[2] $it->[2]{"open"} @{$it->[2]{"open"}} $it->[2]{'open'}[-1] eq 'script'
Truth table50%3/6
A and not(B and C and D && E)
A= $indent_htmlB= $it->[2]C= $it->[2]{"open"}D= @{$it->[2]{"open"}}E= $it->[2]{'open'}[-1] eq 'script'
ABCDEresult
0XXXX0
111110
10XXX1
110XX1
1110X1
111101
391 && !($it->[2] && $it->[2]{open} && $it->[2]{open}->@* && $it->[2]{open}[-1] eq 'script')) {
392 27 my $m1 = $content;
393 27 ▶ if ($poke_content =~ /\n\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($poke_content =~ /\n\z/) 0 27 27
394 0 $m1 =~ s/^[ \t]++/' ' x ($indent * $level)/e;
395 }
396 # clean "blank" lines
397 27 $m1 =~ s/^[ \t]+(?=\n)//gm;
398  
399 27 $result .= $m1;
400 27 next;
401 }
402  
403 # fallback: don't touch the content, add as is
404 0 $result .= $content;
405 0 next;
406 }
407 $result
408 3 }
409  
410 3 1;
411  
412 =head1 NAME
413  
414 Template::LiquidX::Tidy::impl - The implementation of Template::LiquidX::Tidy
415  
416 =head1 SYNOPSIS
417  
418 For internal usage.
419  
420 =head1 METHODS
421  
422 =head2 _tidy_list($obj, $args, $level, $clevel, $stack = {})
423  
424 The Liquid indenter part
425  
426 =over 4
427  
428 =item B<$obj>
429  
430 an object, subclass of Template::Liquid::Document
431  
432 =item B<$args>
433  
434 The indenter configuration
435  
436 =back
437  
438 returns a list of [ level, fragment ] pairs and modifies C<$stack>
439  
440 =head2 _tidy_make_string($obj, $args, @list)
441  
442 Liquid indenter main routine. This does the grunt work of joining
443 together all the tuples returned by the calls to _tidy_list
444  
445 =over 4
446  
447 =item B<$obj>
448  
449 an object, subclass of Template::Liquid::Document
450  
451 =item B<$args>
452  
453 The indenter configuration
454  
455 =back
456  
457 returns the indented document string
458  
459 =cut
460  
↑ Summary Template/LiquidX/Tidy.pm »