public
Fork of halorgium/mephisto
Description: A mirror of the mephisto code-base
Homepage: http://mephistoblog.com/
Clone URL: git://github.com/technoweenie/mephisto.git
Click here to lend your support to: mephisto and make a donation at www.pledgie.com !
update liquid

git-svn-id: http://svn.techno-weenie.net/projects/mephisto/trunk@2718 
567b1171-46fb-0310-a4c9-b4bef9110e78
technoweenie (author)
Tue Jan 23 14:26:11 -0800 2007
commit  769eb0fe49691b965122f29e7ecaffe28bf9236d
tree    7dda5c6c5743737e16387582b3cba417d0e22274
parent  66d25c6340befc5819244d21fa408d741276d6b6
...
7
8
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
11
12
...
57
58
59
60
61
62
63
64
65
66
67
68
69
70
 
71
72
73
 
 
 
 
74
75
76
...
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
...
71
72
73
 
 
 
 
 
 
 
 
 
 
 
74
75
 
 
76
77
78
79
80
81
82
0
@@ -7,6 +7,20 @@ module Liquid
0
   # c.evaluate #=> true
0
   #
0
   class Condition
0
+ @@operators = {
0
+ '==' => lambda { |cond, left, right| cond.send(:equal_variables, left, right) },
0
+ '!=' => lambda { |cond, left, right| !cond.send(:equal_variables, left, right) },
0
+ '<>' => lambda { |cond, left, right| !cond.send(:equal_variables, left, right) },
0
+ '<' => :<,
0
+ '>' => :>,
0
+ '>=' => :>=,
0
+ '<=' => :<=
0
+ }
0
+
0
+ def self.operators
0
+ @@operators
0
+ end
0
+
0
     attr_reader :attachment
0
     attr_accessor :left, :operator, :right
0
   
0
@@ -57,20 +71,12 @@ module Liquid
0
 
0
       left, right = context[left], context[right]
0
 
0
- operation = case op
0
- when '==' then return equal_variables(left, right)
0
- when '!=' then return !equal_variables(left, right)
0
- when '<>' then return !equal_variables(left, right)
0
- when '>' then :>
0
- when '<' then :<
0
- when '>=' then :>=
0
- when '<=' then :<=
0
- else
0
- raise ArgumentError.new("Error in tag '#{name}' - Unknown operator #{op}")
0
- end
0
+ operation = self.class.operators[op] || raise(ArgumentError.new("Error in tag '#{name}' - Unknown operator #{op}"))
0
 
0
- if left.respond_to?(operation) and right.respond_to?(operation)
0
- left.send(operation, right)
0
+ if operation.respond_to?(:call)
0
+ operation.call(self, left, right)
0
+ elsif left.respond_to?(operation) and right.respond_to?(operation)
0
+ left.send(operation, right)
0
       else
0
         nil
0
       end
...
1
2
3
 
4
5
6
...
1
2
 
3
4
5
6
0
@@ -1,6 +1,6 @@
0
 module Liquid
0
   class If < Block
0
- Syntax = /(#{QuotedFragment})\s*([=!<>]+)?\s*(#{QuotedFragment})?/
0
+ Syntax = /(#{QuotedFragment})\s*([=!<>a-z_]+)?\s*(#{QuotedFragment})?/
0
     
0
     def initialize(tag_name, markup, tokens)
0
     
...
6
7
8
9
10
 
 
11
12
13
14
15
16
17
18
19
 
 
 
 
 
 
 
 
 
 
 
20
21
22
23
24
25
 
26
27
28
...
6
7
8
 
 
9
10
11
 
 
 
 
 
 
 
 
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
0
@@ -6,23 +6,27 @@ module Liquid
0
     attr_accessor :filters, :name
0
     
0
     def initialize(markup)
0
- @markup = markup
0
- @name = markup.match(/\s*(#{QuotedFragment})/)[1]
0
+ @markup = markup
0
+ @name = nil
0
       @filters = []
0
- if markup.match(/#{FilterSperator}\s*(.*)/)
0
- filters = Regexp.last_match(1).split(/#{FilterSperator}/)
0
-
0
- filters.each do |f|
0
- if matches = f.match(/\s*(\w+)/)
0
- filtername = matches[1]
0
- filterargs = f.scan(/(?:#{FilterArgumentSeparator}|#{ArgumentSeparator})\s*(#{QuotedFragment})/).flatten
0
- @filters << [filtername.to_sym, filterargs]
0
+ if match = markup.match(/\s*(#{QuotedFragment})/)
0
+ @name = match[1]
0
+ if markup.match(/#{FilterSperator}\s*(.*)/)
0
+ filters = Regexp.last_match(1).split(/#{FilterSperator}/)
0
+
0
+ filters.each do |f|
0
+ if matches = f.match(/\s*(\w+)/)
0
+ filtername = matches[1]
0
+ filterargs = f.scan(/(?:#{FilterArgumentSeparator}|#{ArgumentSeparator})\s*(#{QuotedFragment})/).flatten
0
+ @filters << [filtername.to_sym, filterargs]
0
+ end
0
           end
0
         end
0
       end
0
     end
0
 
0
     def render(context)
0
+ return '' if @name.nil?
0
       output = context[@name]
0
       @filters.inject(output) do |output, filter|
0
         filterargs = filter[1].to_a.collect do |a|
...
92
93
94
 
 
 
 
 
 
 
 
 
95
96
...
92
93
94
95
96
97
98
99
100
101
102
103
104
105
0
@@ -92,4 +92,13 @@ class IfElseTest < Test::Unit::TestCase
0
   def test_syntax_error_no_variable
0
     assert_raise(SyntaxError){ assert_template_result('', '{% if jerry == 1 %}')}
0
   end
0
+
0
+ def test_if_with_custom_condition
0
+ Condition.operators['contains'] = :[]
0
+
0
+ assert_template_result('yes', %({% if 'bob' contains 'o' %}yes{% endif %}))
0
+ assert_template_result('no', %({% if 'bob' contains 'f' %}yes{% else %}no{% endif %}))
0
+ ensure
0
+ Condition.operators.delete 'contains'
0
+ end
0
 end
0
\ No newline at end of file
...
45
46
47
48
 
 
49
50
51
...
45
46
47
 
48
49
50
51
52
0
@@ -45,7 +45,8 @@ class StandardFiltersTest < Test::Unit::TestCase
0
   def test_truncatewords
0
     assert_equal 'one two three', @filters.truncatewords('one two three', 4)
0
     assert_equal 'one two...', @filters.truncatewords('one two three', 2)
0
- assert_equal 'one two three', @filters.truncatewords('one two three')
0
+ assert_equal 'one two three', @filters.truncatewords('one two three')
0
+ assert_equal 'Two small (13&#8221; x 5.5&#8221; x 10&#8221; high) baskets fit inside one large basket (13&#8221;...', @filters.truncatewords('Two small (13&#8221; x 5.5&#8221; x 10&#8221; high) baskets fit inside one large basket (13&#8221; x 16&#8221; x 10.5&#8221; high) with cover.', 15)
0
   end
0
   
0
   def test_strip_html

Comments

    No one has commented yet.