Initial options passing

This commit is contained in:
Tristan Hume
2013-07-30 14:44:41 -04:00
parent 1458396733
commit 84f0c1bef8
8 changed files with 31 additions and 37 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -53,9 +53,9 @@ module Liquid
end
# creates a new <tt>Template</tt> 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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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