locomotivecms-liquid 2.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/History.md +108 -0
- data/MIT-LICENSE +20 -0
- data/README.md +75 -0
- data/lib/extras/liquid_view.rb +51 -0
- data/lib/liquid/block.rb +160 -0
- data/lib/liquid/condition.rb +120 -0
- data/lib/liquid/context.rb +268 -0
- data/lib/liquid/document.rb +18 -0
- data/lib/liquid/drop.rb +74 -0
- data/lib/liquid/errors.rb +21 -0
- data/lib/liquid/extensions.rb +62 -0
- data/lib/liquid/file_system.rb +62 -0
- data/lib/liquid/htmltags.rb +74 -0
- data/lib/liquid/i18n.rb +39 -0
- data/lib/liquid/interrupts.rb +17 -0
- data/lib/liquid/lexer.rb +51 -0
- data/lib/liquid/locales/en.yml +25 -0
- data/lib/liquid/module_ex.rb +62 -0
- data/lib/liquid/parser.rb +89 -0
- data/lib/liquid/standardfilters.rb +285 -0
- data/lib/liquid/strainer.rb +53 -0
- data/lib/liquid/tag.rb +61 -0
- data/lib/liquid/tags/assign.rb +36 -0
- data/lib/liquid/tags/break.rb +21 -0
- data/lib/liquid/tags/capture.rb +40 -0
- data/lib/liquid/tags/case.rb +77 -0
- data/lib/liquid/tags/comment.rb +16 -0
- data/lib/liquid/tags/continue.rb +21 -0
- data/lib/liquid/tags/cycle.rb +61 -0
- data/lib/liquid/tags/decrement.rb +39 -0
- data/lib/liquid/tags/default_content.rb +21 -0
- data/lib/liquid/tags/extends.rb +79 -0
- data/lib/liquid/tags/for.rb +167 -0
- data/lib/liquid/tags/if.rb +100 -0
- data/lib/liquid/tags/ifchanged.rb +20 -0
- data/lib/liquid/tags/include.rb +97 -0
- data/lib/liquid/tags/increment.rb +36 -0
- data/lib/liquid/tags/inherited_block.rb +101 -0
- data/lib/liquid/tags/raw.rb +22 -0
- data/lib/liquid/tags/unless.rb +33 -0
- data/lib/liquid/template.rb +213 -0
- data/lib/liquid/utils.rb +30 -0
- data/lib/liquid/variable.rb +109 -0
- data/lib/liquid/version.rb +4 -0
- data/lib/liquid.rb +72 -0
- data/lib/locomotivecms-liquid.rb +1 -0
- metadata +94 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0dd3865307a040e127b4a088bb3ccda9af424c66
|
4
|
+
data.tar.gz: bf77f86b27a172af5476e7d00b329a647a9bc96a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a6de109bf59a18bd39b7f357636a22aa62be7286a23b87e1aa1bc7e8b5f8981d52ca397656147c16dd62c87bb872c448f2a8bc9c5d5b8158d703e7cb2426628f
|
7
|
+
data.tar.gz: 406c6e6858b0623afe3a5b4eda92fc0b552c4516aa920ec5b16bc3261913223d87a51216fa88cd94c76dfab1573758fe27857ce6ebabbefc6ab5157fbb116c3f
|
data/History.md
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
# Liquid Version History
|
2
|
+
|
3
|
+
IMPORTANT: Liquid 2.6 is going to be the last version of Liquid which maintains explicit Ruby 1.8 compatability.
|
4
|
+
The following releases will only be tested against Ruby 1.9 and Ruby 2.0 and are likely to break on Ruby 1.8.
|
5
|
+
|
6
|
+
## 2.6.0 / Master branch (not yet released)
|
7
|
+
|
8
|
+
* ...
|
9
|
+
* Add optional strict parsing and warn parsing, see #235 [Tristan Hume, trishume]
|
10
|
+
* Add I18n syntax error translation, see #241 [Simon Hørup Eskildsen, Sirupsen]
|
11
|
+
* Make sort filter work on enumerable drops, see #239 [Florian Weingarten, fw42]
|
12
|
+
* Fix clashing method names in enumerable drops, see #238 [Florian Weingarten, fw42]
|
13
|
+
* Make map filter work on enumerable drops, see #233 [Florian Weingarten, fw42]
|
14
|
+
* Fix security issue with map filter, see #230, #232, #234, #237 [Florian Weingarten, fw42]
|
15
|
+
* Improved whitespace stripping for blank blocks, related to #216 [Florian Weingarten, fw42]
|
16
|
+
* Bugfix for #106: fix example servlet [gnowoel]
|
17
|
+
* Bugfix for #97: strip_html filter supports multi-line tags [Jo Liss, joliss]
|
18
|
+
* Bugfix for #114: strip_html filter supports style tags [James Allardice, jamesallardice]
|
19
|
+
* Bugfix for #117: 'now' support for date filter in Ruby 1.9 [Notre Dame Webgroup, ndwebgroup]
|
20
|
+
* Bugfix for #166: truncate filter on UTF-8 strings with Ruby 1.8 [Florian Weingarten, fw42]
|
21
|
+
* Bugfix for #204: 'raw' parsing bug [Florian Weingarten, fw42]
|
22
|
+
* Bugfix for #150: 'for' parsing bug [Peter Schröder, phoet]
|
23
|
+
* Bugfix for #126: Strip CRLF in strip_newline [Peter Schröder, phoet]
|
24
|
+
* Allow a Liquid::Drop to be passed into Template#render [Daniel Huckstep, darkhelmet]
|
25
|
+
* Resource limits [Florian Weingarten, fw42]
|
26
|
+
* Add reverse filter [Jay Strybis, unreal]
|
27
|
+
* Add utf-8 support
|
28
|
+
* Use array instead of Hash to keep the registered filters [Tasos Stathopoulos, astathopoulos]
|
29
|
+
* Cache tokenized partial templates [Tom Burns, boourns]
|
30
|
+
* Avoid warnings in Ruby 1.9.3 [Marcus Stollsteimer, stomar]
|
31
|
+
* Better documentation for 'include' tag (closes #163) [Peter Schröder, phoet]
|
32
|
+
* Use of BigDecimal on filters to have better precision (closes #155) [Arthur Nogueira Neves, arthurnn]
|
33
|
+
|
34
|
+
|
35
|
+
## 2.5.0 / 2013-03-06
|
36
|
+
|
37
|
+
* Prevent Object methods from being called on drops
|
38
|
+
* Avoid symbol injection from liquid
|
39
|
+
* Added break and continue statements
|
40
|
+
* Fix filter parser for args without space separators
|
41
|
+
* Add support for filter keyword arguments
|
42
|
+
|
43
|
+
|
44
|
+
## 2.4.0 / 2012-08-03
|
45
|
+
|
46
|
+
* Performance improvements
|
47
|
+
* Allow filters in `assign`
|
48
|
+
* Add `modulo` filter
|
49
|
+
* Ruby 1.8, 1.9, and Rubinius compatibility fixes
|
50
|
+
* Add support for `quoted['references']` in `tablerow`
|
51
|
+
* Add support for Enumerable to `tablerow`
|
52
|
+
* `strip_html` filter removes html comments
|
53
|
+
|
54
|
+
|
55
|
+
## 2.3.0 / 2011-10-16
|
56
|
+
|
57
|
+
* Several speed/memory improvements
|
58
|
+
* Numerous bug fixes
|
59
|
+
* Added support for MRI 1.9, Rubinius, and JRuby
|
60
|
+
* Added support for integer drop parameters
|
61
|
+
* Added epoch support to `date` filter
|
62
|
+
* New `raw` tag that suppresses parsing
|
63
|
+
* Added `else` option to `for` tag
|
64
|
+
* New `increment` tag
|
65
|
+
* New `split` filter
|
66
|
+
|
67
|
+
|
68
|
+
## 2.2.1 / 2010-08-23
|
69
|
+
|
70
|
+
* Added support for literal tags
|
71
|
+
|
72
|
+
|
73
|
+
## 2.2.0 / 2010-08-22
|
74
|
+
|
75
|
+
* Compatible with Ruby 1.8.7, 1.9.1 and 1.9.2-p0
|
76
|
+
* Merged some changed made by the community
|
77
|
+
|
78
|
+
|
79
|
+
## 1.9.0 / 2008-03-04
|
80
|
+
|
81
|
+
* Fixed gem install rake task
|
82
|
+
* Improve Error encapsulation in liquid by maintaining a own set of exceptions instead of relying on ruby build ins
|
83
|
+
|
84
|
+
|
85
|
+
## Before 1.9.0
|
86
|
+
|
87
|
+
* Added If with or / and expressions
|
88
|
+
* 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.
|
89
|
+
* Added more tags to standard library
|
90
|
+
* Added include tag ( like partials in rails )
|
91
|
+
* [...] Gazillion of detail improvements
|
92
|
+
* Added strainers as filter hosts for better security [Tobias Luetke]
|
93
|
+
* Fixed that rails integration would call filter with the wrong "self" [Michael Geary]
|
94
|
+
* 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]
|
95
|
+
* Removed count helper from standard lib. use size [Tobias Luetke]
|
96
|
+
* Fixed bug with string filter parameters failing to tolerate commas in strings. [Paul Hammond]
|
97
|
+
* 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]
|
98
|
+
{{ 'Typo' | link_to: 'http://typo.leetsoft.com', 'Typo - a modern weblog engine' }}
|
99
|
+
* 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]
|
100
|
+
|
101
|
+
class ProductDrop < Liquid::Drop
|
102
|
+
def top_sales
|
103
|
+
Shop.current.products.find(:all, :order => 'sales', :limit => 10 )
|
104
|
+
end
|
105
|
+
end
|
106
|
+
t = Liquid::Template.parse( ' {% for product in product.top_sales %} {{ product.name }} {% endfor %} ' )
|
107
|
+
t.render('product' => ProductDrop.new )
|
108
|
+
* 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,75 @@
|
|
1
|
+
# Liquid template engine
|
2
|
+
|
3
|
+
* [Contributing guidelines](CONTRIBUTING.md)
|
4
|
+
* [Version history](History.md)
|
5
|
+
* [Liquid documentation from Shopify](http://docs.shopify.com/themes/liquid-basics)
|
6
|
+
* [Liquid Wiki from Shopify](http://wiki.shopify.com/Liquid)
|
7
|
+
* [Liquid Wiki at GitHub](https://github.com/Shopify/liquid/wiki)
|
8
|
+
* [Website](http://liquidmarkup.org/)
|
9
|
+
|
10
|
+
## Introduction
|
11
|
+
|
12
|
+
Liquid is a template engine which was written with very specific requirements:
|
13
|
+
|
14
|
+
* It has to have beautiful and simple markup. Template engines which don't produce good looking markup are no fun to use.
|
15
|
+
* 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.
|
16
|
+
* It has to be stateless. Compile and render steps have to be separate 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.
|
17
|
+
|
18
|
+
## Why you should use Liquid
|
19
|
+
|
20
|
+
* 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**.
|
21
|
+
* You want to render templates directly from the database.
|
22
|
+
* You like smarty (PHP) style template engines.
|
23
|
+
* You need a template engine which does HTML just as well as emails.
|
24
|
+
* You don't like the markup of your current templating engine.
|
25
|
+
|
26
|
+
## What does it look like?
|
27
|
+
|
28
|
+
```html
|
29
|
+
<ul id="products">
|
30
|
+
{% for product in products %}
|
31
|
+
<li>
|
32
|
+
<h2>{{ product.name }}</h2>
|
33
|
+
Only {{ product.price | price }}
|
34
|
+
|
35
|
+
{{ product.description | prettyprint | paragraph }}
|
36
|
+
</li>
|
37
|
+
{% endfor %}
|
38
|
+
</ul>
|
39
|
+
```
|
40
|
+
|
41
|
+
## How to use Liquid
|
42
|
+
|
43
|
+
Liquid supports a very simple API based around the Liquid::Template class.
|
44
|
+
For standard use you can just pass it the content of a file and call render with a parameters hash.
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
@template = Liquid::Template.parse("hi {{name}}") # Parses and compiles the template
|
48
|
+
@template.render('name' => 'tobi') # => "hi tobi"
|
49
|
+
```
|
50
|
+
|
51
|
+
### Error Modes
|
52
|
+
|
53
|
+
Setting the error mode of Liquid lets you specify how strictly you want your templates to be interpreted.
|
54
|
+
Normally the parser is very lax and will accept almost anything without error. Unfortunately this can make
|
55
|
+
it very hard to debug and can lead to unexpected behaviour.
|
56
|
+
|
57
|
+
Liquid also comes with a stricter parser that can be used when editing templates to give better error messages
|
58
|
+
when templates are invalid. You can enable this new parser like this:
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
Liquid::Template.error_mode = :strict # Raises a SyntaxError when invalid syntax is used
|
62
|
+
Liquid::Template.error_mode = :warn # Adds errors to template.errors but continues as normal
|
63
|
+
Liquid::Template.error_mode = :lax # The default mode, accepts almost anything.
|
64
|
+
```
|
65
|
+
|
66
|
+
If you want to set the error mode only on specific templates you can pass `:error_mode` as an option to `parse`:
|
67
|
+
```ruby
|
68
|
+
Liquid::Template.parse(source, :error_mode => :strict)
|
69
|
+
```
|
70
|
+
This is useful for doing things like enabling strict mode only in the theme editor.
|
71
|
+
|
72
|
+
It is recommended that you enable `:strict` or `:warn` mode on new apps to stop invalid templates from being created.
|
73
|
+
It is also recommended that you use it in the template editors of existing apps to give editors better error messages.
|
74
|
+
|
75
|
+
[![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,160 @@
|
|
1
|
+
module Liquid
|
2
|
+
class Block < Tag
|
3
|
+
IsTag = /^#{TagStart}/o
|
4
|
+
IsVariable = /^#{VariableStart}/o
|
5
|
+
FullToken = /^#{TagStart}\s*(\w+)\s*(.*)?#{TagEnd}$/o
|
6
|
+
ContentOfVariable = /^#{VariableStart}(.*)#{VariableEnd}$/o
|
7
|
+
|
8
|
+
def blank?
|
9
|
+
@blank || false
|
10
|
+
end
|
11
|
+
|
12
|
+
def parse(tokens)
|
13
|
+
self.line ||= 1
|
14
|
+
@blank = true
|
15
|
+
@nodelist ||= []
|
16
|
+
@nodelist.clear
|
17
|
+
|
18
|
+
# All child tags of the current block.
|
19
|
+
@children = []
|
20
|
+
|
21
|
+
while token = tokens.shift
|
22
|
+
case token
|
23
|
+
when IsTag
|
24
|
+
if token =~ FullToken
|
25
|
+
|
26
|
+
# if we found the proper block delimiter just end parsing here and let the outer block
|
27
|
+
# proceed
|
28
|
+
if block_delimiter == $1
|
29
|
+
end_tag
|
30
|
+
return
|
31
|
+
end
|
32
|
+
|
33
|
+
# fetch the tag from registered blocks
|
34
|
+
if tag = Template.tags[$1]
|
35
|
+
new_tag = tag.new_with_options($1, $2, tokens, @options.merge(line: line))
|
36
|
+
@blank &&= new_tag.blank?
|
37
|
+
@nodelist << new_tag
|
38
|
+
@children << new_tag
|
39
|
+
else
|
40
|
+
# this tag is not registered with the system
|
41
|
+
# pass it to the current block for special handling or error reporting
|
42
|
+
unknown_tag($1, $2, tokens)
|
43
|
+
end
|
44
|
+
else
|
45
|
+
raise SyntaxError.new(options[:locale].t("errors.syntax.tag_termination", :token => token, :tag_end => TagEnd.inspect), line)
|
46
|
+
end
|
47
|
+
when IsVariable
|
48
|
+
new_var = create_variable(token)
|
49
|
+
@nodelist << new_var
|
50
|
+
@children << new_var
|
51
|
+
@blank = false
|
52
|
+
when ''
|
53
|
+
# pass
|
54
|
+
else
|
55
|
+
self.line += token.count("\n") if @options[:count_lines] || Template.count_lines
|
56
|
+
@nodelist << token
|
57
|
+
@blank &&= (token =~ /\A\s*\z/)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
# restore the line
|
62
|
+
|
63
|
+
# Make sure that it's ok to end parsing in the current block.
|
64
|
+
# Effectively this method will throw an exception unless the current block is
|
65
|
+
# of type Document
|
66
|
+
assert_missing_delimitation!
|
67
|
+
end
|
68
|
+
|
69
|
+
# warnings of this block and all sub-tags
|
70
|
+
def warnings
|
71
|
+
all_warnings = []
|
72
|
+
all_warnings.concat(@warnings) if @warnings
|
73
|
+
|
74
|
+
(@children || []).each do |node|
|
75
|
+
all_warnings.concat(node.warnings || [])
|
76
|
+
end
|
77
|
+
|
78
|
+
all_warnings
|
79
|
+
end
|
80
|
+
|
81
|
+
def end_tag
|
82
|
+
end
|
83
|
+
|
84
|
+
def unknown_tag(tag, params, tokens)
|
85
|
+
case tag
|
86
|
+
when 'else'
|
87
|
+
raise SyntaxError.new(options[:locale].t("errors.syntax.unexpected_else",
|
88
|
+
:block_name => block_name), line)
|
89
|
+
when 'end'
|
90
|
+
raise SyntaxError.new(options[:locale].t("errors.syntax.invalid_delimiter",
|
91
|
+
:block_name => block_name,
|
92
|
+
:block_delimiter => block_delimiter), line)
|
93
|
+
else
|
94
|
+
raise SyntaxError.new(options[:locale].t("errors.syntax.unknown_tag", :tag => tag), line)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def block_delimiter
|
99
|
+
"end#{block_name}"
|
100
|
+
end
|
101
|
+
|
102
|
+
def block_name
|
103
|
+
@tag_name
|
104
|
+
end
|
105
|
+
|
106
|
+
def create_variable(token)
|
107
|
+
token.scan(ContentOfVariable) do |content|
|
108
|
+
return Variable.new(content.first, @options)
|
109
|
+
end
|
110
|
+
raise SyntaxError.new(options[:locale].t("errors.syntax.variable_termination", :token => token, :tag_end => VariableEnd.inspect), line)
|
111
|
+
end
|
112
|
+
|
113
|
+
def render(context)
|
114
|
+
render_all(@nodelist, context)
|
115
|
+
end
|
116
|
+
|
117
|
+
protected
|
118
|
+
|
119
|
+
def assert_missing_delimitation!
|
120
|
+
raise SyntaxError.new(options[:locale].t("errors.syntax.tag_never_closed", :block_name => block_name), line)
|
121
|
+
end
|
122
|
+
|
123
|
+
def render_all(list, context)
|
124
|
+
output = []
|
125
|
+
context.resource_limits[:render_length_current] = 0
|
126
|
+
context.resource_limits[:render_score_current] += list.length
|
127
|
+
|
128
|
+
list.each do |token|
|
129
|
+
# Break out if we have any unhanded interrupts.
|
130
|
+
break if context.has_interrupt?
|
131
|
+
|
132
|
+
begin
|
133
|
+
# If we get an Interrupt that means the block must stop processing. An
|
134
|
+
# Interrupt is any command that stops block execution such as {% break %}
|
135
|
+
# or {% continue %}
|
136
|
+
if token.is_a? Continue or token.is_a? Break
|
137
|
+
context.push_interrupt(token.interrupt)
|
138
|
+
break
|
139
|
+
end
|
140
|
+
|
141
|
+
token_output = (token.respond_to?(:render) ? token.render(context) : token)
|
142
|
+
context.resource_limits[:render_length_current] += (token_output.respond_to?(:length) ? token_output.length : 1)
|
143
|
+
if context.resource_limits_reached?
|
144
|
+
context.resource_limits[:reached] = true
|
145
|
+
raise MemoryError.new("Memory limits exceeded", token.respond_to?(:line) ? token.line : nil)
|
146
|
+
end
|
147
|
+
unless token.is_a?(Block) && token.blank?
|
148
|
+
output << token_output
|
149
|
+
end
|
150
|
+
rescue MemoryError => e
|
151
|
+
raise e
|
152
|
+
rescue ::StandardError => e
|
153
|
+
output << (context.handle_error(e))
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
output.join
|
158
|
+
end
|
159
|
+
end
|
160
|
+
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
|