diff --git a/lib/liquid/document.rb b/lib/liquid/document.rb index ce58fdd..2e47ffb 100644 --- a/lib/liquid/document.rb +++ b/lib/liquid/document.rb @@ -5,7 +5,7 @@ module Liquid def self.parse(tokens, parse_context) doc = new(parse_context) doc.parse(tokens, parse_context) - Optimizer.new.optimize(doc) + doc end attr_reader :parse_context, :body diff --git a/lib/liquid/optimizer.rb b/lib/liquid/optimizer.rb index 97bc44e..529b4f6 100644 --- a/lib/liquid/optimizer.rb +++ b/lib/liquid/optimizer.rb @@ -2,54 +2,18 @@ module Liquid class Optimizer - def optimize(node) - case node - when Liquid::Template then optimize(node.root.body) - when Liquid::Document then optimize(node.body) - when Liquid::BlockBody then optimize_block(node) - when Liquid::Variable then optimize_variable(node) - when Liquid::Assign then optimize_assign(node) - when Liquid::For then optimize_for(node) - when Liquid::If then optimize_if(node) - end - node - end + class << self + attr_accessor :enabled - def optimize_block(block) - block.nodelist.each { |node| optimize(node) } - end + def optimize_variable(node) + return unless enabled - def optimize_variable(node) - # Turn chained `| append: "..."| append: "..."`, into a single `append_all: [...]` - if node.filters.size > 1 && node.filters.all? { |f, _| f == "append" } - node.filters = [["append_all", node.filters.map { |f, (arg)| arg }]] - end - end - - def optimize_assign(node) - optimize(node.from) - end - - def optimize_for(node) - optimize(node.collection_name) - optimize_block(node) - end - - def optimize_if(node) - node.blocks.each do |block| - optimize_condition(block) - optimize(block.attachment) - end - end - - def optimize_condition(node) - case node - when Liquid::ElseCondition - # noop - when Liquid::Condition - optimize(node.left) - optimize(node.right) if node.right + # Turn chained `| append: "..."| append: "..."`, into a single `append_all: [...]` + if node.filters.size > 1 && node.filters.all? { |f, _| f == "append" } + node.filters = [["append_all", node.filters.map { |f, (arg)| arg }]] + end end end + self.enabled = true end end \ No newline at end of file diff --git a/lib/liquid/variable.rb b/lib/liquid/variable.rb index addb495..9015dd2 100644 --- a/lib/liquid/variable.rb +++ b/lib/liquid/variable.rb @@ -31,6 +31,8 @@ module Liquid @line_number = parse_context.line_number parse_with_selected_parser(markup) + + Optimizer.optimize_variable(self) end def raw