diff --git a/lib/liquid.rb b/lib/liquid.rb index 12c9c8a..a58575a 100644 --- a/lib/liquid.rb +++ b/lib/liquid.rb @@ -1,5 +1,5 @@ # Copyright (c) 2005 Tobias Luetke -# +# # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including @@ -7,10 +7,10 @@ # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: -# +# # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. -# +# # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND @@ -22,7 +22,7 @@ $LOAD_PATH.unshift(File.dirname(__FILE__)) module Liquid - FilterSperator = /\|/ + FilterSeparator = /\|/ ArgumentSeparator = ',' FilterArgumentSeparator = ':' VariableAttributeSeparator = '.' @@ -57,14 +57,6 @@ require 'liquid/standardfilters' require 'liquid/condition' require 'liquid/module_ex' -# Load all the tags of the standard library +# Load all the tags of the standard library # Dir[File.dirname(__FILE__) + '/liquid/tags/*.rb'].each { |f| require f } - - - - - - - - diff --git a/lib/liquid/variable.rb b/lib/liquid/variable.rb index 16d7f99..d16a216 100644 --- a/lib/liquid/variable.rb +++ b/lib/liquid/variable.rb @@ -1,5 +1,5 @@ module Liquid - + # Holds variables. Variables are only loaded "just in time" # and are not evaluated as part of the render stage # @@ -10,30 +10,30 @@ module Liquid # # {{ user | link }} # - class Variable + class Variable attr_accessor :filters, :name - + def initialize(markup) - @markup = markup + @markup = markup @name = nil @filters = [] if match = markup.match(/\s*(#{QuotedFragment})/) @name = match[1] - if markup.match(/#{FilterSperator}\s*(.*)/) - filters = Regexp.last_match(1).split(/#{FilterSperator}/) - - filters.each do |f| + if markup.match(/#{FilterSeparator}\s*(.*)/) + filters = Regexp.last_match(1).split(/#{FilterSeparator}/) + + filters.each do |f| if matches = f.match(/\s*(\w+)/) filtername = matches[1] - filterargs = f.scan(/(?:#{FilterArgumentSeparator}|#{ArgumentSeparator})\s*(#{QuotedFragment})/).flatten + filterargs = f.scan(/(?:#{FilterArgumentSeparator}|#{ArgumentSeparator})\s*(#{QuotedFragment})/).flatten @filters << [filtername.to_sym, filterargs] end end end end - end + end - def render(context) + def render(context) return '' if @name.nil? output = context[@name] @filters.inject(output) do |output, filter| @@ -45,7 +45,7 @@ module Liquid rescue FilterNotFound raise FilterNotFound, "Error - filter '#{filter[0]}' in '#{@markup.strip}' could not be found." end - end + end output end end diff --git a/test/filter_test.rb b/test/filter_test.rb index 6cc5221..c66fe06 100644 --- a/test/filter_test.rb +++ b/test/filter_test.rb @@ -1,12 +1,11 @@ #!/usr/bin/env ruby require File.dirname(__FILE__) + '/helper' - module MoneyFilter def money(input) sprintf(' %d$ ', input) end - + def money_with_underscore(input) sprintf(' %d$ ', input) end @@ -18,33 +17,32 @@ module CanadianMoneyFilter end end - class FiltersTest < Test::Unit::TestCase include Liquid - + def setup @context = Context.new end - - def test_local_filter + + def test_local_filter @context['var'] = 1000 @context.add_filters(MoneyFilter) assert_equal ' 1000$ ', Variable.new("var | money").render(@context) - end - + end + def test_underscore_in_filter_name @context['var'] = 1000 @context.add_filters(MoneyFilter) assert_equal ' 1000$ ', Variable.new("var | money_with_underscore").render(@context) end - def test_second_filter_overwrites_first + def test_second_filter_overwrites_first @context['var'] = 1000 @context.add_filters(MoneyFilter) - @context.add_filters(CanadianMoneyFilter) - assert_equal ' 1000$ CAD ', Variable.new("var | money").render(@context) + @context.add_filters(CanadianMoneyFilter) + assert_equal ' 1000$ CAD ', Variable.new("var | money").render(@context) end - + def test_size @context['var'] = 'abcd' @context.add_filters(MoneyFilter) @@ -53,7 +51,7 @@ class FiltersTest < Test::Unit::TestCase def test_join @context['var'] = [1,2,3,4] - assert_equal "1 2 3 4", Variable.new("var | join").render(@context) + assert_equal "1 2 3 4", Variable.new("var | join").render(@context) end def test_sort @@ -67,15 +65,15 @@ class FiltersTest < Test::Unit::TestCase assert_equal [3], Variable.new("value | sort").render(@context) assert_equal ['are', 'flattened'], Variable.new("arrays | sort").render(@context) end - + def test_strip_html @context['var'] = "bla blub" - assert_equal "bla blub", Variable.new("var | strip_html").render(@context) + assert_equal "bla blub", Variable.new("var | strip_html").render(@context) end def test_capitalize @context['var'] = "blub" - assert_equal "Blub", Variable.new("var | capitalize").render(@context) + assert_equal "Blub", Variable.new("var | capitalize").render(@context) end end @@ -84,15 +82,14 @@ class FiltersInTemplate < Test::Unit::TestCase def test_local_global Template.register_filter(MoneyFilter) - + assert_equal " 1000$ ", Template.parse("{{1000 | money}}").render(nil, nil) assert_equal " 1000$ CAD ", Template.parse("{{1000 | money}}").render(nil, :filters => CanadianMoneyFilter) assert_equal " 1000$ CAD ", Template.parse("{{1000 | money}}").render(nil, :filters => [CanadianMoneyFilter]) end - + def test_local_filter_with_deprecated_syntax assert_equal " 1000$ CAD ", Template.parse("{{1000 | money}}").render(nil, CanadianMoneyFilter) assert_equal " 1000$ CAD ", Template.parse("{{1000 | money}}").render(nil, [CanadianMoneyFilter]) end - end diff --git a/test/variable_test.rb b/test/variable_test.rb index 4cd1f38..058383a 100644 --- a/test/variable_test.rb +++ b/test/variable_test.rb @@ -8,7 +8,7 @@ class VariableTest < Test::Unit::TestCase var = Variable.new('hello') assert_equal 'hello', var.name end - + def test_filters var = Variable.new('hello | textileze') assert_equal 'hello', var.name @@ -25,15 +25,15 @@ class VariableTest < Test::Unit::TestCase var = Variable.new(%! 'typo' | link_to: 'Typo', true !) assert_equal %!'typo'!, var.name assert_equal [[:link_to,["'Typo'", "true"]]], var.filters - + var = Variable.new(%! 'typo' | link_to: 'Typo', false !) assert_equal %!'typo'!, var.name assert_equal [[:link_to,["'Typo'", "false"]]], var.filters - + var = Variable.new(%! 'foo' | repeat: 3 !) assert_equal %!'foo'!, var.name assert_equal [[:repeat,["3"]]], var.filters - + var = Variable.new(%! 'foo' | repeat: 3, 3 !) assert_equal %!'foo'!, var.name assert_equal [[:repeat,["3","3"]]], var.filters @@ -45,20 +45,20 @@ class VariableTest < Test::Unit::TestCase var = Variable.new(%! hello | strftime: '%Y, okay?'!) assert_equal 'hello', var.name assert_equal [[:strftime,["'%Y, okay?'"]]], var.filters - + var = Variable.new(%! hello | things: "%Y, okay?", 'the other one'!) assert_equal 'hello', var.name assert_equal [[:things,["\"%Y, okay?\"","'the other one'"]]], var.filters end - + def test_filter_with_date_parameter var = Variable.new(%! '2006-06-06' | date: "%m/%d/%Y"!) assert_equal "'2006-06-06'", var.name assert_equal [[:date,["\"%m/%d/%Y\""]]], var.filters - + end - + def test_filters_without_whitespace var = Variable.new('hello | textileze | paragraph') assert_equal 'hello', var.name @@ -68,11 +68,11 @@ class VariableTest < Test::Unit::TestCase assert_equal 'hello', var.name assert_equal [[:textileze,[]], [:paragraph,[]]], var.filters end - + def test_symbol 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 + assert_equal [[:image,["'med'"]]], var.filters end def test_string_single_quoted @@ -84,7 +84,7 @@ class VariableTest < Test::Unit::TestCase var = Variable.new(%| 'hello' |) assert_equal "'hello'", var.name end - + def test_integer var = Variable.new(%| 1000 |) assert_equal "1000", var.name @@ -94,7 +94,7 @@ class VariableTest < Test::Unit::TestCase var = Variable.new(%| 1000.01 |) assert_equal "1000.01", var.name end - + def test_string_with_special_chars var = Variable.new(%| 'hello! $!@.;"ddasd" ' |) assert_equal %|'hello! $!@.;"ddasd" '|, var.name @@ -109,14 +109,14 @@ end class VariableResolutionTest < Test::Unit::TestCase include Liquid - - def test_simple_variable + + def test_simple_variable template = Template.parse(%|{{test}}|) assert_equal 'worked', template.render('test' => 'worked') assert_equal 'worked wonderfully', template.render('test' => 'worked wonderfully') end - def test_simple_with_whitespaces + def test_simple_with_whitespaces template = Template.parse(%| {{ test }} |) assert_equal ' worked ', template.render('test' => 'worked') assert_equal ' worked wonderfully ', template.render('test' => 'worked wonderfully')