drnic-liquid 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. data/CHANGELOG +44 -0
  2. data/History.txt +44 -0
  3. data/MIT-LICENSE +20 -0
  4. data/Manifest.txt +34 -0
  5. data/README.rdoc +44 -0
  6. data/Rakefile +30 -0
  7. data/example/server/example_servlet.rb +37 -0
  8. data/example/server/liquid_servlet.rb +28 -0
  9. data/example/server/server.rb +12 -0
  10. data/example/server/templates/index.liquid +6 -0
  11. data/example/server/templates/products.liquid +45 -0
  12. data/init.rb +8 -0
  13. data/lib/extras/liquid_view.rb +51 -0
  14. data/lib/liquid.rb +70 -0
  15. data/lib/liquid/block.rb +102 -0
  16. data/lib/liquid/condition.rb +120 -0
  17. data/lib/liquid/context.rb +221 -0
  18. data/lib/liquid/document.rb +17 -0
  19. data/lib/liquid/drop.rb +51 -0
  20. data/lib/liquid/errors.rb +11 -0
  21. data/lib/liquid/extensions.rb +56 -0
  22. data/lib/liquid/file_system.rb +62 -0
  23. data/lib/liquid/htmltags.rb +74 -0
  24. data/lib/liquid/module_ex.rb +62 -0
  25. data/lib/liquid/standardfilters.rb +209 -0
  26. data/lib/liquid/strainer.rb +51 -0
  27. data/lib/liquid/tag.rb +26 -0
  28. data/lib/liquid/tags/assign.rb +33 -0
  29. data/lib/liquid/tags/capture.rb +35 -0
  30. data/lib/liquid/tags/case.rb +83 -0
  31. data/lib/liquid/tags/comment.rb +9 -0
  32. data/lib/liquid/tags/cycle.rb +59 -0
  33. data/lib/liquid/tags/for.rb +136 -0
  34. data/lib/liquid/tags/if.rb +79 -0
  35. data/lib/liquid/tags/ifchanged.rb +20 -0
  36. data/lib/liquid/tags/include.rb +55 -0
  37. data/lib/liquid/tags/unless.rb +33 -0
  38. data/lib/liquid/template.rb +147 -0
  39. data/lib/liquid/variable.rb +49 -0
  40. data/liquid.gemspec +40 -0
  41. data/performance/shopify.rb +92 -0
  42. data/performance/shopify/comment_form.rb +33 -0
  43. data/performance/shopify/database.rb +45 -0
  44. data/performance/shopify/json_filter.rb +7 -0
  45. data/performance/shopify/liquid.rb +18 -0
  46. data/performance/shopify/money_filter.rb +18 -0
  47. data/performance/shopify/paginate.rb +93 -0
  48. data/performance/shopify/shop_filter.rb +98 -0
  49. data/performance/shopify/tag_filter.rb +25 -0
  50. data/performance/shopify/vision.database.yml +945 -0
  51. data/performance/shopify/weight_filter.rb +11 -0
  52. data/performance/tests/dropify/article.liquid +74 -0
  53. data/performance/tests/dropify/blog.liquid +33 -0
  54. data/performance/tests/dropify/cart.liquid +66 -0
  55. data/performance/tests/dropify/collection.liquid +22 -0
  56. data/performance/tests/dropify/index.liquid +47 -0
  57. data/performance/tests/dropify/page.liquid +8 -0
  58. data/performance/tests/dropify/product.liquid +68 -0
  59. data/performance/tests/dropify/theme.liquid +105 -0
  60. data/performance/tests/ripen/article.liquid +74 -0
  61. data/performance/tests/ripen/blog.liquid +13 -0
  62. data/performance/tests/ripen/cart.liquid +54 -0
  63. data/performance/tests/ripen/collection.liquid +29 -0
  64. data/performance/tests/ripen/index.liquid +32 -0
  65. data/performance/tests/ripen/page.liquid +4 -0
  66. data/performance/tests/ripen/product.liquid +75 -0
  67. data/performance/tests/ripen/theme.liquid +85 -0
  68. data/performance/tests/tribble/404.liquid +56 -0
  69. data/performance/tests/tribble/article.liquid +98 -0
  70. data/performance/tests/tribble/blog.liquid +41 -0
  71. data/performance/tests/tribble/cart.liquid +134 -0
  72. data/performance/tests/tribble/collection.liquid +70 -0
  73. data/performance/tests/tribble/index.liquid +94 -0
  74. data/performance/tests/tribble/page.liquid +56 -0
  75. data/performance/tests/tribble/product.liquid +116 -0
  76. data/performance/tests/tribble/search.liquid +51 -0
  77. data/performance/tests/tribble/theme.liquid +90 -0
  78. data/performance/tests/vogue/article.liquid +66 -0
  79. data/performance/tests/vogue/blog.liquid +32 -0
  80. data/performance/tests/vogue/cart.liquid +58 -0
  81. data/performance/tests/vogue/collection.liquid +19 -0
  82. data/performance/tests/vogue/index.liquid +22 -0
  83. data/performance/tests/vogue/page.liquid +3 -0
  84. data/performance/tests/vogue/product.liquid +62 -0
  85. data/performance/tests/vogue/theme.liquid +122 -0
  86. data/test/assign_test.rb +11 -0
  87. data/test/block_test.rb +58 -0
  88. data/test/condition_test.rb +109 -0
  89. data/test/context_test.rb +482 -0
  90. data/test/drop_test.rb +162 -0
  91. data/test/error_handling_test.rb +89 -0
  92. data/test/extra/breakpoint.rb +547 -0
  93. data/test/extra/caller.rb +80 -0
  94. data/test/file_system_test.rb +30 -0
  95. data/test/filter_test.rb +95 -0
  96. data/test/helper.rb +20 -0
  97. data/test/html_tag_test.rb +31 -0
  98. data/test/if_else_test.rb +131 -0
  99. data/test/include_tag_test.rb +115 -0
  100. data/test/module_ex_test.rb +89 -0
  101. data/test/output_test.rb +121 -0
  102. data/test/parsing_quirks_test.rb +41 -0
  103. data/test/regexp_test.rb +45 -0
  104. data/test/security_test.rb +41 -0
  105. data/test/standard_filter_test.rb +161 -0
  106. data/test/standard_tag_test.rb +400 -0
  107. data/test/statements_test.rb +137 -0
  108. data/test/strainer_test.rb +21 -0
  109. data/test/template_test.rb +26 -0
  110. data/test/test_helper.rb +20 -0
  111. data/test/unless_else_test.rb +27 -0
  112. data/test/variable_test.rb +172 -0
  113. metadata +187 -0
