| 1 | # Creates a tag to be used in the system, much like a function call. Should probably disallow recursion in order to preserve the non-looping nature of liquid. | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | package WWW::Shopify::Liquid::Tag::CustomTag; | ||
| 7 | use parent 'WWW::Shopify::Liquid::Tag'; | ||
| 8 | |||
| 9 | use Scalar::Util qw(blessed); | ||
| 10 | |||
| 11 | sub new { | ||
| 12 | my ($package, $line, $tag, $arguments, $contents, $parser) = @_; | ||
| 13 | if (!ref($package)) { | ||
| 14 | my @arguments = @$arguments; | ||
| 15 | my ($free) = grep { $_->{core} eq "free" } @arguments; | ||
| 16 | my $self = bless { | ||
| 17 | free => $free ? 1 : 0, | ||
| 18 | name => $tag, | ||
| 19 | contents => $contents | ||
| 20 | }, $package; | ||
| 21 | die new WWW::Shopify::Liquid::Exception("Cannot register custom tag " . $tag . ", would conflict with buildin tag.") if | ||
| 22 | ($parser->free_tags->{$tag} && !$parser->free_tags->{$tag}->isa('WWW::Shopify::Liquid::Tag::CustomTag')) || | ||
| 23 | ($parser->enclosing_tags->{$tag} && !$parser->enclosing_tags->{$tag}->isa('WWW::Shopify::Liquid::Tag::CustomTag')); | ||
| 24 | die new WWW::Shopify::Liquid::Exception("Recursion is disallowed in liquid.") unless int(grep { defined $_ && blessed($_) && $_->isa('WWW::Shopify::Liquid::Tag::CustomTag') && $tag && $_->name && $tag eq $_->name } $contents->tokens) == 0; | ||
| 25 | push(@{$parser->{transient_elements}}, $self) if $parser->transient_custom_operations; | ||
| 26 | $parser->custom_tags->{$self->name} = $self; | ||
| 27 | return $self; | ||
| 28 | } else { | ||
| 29 | my $self = ref(shift)->SUPER::new(@_); | ||
| 30 | $self->{stored_contents} = $self->{contents}->[0]->[0]; | ||
| 31 | $self->{contents} = $parser->custom_tags->{$self->{core}}->{contents}; | ||
| 32 | return $self; | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | sub abstract { return !ref($_[0]); } | ||
| 37 | sub inner_halt_lexing { return 0; } | ||
| 38 | sub inner_ignore_whitespace { return 0; } | ||
| 39 | sub is_free { return $_[0]->{free} ? 1 : 0; } | ||
| 40 | sub is_enclosing { return !$_[0]->{free} ? 1 : 0; } | ||
| 41 | sub tags { return $_[0]->{name}; } | ||
| 42 | sub name { return $_[0]->{name}; } | ||
| 43 | sub ast { return $_[0]->{ast}; } | ||
| 44 | sub min_arguments { return $_[0]->{min_arguments} || 0; } | ||
| 45 | sub max_arguments { return $_[0]->{max_arguments}; } | ||
| 46 | |||
| 47 | sub optimize { | ||
| 48 | return $_[0]; | ||
| 49 | } | ||
| 50 | |||
| 51 | use Scalar::Util qw(blessed); | ||
| 52 | sub render { | ||
| 53 | my ($self, $renderer, $hash) = @_; | ||
| 54 | my @arguments = map { $_->render($renderer, $hash) } @{$self->{arguments}}; | ||
| 55 | $hash->{arguments} = \ @arguments; | ||
| 56 | my $contents = $self->{stored_contents} && blessed($self->{stored_contents}) ? $self->{stored_contents}->render($renderer, $hash) : undef; | ||
| 57 | $hash->{contents} = $contents; | ||
| 58 | return $self->{contents}->render($renderer, $hash); | ||
| 59 | } | ||
| 60 | |||
| 61 | |||
| 62 | package WWW::Shopify::Liquid::Tag::CreateTag; | ||
| 63 | use parent 'WWW::Shopify::Liquid::Tag::Enclosing'; | ||
| 64 | |||
| 65 | sub min_arguments { 2 } | ||
| 66 | sub max_arguments { 4 } | ||
| 67 | |||
| 68 | sub new { | ||
| 69 | my ($package, $line, $tag, $arguments, $contents, $parser) = @_; | ||
| 70 | my $self = $package->SUPER::new($line, $tag, $arguments, $contents, $parser); | ||
| 71 | $parser->parent->register_tag(WWW::Shopify::Liquid::Tag::CustomTag->new([], $arguments->[0]->{core}, $arguments, @{$contents->[0]}, $parser)); | ||
| 72 | return $self; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub render { '' } | ||
| 76 | |||
| 77 | 1; |