File Coverage

blib/lib/WWW/Shopify/Liquid/Tag/Include.pm
Criterion Covered Total %
statement 55 74 74.3
branch 17 32 53.1
condition 6 21 28.5
subroutine 8 11 72.7
pod 0 7 0.0
total 86 145 59.3


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2 37     37   16016 use strict;
  37         109  
  37         1114  
3 37     37   232 use warnings;
  37         93  
  37         1608  
4              
5             package WWW::Shopify::Liquid::Tag::Include;
6 37     37   232 use base 'WWW::Shopify::Liquid::Tag::Free';
  37         95  
  37         3807  
7 37     37   278 use File::Slurp;
  37         102  
  37         33269  
8              
9 0     0 0 0 sub max_arguments { return 1; }
10 0     0 0 0 sub min_arguments { return 1; }
11              
12             sub verify {
13 13     13 0 46 my ($self) = @_;
14             }
15              
16             sub retrieve_include {
17 9     9 0 27 my ($self, $hash, $action, $pipeline, $string, $argument) = @_;
18 9 100 66     26 if ($pipeline->inclusion_context && $pipeline->parent) {
    50          
19             # Inclusion contexts are evaluated from left to right, in order of priority.
20 8 50 33     23 my @inclusion_contexts = $pipeline->inclusion_context && ref($pipeline->inclusion_context) && ref($pipeline->inclusion_context) eq "ARRAY" ? @{$pipeline->inclusion_context} : $pipeline->inclusion_context;
  0         0  
21 8 50       27 die new WWW::Shopify::Liquid::Exception([], "Backtracking not allowed in inclusions.") if $string =~ m/\.\./;
22 8         18 for (@inclusion_contexts) {
23 8 50       21 if (ref($_) eq "CODE") {
24 0         0 return $_->($self, $hash, $action, $pipeline, $string);
25             } else {
26 8         28 my $path = $_ . "/" . $string . ".liquid";
27 8 100       105 return ($path, scalar(read_file($path))) if -e $path;
28             }
29             }
30 1         6 die new WWW::Shopify::Liquid::Exception([], "Can't find include $string.");
31             } elsif ($action eq "render") {
32 1         22 die new WWW::Shopify::Liquid::Exception::Renderer::Unimplemented($self);
33             }
34 0         0 return $self;
35             }
36              
37             sub include_literal {
38 0     0 0 0 my ($self) = @_;
39 0         0 my $literal = $self->{arguments}->[0];
40 0 0 0     0 if ($literal && ref($literal) && $literal->isa('WWW::Shopify::Liquid::Operator::With')) {
      0        
41 0         0 $literal = $literal->{arguments}->[0];
42             } else {
43 0         0 $literal = $literal;
44             }
45 0 0       0 return unless $literal->isa('WWW::Shopify::Liquid::Token::String');
46 0         0 return $literal->{core};
47             }
48              
49             sub process_include {
50 7     7 0 23 my ($self, $hash, $action, $pipeline, $string, $path, $text, $argument) = @_;
51 7         17 my $old_context = $pipeline->parent->lexer->file_context;
52 7         19 $pipeline->parent->lexer->file_context($path);
53 7         19 $pipeline->parent->parser->file_context($path);
54 7         38 $pipeline->parent->renderer->file_context($path);
55             # If text is already an AST, then we do not parse the text.
56 7 50       21 my $ast = ref($text) ? $text : $pipeline->parent->parse_text($text);
57 7 50       41 $hash->{$string} = $argument if defined $argument;
58 7 50       24 if ($action eq "optimize") {
59 0         0 $ast = $pipeline->optimize($hash, $ast);
60 0         0 $pipeline->parent->lexer->file_context($old_context);
61 0         0 $pipeline->parent->parser->file_context($old_context);
62 0         0 $pipeline->parent->renderer->file_context($old_context);
63 0         0 return $ast;
64             } else {
65             # Perform no hash cloning.
66 7         29 my $clone_hash = $pipeline->clone_hash;
67 7         24 $pipeline->clone_hash(0);
68 7         37 my ($result) = $pipeline->render($hash, $ast);
69 7         37 $pipeline->clone_hash($clone_hash);
70 7         25 $pipeline->parent->lexer->file_context($old_context);
71 7         22 $pipeline->parent->parser->file_context($old_context);
72 7         24 $pipeline->parent->renderer->file_context($old_context);
73 7         96 return $result;
74             }
75             }
76              
77             sub process {
78 10     10 0 31 my ($self, $hash, $action, $pipeline) = @_;
79 10 100       31 die new WWW::Shopify::Liquid::Exception([], "Recursive inclusion probable, greater than depth " . $pipeline->max_inclusion_depth . ". Aborting.")
80             if $pipeline->inclusion_depth > $pipeline->max_inclusion_depth;
81 9 50       16 return '' unless int(@{$self->{arguments}}) > 0;
  9         42  
82 9         40 my $result = $self->{arguments}->[0]->$action($pipeline, $hash);
83 9         18 my ($string, $argument);
84 9 50 33     59 if ($result && ref($result) && $result->isa('WWW::Shopify::Liquid::Operator::With')) {
      33        
85 0         0 $string = $result->{operands}->[0]->$action($pipeline, $hash);
86 0         0 $argument = $result->{operands}->[1]->$action($pipeline, $hash);
87             } else {
88 9         17 $string = $result;
89             }
90 9 50 33     30 return $self if !$self->is_processed($string) || !$self->is_processed($argument);
91 9         31 my ($path, $text) = $self->retrieve_include($hash, $action, $pipeline, $string, $argument);
92 7 50       448 return $self if !$self->is_processed($path);
93 7         21 my $include_depth = $pipeline->inclusion_depth;
94 7         25 $pipeline->inclusion_depth($include_depth+1);
95 7         31 $result = $self->process_include($hash, $action, $pipeline, $string, $path, $text, $argument);
96 7         37 $pipeline->inclusion_depth($include_depth);
97 7         33 return $result;
98            
99             }
100              
101              
102              
103             1;