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