From 84f0c1bef88d0912a459de363e1953a494e5d3e0 Mon Sep 17 00:00:00 2001 From: Tristan Hume Date: Tue, 30 Jul 2013 14:44:41 -0400 Subject: [PATCH] Initial options passing --- lib/liquid/block.rb | 6 ++++-- lib/liquid/document.rb | 3 ++- lib/liquid/tag.rb | 5 +++-- lib/liquid/template.rb | 8 ++++---- lib/liquid/variable.rb | 5 +++-- test/liquid/error_handling_test.rb | 12 +++++------- test/liquid/parsing_quirks_test.rb | 13 ++++--------- test/liquid/variable_test.rb | 16 ++++++---------- 8 files changed, 31 insertions(+), 37 deletions(-) diff --git a/lib/liquid/block.rb b/lib/liquid/block.rb index 642bfb3e..233d131c 100644 --- a/lib/liquid/block.rb +++ b/lib/liquid/block.rb @@ -28,7 +28,9 @@ module Liquid # fetch the tag from registered blocks if tag = Template.tags[$1] - new_tag = tag.new($1, $2, tokens) + new_tag = tag.allocate + new_tag.options = @options || {} + new_tag.send(:initialize, $1, $2, tokens) @blank &&= new_tag.blank? @nodelist << new_tag else @@ -80,7 +82,7 @@ module Liquid def create_variable(token) token.scan(ContentOfVariable) do |content| - return Variable.new(content.first) + return Variable.new(content.first, @options) end raise SyntaxError.new("Variable '#{token}' was not properly terminated with regexp: #{VariableEnd.inspect} ") end diff --git a/lib/liquid/document.rb b/lib/liquid/document.rb index a1287629..b802c859 100644 --- a/lib/liquid/document.rb +++ b/lib/liquid/document.rb @@ -1,7 +1,8 @@ module Liquid class Document < Block # we don't need markup to open this block - def initialize(tokens) + def initialize(tokens, options = {}) + @options = options parse(tokens) end diff --git a/lib/liquid/tag.rb b/lib/liquid/tag.rb index 408a86d8..2d2c7a7c 100644 --- a/lib/liquid/tag.rb +++ b/lib/liquid/tag.rb @@ -1,10 +1,11 @@ module Liquid class Tag - attr_accessor :nodelist + attr_accessor :nodelist, :options def initialize(tag_name, markup, tokens) @tag_name = tag_name @markup = markup + @options ||= {} # needs || because might be set before initialize parse(tokens) end @@ -24,7 +25,7 @@ module Liquid end def switch_parse(markup) - case Template.error_mode + case @options[:error_mode] || Template.error_mode when :strict then strict_parse(markup) when :lax then lax_parse(markup) when :warn diff --git a/lib/liquid/template.rb b/lib/liquid/template.rb index aa7bc27d..b43a2a38 100644 --- a/lib/liquid/template.rb +++ b/lib/liquid/template.rb @@ -53,9 +53,9 @@ module Liquid end # creates a new Template object from liquid source code - def parse(source) + def parse(source, options = {}) template = Template.new - template.parse(source) + template.parse(source, options) template end end @@ -67,8 +67,8 @@ module Liquid # Parse source code. # Returns self for easy chaining - def parse(source) - @root = Document.new(tokenize(source)) + def parse(source, options = {}) + @root = Document.new(tokenize(source), options) self end diff --git a/lib/liquid/variable.rb b/lib/liquid/variable.rb index 19574a67..20cb022d 100644 --- a/lib/liquid/variable.rb +++ b/lib/liquid/variable.rb @@ -15,13 +15,14 @@ module Liquid EasyParse = /^ *(\w+(?:\.\w+)*) *$/ attr_accessor :filters, :name - def initialize(markup) + def initialize(markup, options = {}) @markup = markup @name = nil @warning = nil + @options = options || {} - case Template.error_mode + case @options[:error_mode] || Template.error_mode when :strict then strict_parse(markup) when :lax then lax_parse(markup) when :warn diff --git a/test/liquid/error_handling_test.rb b/test/liquid/error_handling_test.rb index 550cade0..43136b8d 100644 --- a/test/liquid/error_handling_test.rb +++ b/test/liquid/error_handling_test.rb @@ -70,13 +70,11 @@ class ErrorHandlingTest < Test::Unit::TestCase end def test_lax_unrecognized_operator - with_lax_parsing do - assert_nothing_raised do - template = Liquid::Template.parse(' {% if 1 =! 2 %}ok{% endif %} ') - assert_equal ' Liquid error: Unknown operator =! ', template.render - assert_equal 1, template.errors.size - assert_equal Liquid::ArgumentError, template.errors.first.class - end + assert_nothing_raised do + template = Liquid::Template.parse(' {% if 1 =! 2 %}ok{% endif %} ', :error_mode => :lax) + assert_equal ' Liquid error: Unknown operator =! ', template.render + assert_equal 1, template.errors.size + assert_equal Liquid::ArgumentError, template.errors.first.class end end diff --git a/test/liquid/parsing_quirks_test.rb b/test/liquid/parsing_quirks_test.rb index 458b05ac..04c13079 100644 --- a/test/liquid/parsing_quirks_test.rb +++ b/test/liquid/parsing_quirks_test.rb @@ -30,7 +30,6 @@ class ParsingQuirksTest < Test::Unit::TestCase end def test_error_on_empty_filter - Template.error_mode = :strict assert_nothing_raised do Template.parse("{{test}}") Template.parse("{{|test}}") @@ -41,7 +40,6 @@ class ParsingQuirksTest < Test::Unit::TestCase end def test_meaningless_parens_error - Template.error_mode = :strict assert_raise(SyntaxError) do markup = "a == 'foo' or (b == 'bar' and c == 'baz') or false" Template.parse("{% if #{markup} %} YES {% endif %}") @@ -49,7 +47,6 @@ class ParsingQuirksTest < Test::Unit::TestCase end def test_unexpected_characters_syntax_error - Template.error_mode = :strict assert_raise(SyntaxError) do markup = "true && false" Template.parse("{% if #{markup} %} YES {% endif %}") @@ -61,12 +58,10 @@ class ParsingQuirksTest < Test::Unit::TestCase end def test_no_error_on_lax_empty_filter - with_lax_parsing do - assert_nothing_raised do - Template.parse("{{test |a|b|}}") - Template.parse("{{test}}") - Template.parse("{{|test|}}") - end + assert_nothing_raised do + Template.parse("{{test |a|b|}}", :error_mode => :lax) + Template.parse("{{test}}", :error_mode => :lax) + Template.parse("{{|test|}}", :error_mode => :lax) end end diff --git a/test/liquid/variable_test.rb b/test/liquid/variable_test.rb index ee3ef08e..a4dd2da2 100644 --- a/test/liquid/variable_test.rb +++ b/test/liquid/variable_test.rb @@ -73,11 +73,9 @@ class VariableTest < Test::Unit::TestCase end def test_symbol - with_lax_parsing do - var = Variable.new("http://disney.com/logo.gif | image: 'med' ") - assert_equal "http://disney.com/logo.gif", var.name - assert_equal [["image",["'med'"]]], var.filters - end + var = Variable.new("http://disney.com/logo.gif | image: 'med' ", :error_mode => :lax) + assert_equal "http://disney.com/logo.gif", var.name + assert_equal [["image",["'med'"]]], var.filters end def test_string_to_filter @@ -123,11 +121,9 @@ class VariableTest < Test::Unit::TestCase end def test_lax_filter_argument_parsing - with_lax_parsing do - var = Variable.new(%! number_of_comments | pluralize: 'comment': 'comments' !) - assert_equal 'number_of_comments', var.name - assert_equal [['pluralize',["'comment'","'comments'"]]], var.filters - end + var = Variable.new(%! number_of_comments | pluralize: 'comment': 'comments' !, :error_mode => :lax) + assert_equal 'number_of_comments', var.name + assert_equal [['pluralize',["'comment'","'comments'"]]], var.filters end def test_strict_filter_argument_parsing