Allow newlines in tags and variables.

This commit is contained in:
Dylan Thacker-Smith
2014-03-20 17:27:03 -04:00
parent face33a137
commit 3b14e27f55
11 changed files with 22 additions and 13 deletions
+1
View File
@@ -3,6 +3,7 @@
## 3.0.0 / not yet released / branch "master" ## 3.0.0 / not yet released / branch "master"
* ... * ...
* Allow newlines in tags and variables
* Tag#parse is called after initialize, which now takes options instead of tokens as the 3rd argument. See #321 [Dylan Thacker-Smith, dylanahsmith] * Tag#parse is called after initialize, which now takes options instead of tokens as the 3rd argument. See #321 [Dylan Thacker-Smith, dylanahsmith]
* Raise `Liquid::ArgumentError` instead of `::ArgumentError` when filter has wrong number of arguments #309 [Bogdan Gusiev, bogdan] * Raise `Liquid::ArgumentError` instead of `::ArgumentError` when filter has wrong number of arguments #309 [Bogdan Gusiev, bogdan]
* Add a to_s default for liquid drops, see #306 [Adam Doeler, releod] * Add a to_s default for liquid drops, see #306 [Adam Doeler, releod]
+2 -2
View File
@@ -40,8 +40,8 @@ module Liquid
Expression = /(?:#{QuotedFragment}(?:#{SpacelessFilter})*)/o Expression = /(?:#{QuotedFragment}(?:#{SpacelessFilter})*)/o
TagAttributes = /(\w+)\s*\:\s*(#{QuotedFragment})/o TagAttributes = /(\w+)\s*\:\s*(#{QuotedFragment})/o
AnyStartingTag = /\{\{|\{\%/ AnyStartingTag = /\{\{|\{\%/
PartialTemplateParser = /#{TagStart}.*?#{TagEnd}|#{VariableStart}.*?#{VariableIncompleteEnd}/o PartialTemplateParser = /#{TagStart}.*?#{TagEnd}|#{VariableStart}.*?#{VariableIncompleteEnd}/om
TemplateParser = /(#{PartialTemplateParser}|#{AnyStartingTag})/o TemplateParser = /(#{PartialTemplateParser}|#{AnyStartingTag})/om
VariableParser = /\[[^\]]+\]|#{VariableSegment}+\??/o VariableParser = /\[[^\]]+\]|#{VariableSegment}+\??/o
end end
+2 -2
View File
@@ -2,8 +2,8 @@ module Liquid
class Block < Tag class Block < Tag
IsTag = /\A#{TagStart}/o IsTag = /\A#{TagStart}/o
IsVariable = /\A#{VariableStart}/o IsVariable = /\A#{VariableStart}/o
FullToken = /\A#{TagStart}\s*(\w+)\s*(.*)?#{TagEnd}\z/o FullToken = /\A#{TagStart}\s*(\w+)\s*(.*)?#{TagEnd}\z/om
ContentOfVariable = /\A#{VariableStart}(.*)#{VariableEnd}\z/o ContentOfVariable = /\A#{VariableStart}(.*)#{VariableEnd}\z/om
def blank? def blank?
@blank || false @blank || false
+3 -3
View File
@@ -171,9 +171,9 @@ module Liquid
LITERALS[key] LITERALS[key]
else else
case key case key
when /\A'(.*)'\z/ # Single quoted strings when /\A'(.*)'\z/m # Single quoted strings
$1 $1
when /\A"(.*)"\z/ # Double quoted strings when /\A"(.*)"\z/m # Double quoted strings
$1 $1
when /\A(-?\d+)\z/ # Integer and floats when /\A(-?\d+)\z/ # Integer and floats
$1.to_i $1.to_i
@@ -218,7 +218,7 @@ module Liquid
# assert_equal 'tobi', @context['hash["name"]'] # assert_equal 'tobi', @context['hash["name"]']
def variable(markup) def variable(markup)
parts = markup.scan(VariableParser) parts = markup.scan(VariableParser)
square_bracketed = /\A\[(.*)\]\z/ square_bracketed = /\A\[(.*)\]\z/m
first_part = parts.shift first_part = parts.shift
+1 -1
View File
@@ -9,7 +9,7 @@ module Liquid
# {{ foo }} # {{ foo }}
# #
class Assign < Tag class Assign < Tag
Syntax = /(#{VariableSignature}+)\s*=\s*(.*)\s*/o Syntax = /(#{VariableSignature}+)\s*=\s*(.*)\s*/om
def initialize(tag_name, markup, options) def initialize(tag_name, markup, options)
super super
+1 -1
View File
@@ -1,7 +1,7 @@
module Liquid module Liquid
class Case < Block class Case < Block
Syntax = /(#{QuotedFragment})/o Syntax = /(#{QuotedFragment})/o
WhenSyntax = /(#{QuotedFragment})(?:(?:\s+or\s+|\s*\,\s*)(#{QuotedFragment}.*))?/o WhenSyntax = /(#{QuotedFragment})(?:(?:\s+or\s+|\s*\,\s*)(#{QuotedFragment}.*))?/om
def initialize(tag_name, markup, options) def initialize(tag_name, markup, options)
super super
+1 -1
View File
@@ -13,7 +13,7 @@ module Liquid
# #
class Cycle < Tag class Cycle < Tag
SimpleSyntax = /\A#{QuotedFragment}+/o SimpleSyntax = /\A#{QuotedFragment}+/o
NamedSyntax = /\A(#{QuotedFragment})\s*\:\s*(.*)/o NamedSyntax = /\A(#{QuotedFragment})\s*\:\s*(.*)/om
def initialize(tag_name, markup, options) def initialize(tag_name, markup, options)
super super
+1 -1
View File
@@ -1,6 +1,6 @@
module Liquid module Liquid
class Raw < Block class Raw < Block
FullTokenPossiblyInvalid = /\A(.*)#{TagStart}\s*(\w+)\s*(.*)?#{TagEnd}\z/o FullTokenPossiblyInvalid = /\A(.*)#{TagStart}\s*(\w+)\s*(.*)?#{TagEnd}\z/om
def parse(tokens) def parse(tokens)
@nodelist ||= [] @nodelist ||= []
+2 -2
View File
@@ -37,9 +37,9 @@ module Liquid
def lax_parse(markup) def lax_parse(markup)
@filters = [] @filters = []
if match = markup.match(/\s*(#{QuotedFragment})(.*)/o) if match = markup.match(/\s*(#{QuotedFragment})(.*)/om)
@name = match[1] @name = match[1]
if match[2].match(/#{FilterSeparator}\s*(.*)/o) if match[2].match(/#{FilterSeparator}\s*(.*)/om)
filters = Regexp.last_match(1).scan(FilterParser) filters = Regexp.last_match(1).scan(FilterParser)
filters.each do |f| filters.each do |f|
if matches = f.match(/\s*(\w+)/) if matches = f.match(/\s*(\w+)/)
+4
View File
@@ -297,4 +297,8 @@ class StandardTagTest < Test::Unit::TestCase
assigns = {'array' => [ 1, 1, 1, 1] } assigns = {'array' => [ 1, 1, 1, 1] }
assert_template_result('1','{%for item in array%}{%ifchanged%}{{item}}{% endifchanged %}{%endfor%}',assigns) assert_template_result('1','{%for item in array%}{%ifchanged%}{{item}}{% endifchanged %}{%endfor%}',assigns)
end end
def test_multiline_tag
assert_template_result '0 1 2 3', "0{%\nfor i in (1..3)\n%} {{\ni\n}}{%\nendfor\n%}"
end
end # StandardTagTest end # StandardTagTest
+4
View File
@@ -197,4 +197,8 @@ class VariableResolutionTest < Test::Unit::TestCase
} }
assert_equal "Unknown variable 'test'", e.message assert_equal "Unknown variable 'test'", e.message
end end
def test_multiline_variable
assert_equal 'worked', Template.parse("{{\ntest\n}}").render!('test' => 'worked')
end
end # VariableTest end # VariableTest