spinto-liquid 2.3.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.md +56 -0
- data/MIT-LICENSE +20 -0
- data/README.md +44 -0
- data/lib/extras/liquid_view.rb +51 -0
- data/lib/liquid/block.rb +101 -0
- data/lib/liquid/condition.rb +120 -0
- data/lib/liquid/context.rb +245 -0
- data/lib/liquid/document.rb +17 -0
- data/lib/liquid/drop.rb +49 -0
- data/lib/liquid/errors.rb +11 -0
- data/lib/liquid/extensions.rb +62 -0
- data/lib/liquid/file_system.rb +62 -0
- data/lib/liquid/htmltags.rb +75 -0
- data/lib/liquid/module_ex.rb +62 -0
- data/lib/liquid/standardfilters.rb +241 -0
- data/lib/liquid/strainer.rb +54 -0
- data/lib/liquid/tag.rb +26 -0
- data/lib/liquid/tags/assign.rb +33 -0
- data/lib/liquid/tags/capture.rb +35 -0
- data/lib/liquid/tags/case.rb +79 -0
- data/lib/liquid/tags/comment.rb +9 -0
- data/lib/liquid/tags/cycle.rb +59 -0
- data/lib/liquid/tags/decrement.rb +39 -0
- data/lib/liquid/tags/for.rb +190 -0
- data/lib/liquid/tags/if.rb +79 -0
- data/lib/liquid/tags/ifchanged.rb +20 -0
- data/lib/liquid/tags/include.rb +65 -0
- data/lib/liquid/tags/increment.rb +35 -0
- data/lib/liquid/tags/raw.rb +21 -0
- data/lib/liquid/tags/unless.rb +33 -0
- data/lib/liquid/template.rb +150 -0
- data/lib/liquid/variable.rb +50 -0
- data/lib/liquid.rb +66 -0
- data/test/liquid/assign_test.rb +21 -0
- data/test/liquid/block_test.rb +58 -0
- data/test/liquid/capture_test.rb +40 -0
- data/test/liquid/condition_test.rb +127 -0
- data/test/liquid/context_test.rb +478 -0
- data/test/liquid/drop_test.rb +162 -0
- data/test/liquid/error_handling_test.rb +81 -0
- data/test/liquid/file_system_test.rb +29 -0
- data/test/liquid/filter_test.rb +106 -0
- data/test/liquid/module_ex_test.rb +87 -0
- data/test/liquid/output_test.rb +116 -0
- data/test/liquid/parsing_quirks_test.rb +52 -0
- data/test/liquid/regexp_test.rb +44 -0
- data/test/liquid/security_test.rb +41 -0
- data/test/liquid/standard_filter_test.rb +195 -0
- data/test/liquid/strainer_test.rb +25 -0
- data/test/liquid/tags/for_tag_test.rb +215 -0
- data/test/liquid/tags/html_tag_test.rb +39 -0
- data/test/liquid/tags/if_else_tag_test.rb +160 -0
- data/test/liquid/tags/include_tag_test.rb +139 -0
- data/test/liquid/tags/increment_tag_test.rb +24 -0
- data/test/liquid/tags/raw_tag_test.rb +15 -0
- data/test/liquid/tags/standard_tag_test.rb +295 -0
- data/test/liquid/tags/statements_test.rb +134 -0
- data/test/liquid/tags/unless_else_tag_test.rb +26 -0
- data/test/liquid/template_test.rb +74 -0
- data/test/liquid/variable_test.rb +170 -0
- data/test/test_helper.rb +29 -0
- metadata +136 -0
data/History.md
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# Liquid Version History
|
2
|
+
|
3
|
+
## 2.3.0 / 2011-10-16
|
4
|
+
|
5
|
+
* Several speed/memory improvements
|
6
|
+
* Numerous bug fixes
|
7
|
+
* Added support for MRI 1.9, Rubinius, and JRuby
|
8
|
+
* Added support for integer drop parameters
|
9
|
+
* Added epoch support to `date` filter
|
10
|
+
* New `raw` tag that suppresses parsing
|
11
|
+
* Added `else` option to `for` tag
|
12
|
+
* New `increment` tag
|
13
|
+
* New `split` filter
|
14
|
+
|
15
|
+
|
16
|
+
## 2.2.1 / 2010-08-23
|
17
|
+
|
18
|
+
* Added support for literal tags
|
19
|
+
|
20
|
+
|
21
|
+
## 2.2.0 / 2010-08-22
|
22
|
+
|
23
|
+
* Compatible with Ruby 1.8.7, 1.9.1 and 1.9.2-p0
|
24
|
+
* Merged some changed made by the community
|
25
|
+
|
26
|
+
|
27
|
+
## 1.9.0 / 2008-03-04
|
28
|
+
|
29
|
+
* Fixed gem install rake task
|
30
|
+
* Improve Error encapsulation in liquid by maintaining a own set of exceptions instead of relying on ruby build ins
|
31
|
+
|
32
|
+
|
33
|
+
## Before 1.9.0
|
34
|
+
|
35
|
+
* Added If with or / and expressions
|
36
|
+
* Implemented .to_liquid for all objects which can be passed to liquid like Strings Arrays Hashes Numerics and Booleans. To export new objects to liquid just implement .to_liquid on them and return objects which themselves have .to_liquid methods.
|
37
|
+
* Added more tags to standard library
|
38
|
+
* Added include tag ( like partials in rails )
|
39
|
+
* [...] Gazillion of detail improvements
|
40
|
+
* Added strainers as filter hosts for better security [Tobias Luetke]
|
41
|
+
* Fixed that rails integration would call filter with the wrong "self" [Michael Geary]
|
42
|
+
* Fixed bad error reporting when a filter called a method which doesn't exist. Liquid told you that it couldn't find the filter which was obviously misleading [Tobias Luetke]
|
43
|
+
* Removed count helper from standard lib. use size [Tobias Luetke]
|
44
|
+
* Fixed bug with string filter parameters failing to tolerate commas in strings. [Paul Hammond]
|
45
|
+
* Improved filter parameters. Filter parameters are now context sensitive; Types are resolved according to the rules of the context. Multiple parameters are now separated by the Liquid::ArgumentSeparator: , by default [Paul Hammond]
|
46
|
+
{{ 'Typo' | link_to: 'http://typo.leetsoft.com', 'Typo - a modern weblog engine' }}
|
47
|
+
* Added Liquid::Drop. A base class which you can use for exporting proxy objects to liquid which can acquire more data when used in liquid. [Tobias Luetke]
|
48
|
+
|
49
|
+
class ProductDrop < Liquid::Drop
|
50
|
+
def top_sales
|
51
|
+
Shop.current.products.find(:all, :order => 'sales', :limit => 10 )
|
52
|
+
end
|
53
|
+
end
|
54
|
+
t = Liquid::Template.parse( ' {% for product in product.top_sales %} {{ product.name }} {% endfor %} ' )
|
55
|
+
t.render('product' => ProductDrop.new )
|
56
|
+
* Added filter parameters support. Example: {{ date | format_date: "%Y" }} [Paul Hammond]
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2005, 2006 Tobias Luetke
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# Liquid template engine
|
2
|
+
|
3
|
+
## Introduction
|
4
|
+
|
5
|
+
Liquid is a template engine which was written with very specific requirements:
|
6
|
+
|
7
|
+
* It has to have beautiful and simple markup. Template engines which don't produce good looking markup are no fun to use.
|
8
|
+
* It needs to be non evaling and secure. Liquid templates are made so that users can edit them. You don't want to run code on your server which your users wrote.
|
9
|
+
* It has to be stateless. Compile and render steps have to be seperate so that the expensive parsing and compiling can be done once and later on you can just render it passing in a hash with local variables and objects.
|
10
|
+
|
11
|
+
## Why you should use Liquid
|
12
|
+
|
13
|
+
* You want to allow your users to edit the appearance of your application but don't want them to run **insecure code on your server**.
|
14
|
+
* You want to render templates directly from the database.
|
15
|
+
* You like smarty (PHP) style template engines.
|
16
|
+
* You need a template engine which does HTML just as well as emails.
|
17
|
+
* You don't like the markup of your current templating engine.
|
18
|
+
|
19
|
+
## What does it look like?
|
20
|
+
|
21
|
+
```html
|
22
|
+
<ul id="products">
|
23
|
+
{% for product in products %}
|
24
|
+
<li>
|
25
|
+
<h2>{{ product.name }}</h2>
|
26
|
+
Only {{ product.price | price }}
|
27
|
+
|
28
|
+
{{ product.description | prettyprint | paragraph }}
|
29
|
+
</li>
|
30
|
+
{% endfor %}
|
31
|
+
</ul>
|
32
|
+
```
|
33
|
+
|
34
|
+
## How to use Liquid
|
35
|
+
|
36
|
+
Liquid supports a very simple API based around the Liquid::Template class.
|
37
|
+
For standard use you can just pass it the content of a file and call render with a parameters hash.
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
@template = Liquid::Template.parse("hi {{name}}") # Parses and compiles the template
|
41
|
+
@template.render('name' => 'tobi') # => "hi tobi"
|
42
|
+
```
|
43
|
+
|
44
|
+
[![Build Status](https://secure.travis-ci.org/Shopify/liquid.png)](http://travis-ci.org/Shopify/liquid)
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# LiquidView is a action view extension class. You can register it with rails
|
2
|
+
# and use liquid as an template system for .liquid files
|
3
|
+
#
|
4
|
+
# Example
|
5
|
+
#
|
6
|
+
# ActionView::Base::register_template_handler :liquid, LiquidView
|
7
|
+
class LiquidView
|
8
|
+
PROTECTED_ASSIGNS = %w( template_root response _session template_class action_name request_origin session template
|
9
|
+
_response url _request _cookies variables_added _flash params _headers request cookies
|
10
|
+
ignore_missing_templates flash _params logger before_filter_chain_aborted headers )
|
11
|
+
PROTECTED_INSTANCE_VARIABLES = %w( @_request @controller @_first_render @_memoized__pick_template @view_paths
|
12
|
+
@helpers @assigns_added @template @_render_stack @template_format @assigns )
|
13
|
+
|
14
|
+
def self.call(template)
|
15
|
+
"LiquidView.new(self).render(template, local_assigns)"
|
16
|
+
end
|
17
|
+
|
18
|
+
def initialize(view)
|
19
|
+
@view = view
|
20
|
+
end
|
21
|
+
|
22
|
+
def render(template, local_assigns = nil)
|
23
|
+
@view.controller.headers["Content-Type"] ||= 'text/html; charset=utf-8'
|
24
|
+
|
25
|
+
# Rails 2.2 Template has source, but not locals
|
26
|
+
if template.respond_to?(:source) && !template.respond_to?(:locals)
|
27
|
+
assigns = (@view.instance_variables - PROTECTED_INSTANCE_VARIABLES).inject({}) do |hash, ivar|
|
28
|
+
hash[ivar[1..-1]] = @view.instance_variable_get(ivar)
|
29
|
+
hash
|
30
|
+
end
|
31
|
+
else
|
32
|
+
assigns = @view.assigns.reject{ |k,v| PROTECTED_ASSIGNS.include?(k) }
|
33
|
+
end
|
34
|
+
|
35
|
+
source = template.respond_to?(:source) ? template.source : template
|
36
|
+
local_assigns = (template.respond_to?(:locals) ? template.locals : local_assigns) || {}
|
37
|
+
|
38
|
+
if content_for_layout = @view.instance_variable_get("@content_for_layout")
|
39
|
+
assigns['content_for_layout'] = content_for_layout
|
40
|
+
end
|
41
|
+
assigns.merge!(local_assigns.stringify_keys)
|
42
|
+
|
43
|
+
liquid = Liquid::Template.parse(source)
|
44
|
+
liquid.render(assigns, :filters => [@view.controller.master_helper_module], :registers => {:action_view => @view, :controller => @view.controller})
|
45
|
+
end
|
46
|
+
|
47
|
+
def compilable?
|
48
|
+
false
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
data/lib/liquid/block.rb
ADDED
@@ -0,0 +1,101 @@
|
|
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
|
+
|
70
|
+
def block_name
|
71
|
+
@tag_name
|
72
|
+
end
|
73
|
+
|
74
|
+
def create_variable(token)
|
75
|
+
token.scan(ContentOfVariable) do |content|
|
76
|
+
return Variable.new(content.first)
|
77
|
+
end
|
78
|
+
raise SyntaxError.new("Variable '#{token}' was not properly terminated with regexp: #{VariableEnd.inspect} ")
|
79
|
+
end
|
80
|
+
|
81
|
+
def render(context)
|
82
|
+
render_all(@nodelist, context)
|
83
|
+
end
|
84
|
+
|
85
|
+
protected
|
86
|
+
|
87
|
+
def assert_missing_delimitation!
|
88
|
+
raise SyntaxError.new("#{block_name} tag was never closed")
|
89
|
+
end
|
90
|
+
|
91
|
+
def render_all(list, context)
|
92
|
+
list.collect do |token|
|
93
|
+
begin
|
94
|
+
token.respond_to?(:render) ? token.render(context) : token
|
95
|
+
rescue ::StandardError => e
|
96
|
+
context.handle_error(e)
|
97
|
+
end
|
98
|
+
end.join
|
99
|
+
end
|
100
|
+
end
|
101
|
+
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 && right ? left.include?(right) : false }
|
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,245 @@
|
|
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, :errors, :registers, :environments
|
17
|
+
|
18
|
+
def initialize(environments = {}, outer_scope = {}, registers = {}, rethrow_errors = false)
|
19
|
+
@environments = [environments].flatten
|
20
|
+
@scopes = [(outer_scope || {})]
|
21
|
+
@registers = registers
|
22
|
+
@errors = []
|
23
|
+
@rethrow_errors = rethrow_errors
|
24
|
+
squash_instance_assigns_with_environments
|
25
|
+
end
|
26
|
+
|
27
|
+
def strainer
|
28
|
+
@strainer ||= Strainer.create(self)
|
29
|
+
end
|
30
|
+
|
31
|
+
# Adds filters to this context.
|
32
|
+
#
|
33
|
+
# Note that this does not register the filters with the main Template object. see <tt>Template.register_filter</tt>
|
34
|
+
# for that
|
35
|
+
def add_filters(filters)
|
36
|
+
filters = [filters].flatten.compact
|
37
|
+
|
38
|
+
filters.each do |f|
|
39
|
+
raise ArgumentError, "Expected module but got: #{f.class}" unless f.is_a?(Module)
|
40
|
+
strainer.extend(f)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def handle_error(e)
|
45
|
+
errors.push(e)
|
46
|
+
raise if @rethrow_errors
|
47
|
+
|
48
|
+
case e
|
49
|
+
when SyntaxError
|
50
|
+
"Liquid syntax error: #{e.message}"
|
51
|
+
else
|
52
|
+
"Liquid error: #{e.message}"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def invoke(method, *args)
|
57
|
+
if strainer.respond_to?(method)
|
58
|
+
strainer.__send__(method, *args)
|
59
|
+
else
|
60
|
+
args.first
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# Push new local scope on the stack. use <tt>Context#stack</tt> instead
|
65
|
+
def push(new_scope={})
|
66
|
+
@scopes.unshift(new_scope)
|
67
|
+
raise StackLevelError, "Nesting too deep" if @scopes.length > 100
|
68
|
+
end
|
69
|
+
|
70
|
+
# Merge a hash of variables in the current local scope
|
71
|
+
def merge(new_scopes)
|
72
|
+
@scopes[0].merge!(new_scopes)
|
73
|
+
end
|
74
|
+
|
75
|
+
# Pop from the stack. use <tt>Context#stack</tt> instead
|
76
|
+
def pop
|
77
|
+
raise ContextError if @scopes.size == 1
|
78
|
+
@scopes.shift
|
79
|
+
end
|
80
|
+
|
81
|
+
# Pushes a new local scope on the stack, pops it at the end of the block
|
82
|
+
#
|
83
|
+
# Example:
|
84
|
+
# context.stack do
|
85
|
+
# context['var'] = 'hi'
|
86
|
+
# end
|
87
|
+
#
|
88
|
+
# context['var] #=> nil
|
89
|
+
def stack(new_scope={})
|
90
|
+
push(new_scope)
|
91
|
+
yield
|
92
|
+
ensure
|
93
|
+
pop
|
94
|
+
end
|
95
|
+
|
96
|
+
def clear_instance_assigns
|
97
|
+
@scopes[0] = {}
|
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
|
+
LITERALS = {
|
115
|
+
nil => nil, 'nil' => nil, 'null' => nil, '' => nil,
|
116
|
+
'true' => true,
|
117
|
+
'false' => false,
|
118
|
+
'blank' => :blank?,
|
119
|
+
'empty' => :empty?
|
120
|
+
}
|
121
|
+
|
122
|
+
# Look up variable, either resolve directly after considering the name. We can directly handle
|
123
|
+
# Strings, digits, floats and booleans (true,false).
|
124
|
+
# If no match is made we lookup the variable in the current scope and
|
125
|
+
# later move up to the parent blocks to see if we can resolve the variable somewhere up the tree.
|
126
|
+
# Some special keywords return symbols. Those symbols are to be called on the rhs object in expressions
|
127
|
+
#
|
128
|
+
# Example:
|
129
|
+
# products == empty #=> products.empty?
|
130
|
+
def resolve(key)
|
131
|
+
if LITERALS.key?(key)
|
132
|
+
LITERALS[key]
|
133
|
+
else
|
134
|
+
case key
|
135
|
+
when /^'(.*)'$/ # Single quoted strings
|
136
|
+
$1
|
137
|
+
when /^"(.*)"$/ # Double quoted strings
|
138
|
+
$1
|
139
|
+
when /^(-?\d+)$/ # Integer and floats
|
140
|
+
$1.to_i
|
141
|
+
when /^\((\S+)\.\.(\S+)\)$/ # Ranges
|
142
|
+
(resolve($1).to_i..resolve($2).to_i)
|
143
|
+
when /^(-?\d[\d\.]+)$/ # Floats
|
144
|
+
$1.to_f
|
145
|
+
else
|
146
|
+
variable(key)
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
# Fetches an object starting at the local scope and then moving up the hierachy
|
152
|
+
def find_variable(key)
|
153
|
+
scope = @scopes.find { |s| s.has_key?(key) }
|
154
|
+
|
155
|
+
if scope.nil?
|
156
|
+
@environments.each do |e|
|
157
|
+
if variable = lookup_and_evaluate(e, key)
|
158
|
+
scope = e
|
159
|
+
break
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
scope ||= @environments.last || @scopes.last
|
165
|
+
variable ||= lookup_and_evaluate(scope, key)
|
166
|
+
|
167
|
+
variable = variable.to_liquid
|
168
|
+
variable.context = self if variable.respond_to?(:context=)
|
169
|
+
|
170
|
+
return variable
|
171
|
+
end
|
172
|
+
|
173
|
+
# Resolves namespaced queries gracefully.
|
174
|
+
#
|
175
|
+
# Example
|
176
|
+
# @context['hash'] = {"name" => 'tobi'}
|
177
|
+
# assert_equal 'tobi', @context['hash.name']
|
178
|
+
# assert_equal 'tobi', @context['hash["name"]']
|
179
|
+
def variable(markup)
|
180
|
+
parts = markup.scan(VariableParser)
|
181
|
+
square_bracketed = /^\[(.*)\]$/
|
182
|
+
|
183
|
+
first_part = parts.shift
|
184
|
+
|
185
|
+
if first_part =~ square_bracketed
|
186
|
+
first_part = resolve($1)
|
187
|
+
end
|
188
|
+
|
189
|
+
if object = find_variable(first_part)
|
190
|
+
|
191
|
+
parts.each do |part|
|
192
|
+
part = resolve($1) if part_resolved = (part =~ square_bracketed)
|
193
|
+
|
194
|
+
# If object is a hash- or array-like object we look for the
|
195
|
+
# presence of the key and if its available we return it
|
196
|
+
if object.respond_to?(:[]) and
|
197
|
+
((object.respond_to?(:has_key?) and object.has_key?(part)) or
|
198
|
+
(object.respond_to?(:fetch) and part.is_a?(Integer)))
|
199
|
+
|
200
|
+
# if its a proc we will replace the entry with the proc
|
201
|
+
res = lookup_and_evaluate(object, part)
|
202
|
+
object = res.to_liquid
|
203
|
+
|
204
|
+
# Some special cases. If the part wasn't in square brackets and
|
205
|
+
# no key with the same name was found we interpret following calls
|
206
|
+
# as commands and call them on the current object
|
207
|
+
elsif !part_resolved and object.respond_to?(part) and ['size', 'first', 'last'].include?(part)
|
208
|
+
|
209
|
+
object = object.send(part.intern).to_liquid
|
210
|
+
|
211
|
+
# No key was present with the desired value and it wasn't one of the directly supported
|
212
|
+
# keywords either. The only thing we got left is to return nil
|
213
|
+
else
|
214
|
+
return nil
|
215
|
+
end
|
216
|
+
|
217
|
+
# If we are dealing with a drop here we have to
|
218
|
+
object.context = self if object.respond_to?(:context=)
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
object
|
223
|
+
end # variable
|
224
|
+
|
225
|
+
def lookup_and_evaluate(obj, key)
|
226
|
+
if (value = obj[key]).is_a?(Proc) && obj.respond_to?(:[]=)
|
227
|
+
obj[key] = (value.arity == 0) ? value.call : value.call(self)
|
228
|
+
else
|
229
|
+
value
|
230
|
+
end
|
231
|
+
end # lookup_and_evaluate
|
232
|
+
|
233
|
+
def squash_instance_assigns_with_environments
|
234
|
+
@scopes.last.each_key do |k|
|
235
|
+
@environments.each do |env|
|
236
|
+
if env.has_key?(k)
|
237
|
+
scopes.last[k] = lookup_and_evaluate(env, k)
|
238
|
+
break
|
239
|
+
end
|
240
|
+
end
|
241
|
+
end
|
242
|
+
end # squash_instance_assigns_with_environments
|
243
|
+
end # Context
|
244
|
+
|
245
|
+
end # Liquid
|