« blib/lib/WWW/Shopify/Liquid/Operator/NotEquals.pm
↑ Summary
blib/lib/WWW/Shopify/Liquid/Element.pm »
| 1 | #!/usr/bin/perl | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | |||
| 6 | # The special-ist tag in the world. | ||
| 7 | package WWW::Shopify::Liquid::Tag::For; | ||
| 8 | use base 'WWW::Shopify::Liquid::Tag::Enclosing'; | ||
| 9 | |||
| 10 | sub min_arguments { return 1; } | ||
| 11 | sub max_arguments { return 1; } | ||
| 12 | |||
| 13 | sub verify { | ||
| 14 | my ($self) = @_; | ||
| 15 | die new WWW::Shopify::Liquid::Exception::Parser::Arguments($self, "Requires in operator to be part of loop.") unless | ||
| 16 | $self->{arguments}->[0]->isa('WWW::Shopify::Liquid::Operator::In'); | ||
| 17 | die new WWW::Shopify::Liquid::Exception::Parser::Arguments($self, "Requires the opening variable of a loop to be a simple variable.") unless | ||
| 18 | $self->{arguments}->[0]->{operands}->[0] && $self->{arguments}->[0]->{operands}->[0]->isa('WWW::Shopify::Liquid::Token::Variable') && | ||
| 19 | int(@{$self->{arguments}->[0]->{operands}->[0]->{core}}) == 1 && $self->{arguments}->[0]->{operands}->[0]->{core}->[0]->isa('WWW::Shopify::Liquid::Token::String'); | ||
| 20 | } | ||
| 21 | sub inner_tags { return qw(else) } | ||
| 22 | |||
| 23 | use List::Util qw(min); | ||
| 24 | use Scalar::Util qw(looks_like_number blessed reftype); | ||
| 25 | use Clone qw(clone); | ||
| 26 | |||
| 27 | |||
| 28 | |||
| 29 | sub new { | ||
| 30 | my $package = shift; | ||
| 31 | my $self = bless { | ||
| 32 | line => shift, | ||
| 33 | core => shift, | ||
| 34 | arguments => shift, | ||
| 35 | contents => undef, | ||
| 36 | false_path => undef | ||
| 37 | }, $package; | ||
| 38 | $self->interpret_inner_tokens(@{$_[0]}); | ||
| 39 | return $self; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub interpret_inner_tokens { | ||
| 43 | my ($self, @tokens) = @_; | ||
| 44 | # Comes in [for_path], [tag, other_path]... | ||
| 45 | my $token = shift(@tokens); | ||
| 46 | return undef unless $token; | ||
| 47 | $self->{contents} = $token->[0]; | ||
| 48 | if (int(@tokens) > 0) { | ||
| 49 | die new WWW::Shopify::Liquid::Exception::Parser($self, "else cannot be anywhere, except the end tag of the for statement.") if int(@tokens) > 1 || $tokens[0]->[0]->tag ne "else"; | ||
| 50 | $self->{false_path} = $tokens[0]->[1]; | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | sub render_loop { | ||
| 55 | my ($self, $renderer, $hash, $op1, $op2, $start, $end, @array) = @_; | ||
| 56 | |||
| 57 | my @texts = (); | ||
| 58 | my $all_processed = 1; | ||
| 59 | my $var = $op1->{core}->[0]->{core}; | ||
| 60 | my $content; | ||
| 61 | |||
| 62 | if ($renderer->state && exists $renderer->state->{values}->{$self . "-1"}) { | ||
| 63 | $start = $renderer->state->{values}->{$self . "-1"}->{index}; | ||
| 64 | @texts = @{$renderer->state->{values}->{$self . "-1"}->{texts}}; | ||
| 65 | delete $renderer->state->{values}->{$self . "-1"}; | ||
| 66 | |||
| 67 | } | ||
| 68 | for ($start..$end) { | ||
| 69 | $hash->{$var} = $array[$_]; | ||
| 70 | $hash->{forloop} = { | ||
| 71 | index => ($_+1), index0 => $_, first => $_ == 0, last => $_ == $#array, | ||
| 72 | length => int(@array), rindex0 => (($#array - $_) + 1), rindex => (($#array - $_)), | ||
| 73 | }; | ||
| 74 | eval { | ||
| 75 | $content = $self->{contents}->render($renderer, $hash); | ||
| 76 | }; | ||
| 77 | if (my $exp = $@) { | ||
| 78 | if (defined $exp && blessed($exp) && $exp->isa('WWW::Shopify::Liquid::Exception::Control')) { | ||
| 79 | if ($exp->isa('WWW::Shopify::Liquid::Exception::Control::Pause')) { | ||
| 80 | $exp->register_value($self . "-1", { | ||
| 81 | index => $_, | ||
| 82 | texts => \@texts | ||
| 83 | }); | ||
| 84 | die $exp; | ||
| 85 | } else { | ||
| 86 | push(@texts, @{$exp->initial_render}) if $exp->initial_render && int(@{$exp->initial_render}) > 0; | ||
| 87 | if ($exp->isa('WWW::Shopify::Liquid::Exception::Control::Break')) { | ||
| 88 | last; | ||
| 89 | } elsif ($exp->isa('WWW::Shopify::Liquid::Exception::Control::Continue')) { | ||
| 90 | next; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | } else { | ||
| 94 | die $exp; | ||
| 95 | } | ||
| 96 | } | ||
| 97 | $all_processed = 0 if !$self->is_processed($content); | ||
| 98 | push(@texts, $content); | ||
| 99 | } | ||
| 100 | |||
| 101 | return join('', grep { defined $_ } @texts) if $all_processed; | ||
| 102 | return $self; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub expand_concatenations { | ||
| 106 | my ($self, $result) = @_; | ||
| 107 | return blessed($result) && $result->isa('WWW::Shopify::Liquid::Operator::Concatenate') ? (map { $self->expand_concatenations($_) } @{$result->{operands}}) : ($result); | ||
| 108 | } | ||
| 109 | |||
| 110 | sub optimize_loop { | ||
| 111 | my ($self, $optimizer, $hash, $op1, $op2, $start, $end, @array) = @_; | ||
| 112 | |||
| 113 | |||
| 114 | # First step, we replace everything by a big concatenate. | ||
| 115 | my @texts = (); | ||
| 116 | |||
| 117 | my $var = $op1->{core}->[0]->{core}; | ||
| 118 | return '' if $start > $end; | ||
| 119 | |||
| 120 | my @parts = map { | ||
| 121 | $hash->{$var} = $array[$_]; | ||
| 122 | $hash->{forloop} = { | ||
| 123 | index => ($_+1), index0 => $_, first => $_ == 0, last => $_ == $#array, | ||
| 124 | length => int(@array), rindex0 => (($#array - $_) + 1), rindex => (($#array - $_)), | ||
| 125 | }; | ||
| 126 | my $result; | ||
| 127 | eval { | ||
| 128 | $result = $self->is_processed($self->{contents}) ? $self->{contents} : clone($self->{contents})->optimize($optimizer, $hash); | ||
| 129 | }; | ||
| 130 | if (my $exp = $@) { | ||
| 131 | if (defined $exp && blessed($exp) && $exp->isa('WWW::Shopify::Liquid::Exception::Control')) { | ||
| 132 | push(@texts, @{$exp->initial_render}) if $exp->initial_render && int(@{$exp->initial_render}) > 0; | ||
| 133 | if ($exp->isa('WWW::Shopify::Liquid::Exception::Control::Break')) { | ||
| 134 | last; | ||
| 135 | } elsif ($exp->isa('WWW::Shopify::Liquid::Exception::Control::Continue')) { | ||
| 136 | next; | ||
| 137 | } | ||
| 138 | } else { | ||
| 139 | die $exp; | ||
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 143 | $self->expand_concatenations($result); | ||
| 144 | } ($start..$end); | ||
| 145 | |||
| 146 | return $parts[0] if int(@parts) == 1; | ||
| 147 | return WWW::Shopify::Liquid::Operator::Concatenate->new($self->{line}, "", @parts); | ||
| 148 | } | ||
| 149 | |||
| 150 | # Called to expand the second operand in "in" argument list. | ||
| 151 | sub apply { | ||
| 152 | my ($self, $hash, $operand) = @_; | ||
| 153 | return [keys(%$operand)] if ref($operand) eq 'HASH'; | ||
| 154 | return $operand; | ||
| 155 | } | ||
| 156 | |||
| 157 | # Should eventually support loop unrolling. | ||
| 158 | sub process { | ||
| 159 | my ($self, $hash, $action, $pipeline) = @_; | ||
| 160 | my @args = @{$self->{arguments}}; | ||
| 161 | |||
| 162 | |||
| 163 | my ($op1, $op2) = @{$args[0]->{operands}}; | ||
| 164 | $op2 = $op2->$action($pipeline, $hash) if !$self->is_processed($op2); | ||
| 165 | |||
| 166 | $self->{arguments}->[0]->{operands}->[1] = $op2 if $self->is_processed($op2) && $action eq 'optimize'; | ||
| 167 | if ($action eq "optimize" && !$self->is_processed($self->{contents})) { | ||
| 168 | # Ensure that the loop variable is unset for at least the duration of the loop while we do an initial optimization. | ||
| 169 | my $var = $op1->{core}->[0]->{core}; | ||
| 170 | delete $hash->{$var}; | ||
| 171 | # And, in fact, make sure that ALL variables that get set in the loop get unset here. | ||
| 172 | for (grep { ref($_) eq 'WWW::Shopify::Liquid::Tag::Assign' } $self->{contents}->tokens) { | ||
| 173 | next if int(@{$_->{arguments}->[0]->{operands}->[0]->{core}}) == 1 && | ||
| 174 | blessed($_->{arguments}->[0]->{operands}->[0]->{core}->[0]->{core}) && | ||
| 175 | $_->{arguments}->[0]->{operands}->[0]->{core}->[0]->{core}->isa('WWW::Shopify::Liquid::Token::String'); | ||
| 176 | delete $hash->{$_->{arguments}->[0]->{operands}->[0]->{core}->[0]->{core}}; | ||
| 177 | } | ||
| 178 | $pipeline->push_conditional_state; | ||
| 179 | $self->{contents} = $self->{contents}->optimize($pipeline, $hash); | ||
| 180 | $pipeline->pop_conditional_state; | ||
| 181 | } | ||
| 182 | $op2 = $self->apply($hash, $op2); | ||
| 183 | return $self if (!$self->is_processed($op2) && $action eq "optimize"); | ||
| 184 | return '' if (!$self->is_processed($op2) && $action eq "render"); | ||
| 185 | return (defined $self->{false_path} ? $self->{false_path}->$action($pipeline, $hash) : '') if ref($op2) ne "ARRAY" || int(@$op2) == 0; | ||
| 186 | die new WWW::Shopify::Liquid::Exception::Renderer::Arguments($self, "Requires an array in for loop.") unless ref($op2) eq "ARRAY"; | ||
| 187 | |||
| 188 | my @array = @$op2; | ||
| 189 | my $limit = int(@array); | ||
| 190 | my $offset = 0; | ||
| 191 | |||
| 192 | |||
| 193 | my ($limit_arg) = grep { $_->isa('WWW::Shopify::Liquid::Token::Variable::Named') && $_->{name} eq "limit" } @args; | ||
| 194 | my $limit_result = $limit_arg->$action($pipeline, $hash) if $limit_arg; | ||
| 195 | return $self if !$self->is_processed($limit_result); | ||
| 196 | $limit = $limit_result->{limit} if $limit_result && ref($limit_result) && ref($limit_result) eq 'HASH' && looks_like_number($limit_result->{limit}); | ||
| 197 | |||
| 198 | |||
| 199 | my ($offset_arg) = grep { $_->isa('WWW::Shopify::Liquid::Token::Variable::Named') && $_->{name} eq "offset" } @args; | ||
| 200 | my $offset_result = $offset_arg->$action($pipeline, $hash) if $offset_arg; | ||
| 201 | $offset = $offset_result->{offset} if $offset_result && ref($offset_result) && ref($offset_result) eq 'HASH' && looks_like_number($offset_result->{offset}); | ||
| 202 | |||
| 203 | my $dispatch = $action . "_loop"; | ||
| 204 | |||
| 205 | return $self->$dispatch($pipeline, $hash, $op1, $op2, $offset, min($#array, $limit+$offset-1), @array); | ||
| 206 | } | ||
| 207 | |||
| 208 | |||
| 209 | |||
| 210 | 1; |