mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-12 23:40:45 -07:00
Initial options passing
This commit is contained in:
+4
-2
@@ -28,7 +28,9 @@ module Liquid
|
|||||||
|
|
||||||
# fetch the tag from registered blocks
|
# fetch the tag from registered blocks
|
||||||
if tag = Template.tags[$1]
|
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?
|
@blank &&= new_tag.blank?
|
||||||
@nodelist << new_tag
|
@nodelist << new_tag
|
||||||
else
|
else
|
||||||
@@ -80,7 +82,7 @@ module Liquid
|
|||||||
|
|
||||||
def create_variable(token)
|
def create_variable(token)
|
||||||
token.scan(ContentOfVariable) do |content|
|
token.scan(ContentOfVariable) do |content|
|
||||||
return Variable.new(content.first)
|
return Variable.new(content.first, @options)
|
||||||
end
|
end
|
||||||
raise SyntaxError.new("Variable '#{token}' was not properly terminated with regexp: #{VariableEnd.inspect} ")
|
raise SyntaxError.new("Variable '#{token}' was not properly terminated with regexp: #{VariableEnd.inspect} ")
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
module Liquid
|
module Liquid
|
||||||
class Document < Block
|
class Document < Block
|
||||||
# we don't need markup to open this block
|
# we don't need markup to open this block
|
||||||
def initialize(tokens)
|
def initialize(tokens, options = {})
|
||||||
|
@options = options
|
||||||
parse(tokens)
|
parse(tokens)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
+3
-2
@@ -1,10 +1,11 @@
|
|||||||
module Liquid
|
module Liquid
|
||||||
class Tag
|
class Tag
|
||||||
attr_accessor :nodelist
|
attr_accessor :nodelist, :options
|
||||||
|
|
||||||
def initialize(tag_name, markup, tokens)
|
def initialize(tag_name, markup, tokens)
|
||||||
@tag_name = tag_name
|
@tag_name = tag_name
|
||||||
@markup = markup
|
@markup = markup
|
||||||
|
@options ||= {} # needs || because might be set before initialize
|
||||||
parse(tokens)
|
parse(tokens)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -24,7 +25,7 @@ module Liquid
|
|||||||
end
|
end
|
||||||
|
|
||||||
def switch_parse(markup)
|
def switch_parse(markup)
|
||||||
case Template.error_mode
|
case @options[:error_mode] || Template.error_mode
|
||||||
when :strict then strict_parse(markup)
|
when :strict then strict_parse(markup)
|
||||||
when :lax then lax_parse(markup)
|
when :lax then lax_parse(markup)
|
||||||
when :warn
|
when :warn
|
||||||
|
|||||||
@@ -53,9 +53,9 @@ module Liquid
|
|||||||
end
|
end
|
||||||
|
|
||||||
# creates a new <tt>Template</tt> object from liquid source code
|
# creates a new <tt>Template</tt> object from liquid source code
|
||||||
def parse(source)
|
def parse(source, options = {})
|
||||||
template = Template.new
|
template = Template.new
|
||||||
template.parse(source)
|
template.parse(source, options)
|
||||||
template
|
template
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -67,8 +67,8 @@ module Liquid
|
|||||||
|
|
||||||
# Parse source code.
|
# Parse source code.
|
||||||
# Returns self for easy chaining
|
# Returns self for easy chaining
|
||||||
def parse(source)
|
def parse(source, options = {})
|
||||||
@root = Document.new(tokenize(source))
|
@root = Document.new(tokenize(source), options)
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -15,13 +15,14 @@ module Liquid
|
|||||||
EasyParse = /^ *(\w+(?:\.\w+)*) *$/
|
EasyParse = /^ *(\w+(?:\.\w+)*) *$/
|
||||||
attr_accessor :filters, :name
|
attr_accessor :filters, :name
|
||||||
|
|
||||||
def initialize(markup)
|
def initialize(markup, options = {})
|
||||||
@markup = markup
|
@markup = markup
|
||||||
@name = nil
|
@name = nil
|
||||||
@warning = nil
|
@warning = nil
|
||||||
|
@options = options || {}
|
||||||
|
|
||||||
|
|
||||||
case Template.error_mode
|
case @options[:error_mode] || Template.error_mode
|
||||||
when :strict then strict_parse(markup)
|
when :strict then strict_parse(markup)
|
||||||
when :lax then lax_parse(markup)
|
when :lax then lax_parse(markup)
|
||||||
when :warn
|
when :warn
|
||||||
|
|||||||
@@ -70,13 +70,11 @@ class ErrorHandlingTest < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_lax_unrecognized_operator
|
def test_lax_unrecognized_operator
|
||||||
with_lax_parsing do
|
assert_nothing_raised do
|
||||||
assert_nothing_raised do
|
template = Liquid::Template.parse(' {% if 1 =! 2 %}ok{% endif %} ', :error_mode => :lax)
|
||||||
template = Liquid::Template.parse(' {% if 1 =! 2 %}ok{% endif %} ')
|
assert_equal ' Liquid error: Unknown operator =! ', template.render
|
||||||
assert_equal ' Liquid error: Unknown operator =! ', template.render
|
assert_equal 1, template.errors.size
|
||||||
assert_equal 1, template.errors.size
|
assert_equal Liquid::ArgumentError, template.errors.first.class
|
||||||
assert_equal Liquid::ArgumentError, template.errors.first.class
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -30,7 +30,6 @@ class ParsingQuirksTest < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_error_on_empty_filter
|
def test_error_on_empty_filter
|
||||||
Template.error_mode = :strict
|
|
||||||
assert_nothing_raised do
|
assert_nothing_raised do
|
||||||
Template.parse("{{test}}")
|
Template.parse("{{test}}")
|
||||||
Template.parse("{{|test}}")
|
Template.parse("{{|test}}")
|
||||||
@@ -41,7 +40,6 @@ class ParsingQuirksTest < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_meaningless_parens_error
|
def test_meaningless_parens_error
|
||||||
Template.error_mode = :strict
|
|
||||||
assert_raise(SyntaxError) do
|
assert_raise(SyntaxError) do
|
||||||
markup = "a == 'foo' or (b == 'bar' and c == 'baz') or false"
|
markup = "a == 'foo' or (b == 'bar' and c == 'baz') or false"
|
||||||
Template.parse("{% if #{markup} %} YES {% endif %}")
|
Template.parse("{% if #{markup} %} YES {% endif %}")
|
||||||
@@ -49,7 +47,6 @@ class ParsingQuirksTest < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_unexpected_characters_syntax_error
|
def test_unexpected_characters_syntax_error
|
||||||
Template.error_mode = :strict
|
|
||||||
assert_raise(SyntaxError) do
|
assert_raise(SyntaxError) do
|
||||||
markup = "true && false"
|
markup = "true && false"
|
||||||
Template.parse("{% if #{markup} %} YES {% endif %}")
|
Template.parse("{% if #{markup} %} YES {% endif %}")
|
||||||
@@ -61,12 +58,10 @@ class ParsingQuirksTest < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_no_error_on_lax_empty_filter
|
def test_no_error_on_lax_empty_filter
|
||||||
with_lax_parsing do
|
assert_nothing_raised do
|
||||||
assert_nothing_raised do
|
Template.parse("{{test |a|b|}}", :error_mode => :lax)
|
||||||
Template.parse("{{test |a|b|}}")
|
Template.parse("{{test}}", :error_mode => :lax)
|
||||||
Template.parse("{{test}}")
|
Template.parse("{{|test|}}", :error_mode => :lax)
|
||||||
Template.parse("{{|test|}}")
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -73,11 +73,9 @@ class VariableTest < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_symbol
|
def test_symbol
|
||||||
with_lax_parsing do
|
var = Variable.new("http://disney.com/logo.gif | image: 'med' ", :error_mode => :lax)
|
||||||
var = Variable.new("http://disney.com/logo.gif | image: 'med' ")
|
assert_equal "http://disney.com/logo.gif", var.name
|
||||||
assert_equal "http://disney.com/logo.gif", var.name
|
assert_equal [["image",["'med'"]]], var.filters
|
||||||
assert_equal [["image",["'med'"]]], var.filters
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_string_to_filter
|
def test_string_to_filter
|
||||||
@@ -123,11 +121,9 @@ class VariableTest < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_lax_filter_argument_parsing
|
def test_lax_filter_argument_parsing
|
||||||
with_lax_parsing do
|
var = Variable.new(%! number_of_comments | pluralize: 'comment': 'comments' !, :error_mode => :lax)
|
||||||
var = Variable.new(%! number_of_comments | pluralize: 'comment': 'comments' !)
|
assert_equal 'number_of_comments', var.name
|
||||||
assert_equal 'number_of_comments', var.name
|
assert_equal [['pluralize',["'comment'","'comments'"]]], var.filters
|
||||||
assert_equal [['pluralize',["'comment'","'comments'"]]], var.filters
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_strict_filter_argument_parsing
|
def test_strict_filter_argument_parsing
|
||||||
|
|||||||
Reference in New Issue
Block a user