@@ -0,0 +1,102 @@
1
+ module Liquid
2
+
3
+ class Block < Tag
4
+ IsTag = /^#{TagStart}/
5
+ IsVariable = /^#{VariableStart}/
6
+ FullToken = /^#{TagStart}\s*(\w+)\s*(.*)?#{TagEnd}$/
7
+ ContentOfVariable = /^#{VariableStart}(.*)#{VariableEnd}$/
8
+
9
+ def parse(tokens)
10
+ @nodelist ||= []
11
+ @nodelist.clear
12
+
13
+ while token = tokens.shift
14
+
15
+ case token
16
+ when IsTag
17
+ if token =~ FullToken
18
+
19
+ # if we found the proper block delimitor just end parsing here and let the outer block
20
+ # proceed
21
+ if block_delimiter == $1
22
+ end_tag
23
+ return
24
+ end
25
+
26
+ # fetch the tag from registered blocks
27
+ if tag = Template.tags[$1]
28
+ @nodelist << tag.new($1, $2, tokens)
29
+ else
30
+ # this tag is not registered with the system
31
+ # pass it to the current block for special handling or error reporting
32
+ unknown_tag($1, $2, tokens)
33
+ end
34
+ else
35
+ raise SyntaxError, "Tag '#{token}' was not properly terminated with regexp: #{TagEnd.inspect} "
36
+ end
37
+ when IsVariable
38
+ @nodelist << create_variable(token)
39
+ when ''
40
+ # pass
41
+ else
42
+ @nodelist << token
43
+ end
44
+ end
45
+
46
+ # Make sure that its ok to end parsing in the current block.
47
+ # Effectively this method will throw and exception unless the current block is
48
+ # of type Document
49
+ assert_missing_delimitation!
50
+ end
51
+
52
+ def end_tag
53
+ end
54
+
55
+ def unknown_tag(tag, params, tokens)
56
+ case tag
57
+ when 'else'
58
+ raise SyntaxError, "#{block_name} tag does not expect else tag"
59
+ # when 'end'
60
+ # raise SyntaxError, "'end' is not a valid delimiter for #{block_name} tags. use #{block_delimiter}"
61
+ else
62
+ raise SyntaxError, "Unknown tag '#{tag}'"
63
+ end
64
+ end
65
+
66
+ def block_delimiter
67
+ "end#{block_name}"
68
+ "end"
69
+ end
70
+
71
+ def block_name
72
+ @tag_name
73
+ end
74
+
75
+ def create_variable(token)
76
+ token.scan(ContentOfVariable) do |content|
77
+ return Variable.new(content.first)
78
+ end
79
+ raise SyntaxError.new("Variable '#{token}' was not properly terminated with regexp: #{VariableEnd.inspect} ")
80
+ end
81
+
82
+ def render(context)
83
+ render_all(@nodelist, context)
84
+ end
85
+
86
+ protected
87
+
88
+ def assert_missing_delimitation!
89
+ raise SyntaxError.new("#{block_name} tag was never closed")
90
+ end
91
+
92
+ def render_all(list, context)
93
+ list.collect do |token|
94
+ begin
95
+ token.respond_to?(:render) ? token.render(context) : token
96
+ rescue Exception => e
97
+ context.handle_error(e)
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,120 @@
1
+ module Liquid
2
+ # Container for liquid nodes which conveniently wraps decision making logic
3
+ #
4
+ # Example:
5
+ #
6
+ # c = Condition.new('1', '==', '1')
7
+ # c.evaluate #=> true
8
+ #
9
+ class Condition #:nodoc:
10
+ @@operators = {
11
+ '==' => lambda { |cond, left, right| cond.send(:equal_variables, left, right) },
12
+ '!=' => lambda { |cond, left, right| !cond.send(:equal_variables, left, right) },
13
+ '<>' => lambda { |cond, left, right| !cond.send(:equal_variables, left, right) },
14
+ '<' => :<,
15
+ '>' => :>,
16
+ '>=' => :>=,
17
+ '<=' => :<=,
18
+ 'contains' => lambda { |cond, left, right| left.include?(right) },
19
+ }
20
+
21
+ def self.operators
22
+ @@operators
23
+ end
24
+
25
+ attr_reader :attachment
26
+ attr_accessor :left, :operator, :right
27
+
28
+ def initialize(left = nil, operator = nil, right = nil)
29
+ @left, @operator, @right = left, operator, right
30
+ @child_relation = nil
31
+ @child_condition = nil
32
+ end
33
+
34
+ def evaluate(context = Context.new)
35
+ result = interpret_condition(left, right, operator, context)
36
+
37
+ case @child_relation
38
+ when :or
39
+ result || @child_condition.evaluate(context)
40
+ when :and
41
+ result && @child_condition.evaluate(context)
42
+ else
43
+ result
44
+ end
45
+ end
46
+
47
+ def or(condition)
48
+ @child_relation, @child_condition = :or, condition
49
+ end
50
+
51
+ def and(condition)
52
+ @child_relation, @child_condition = :and, condition
53
+ end
54
+
55
+ def attach(attachment)
56
+ @attachment = attachment
57
+ end
58
+
59
+ def else?
60
+ false
61
+ end
62
+
63
+ def inspect
64
+ "#<Condition #{[@left, @operator, @right].compact.join(' ')}>"
65
+ end
66
+
67
+ private
68
+
69
+ def equal_variables(left, right)
70
+ if left.is_a?(Symbol)
71
+ if right.respond_to?(left)
72
+ return right.send(left.to_s)
73
+ else
74
+ return nil
75
+ end
76
+ end
77
+
78
+ if right.is_a?(Symbol)
79
+ if left.respond_to?(right)
80
+ return left.send(right.to_s)
81
+ else
82
+ return nil
83
+ end
84
+ end
85
+
86
+ left == right
87
+ end
88
+
89
+ def interpret_condition(left, right, op, context)
90
+ # If the operator is empty this means that the decision statement is just
91
+ # a single variable. We can just poll this variable from the context and
92
+ # return this as the result.
93
+ return context[left] if op == nil
94
+
95
+ left, right = context[left], context[right]
96
+
97
+ operation = self.class.operators[op] || raise(ArgumentError.new("Unknown operator #{op}"))
98
+
99
+ if operation.respond_to?(:call)
100
+ operation.call(self, left, right)
101
+ elsif left.respond_to?(operation) and right.respond_to?(operation)
102
+ left.send(operation, right)
103
+ else
104
+ nil
105
+ end
106
+ end
107
+ end
108
+
109
+
110
+ class ElseCondition < Condition
111
+ def else?
112
+ true
113
+ end
114
+
115
+ def evaluate(context)
116
+ true
117
+ end
118
+ end
119
+
120
+ end
@@ -0,0 +1,221 @@
1
+ module Liquid
2
+
3
+ # Context keeps the variable stack and resolves variables, as well as keywords
4
+ #
5
+ # context['variable'] = 'testing'
6
+ # context['variable'] #=> 'testing'
7
+ # context['true'] #=> true
8
+ # context['10.2232'] #=> 10.2232
9
+ #
10
+ # context.stack do
11
+ # context['bob'] = 'bobsen'
12
+ # end
13
+ #
14
+ # context['bob'] #=> nil class Context
15
+ class Context
16
+ attr_reader :scopes
17
+ attr_reader :errors, :registers
18
+
19
+ def initialize(assigns = {}, registers = {}, rethrow_errors = false)
20
+ @scopes = [(assigns || {})]
21
+ @registers = registers
22
+ @errors = []
23
+ @rethrow_errors = rethrow_errors
24
+ end
25
+
26
+ def strainer
27
+ @strainer ||= Strainer.create(self)
28
+ end
29
+
30
+ # adds filters to this context.
31
+ # this does not register the filters with the main Template object. see <tt>Template.register_filter</tt>
32
+ # for that
33
+ def add_filters(filters)
34
+ filters = [filters].flatten.compact
35
+
36
+ filters.each do |f|
37
+ raise ArgumentError, "Expected module but got: #{f.class}" unless f.is_a?(Module)
38
+ strainer.extend(f)
39
+ end
40
+ end
41
+
42
+ def handle_error(e)
43
+ errors.push(e)
44
+ raise if @rethrow_errors
45
+
46
+ case e
47
+ when SyntaxError
48
+ "Liquid syntax error: #{e.message}"
49
+ else
50
+ "Liquid error: #{e.message}"
51
+ end
52
+ end
53
+
54
+
55
+ def invoke(method, *args)
56
+ if strainer.respond_to?(method)
57
+ strainer.__send__(method, *args)
58
+ else
59
+ raise FilterNotFound, "Filter '#{method}' not found"
60
+ end
61
+ end
62
+
63
+ # push new local scope on the stack. use <tt>Context#stack</tt> instead
64
+ def push
65
+ raise StackLevelError, "Nesting too deep" if @scopes.length > 100
66
+ @scopes.unshift({})
67
+ end
68
+
69
+ # merge a hash of variables in the current local scope
70
+ def merge(new_scopes)
71
+ @scopes[0].merge!(new_scopes)
72
+ end
73
+
74
+ # pop from the stack. use <tt>Context#stack</tt> instead
75
+ def pop
76
+ raise ContextError if @scopes.size == 1
77
+ @scopes.shift
78
+ end
79
+
80
+ # pushes a new local scope on the stack, pops it at the end of the block
81
+ #
82
+ # Example:
83
+ #
84
+ # context.stack do
85
+ # context['var'] = 'hi'
86
+ # end
87
+ # context['var] #=> nil
88
+ #
89
+ def stack(&block)
90
+ result = nil
91
+ push
92
+ begin
93
+ result = yield
94
+ ensure
95
+ pop
96
+ end
97
+ result
98
+ end
99
+
100
+ # Only allow String, Numeric, Hash, Array, Proc, Boolean or <tt>Liquid::Drop</tt>
101
+ def []=(key, value)
102
+ @scopes[0][key] = value
103
+ end
104
+
105
+ def [](key)
106
+ resolve(key)
107
+ end
108
+
109
+ def has_key?(key)
110
+ resolve(key) != nil
111
+ end
112
+
113
+ private
114
+
115
+ # Look up variable, either resolve directly after considering the name. We can directly handle
116
+ # Strings, digits, floats and booleans (true,false). If no match is made we lookup the variable in the current scope and
117
+ # later move up to the parent blocks to see if we can resolve the variable somewhere up the tree.
118
+ # Some special keywords return symbols. Those symbols are to be called on the rhs object in expressions
119
+ #
120
+ # Example:
121
+ #
122
+ # products == empty #=> products.empty?
123
+ #
124
+ def resolve(key)
125
+ case key
126
+ when nil, 'nil', 'null', ''
127
+ nil
128
+ when 'true'
129
+ true
130
+ when 'false'
131
+ false
132
+ when 'blank'
133
+ :blank?
134
+ when 'empty'
135
+ :empty?
136
+ # Single quoted strings
137
+ when /^'(.*)'$/
138
+ $1.to_s
139
+ # Double quoted strings
140
+ when /^"(.*)"$/
141
+ $1.to_s
142
+ # Integer and floats
143
+ when /^(\d+)$/
144
+ $1.to_i
145
+ # Ranges
146
+ when /^\((\S+)\.\.(\S+)\)$/
147
+ (resolve($1).to_i..resolve($2).to_i)
148
+ # Floats
149
+ when /^(\d[\d\.]+)$/
150
+ $1.to_f
151
+ else
152
+ variable(key)
153
+ end
154
+ end
155
+
156
+ # fetches an object starting at the local scope and then moving up
157
+ # the hierachy
158
+ def find_variable(key)
159
+ scope = @scopes[0..-2].find { |s| s.has_key?(key) } || @scopes.last
160
+ variable = scope[key]
161
+ variable = scope[key] = variable.call(self) if variable.is_a?(Proc)
162
+ variable = variable.to_liquid
163
+ variable.context = self if variable.respond_to?(:context=)
164
+ return variable
165
+ end
166
+
167
+ # resolves namespaced queries gracefully.
168
+ #
169
+ # Example
170
+ #
171
+ # @context['hash'] = {"name" => 'tobi'}
172
+ # assert_equal 'tobi', @context['hash.name']
173
+ # assert_equal 'tobi', @context['hash["name"]']
174
+ #
175
+ def variable(markup)
176
+ parts = markup.scan(VariableParser)
177
+ square_bracketed = /^\[(.*)\]$/
178
+
179
+ first_part = parts.shift
180
+ if first_part =~ square_bracketed
181
+ first_part = resolve($1)
182
+ end
183
+
184
+ if object = find_variable(first_part)
185
+
186
+ parts.each do |part|
187
+ part = resolve($1) if part_resolved = (part =~ square_bracketed)
188
+
189
+ # If object is a hash- or array-like object we look for the
190
+ # presence of the key and if its available we return it
191
+ if object.respond_to?(:[]) and
192
+ ((object.respond_to?(:has_key?) and object.has_key?(part)) or
193
+ (object.respond_to?(:fetch) and part.is_a?(Integer)))
194
+
195
+ # if its a proc we will replace the entry with the proc
196
+ res = object[part]
197
+ res = object[part] = res.call(self) if res.is_a?(Proc) and object.respond_to?(:[]=)
198
+ object = res.to_liquid
199
+
200
+ # Some special cases. If the part wasn't in square brackets and
201
+ # no key with the same name was found we interpret following calls
202
+ # as commands and call them on the current object
203
+ elsif !part_resolved and object.respond_to?(part) and ['size', 'first', 'last'].include?(part)
204
+
205
+ object = object.send(part.intern).to_liquid
206
+
207
+ # No key was present with the desired value and it wasn't one of the directly supported
208
+ # keywords either. The only thing we got left is to return nil
209
+ else
210
+ return nil
211
+ end
212
+
213
+ # If we are dealing with a drop here we have to
214
+ object.context = self if object.respond_to?(:context=)
215
+ end
216
+ end
217
+
218
+ object
219
+ end
220
+ end
221
+ end
@@ -0,0 +1,17 @@
1
+ module Liquid
2
+ class Document < Block
3
+ # we don't need markup to open this block
4
+ def initialize(tokens)
5
+ parse(tokens)
6
+ end
7
+
8
+ # There isn't a real delimter
9
+ def block_delimiter
10
+ []
11
+ end
12
+
13
+ # Document blocks don't need to be terminated since they are not actually opened
14
+ def assert_missing_delimitation!
15
+ end
16
+ end
17
+ end