From 15e1d461252b2d016f17d2c75dd81c6a2dac6c53 Mon Sep 17 00:00:00 2001 From: Dylan Thacker-Smith Date: Wed, 15 Oct 2014 00:24:44 -0400 Subject: [PATCH] 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. --- lib/liquid/block.rb | 4 ++-- lib/liquid/block_body.rb | 25 ++++++++++--------------- lib/liquid/document.rb | 16 +++++++++++----- lib/liquid/tags/case.rb | 6 +++--- lib/liquid/tags/for.rb | 4 ++-- lib/liquid/tags/if.rb | 2 +- 6 files changed, 29 insertions(+), 28 deletions(-) diff --git a/lib/liquid/block.rb b/lib/liquid/block.rb index 010fa0c..e6db869 100644 --- a/lib/liquid/block.rb +++ b/lib/liquid/block.rb @@ -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 diff --git a/lib/liquid/block_body.rb b/lib/liquid/block_body.rb index 19169dd..6d356e9 100644 --- a/lib/liquid/block_body.rb +++ b/lib/liquid/block_body.rb @@ -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 diff --git a/lib/liquid/document.rb b/lib/liquid/document.rb index f7485a4..c5709cb 100644 --- a/lib/liquid/document.rb +++ b/lib/liquid/document.rb @@ -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 diff --git a/lib/liquid/tags/case.rb b/lib/liquid/tags/case.rb index f10f53d..dea0be2 100644 --- a/lib/liquid/tags/case.rb +++ b/lib/liquid/tags/case.rb @@ -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 diff --git a/lib/liquid/tags/for.rb b/lib/liquid/tags/for.rb index 253c3fe..31da9d6 100644 --- a/lib/liquid/tags/for.rb +++ b/lib/liquid/tags/for.rb @@ -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) diff --git a/lib/liquid/tags/if.rb b/lib/liquid/tags/if.rb index 3d4f3db..ba668f2 100644 --- a/lib/liquid/tags/if.rb +++ b/lib/liquid/tags/if.rb @@ -58,7 +58,7 @@ module Liquid end @blocks.push(block) - block.attach(BlockBody.new(options)) + block.attach(BlockBody.new) end def lax_parse(markup)