mirror of
https://github.com/Shopify/liquid.git
synced 2026-10-02 16:45:12 -07:00
Shift tag parsing into parse method
This commit is contained in:
+2
-1
@@ -7,6 +7,7 @@ module Liquid
|
|||||||
def initialize(tag_name, markup, options)
|
def initialize(tag_name, markup, options)
|
||||||
super
|
super
|
||||||
@blank = true
|
@blank = true
|
||||||
|
@body = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse(tokens)
|
def parse(tokens)
|
||||||
@@ -17,7 +18,7 @@ module Liquid
|
|||||||
|
|
||||||
# For backwards compatibility
|
# For backwards compatibility
|
||||||
def render(context)
|
def render(context)
|
||||||
@body.render(context)
|
@body&.render(context)
|
||||||
end
|
end
|
||||||
|
|
||||||
def blank?
|
def blank?
|
||||||
|
|||||||
@@ -17,8 +17,6 @@ module Liquid
|
|||||||
disabled_tags.push(*tags)
|
disabled_tags.push(*tags)
|
||||||
end
|
end
|
||||||
|
|
||||||
private :new
|
|
||||||
|
|
||||||
def disabled_tags
|
def disabled_tags
|
||||||
@disabled_tags ||= []
|
@disabled_tags ||= []
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -18,13 +18,12 @@ module Liquid
|
|||||||
|
|
||||||
attr_reader :to, :from
|
attr_reader :to, :from
|
||||||
|
|
||||||
def initialize(tag_name, markup, options)
|
def parse(_tokens)
|
||||||
super
|
if @markup =~ Syntax
|
||||||
if markup =~ Syntax
|
|
||||||
@to = Regexp.last_match(1)
|
@to = Regexp.last_match(1)
|
||||||
@from = Variable.new(Regexp.last_match(2), options)
|
@from = Variable.new(Regexp.last_match(2), @parse_context)
|
||||||
else
|
else
|
||||||
raise SyntaxError, options[:locale].t(self.class.syntax_error_translation_key)
|
raise SyntaxError, @parse_context[:locale].t(self.class.syntax_error_translation_key)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -15,13 +15,13 @@ module Liquid
|
|||||||
class Capture < Block
|
class Capture < Block
|
||||||
Syntax = /(#{VariableSignature}+)/o
|
Syntax = /(#{VariableSignature}+)/o
|
||||||
|
|
||||||
def initialize(tag_name, markup, options)
|
def parse(_tokens)
|
||||||
super
|
if @markup =~ Syntax
|
||||||
if markup =~ Syntax
|
|
||||||
@to = Regexp.last_match(1)
|
@to = Regexp.last_match(1)
|
||||||
else
|
else
|
||||||
raise SyntaxError, options[:locale].t("errors.syntax.capture")
|
raise SyntaxError, @parse_context[:locale].t("errors.syntax.capture")
|
||||||
end
|
end
|
||||||
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_to_output_buffer(context, output)
|
def render_to_output_buffer(context, output)
|
||||||
|
|||||||
@@ -7,18 +7,15 @@ module Liquid
|
|||||||
|
|
||||||
attr_reader :blocks, :left
|
attr_reader :blocks, :left
|
||||||
|
|
||||||
def initialize(tag_name, markup, options)
|
def parse(tokens)
|
||||||
super
|
|
||||||
@blocks = []
|
@blocks = []
|
||||||
|
|
||||||
if markup =~ Syntax
|
if @markup =~ Syntax
|
||||||
@left = Expression.parse(Regexp.last_match(1))
|
@left = Expression.parse(Regexp.last_match(1))
|
||||||
else
|
else
|
||||||
raise SyntaxError, options[:locale].t("errors.syntax.case")
|
raise SyntaxError, @parse_context[:locale].t("errors.syntax.case")
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def parse(tokens)
|
|
||||||
body = BlockBody.new
|
body = BlockBody.new
|
||||||
body = @blocks.last.attachment while parse_body(body, tokens)
|
body = @blocks.last.attachment while parse_body(body, tokens)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -19,17 +19,16 @@ module Liquid
|
|||||||
|
|
||||||
attr_reader :variables
|
attr_reader :variables
|
||||||
|
|
||||||
def initialize(tag_name, markup, options)
|
def parse(_tokens)
|
||||||
super
|
case @markup
|
||||||
case markup
|
|
||||||
when NamedSyntax
|
when NamedSyntax
|
||||||
@variables = variables_from_string(Regexp.last_match(2))
|
@variables = variables_from_string(Regexp.last_match(2))
|
||||||
@name = Expression.parse(Regexp.last_match(1))
|
@name = Expression.parse(Regexp.last_match(1))
|
||||||
when SimpleSyntax
|
when SimpleSyntax
|
||||||
@variables = variables_from_string(markup)
|
@variables = variables_from_string(@markup)
|
||||||
@name = @variables.to_s
|
@name = @variables.to_s
|
||||||
else
|
else
|
||||||
raise SyntaxError, options[:locale].t("errors.syntax.cycle")
|
raise SyntaxError, @parse_context[:locale].t("errors.syntax.cycle")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -20,9 +20,8 @@ module Liquid
|
|||||||
# Hello: -3
|
# Hello: -3
|
||||||
#
|
#
|
||||||
class Decrement < Tag
|
class Decrement < Tag
|
||||||
def initialize(tag_name, markup, options)
|
def parse(_tokens)
|
||||||
super
|
@variable = @markup.strip
|
||||||
@variable = markup.strip
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_to_output_buffer(context, output)
|
def render_to_output_buffer(context, output)
|
||||||
|
|||||||
@@ -12,13 +12,14 @@ module Liquid
|
|||||||
# {% echo user | link %}
|
# {% echo user | link %}
|
||||||
#
|
#
|
||||||
class Echo < Tag
|
class Echo < Tag
|
||||||
def initialize(tag_name, markup, parse_context)
|
attr_reader :variable
|
||||||
super
|
|
||||||
@variable = Variable.new(markup, parse_context)
|
def parse(_tokens)
|
||||||
|
@variable = Variable.new(@markup, @parse_context)
|
||||||
end
|
end
|
||||||
|
|
||||||
def render(context)
|
def render(context)
|
||||||
@variable.render_to_output_buffer(context, +'')
|
variable&.render_to_output_buffer(context, +'')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -50,15 +50,11 @@ module Liquid
|
|||||||
|
|
||||||
attr_reader :collection_name, :variable_name, :limit, :from
|
attr_reader :collection_name, :variable_name, :limit, :from
|
||||||
|
|
||||||
def initialize(tag_name, markup, options)
|
def parse(tokens)
|
||||||
super
|
|
||||||
@from = @limit = nil
|
@from = @limit = nil
|
||||||
parse_with_selected_parser(markup)
|
|
||||||
@for_block = BlockBody.new
|
@for_block = BlockBody.new
|
||||||
@else_block = nil
|
@else_block = nil
|
||||||
end
|
parse_with_selected_parser(@markup)
|
||||||
|
|
||||||
def parse(tokens)
|
|
||||||
return unless parse_body(@for_block, tokens)
|
return unless parse_body(@for_block, tokens)
|
||||||
parse_body(@else_block, tokens)
|
parse_body(@else_block, tokens)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -18,17 +18,13 @@ module Liquid
|
|||||||
|
|
||||||
attr_reader :blocks
|
attr_reader :blocks
|
||||||
|
|
||||||
def initialize(tag_name, markup, options)
|
|
||||||
super
|
|
||||||
@blocks = []
|
|
||||||
push_block('if', markup)
|
|
||||||
end
|
|
||||||
|
|
||||||
def nodelist
|
def nodelist
|
||||||
@blocks.map(&:attachment)
|
@blocks.map(&:attachment)
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse(tokens)
|
def parse(tokens)
|
||||||
|
@blocks = []
|
||||||
|
push_block('if', @markup)
|
||||||
while parse_body(@blocks.last.attachment, tokens)
|
while parse_body(@blocks.last.attachment, tokens)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -21,10 +21,8 @@ module Liquid
|
|||||||
|
|
||||||
attr_reader :template_name_expr, :variable_name_expr, :attributes
|
attr_reader :template_name_expr, :variable_name_expr, :attributes
|
||||||
|
|
||||||
def initialize(tag_name, markup, options)
|
def parse(_tokens)
|
||||||
super
|
if @markup =~ SYNTAX
|
||||||
|
|
||||||
if markup =~ SYNTAX
|
|
||||||
|
|
||||||
template_name = Regexp.last_match(1)
|
template_name = Regexp.last_match(1)
|
||||||
variable_name = Regexp.last_match(3)
|
variable_name = Regexp.last_match(3)
|
||||||
@@ -34,18 +32,15 @@ module Liquid
|
|||||||
@template_name_expr = Expression.parse(template_name)
|
@template_name_expr = Expression.parse(template_name)
|
||||||
@attributes = {}
|
@attributes = {}
|
||||||
|
|
||||||
markup.scan(TagAttributes) do |key, value|
|
@markup.scan(TagAttributes) do |key, value|
|
||||||
@attributes[key] = Expression.parse(value)
|
@attributes[key] = Expression.parse(value)
|
||||||
end
|
end
|
||||||
|
|
||||||
else
|
else
|
||||||
raise SyntaxError, options[:locale].t("errors.syntax.include")
|
raise SyntaxError, @parse_context[:locale].t("errors.syntax.include")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse(_tokens)
|
|
||||||
end
|
|
||||||
|
|
||||||
def render_to_output_buffer(context, output)
|
def render_to_output_buffer(context, output)
|
||||||
template_name = context.evaluate(@template_name_expr)
|
template_name = context.evaluate(@template_name_expr)
|
||||||
raise ArgumentError, options[:locale].t("errors.argument.include") unless template_name
|
raise ArgumentError, options[:locale].t("errors.argument.include") unless template_name
|
||||||
|
|||||||
@@ -17,9 +17,8 @@ module Liquid
|
|||||||
# Hello: 2
|
# Hello: 2
|
||||||
#
|
#
|
||||||
class Increment < Tag
|
class Increment < Tag
|
||||||
def initialize(tag_name, markup, options)
|
def parse(_tokens)
|
||||||
super
|
@variable = @markup.strip
|
||||||
@variable = markup.strip
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_to_output_buffer(context, output)
|
def render_to_output_buffer(context, output)
|
||||||
|
|||||||
@@ -5,13 +5,8 @@ module Liquid
|
|||||||
Syntax = /\A\s*\z/
|
Syntax = /\A\s*\z/
|
||||||
FullTokenPossiblyInvalid = /\A(.*)#{TagStart}\s*(\w+)\s*(.*)?#{TagEnd}\z/om
|
FullTokenPossiblyInvalid = /\A(.*)#{TagStart}\s*(\w+)\s*(.*)?#{TagEnd}\z/om
|
||||||
|
|
||||||
def initialize(tag_name, markup, parse_context)
|
|
||||||
super
|
|
||||||
|
|
||||||
ensure_valid_markup(tag_name, markup, parse_context)
|
|
||||||
end
|
|
||||||
|
|
||||||
def parse(tokens)
|
def parse(tokens)
|
||||||
|
ensure_valid_markup(@tag_name, @markup, @parse_context)
|
||||||
@body = +''
|
@body = +''
|
||||||
while (token = tokens.shift)
|
while (token = tokens.shift)
|
||||||
if token =~ FullTokenPossiblyInvalid
|
if token =~ FullTokenPossiblyInvalid
|
||||||
|
|||||||
@@ -8,10 +8,8 @@ module Liquid
|
|||||||
|
|
||||||
attr_reader :template_name_expr, :attributes
|
attr_reader :template_name_expr, :attributes
|
||||||
|
|
||||||
def initialize(tag_name, markup, options)
|
def parse(_tokens)
|
||||||
super
|
raise SyntaxError, @parse_context[:locale].t("errors.syntax.render") unless @markup =~ SYNTAX
|
||||||
|
|
||||||
raise SyntaxError, options[:locale].t("errors.syntax.render") unless markup =~ SYNTAX
|
|
||||||
|
|
||||||
template_name = Regexp.last_match(1)
|
template_name = Regexp.last_match(1)
|
||||||
variable_name = Regexp.last_match(3)
|
variable_name = Regexp.last_match(3)
|
||||||
@@ -21,7 +19,7 @@ module Liquid
|
|||||||
@template_name_expr = Expression.parse(template_name)
|
@template_name_expr = Expression.parse(template_name)
|
||||||
|
|
||||||
@attributes = {}
|
@attributes = {}
|
||||||
markup.scan(TagAttributes) do |key, value|
|
@markup.scan(TagAttributes) do |key, value|
|
||||||
@attributes[key] = Expression.parse(value)
|
@attributes[key] = Expression.parse(value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -6,18 +6,18 @@ module Liquid
|
|||||||
|
|
||||||
attr_reader :variable_name, :collection_name, :attributes
|
attr_reader :variable_name, :collection_name, :attributes
|
||||||
|
|
||||||
def initialize(tag_name, markup, options)
|
def parse(_tokens)
|
||||||
super
|
if @markup =~ Syntax
|
||||||
if markup =~ Syntax
|
|
||||||
@variable_name = Regexp.last_match(1)
|
@variable_name = Regexp.last_match(1)
|
||||||
@collection_name = Expression.parse(Regexp.last_match(2))
|
@collection_name = Expression.parse(Regexp.last_match(2))
|
||||||
@attributes = {}
|
@attributes = {}
|
||||||
markup.scan(TagAttributes) do |key, value|
|
@markup.scan(TagAttributes) do |key, value|
|
||||||
@attributes[key] = Expression.parse(value)
|
@attributes[key] = Expression.parse(value)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
raise SyntaxError, options[:locale].t("errors.syntax.table_row")
|
raise SyntaxError, @parse_context[:locale].t("errors.syntax.table_row")
|
||||||
end
|
end
|
||||||
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_to_output_buffer(context, output)
|
def render_to_output_buffer(context, output)
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
class TagTest < Minitest::Test
|
||||||
|
include Liquid
|
||||||
|
|
||||||
|
def test_all_tags_with_no_parse_can_render
|
||||||
|
Template.tags.each do |key, _tag|
|
||||||
|
Template.tags[key].new(key, '', ParseContext.new).render(Context.new)
|
||||||
|
assert_nil(nil)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_all_tags_are_registered
|
||||||
|
tags = Template.tags.map { |key, _tag| key }
|
||||||
|
expected_tags = %w(tablerow echo if break for assign ifchanged case include continue capture decrement unless increment comment raw render cycle)
|
||||||
|
assert_equal(expected_tags, tags)
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user