Avoid storing options instance variable in BlockBody.

There is no need to pass parse options to the BlockBody initializer, since
it does all the parsing in the parse method, unlike tags which parse the
tag markup in the initializer.
This commit is contained in:
Dylan Thacker-Smith 2014-10-15 00:24:44 -04:00 committed by Justin Li
parent 73fcd42403
commit 15e1d46125
6 changed files with 29 additions and 28 deletions

View File

@ -6,7 +6,7 @@ module Liquid
end
def parse(tokens)
@body = BlockBody.new(options)
@body = BlockBody.new
while more = parse_body(@body, tokens)
end
end
@ -60,7 +60,7 @@ module Liquid
protected
def parse_body(body, tokens)
body.parse(tokens) do |end_tag_name, end_tag_params|
body.parse(tokens, options) do |end_tag_name, end_tag_params|
@blank &&= body.blank?
return false if end_tag_name == block_delimiter

View File

@ -5,21 +5,14 @@ module Liquid
TAGSTART = "{%".freeze
VARSTART = "{{".freeze
def self.parse(tokens, options)
body = new(options)
body.parse(tokens)
body
end
attr_reader :nodelist
def initialize(options)
def initialize
@nodelist = []
@blank = true
@options = options
end
def parse(tokens)
def parse(tokens, options)
while token = tokens.shift
begin
unless token.empty?
@ -31,7 +24,7 @@ module Liquid
# fetch the tag from registered blocks
if tag = Template.tags[tag_name]
markup = token.child(markup) if token.is_a?(Token)
new_tag = tag.parse(tag_name, markup, tokens, @options)
new_tag = tag.parse(tag_name, markup, tokens, options)
new_tag.line_number = token.line_number if token.is_a?(Token)
@blank &&= new_tag.blank?
@nodelist << new_tag
@ -41,10 +34,10 @@ module Liquid
return yield tag_name, markup
end
else
raise SyntaxError.new(@options[:locale].t("errors.syntax.tag_termination".freeze, :token => token, :tag_end => TagEnd.inspect))
raise SyntaxError.new(options[:locale].t("errors.syntax.tag_termination".freeze, :token => token, :tag_end => TagEnd.inspect))
end
when token.start_with?(VARSTART)
new_var = create_variable(token)
new_var = create_variable(token, options)
new_var.line_number = token.line_number if token.is_a?(Token)
@nodelist << new_var
@blank = false
@ -107,6 +100,8 @@ module Liquid
output.join
end
private
def render_token(token, context)
token_output = (token.respond_to?(:render) ? token.render(context) : token)
context.increment_used_resources(:render_length_current, token_output)
@ -117,12 +112,12 @@ module Liquid
token_output
end
def create_variable(token)
def create_variable(token, options)
token.scan(ContentOfVariable) do |content|
markup = token.is_a?(Token) ? token.child(content.first) : content.first
return Variable.new(markup, @options)
return Variable.new(markup, options)
end
raise SyntaxError.new(@options[:locale].t("errors.syntax.variable_termination".freeze, :token => token, :tag_end => VariableEnd.inspect))
raise SyntaxError.new(options[:locale].t("errors.syntax.variable_termination".freeze, :token => token, :tag_end => VariableEnd.inspect))
end
end
end

View File

@ -1,17 +1,23 @@
module Liquid
class Document < BlockBody
def parse(tokens)
def self.parse(tokens, options)
doc = new
doc.parse(tokens, options)
doc
end
def parse(tokens, options)
super do |end_tag_name, end_tag_params|
unknown_tag(end_tag_name) if end_tag_name
unknown_tag(end_tag_name, options) if end_tag_name
end
end
def unknown_tag(tag)
def unknown_tag(tag, options)
case tag
when 'else'.freeze, 'end'.freeze
raise SyntaxError.new(@options[:locale].t("errors.syntax.unexpected_outer_tag".freeze, :tag => tag))
raise SyntaxError.new(options[:locale].t("errors.syntax.unexpected_outer_tag".freeze, :tag => tag))
else
raise SyntaxError.new(@options[:locale].t("errors.syntax.unknown_tag".freeze, :tag => tag))
raise SyntaxError.new(options[:locale].t("errors.syntax.unknown_tag".freeze, :tag => tag))
end
end
end

View File

@ -15,7 +15,7 @@ module Liquid
end
def parse(tokens)
body = BlockBody.new(options)
body = BlockBody.new
while more = parse_body(body, tokens)
body = @blocks.last.attachment
end
@ -56,7 +56,7 @@ module Liquid
private
def record_when_condition(markup)
body = BlockBody.new(options)
body = BlockBody.new
while markup
if not markup =~ WhenSyntax
@ -77,7 +77,7 @@ module Liquid
end
block = ElseCondition.new
block.attach(BlockBody.new(options))
block.attach(BlockBody.new)
@blocks << block
end
end

View File

@ -49,7 +49,7 @@ module Liquid
def initialize(tag_name, markup, options)
super
parse_with_selected_parser(markup)
@for_block = BlockBody.new(options)
@for_block = BlockBody.new
end
def parse(tokens)
@ -64,7 +64,7 @@ module Liquid
def unknown_tag(tag, markup, tokens)
return super unless tag == 'else'.freeze
@else_block = BlockBody.new(options)
@else_block = BlockBody.new
end
def render(context)

View File

@ -58,7 +58,7 @@ module Liquid
end
@blocks.push(block)
block.attach(BlockBody.new(options))
block.attach(BlockBody.new)
end
def lax_parse(markup)