File Coverage

blib/lib/WWW/Shopify/Liquid/Beautifier.pm
Criterion Covered Total %
statement 44 44 100.0
branch 18 20 90.0
condition 39 42 92.8
subroutine 9 9 100.0
pod 0 4 0.0
total 110 119 92.4


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3 1     1   5998 use strict;
  1         3  
  1         42  
4 1     1   8 use warnings;
  1         3  
  1         43  
5              
6 1     1   8 use WWW::Shopify::Liquid;
  1         4  
  1         78  
7              
8             package WWW::Shopify::Liquid::Beautifier;
9 1     1   10 use base 'WWW::Shopify::Liquid::Pipeline';
  1         3  
  1         185  
10 1     1   10 use Scalar::Util qw(blessed);
  1         3  
  1         925  
11              
12 1     1 0 777 sub new { my $package = shift; return bless { tag_enclosing => {}, @_ }, $package; }
  1         13  
13              
14             sub register_tag {
15 21     21 0 65 my ($self, $tag) = @_;
16 21 100       149 $self->{tag_enclosing}->{$tag->name} = $tag->is_free ? 0 : 1;
17             }
18              
19             sub beautify {
20 6     6 0 109 my ($self, @tokens) = @_;
21             # Essentially run through these and insert spaces whenever there's any whitespace.
22 6 50       27 @tokens = grep { blessed($_) && !$_->isa('WWW::Shopify::Liquid::Token::Text::Whitespace') } @tokens;
  130         973  
23 6         16 my @result;
24 6         18 my $level = 0;
25 6         20 my $modification = 0;
26 6         31 for my $idx (0..$#tokens) {
27 128         237 my $loop_mod = 0;
28 128         244 my $token = $tokens[$idx];
29 128 100 100     446 $modification = 0 if $level == 0 && $token->isa('WWW::Shopify::Liquid::Token::Text') && $token->{core} =~ m/\n/s;
      100        
30 128 100 66     659 $level-- if $token->isa('WWW::Shopify::Liquid::Token::Tag') && ($token->{tag} =~ m/^end/ && $self->{tag_enclosing}->{do { my $a = $token->{tag}; $a =~ s/^end//; $a }});
  33   66     86  
  33         128  
  33         196  
31 128 100 100     1516 push(@result, WWW::Shopify::Liquid::Token::Text::Whitespace->new(undef, "\n")) if $idx > 0 && (!$tokens[$idx-1]->isa('WWW::Shopify::Liquid::Token::Text') || ($tokens[$idx-1]->{core} !~ m/\n\s*$/ && $tokens[$idx-1]->{core} !~ m/^\s*$/s)) && (!$tokens[$idx]->isa('WWW::Shopify::Liquid::Token::Text') || $tokens[$idx]->{core} !~ m/^\n/);
      100        
      100        
      100        
32 128 100 100     689 $loop_mod = -1 if $token->isa('WWW::Shopify::Liquid::Token::Tag') && ($token->{tag} =~ m/(elsif|else)/);
33 128 100       704 push(@result, WWW::Shopify::Liquid::Token::Text::Whitespace->new(undef, join("", ("\t" x ($level+$modification+$loop_mod))))) if $level+$modification+$loop_mod > 0;
34 128 100 100     594 $modification = length($1) if $level == 0 && $token->isa('WWW::Shopify::Liquid::Token::Tag') && $idx > 0 && $tokens[$idx-1]->isa('WWW::Shopify::Liquid::Token::Text') && $tokens[$idx-1]->{core} =~ m/(\t*)$/;
      100        
      100        
      66        
35 128 100 100     677 $level++ if $token->isa('WWW::Shopify::Liquid::Token::Tag') && $self->{tag_enclosing}->{$token->{tag}};
36 128         344 push(@result, $token);
37             }
38 6         87 return @result;
39             }
40              
41             sub compress {
42 1     1 0 18 my ($self, @tokens) = @_;
43             # Essentially run through these and insert spaces whenever there's any whitespace.
44 1 50       3 @tokens = grep { blessed($_) && !$_->isa('WWW::Shopify::Liquid::Token::Text::Whitespace') } @tokens;
  29         108  
45 1         5 return @tokens;
46             }
47              
48             1;