Template/LiquidX/Tidy.pm

filter: Statementstmt 56.7% 67 / 118 Branchbran 5.2% 2 / 38 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 81.8% 18 / 22 Podpod 25.0% 1 / 4 totaltotal 46.3% 88 / 190 SCAR 54.1
CC
32
Cov
43%
transform
420.0
dump
42.0
tidy
6.3
CRAP 223.1
« Template/LiquidX/Tidy/impl.pm ↑ Summary App/LiquidTidy.pm »
1 package Template::LiquidX::Tidy;
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 Template::LiquidX::Tidy::Liquid::Utility;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Template::Liquid;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Template::Liquid::Document;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Template::Liquid::Tag::If;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Template::Liquid::Tag::Case;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 our @EXPORT_OK = qw(transform_back);
17  
18 3 our %defaults = (
19 indent => 4,
20 force_nl => 0,
21 force_nl_tags => 'for endfor '
22 .'comment endcomment '
23 .'if unless elsif else endif endunless '
24 # .'capture '
25 .'case when endcase',
26 short_if => 8,
27 html => 1,
28 );
29  
30 3 our $_patched;
31 sub _patch_liquid_store_block_markup {
32 3 ▶ return if $_patched++;
Subroutine _patch_liquid_store_block_markup: called CC=2 SCAR=7.4
Branch: 1 branch
BranchTrueFalseTotal
if $Template::LiquidX::Tidy::_patched++ 0 3 3
33 3 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 3 my $_if_push_block = \&Template::Liquid::Tag::If::push_block;
36 *Template::Liquid::Tag::If::push_block = sub {
37 6 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
38 6 my ($vars) = @_;
39 6 my $block = $_if_push_block->($self, $vars);
40 6 $block->{markup} = $vars->{markup};
41 6 $block
42 3 };
43  
44 3 my $_case_push_block = \&Template::Liquid::Tag::Case::push_block;
45 *Template::Liquid::Tag::Case::push_block = sub {
46 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
47 0 my ($vars) = @_;
48 0 my $block = $_case_push_block->($self, $vars);
49 0 $block->{markup} = $vars->{markup};
50 0 $block
51 3 };
52  
53 3 return;
54 }
55  
56 BEGIN {
57 3 ▶ _patch_liquid_store_block_markup();
Subroutine BEGIN: called CC=1 SCAR=0.0
58 }
59  
60 3 package Template::Liquid::Document {
61 3 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 3 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 3 ▶ use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 3 ▶ use Template::LiquidX::Tidy::impl qw(_tidy_list _tidy_make_string);
Subroutine BEGIN: called CC=1 SCAR=0.0
66  
67 ▶ sub dump ($self) {
Subroutine dump: not called CC=6 SCAR=37.4
Pod undocumented
68 0 my $return = '';
69 0 ▶ $return .= $self->{markup} if defined $self->{markup};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"markup"} 0 0 0
70 0 for my $node (@{$self->{nodelist}},
71 0 @{$self->{blocks}}) {
72 0 ▶ my $rendering = ref $node ? $node->dump() : $node;
Branch: 1 branch
BranchTrueFalseTotal
ref $node ? : 0 0 0
73 0 ▶ $return .= $rendering if defined $rendering;
Branch: 1 branch
BranchTrueFalseTotal
if defined $rendering 0 0 0
74 }
75 0 ▶ $return .= $self->{markup_2} if defined $self->{markup_2};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"markup_2"} 0 0 0
76 0 $return
77 }
78  
79 3 ▶ sub tidy ($self, $args = {}, $level = 0, $clevel = 0, $list = undef) {
Subroutine tidy: called CC=6 SCAR=18.4
Pod undocumented
80 3 my @list = _tidy_list($self, $args, $level, $clevel);
81 3 ▶ if ($list) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list) { } 0 3 3
82 @list
83 0 } else {
84 3 _tidy_make_string($self, $args, @list)
85 }
86 }
87  
88 0 ▶ sub transform ($self, $args = {}, $trans_map = {}, $is_block = undef) {
Subroutine transform: not called CC=20 SCAR=60.4
Pod undocumented
89 0 my $return = '';
90 0 ▶ $trans_map->{__i} ||= 1;
Condition0%0/2
$trans_map->{"__i"} ||= 1
l!l
00
MC/DC0%0/1
$trans_map->{"__i"} ||= 1
$trans_map->{"__i"}
Truth table0%0/2
A
A= $trans_map->{"__i"}
Aresult
11
00
91 0 my $i = $trans_map->{__i}++;
92 0 $trans_map->{ $i } = $self;
93  
94 0 ▶ if (defined $self->{markup}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"markup"}) 0 0 0
95 0 my $ent = '&#' . (1_040_000 + $i) . ';';
96 0 ▶ if ($self->{markup} =~ /^\s*\{\{/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'markup'} =~ /^\s*\{\{/) { } 0 0 0
elsif ($self->{'blocks'}) { } 0 0 0
97 0 $return .= $ent;
98 }
99 elsif ($self->{blocks}) {
100 0 $return .= '<div id="' . $ent . '">';
101 }
102 else {
103 0 $return .= '<i id="' . $ent . '"></i>';
104 }
105 }
106  
107 0 ▶ if ($self->{nodelist}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"nodelist"}) 0 0 0
108 0 for my $node ($self->{nodelist}->@*) {
109 0 my $rendering;
110 0 ▶ if (ref $node) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node) { } 0 0 0
111 0 ($rendering) = $node->transform($args, $trans_map);
112 }
113 else {
114 0 $rendering = $node;
115 }
116  
117 0 ▶ $return .= $rendering if defined $rendering;
Branch: 1 branch
BranchTrueFalseTotal
if defined $rendering 0 0 0
118 }
119 }
120  
121 0 ▶ if ($self->{blocks}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"blocks"}) 0 0 0
122 0 for my $block ($self->{blocks}->@*) {
123 0 my $rendering;
124 0 ▶ if (ref $block) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $block) { } 0 0 0
125 0 ($rendering) = $block->transform($args, $trans_map, 1);
126 }
127 else {
128 0 $rendering = $block;
129 }
130  
131 0 ▶ $return .= $rendering if defined $rendering;
Branch: 1 branch
BranchTrueFalseTotal
if defined $rendering 0 0 0
132 }
133 }
134  
135 0 ▶ if (defined $self->{markup_2}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"markup_2"}) 0 0 0
136 0 my $ent = '&#' . (1_041_000 + $i) . ';';
137 0 ▶ if ($self->{markup} =~ /^\s*\{\{/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'markup'} =~ /^\s*\{\{/) { } 0 0 0
elsif (defined $self->{'markup'} and $self->{'blocks'}) { } 0 0 0
138 0 $return .= $ent;
139 }
140 ▶ elsif (defined $self->{markup} && $self->{blocks}) {
Condition0%0/3
defined $self->{"markup"} and $self->{"blocks"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"markup"} and $self->{"blocks"}
defined $self->{"markup"} $self->{"blocks"}
Truth table0%0/3
A and B
A= defined $self->{"markup"}B= $self->{"blocks"}
ABresult
0X0
100
111
141 0 $return .= '</div><!-- id="' . $ent . '" -->';
142 }
143 else {
144 0 $return .= '<i id="' . $ent . '"></i>';
145 }
146 }
147  
148 0 ($return, $trans_map)
149 }
150 };
151  
152  
153 ▶sub transform_back ($trans, $map) {
Subroutine transform_back: not called CC=2 SCAR=17.9
Pod documented
154 0 $trans =~ s{
155 (?: (?<command> <i \s+ id\s*=\s*"&\#104(?<content_part> [01])(?<id> [0-9]{3});"></i> )
156 | (?<opentag> <div \s+ id\s*=\s*"&\#104(?<content_part> 0)(?<id> [0-9]{3});">)
157 | (?<closetag> </div><!-- \s+ id\s*=\s*"&\#104(?<content_part> 1)(?<id> [0-9]{3});" \s+ --> )
158 | (?<content> &\#104(?<content_part> [01])(?<id> [0-9]{3}); )
159 )
160 }{
161 0 ▶ $map->{ $+{id} + 0 }{ $+{content_part} ? 'markup_2' : 'markup' }
Branch: 1 branch
BranchTrueFalseTotal
$+{'content_part'} ? : 0 0 0
162 }grex;
163 }
164  
165 3 1;
166  
167 =head1 NAME
168  
169 Template::LiquidX::Tidy - Indentation for Liquid template documents
170  
171 =head1 SYNOPSIS
172  
173 use Template::LiquidX::Tidy;
174 use Template::Liquid;
175 use Template::LiquidX::Tag::...; # any additional tag modules you need
176  
177 my $parsed = Template::Liquid->parse($template_string);
178 my $tidy_string = $parsed->{document}->tidy(\%options);
179  
180 =head1 DESCRIPTION
181  
182 The LiquidX::Tidy module enhances a parsed Template::Liquid::Document
183 with a method to indent the document source code according to some
184 options.
185  
186 You can also use the command line client L<liquid_tidy> to indent your
187 template source codes.
188  
189 =head1 METHODS
190  
191 =head2 Template::Liquid::Document::tidy(%options)
192  
193 This method is to be called on a L<Template::Liquid::Document>, i.e. some
194 node in the document created by C<Template::Liquid-E<gt>parse>.
195  
196 It returns a string of the formatted and indented document node.
197  
198 Th following options are possible:
199  
200 =over 4
201  
202 =item B<html> =E<gt> boolean
203  
204 Indent HTML code. Defaults to on.
205  
206 =item B<indent> =E<gt> number
207  
208 The number of spaces for each indentation level. Default 4.
209  
210 =item B<force_nl> =E<gt> boolean
211  
212 Whether to forcibly add line breaks into tags listed as
213 force_nl_tags.
214  
215 Default no for the module, yes for the command line client.
216  
217 =item B<short_if> =E<gt> number
218  
219 The length of a text inbetween C<{% if %}> that should be exempt from force_nl
220  
221 =item B<force_nl_tags> =E<gt> 'for endfor ...'
222  
223 A space separated list of tags where C<force_nl> will add line breaks.
224  
225 Default tags: for endfor comment endcomment if unless elsif else endif
226 endunless case when endcase
227  
228 =back
229  
230 =head2 Template::Liquid::Document::dump()
231  
232 This method is to be called on a L<Template::Liquid::Document>, i.e. some
233 node in the document created by C<Template::Liquid-E<gt>parse>.
234  
235 It returns a copy of the source document.
236  
237 =head2 Template::Liquid::Document::transform()
238  
239 my ($transformed_document, $replacement_map) = $document->transform();
240 my $new_document = Template::LiquidX::Tidy::transform_back(
241 $transformed_document, $replacement_map);
242  
243 This method is to be called on a L<Template::Liquid::Document>, i.e. some
244 node in the document created by C<Template::Liquid-E<gt>parse>.
245  
246 It returns a tuple with the source document where all Liquid tags have
247 been replaced by HTML entities and blank C<E<lt>divE<gt>> and
248 C<E<lt>iE<gt>> tags. This HTML document can the be further processed
249 before using the C<transform_back> method to put back the Liquid tags.
250  
251 =head2 Template::LiquidX::Tidy::transform_back($transformed_document, $replacement_map)
252  
253 This method returns a new template string undoing a transform
254 operation.
255  
256 =cut
257  
« Template/LiquidX/Tidy/impl.pm ↑ Summary App/LiquidTidy.pm »