Reverted james filter in tags branch

This reverts commit 282786d7e2.

Conflicts:

	lib/liquid.rb
	lib/liquid/variable.rb
	test/if_else_test.rb
This commit is contained in:
Tobias Lütke
2009-04-06 08:30:19 -06:00
parent e3fd003a74
commit edcc14f148
11 changed files with 20 additions and 56 deletions
+2 -8
View File
@@ -22,7 +22,7 @@
$LOAD_PATH.unshift(File.dirname(__FILE__)) $LOAD_PATH.unshift(File.dirname(__FILE__))
module Liquid module Liquid
FilterSeparator = /\|/ FilterSperator = /\|/
ArgumentSeparator = ',' ArgumentSeparator = ','
FilterArgumentSeparator = ':' FilterArgumentSeparator = ':'
VariableAttributeSeparator = '.' VariableAttributeSeparator = '.'
@@ -33,13 +33,7 @@ module Liquid
VariableStart = /\{\{/ VariableStart = /\{\{/
VariableEnd = /\}\}/ VariableEnd = /\}\}/
VariableIncompleteEnd = /\}\}?/ VariableIncompleteEnd = /\}\}?/
QuotedString = /"[^"]+"|'[^']+'/ QuotedFragment = /"[^"]+"|'[^']+'|[^\s,|]+/
QuotedFragment = /#{QuotedString}|(?:[^\s,\|'"]|#{QuotedString})+/
StrictQuotedFragment = /"[^"]+"|'[^']+'|[^\s,\|,\:,\,]+/
FirstFilterArgument = /#{FilterArgumentSeparator}(?:#{StrictQuotedFragment})/
OtherFilterArgument = /#{ArgumentSeparator}(?:#{StrictQuotedFragment})/
SpacelessFilter = /#{FilterSeparator}(?:#{StrictQuotedFragment})(?:#{FirstFilterArgument}(?:#{OtherFilterArgument})*)?/
Expression = /(?:#{QuotedFragment}(?:#{SpacelessFilter})*)/
TagAttributes = /(\w+)\s*\:\s*(#{QuotedFragment})/ TagAttributes = /(\w+)\s*\:\s*(#{QuotedFragment})/
AnyStartingTag = /\{\{|\{\%/ AnyStartingTag = /\{\{|\{\%/
PartialTemplateParser = /#{TagStart}.*?#{TagEnd}|#{VariableStart}.*?#{VariableIncompleteEnd}/ PartialTemplateParser = /#{TagStart}.*?#{TagEnd}|#{VariableStart}.*?#{VariableIncompleteEnd}/
-7
View File
@@ -133,9 +133,6 @@ module Liquid
:blank? :blank?
when 'empty' when 'empty'
:empty? :empty?
# filtered variables
when SpacelessFilter
filtered_variable(key)
# Single quoted strings # Single quoted strings
when /^'(.*)'$/ when /^'(.*)'$/
$1.to_s $1.to_s
@@ -224,9 +221,5 @@ module Liquid
object object
end end
def filtered_variable(markup)
Variable.new(markup).render(self)
end
end end
end end
+1 -1
View File
@@ -9,7 +9,7 @@ module Liquid
# {{ monkey }} # {{ monkey }}
# #
class Assign < Tag class Assign < Tag
Syntax = /(#{VariableSignature}+)\s*=\s*(#{Expression}+)/ Syntax = /(#{VariableSignature}+)\s*=\s*(#{QuotedFragment}+)/
def initialize(tag_name, markup, tokens) def initialize(tag_name, markup, tokens)
if markup =~ Syntax if markup =~ Syntax
+2 -2
View File
@@ -1,7 +1,7 @@
module Liquid module Liquid
class Case < Block class Case < Block
Syntax = /(#{Expression})/ Syntax = /(#{QuotedFragment})/
WhenSyntax = /(#{Expression})(?:(?:\s+or\s+|\s*\,\s*)(#{Expression}.*))?/ WhenSyntax = /(#{QuotedFragment})(?:(?:\s+or\s+|\s*\,\s*)(#{QuotedFragment}.*))?/
def initialize(tag_name, markup, tokens) def initialize(tag_name, markup, tokens)
@blocks = [] @blocks = []
+3 -3
View File
@@ -13,8 +13,8 @@ module Liquid
# <div class="green"> Item five</div> # <div class="green"> Item five</div>
# #
class Cycle < Tag class Cycle < Tag
SimpleSyntax = /^#{Expression}/ SimpleSyntax = /^#{QuotedFragment}/
NamedSyntax = /^(#{Expression})\s*\:\s*(.*)/ NamedSyntax = /^(#{QuotedFragment})\s*\:\s*(.*)/
def initialize(tag_name, markup, tokens) def initialize(tag_name, markup, tokens)
case markup case markup
@@ -48,7 +48,7 @@ module Liquid
def variables_from_string(markup) def variables_from_string(markup)
markup.split(',').collect do |var| markup.split(',').collect do |var|
var =~ /\s*(#{Expression})\s*/ var =~ /\s*(#{QuotedFragment})\s*/
$1 ? $1 : nil $1 ? $1 : nil
end.compact end.compact
end end
+1 -1
View File
@@ -42,7 +42,7 @@ module Liquid
# forloop.last:: Returns true if the item is the last item. # forloop.last:: Returns true if the item is the last item.
# #
class For < Block class For < Block
Syntax = /(\w+)\s+in\s+(#{Expression}+)\s*(reversed)?/ Syntax = /(\w+)\s+in\s+(#{QuotedFragment}+)\s*(reversed)?/
def initialize(tag_name, markup, tokens) def initialize(tag_name, markup, tokens)
if markup =~ Syntax if markup =~ Syntax
+1 -1
View File
@@ -13,7 +13,7 @@ module Liquid
# #
class If < Block class If < Block
SyntaxHelp = "Syntax Error in tag 'if' - Valid syntax: if [expression]" SyntaxHelp = "Syntax Error in tag 'if' - Valid syntax: if [expression]"
Syntax = /(#{Expression})\s*([=!<>a-z_]+)?\s*(#{Expression})?/ Syntax = /(#{QuotedFragment})\s*([=!<>a-z_]+)?\s*(#{QuotedFragment})?/
def initialize(tag_name, markup, tokens) def initialize(tag_name, markup, tokens)
+3 -2
View File
@@ -19,8 +19,9 @@ module Liquid
@filters = [] @filters = []
if match = markup.match(/\s*(#{QuotedFragment})/) if match = markup.match(/\s*(#{QuotedFragment})/)
@name = match[1] @name = match[1]
if markup.match(/#{FilterSeparator}\s*(.*)/) if markup.match(/#{FilterSperator}\s*(.*)/)
filters = Regexp.last_match(1).split(/#{FilterSeparator}/) filters = Regexp.last_match(1).split(/#{FilterSperator}/)
filters.each do |f| filters.each do |f|
if matches = f.match(/\s*(\w+)/) if matches = f.match(/\s*(\w+)/)
filtername = matches[1] filtername = matches[1]
+3 -4
View File
@@ -1,11 +1,10 @@
require File.dirname(__FILE__) + '/helper' require File.dirname(__FILE__) + '/helper'
class IfElseTest < Test::Unit::TestCase class AssignTest < Test::Unit::TestCase
include Liquid include Liquid
def test_with_filtered_expressions def test_assigned_variable
assert_template_result('foo','{% assign foo = values|sort|last %}{{ foo }}', 'values' => %w{foo bar baz}) assert_template_result('.foo.','{% assign foo = values %}.{{ foo }}.', 'values' => %w{foo bar baz})
assert_template_result('foo','{% assign sorted = values|sort %}{{ sorted | last }}', 'values' => %w{foo bar baz})
end end
end end
-19
View File
@@ -112,25 +112,6 @@ class IfElseTest < Test::Unit::TestCase
assert_template_result('elsif','{% if false %}if{% elsif true %}elsif{% endif %}') assert_template_result('elsif','{% if false %}if{% elsif true %}elsif{% endif %}')
end end
def test_with_filtered_expressions
assert_template_result('yes','{% if "BLAH"|downcase == "blah" %}yes{% endif %}')
assert_template_result('yes','{% if "FOO BAR"|truncatewords:1,"--" == "FOO--" %}yes{% endif %}')
assert_template_result('yes','{% if "FOO BAR"|truncatewords:1,"--"|downcase == "foo--" %}yes{% endif %}')
assert_template_result('yes','{% if "foo--" == "FOO BAR"|truncatewords:1,"--"|downcase %}yes{% endif %}')
# array transformation, to make sure we aren't converting arrays to strings somewhere along the way:
assert_template_result('yes','{% if values|sort == sorted %}yes{% endif %}', 'values' => %w{foo bar baz}, 'sorted' => %w{bar baz foo})
end
def test_allow_no_spaces_in_filtered_expressions
assert_template_result('','{% if "foo--" == "FOO BAR" |truncatewords:1,"--"|downcase %}yes{% endif %}')
assert_template_result('','{% if "foo--" == "FOO BAR"| truncatewords:1,"--"|downcase %}yes{% endif %}')
assert_template_result('','{% if "foo--" == "FOO BAR"|truncatewords :1,"--"|downcase %}yes{% endif %}')
assert_template_result('','{% if "foo--" == "FOO BAR"|truncatewords: 1,"--"|downcase %}yes{% endif %}')
assert_template_result('','{% if "foo--" == "FOO BAR"|truncatewords:1 ,"--"|downcase %}yes{% endif %}')
assert_template_result('','{% if "foo--" == "FOO BAR"|truncatewords:1, "--"|downcase %}yes{% endif %}')
assert_template_result('','{% if "foo--" == "FOO BAR"|truncatewords:1,"--" |downcase %}yes{% endif %}')
end
def test_syntax_error_no_variable def test_syntax_error_no_variable
assert_raise(SyntaxError){ assert_template_result('', '{% if jerry == 1 %}')} assert_raise(SyntaxError){ assert_template_result('', '{% if jerry == 1 %}')}
end end
+3 -7
View File
@@ -99,10 +99,6 @@ HERE
assert_template_result('+--', '{%for item in array%}{% if forloop.first %}+{% else %}-{% endif %}{%endfor%}', assigns) assert_template_result('+--', '{%for item in array%}{% if forloop.first %}+{% else %}-{% endif %}{%endfor%}', assigns)
end end
def test_for_with_filtered_expressions
assert_template_result('abc','{% for letter in letters|sort %}{{ letter }}{% endfor %}', 'letters' => %w{c b a})
end
def test_limiting def test_limiting
assigns = {'array' => [1,2,3,4,5,6,7,8,9,0]} assigns = {'array' => [1,2,3,4,5,6,7,8,9,0]}
assert_template_result('12','{%for i in array limit:2 %}{{ i }}{%endfor%}',assigns) assert_template_result('12','{%for i in array limit:2 %}{{ i }}{%endfor%}',assigns)
@@ -353,10 +349,10 @@ HERE
assert_template_result('one two one','{%cycle "one", "two"%} {%cycle "one", "two"%} {%cycle "one", "two"%}') assert_template_result('one two one','{%cycle "one", "two"%} {%cycle "one", "two"%} {%cycle "one", "two"%}')
assert_template_result('text-align: left text-align: right','{%cycle "text-align: left", "text-align: right" %} {%cycle "text-align: left", "text-align: right"%}') assert_template_result('text-align: left,text-align: right','{%cycle "text-align: left", "text-align: right" %},{%cycle "text-align: left", "text-align: right"%}')
assert_template_result(' ','{% cycle "", "", "</tr><tr>" %}') assert_template_result('.','.{% cycle "", "", "</tr><tr>" %}')
assert_template_result(' </tr><tr> ','{% cycle "", "", "</tr><tr>" %} {% cycle "", "", "</tr><tr>" %} {% cycle "", "", "</tr><tr>" %} {% cycle "", "", "</tr><tr>" %}') assert_template_result('...</tr><tr> ','.{% cycle "", "", "</tr><tr>" %}.{% cycle "", "", "</tr><tr>" %}.{% cycle "", "", "</tr><tr>" %} {% cycle "", "", "</tr><tr>" %}')
end end