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-11-03 17:07:42 -05:00
committed by Justin Li
parent 73fcd42403
commit 15e1d46125
6 changed files with 29 additions and 28 deletions
+2 -2
View File
@@ -6,7 +6,7 @@ module Liquid
end end
def parse(tokens) def parse(tokens)
@body = BlockBody.new(options) @body = BlockBody.new
while more = parse_body(@body, tokens) while more = parse_body(@body, tokens)
end end
end end
@@ -60,7 +60,7 @@ module Liquid
protected protected
def parse_body(body, tokens) 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? @blank &&= body.blank?
return false if end_tag_name == block_delimiter return false if end_tag_name == block_delimiter
+10 -15
View File
@@ -5,21 +5,14 @@ module Liquid
TAGSTART = "{%".freeze TAGSTART = "{%".freeze
VARSTART = "{{".freeze VARSTART = "{{".freeze
def self.parse(tokens, options)
body = new(options)
body.parse(tokens)
body
end
attr_reader :nodelist attr_reader :nodelist
def initialize(options) def initialize
@nodelist = [] @nodelist = []
@blank = true @blank = true
@options = options
end end
def parse(tokens) def parse(tokens, options)
while token = tokens.shift while token = tokens.shift
begin begin
unless token.empty? unless token.empty?
@@ -31,7 +24,7 @@ module Liquid
# fetch the tag from registered blocks # fetch the tag from registered blocks
if tag = Template.tags[tag_name] if tag = Template.tags[tag_name]
markup = token.child(markup) if token.is_a?(Token) 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) new_tag.line_number = token.line_number if token.is_a?(Token)
@blank &&= new_tag.blank? @blank &&= new_tag.blank?
@nodelist << new_tag @nodelist << new_tag
@@ -41,10 +34,10 @@ module Liquid
return yield tag_name, markup return yield tag_name, markup
end end
else 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 end
when token.start_with?(VARSTART) 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) new_var.line_number = token.line_number if token.is_a?(Token)
@nodelist << new_var @nodelist << new_var
@blank = false @blank = false
@@ -107,6 +100,8 @@ module Liquid
output.join output.join
end end
private
def render_token(token, context) def render_token(token, context)
token_output = (token.respond_to?(:render) ? token.render(context) : token) token_output = (token.respond_to?(:render) ? token.render(context) : token)
context.increment_used_resources(:render_length_current, token_output) context.increment_used_resources(:render_length_current, token_output)
@@ -117,12 +112,12 @@ module Liquid
token_output token_output
end end
def create_variable(token) def create_variable(token, options)
token.scan(ContentOfVariable) do |content| token.scan(ContentOfVariable) do |content|
markup = token.is_a?(Token) ? token.child(content.first) : content.first markup = token.is_a?(Token) ? token.child(content.first) : content.first
return Variable.new(markup, @options) return Variable.new(markup, options)
end 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 end
end end
+11 -5
View File
@@ -1,17 +1,23 @@
module Liquid module Liquid
class Document < BlockBody 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| 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
end end
def unknown_tag(tag) def unknown_tag(tag, options)
case tag case tag
when 'else'.freeze, 'end'.freeze 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 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 end
end end
+3 -3
View File
@@ -15,7 +15,7 @@ module Liquid
end end
def parse(tokens) def parse(tokens)
body = BlockBody.new(options) body = BlockBody.new
while more = parse_body(body, tokens) while more = parse_body(body, tokens)
body = @blocks.last.attachment body = @blocks.last.attachment
end end
@@ -56,7 +56,7 @@ module Liquid
private private
def record_when_condition(markup) def record_when_condition(markup)
body = BlockBody.new(options) body = BlockBody.new
while markup while markup
if not markup =~ WhenSyntax if not markup =~ WhenSyntax
@@ -77,7 +77,7 @@ module Liquid
end end
block = ElseCondition.new block = ElseCondition.new
block.attach(BlockBody.new(options)) block.attach(BlockBody.new)
@blocks << block @blocks << block
end end
end end
+2 -2
View File
@@ -49,7 +49,7 @@ module Liquid
def initialize(tag_name, markup, options) def initialize(tag_name, markup, options)
super super
parse_with_selected_parser(markup) parse_with_selected_parser(markup)
@for_block = BlockBody.new(options) @for_block = BlockBody.new
end end
def parse(tokens) def parse(tokens)
@@ -64,7 +64,7 @@ module Liquid
def unknown_tag(tag, markup, tokens) def unknown_tag(tag, markup, tokens)
return super unless tag == 'else'.freeze return super unless tag == 'else'.freeze
@else_block = BlockBody.new(options) @else_block = BlockBody.new
end end
def render(context) def render(context)
+1 -1
View File
@@ -58,7 +58,7 @@ module Liquid
end end
@blocks.push(block) @blocks.push(block)
block.attach(BlockBody.new(options)) block.attach(BlockBody.new)
end end
def lax_parse(markup) def lax_parse(markup)