liquor 0.1.1 → 0.9.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -9
- data/Gemfile +7 -0
- data/Guardfile +11 -0
- data/MIT-LICENSE +6 -2
- data/README.md +4 -122
- data/Rakefile +20 -23
- data/doc/language-spec.html +768 -0
- data/doc/language-spec.md +698 -0
- data/lib/liquor.rb +39 -68
- data/lib/liquor/ast_tools.rb +28 -0
- data/lib/liquor/compiler.rb +110 -0
- data/lib/liquor/context.rb +76 -254
- data/lib/liquor/diagnostics.rb +151 -0
- data/lib/liquor/drop/drop.rb +168 -0
- data/lib/liquor/drop/drop_delegation.rb +24 -0
- data/lib/liquor/drop/drop_scope.rb +118 -0
- data/lib/liquor/drop/dropable.rb +17 -0
- data/lib/liquor/emitter.rb +313 -0
- data/lib/liquor/extensions/kaminari.rb +14 -0
- data/lib/liquor/extensions/pagination.rb +235 -0
- data/lib/liquor/extensions/rails.rb +97 -0
- data/lib/liquor/extensions/thinking_sphinx.rb +14 -0
- data/lib/liquor/extensions/tire.rb +30 -0
- data/lib/liquor/external.rb +79 -0
- data/lib/liquor/function.rb +94 -0
- data/lib/liquor/grammar/lexer.rb +1223 -0
- data/lib/liquor/grammar/lexer.rl +297 -0
- data/lib/liquor/grammar/parser.racc +288 -0
- data/lib/liquor/grammar/parser.rb +885 -0
- data/lib/liquor/library.rb +41 -0
- data/lib/liquor/manager.rb +146 -0
- data/lib/liquor/runtime.rb +167 -0
- data/lib/liquor/stdlib/builtin_functions.rb +315 -0
- data/lib/liquor/stdlib/builtin_tags.rb +228 -0
- data/lib/liquor/stdlib/html_truncater.rb +162 -0
- data/lib/liquor/stdlib/partial_tags.rb +76 -0
- data/lib/liquor/tag.rb +83 -14
- data/lib/liquor/version.rb +1 -1
- data/liquor.gemspec +29 -6
- data/spec/builtins_spec.rb +264 -0
- data/spec/compiler_spec.rb +136 -0
- data/spec/context_spec.rb +49 -0
- data/spec/drop_delegation_spec.rb +21 -0
- data/spec/drop_spec.rb +222 -0
- data/spec/errors_spec.rb +40 -0
- data/spec/external_spec.rb +207 -0
- data/spec/function_spec.rb +80 -0
- data/spec/lexer_spec.rb +173 -0
- data/spec/library_spec.rb +18 -0
- data/spec/manager_spec.rb +84 -0
- data/spec/parser_spec.rb +381 -0
- data/spec/partials_spec.rb +74 -0
- data/spec/runtime_spec.rb +97 -0
- data/spec/spec_helper.rb +94 -0
- data/spec/tag_spec.rb +7 -0
- metadata +216 -173
- data/AUTHORS +0 -2
- data/CHANGELOG +0 -48
- data/Gemfile.lock +0 -91
- data/History.txt +0 -44
- data/LICENSE +0 -23
- data/example/server/example_servlet.rb +0 -37
- data/example/server/liquid_servlet.rb +0 -28
- data/example/server/liquor_servlet.rb +0 -28
- data/example/server/server.rb +0 -12
- data/example/server/templates/index.liquid +0 -6
- data/example/server/templates/index.liquor +0 -6
- data/example/server/templates/products.liquid +0 -45
- data/example/server/templates/products.liquor +0 -45
- data/init.rb +0 -8
- data/lib/extras/liquid_view.rb +0 -51
- data/lib/extras/liquor_view.rb +0 -51
- data/lib/liquor/block.rb +0 -101
- data/lib/liquor/condition.rb +0 -120
- data/lib/liquor/document.rb +0 -17
- data/lib/liquor/drop.rb +0 -256
- data/lib/liquor/errors.rb +0 -11
- data/lib/liquor/extensions.rb +0 -72
- data/lib/liquor/file_system.rb +0 -62
- data/lib/liquor/htmltags.rb +0 -74
- data/lib/liquor/module_ex.rb +0 -60
- data/lib/liquor/standardfilters.rb +0 -315
- data/lib/liquor/strainer.rb +0 -58
- data/lib/liquor/tags/assign.rb +0 -33
- data/lib/liquor/tags/capture.rb +0 -35
- data/lib/liquor/tags/case.rb +0 -83
- data/lib/liquor/tags/comment.rb +0 -9
- data/lib/liquor/tags/content_for.rb +0 -54
- data/lib/liquor/tags/cycle.rb +0 -59
- data/lib/liquor/tags/for.rb +0 -136
- data/lib/liquor/tags/if.rb +0 -80
- data/lib/liquor/tags/ifchanged.rb +0 -20
- data/lib/liquor/tags/include.rb +0 -56
- data/lib/liquor/tags/unless.rb +0 -33
- data/lib/liquor/tags/yield.rb +0 -49
- data/lib/liquor/template.rb +0 -181
- data/lib/liquor/variable.rb +0 -52
- data/performance/shopify.rb +0 -92
- data/performance/shopify/comment_form.rb +0 -33
- data/performance/shopify/database.rb +0 -45
- data/performance/shopify/json_filter.rb +0 -7
- data/performance/shopify/liquid.rb +0 -18
- data/performance/shopify/liquor.rb +0 -18
- data/performance/shopify/money_filter.rb +0 -18
- data/performance/shopify/paginate.rb +0 -93
- data/performance/shopify/shop_filter.rb +0 -98
- data/performance/shopify/tag_filter.rb +0 -25
- data/performance/shopify/vision.database.yml +0 -945
- data/performance/shopify/weight_filter.rb +0 -11
- data/performance/tests/dropify/article.liquid +0 -74
- data/performance/tests/dropify/blog.liquid +0 -33
- data/performance/tests/dropify/cart.liquid +0 -66
- data/performance/tests/dropify/collection.liquid +0 -22
- data/performance/tests/dropify/index.liquid +0 -47
- data/performance/tests/dropify/page.liquid +0 -8
- data/performance/tests/dropify/product.liquid +0 -68
- data/performance/tests/dropify/theme.liquid +0 -105
- data/performance/tests/ripen/article.liquid +0 -74
- data/performance/tests/ripen/blog.liquid +0 -13
- data/performance/tests/ripen/cart.liquid +0 -54
- data/performance/tests/ripen/collection.liquid +0 -29
- data/performance/tests/ripen/index.liquid +0 -32
- data/performance/tests/ripen/page.liquid +0 -4
- data/performance/tests/ripen/product.liquid +0 -75
- data/performance/tests/ripen/theme.liquid +0 -85
- data/performance/tests/tribble/404.liquid +0 -56
- data/performance/tests/tribble/article.liquid +0 -98
- data/performance/tests/tribble/blog.liquid +0 -41
- data/performance/tests/tribble/cart.liquid +0 -134
- data/performance/tests/tribble/collection.liquid +0 -70
- data/performance/tests/tribble/index.liquid +0 -94
- data/performance/tests/tribble/page.liquid +0 -56
- data/performance/tests/tribble/product.liquid +0 -116
- data/performance/tests/tribble/search.liquid +0 -51
- data/performance/tests/tribble/theme.liquid +0 -90
- data/performance/tests/vogue/article.liquid +0 -66
- data/performance/tests/vogue/blog.liquid +0 -32
- data/performance/tests/vogue/cart.liquid +0 -58
- data/performance/tests/vogue/collection.liquid +0 -19
- data/performance/tests/vogue/index.liquid +0 -22
- data/performance/tests/vogue/page.liquid +0 -3
- data/performance/tests/vogue/product.liquid +0 -62
- data/performance/tests/vogue/theme.liquid +0 -122
- data/test/assign_test.rb +0 -11
- data/test/block_test.rb +0 -58
- data/test/capture_test.rb +0 -41
- data/test/condition_test.rb +0 -115
- data/test/content_for_test.rb +0 -15
- data/test/context_test.rb +0 -479
- data/test/drop_test.rb +0 -162
- data/test/error_handling_test.rb +0 -89
- data/test/extra/breakpoint.rb +0 -547
- data/test/extra/caller.rb +0 -80
- data/test/file_system_test.rb +0 -30
- data/test/filter_test.rb +0 -147
- data/test/helper.rb +0 -24
- data/test/html_tag_test.rb +0 -31
- data/test/if_else_test.rb +0 -139
- data/test/include_tag_test.rb +0 -129
- data/test/module_ex_test.rb +0 -89
- data/test/output_test.rb +0 -121
- data/test/parsing_quirks_test.rb +0 -54
- data/test/regexp_test.rb +0 -45
- data/test/security_test.rb +0 -41
- data/test/standard_filter_test.rb +0 -170
- data/test/standard_tag_test.rb +0 -405
- data/test/statements_test.rb +0 -137
- data/test/strainer_test.rb +0 -27
- data/test/template_test.rb +0 -82
- data/test/test_helper.rb +0 -28
- data/test/unless_else_test.rb +0 -27
- data/test/variable_test.rb +0 -173
- data/test/yield_test.rb +0 -24
data/lib/liquor/strainer.rb
DELETED
@@ -1,58 +0,0 @@
|
|
1
|
-
require 'set'
|
2
|
-
|
3
|
-
module Liquor
|
4
|
-
|
5
|
-
parent_object = if defined? BlankObject
|
6
|
-
BlankObject
|
7
|
-
else
|
8
|
-
Object
|
9
|
-
end
|
10
|
-
|
11
|
-
# Strainer is the parent class for the filters system.
|
12
|
-
# New filters are mixed into the strainer class which is then instanciated for each liquor template render run.
|
13
|
-
#
|
14
|
-
# One of the strainer's responsibilities is to keep malicious method calls out
|
15
|
-
class Strainer < parent_object #:nodoc:
|
16
|
-
INTERNAL_METHOD = /^__/
|
17
|
-
@@required_methods = Set.new([:__id__, :__send__, :respond_to?, :extend, :methods, :class, :object_id])
|
18
|
-
|
19
|
-
# Ruby 1.9.2 introduces Object#respond_to_missing?, which is invoked by Object#respond_to?
|
20
|
-
@@required_methods << :respond_to_missing? if Object.respond_to? :respond_to_missing?
|
21
|
-
|
22
|
-
@@filters = {}
|
23
|
-
|
24
|
-
def initialize(context)
|
25
|
-
@context = context
|
26
|
-
end
|
27
|
-
|
28
|
-
def self.global_filter(filter)
|
29
|
-
raise ArgumentError, "Passed filter is not a module" unless filter.is_a?(Module)
|
30
|
-
@@filters[filter.name] = filter
|
31
|
-
end
|
32
|
-
|
33
|
-
def self.get_filters
|
34
|
-
@@filters
|
35
|
-
end
|
36
|
-
|
37
|
-
def self.create(context)
|
38
|
-
strainer = Strainer.new(context)
|
39
|
-
@@filters.each { |k,m| strainer.extend(m) }
|
40
|
-
strainer
|
41
|
-
end
|
42
|
-
|
43
|
-
def respond_to?(method, include_private = false)
|
44
|
-
method_name = method.to_s
|
45
|
-
return false if method_name =~ INTERNAL_METHOD
|
46
|
-
return false if @@required_methods.include?(method_name)
|
47
|
-
super
|
48
|
-
end
|
49
|
-
|
50
|
-
# remove all standard methods from the bucket so circumvent security
|
51
|
-
# problems
|
52
|
-
instance_methods.each do |m|
|
53
|
-
unless @@required_methods.include?(m.to_sym)
|
54
|
-
undef_method m
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
data/lib/liquor/tags/assign.rb
DELETED
@@ -1,33 +0,0 @@
|
|
1
|
-
module Liquor
|
2
|
-
|
3
|
-
# Assign sets a variable in your template.
|
4
|
-
#
|
5
|
-
# {% assign foo = 'monkey' %}
|
6
|
-
#
|
7
|
-
# You can then use the variable later in the page.
|
8
|
-
#
|
9
|
-
# {{ monkey }}
|
10
|
-
#
|
11
|
-
class Assign < Tag
|
12
|
-
Syntax = /(#{VariableSignature}+)\s*=\s*(#{Expression}+)/
|
13
|
-
|
14
|
-
def initialize(tag_name, markup, tokens)
|
15
|
-
if markup =~ Syntax
|
16
|
-
@to = $1
|
17
|
-
@from = $2
|
18
|
-
else
|
19
|
-
raise SyntaxError.new("Syntax Error in 'assign' - Valid syntax: assign [var] = [source]")
|
20
|
-
end
|
21
|
-
|
22
|
-
super
|
23
|
-
end
|
24
|
-
|
25
|
-
def render(context)
|
26
|
-
context.scopes.last[@to] = context[@from]
|
27
|
-
''
|
28
|
-
end
|
29
|
-
|
30
|
-
end
|
31
|
-
|
32
|
-
Template.register_tag('assign', Assign)
|
33
|
-
end
|
data/lib/liquor/tags/capture.rb
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
module Liquor
|
2
|
-
|
3
|
-
# Capture stores the result of a block into a variable without rendering it inplace.
|
4
|
-
#
|
5
|
-
# {% capture heading %}
|
6
|
-
# Monkeys!
|
7
|
-
# {% endcapture %}
|
8
|
-
# ...
|
9
|
-
# <h1>{{ monkeys }}</h1>
|
10
|
-
#
|
11
|
-
# Capture is useful for saving content for use later in your template, such as
|
12
|
-
# in a sidebar or footer.
|
13
|
-
#
|
14
|
-
class Capture < Block
|
15
|
-
Syntax = /(\w+)/
|
16
|
-
|
17
|
-
def initialize(tag_name, markup, tokens)
|
18
|
-
if markup =~ Syntax
|
19
|
-
@to = $1
|
20
|
-
else
|
21
|
-
raise SyntaxError.new("Syntax Error in 'capture' - Valid syntax: capture [var]")
|
22
|
-
end
|
23
|
-
|
24
|
-
super
|
25
|
-
end
|
26
|
-
|
27
|
-
def render(context)
|
28
|
-
output = super
|
29
|
-
context.scopes.last[@to.to_s] = output.join
|
30
|
-
''
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
Template.register_tag('capture', Capture)
|
35
|
-
end
|
data/lib/liquor/tags/case.rb
DELETED
@@ -1,83 +0,0 @@
|
|
1
|
-
module Liquor
|
2
|
-
class Case < Block
|
3
|
-
Syntax = /(#{Expression})/
|
4
|
-
WhenSyntax = /(#{Expression})(?:(?:\s+or\s+|\s*\,\s*)(#{Expression}.*))?/
|
5
|
-
|
6
|
-
def initialize(tag_name, markup, tokens)
|
7
|
-
@blocks = []
|
8
|
-
|
9
|
-
if markup =~ Syntax
|
10
|
-
@left = $1
|
11
|
-
else
|
12
|
-
raise SyntaxError.new("Syntax Error in tag 'case' - Valid syntax: case [condition]")
|
13
|
-
end
|
14
|
-
|
15
|
-
super
|
16
|
-
end
|
17
|
-
|
18
|
-
def unknown_tag(tag, markup, tokens)
|
19
|
-
@nodelist = []
|
20
|
-
case tag
|
21
|
-
when 'when'
|
22
|
-
record_when_condition(markup)
|
23
|
-
when 'else'
|
24
|
-
record_else_condition(markup)
|
25
|
-
else
|
26
|
-
super
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
def render(context)
|
31
|
-
context.stack do
|
32
|
-
execute_else_block = true
|
33
|
-
|
34
|
-
@blocks.inject([]) do |output, block|
|
35
|
-
|
36
|
-
if block.else?
|
37
|
-
|
38
|
-
return render_all(block.attachment, context) if execute_else_block
|
39
|
-
|
40
|
-
elsif block.evaluate(context)
|
41
|
-
|
42
|
-
execute_else_block = false
|
43
|
-
output += render_all(block.attachment, context)
|
44
|
-
end
|
45
|
-
|
46
|
-
output
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
private
|
52
|
-
|
53
|
-
def record_when_condition(markup)
|
54
|
-
while markup
|
55
|
-
# Create a new nodelist and assign it to the new block
|
56
|
-
if not markup =~ WhenSyntax
|
57
|
-
raise SyntaxError.new("Syntax Error in tag 'case' - Valid when condition: {% when [condition] [or condition2...] %} ")
|
58
|
-
end
|
59
|
-
|
60
|
-
markup = $2
|
61
|
-
|
62
|
-
block = Condition.new(@left, '==', $1)
|
63
|
-
block.attach(@nodelist)
|
64
|
-
@blocks.push(block)
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
def record_else_condition(markup)
|
69
|
-
|
70
|
-
if not markup.strip.empty?
|
71
|
-
raise SyntaxError.new("Syntax Error in tag 'case' - Valid else condition: {% else %} (no parameters) ")
|
72
|
-
end
|
73
|
-
|
74
|
-
block = ElseCondition.new
|
75
|
-
block.attach(@nodelist)
|
76
|
-
@blocks << block
|
77
|
-
end
|
78
|
-
|
79
|
-
|
80
|
-
end
|
81
|
-
|
82
|
-
Template.register_tag('case', Case)
|
83
|
-
end
|
data/lib/liquor/tags/comment.rb
DELETED
@@ -1,54 +0,0 @@
|
|
1
|
-
module Liquor
|
2
|
-
|
3
|
-
# The content_for method allows you to insert content into a yield block in your layout.
|
4
|
-
# You only use content_for to insert content in named yields.
|
5
|
-
#
|
6
|
-
# In your layout:
|
7
|
-
# <title>{% yield title %}</title>
|
8
|
-
# <body>{% yield %}</body>
|
9
|
-
#
|
10
|
-
# In the view:
|
11
|
-
# {% content_for title %} The title {% end_content_for %}
|
12
|
-
# The body
|
13
|
-
#
|
14
|
-
#
|
15
|
-
# Will produce:
|
16
|
-
# <title>The title</title>
|
17
|
-
# <body>The body</body>
|
18
|
-
#
|
19
|
-
#
|
20
|
-
class ContentFor < Block
|
21
|
-
SyntaxHelp = "Syntax Error in tag 'content_for' - Valid syntax: content_for [name]"
|
22
|
-
Syntax = /(#{VariableSignature}+)/
|
23
|
-
|
24
|
-
def initialize(tag_name, markup, tokens)
|
25
|
-
if markup =~ Syntax
|
26
|
-
@name = $1
|
27
|
-
else
|
28
|
-
raise SyntaxError.new(SyntaxHelp)
|
29
|
-
end
|
30
|
-
|
31
|
-
super
|
32
|
-
end
|
33
|
-
|
34
|
-
def render(context)
|
35
|
-
result = ''
|
36
|
-
|
37
|
-
context.stack do
|
38
|
-
result = render_all(@nodelist, context).join
|
39
|
-
end
|
40
|
-
|
41
|
-
context['content_for'] = {} unless context.has_key? 'content_for'
|
42
|
-
context['content_for'][@name] = result
|
43
|
-
|
44
|
-
''
|
45
|
-
end
|
46
|
-
|
47
|
-
def block_delimiter
|
48
|
-
"end_#{block_name}"
|
49
|
-
end
|
50
|
-
|
51
|
-
end
|
52
|
-
|
53
|
-
Template.register_tag('content_for', ContentFor)
|
54
|
-
end
|
data/lib/liquor/tags/cycle.rb
DELETED
@@ -1,59 +0,0 @@
|
|
1
|
-
module Liquor
|
2
|
-
|
3
|
-
# Cycle is usually used within a loop to alternate between values, like colors or DOM classes.
|
4
|
-
#
|
5
|
-
# {% for item in items %}
|
6
|
-
# <div class="{% cycle 'red', 'green', 'blue' %}"> {{ item }} </div>
|
7
|
-
# {% end %}
|
8
|
-
#
|
9
|
-
# <div class="red"> Item one </div>
|
10
|
-
# <div class="green"> Item two </div>
|
11
|
-
# <div class="blue"> Item three </div>
|
12
|
-
# <div class="red"> Item four </div>
|
13
|
-
# <div class="green"> Item five</div>
|
14
|
-
#
|
15
|
-
class Cycle < Tag
|
16
|
-
SimpleSyntax = /^#{Expression}+/
|
17
|
-
NamedSyntax = /^(#{Expression})\s*\:\s*(.*)/
|
18
|
-
|
19
|
-
def initialize(tag_name, markup, tokens)
|
20
|
-
case markup
|
21
|
-
when NamedSyntax
|
22
|
-
@variables = variables_from_string($2)
|
23
|
-
@name = $1
|
24
|
-
when SimpleSyntax
|
25
|
-
@variables = variables_from_string(markup)
|
26
|
-
@name = "'#{@variables.to_s}'"
|
27
|
-
else
|
28
|
-
raise SyntaxError.new("Syntax Error in 'cycle' - Valid syntax: cycle [name :] var [, var2, var3 ...]")
|
29
|
-
end
|
30
|
-
super
|
31
|
-
end
|
32
|
-
|
33
|
-
def render(context)
|
34
|
-
context.registers[:cycle] ||= Hash.new(0)
|
35
|
-
|
36
|
-
context.stack do
|
37
|
-
key = context[@name]
|
38
|
-
iteration = context.registers[:cycle][key]
|
39
|
-
result = context[@variables[iteration]]
|
40
|
-
iteration += 1
|
41
|
-
iteration = 0 if iteration >= @variables.size
|
42
|
-
context.registers[:cycle][key] = iteration
|
43
|
-
result
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
private
|
48
|
-
|
49
|
-
def variables_from_string(markup)
|
50
|
-
markup.split(',').collect do |var|
|
51
|
-
var =~ /\s*(#{Expression})\s*/
|
52
|
-
$1 ? $1 : nil
|
53
|
-
end.compact
|
54
|
-
end
|
55
|
-
|
56
|
-
end
|
57
|
-
|
58
|
-
Template.register_tag('cycle', Cycle)
|
59
|
-
end
|
data/lib/liquor/tags/for.rb
DELETED
@@ -1,136 +0,0 @@
|
|
1
|
-
module Liquor
|
2
|
-
|
3
|
-
# "For" iterates over an array or collection.
|
4
|
-
# Several useful variables are available to you within the loop.
|
5
|
-
#
|
6
|
-
# == Basic usage:
|
7
|
-
# {% for item in collection %}
|
8
|
-
# {{ forloop.index }}: {{ item.name }}
|
9
|
-
# {% endfor %}
|
10
|
-
#
|
11
|
-
# == Advanced usage:
|
12
|
-
# {% for item in collection %}
|
13
|
-
# <div {% if forloop.first %}class="first"{% endif %}>
|
14
|
-
# Item {{ forloop.index }}: {{ item.name }}
|
15
|
-
# </div>
|
16
|
-
# {% endfor %}
|
17
|
-
#
|
18
|
-
# You can also define a limit and offset much like SQL. Remember
|
19
|
-
# that offset starts at 0 for the first item.
|
20
|
-
#
|
21
|
-
# {% for item in collection limit:5 offset:10 %}
|
22
|
-
# {{ item.name }}
|
23
|
-
# {% end %}
|
24
|
-
#
|
25
|
-
# To reverse the for loop simply use {% for item in collection reversed %}
|
26
|
-
#
|
27
|
-
# == Available variables:
|
28
|
-
#
|
29
|
-
# forloop.name:: 'item-collection'
|
30
|
-
# forloop.length:: Length of the loop
|
31
|
-
# forloop.index:: The current item's position in the collection;
|
32
|
-
# forloop.index starts at 1.
|
33
|
-
# This is helpful for non-programmers who start believe
|
34
|
-
# the first item in an array is 1, not 0.
|
35
|
-
# forloop.index0:: The current item's position in the collection
|
36
|
-
# where the first item is 0
|
37
|
-
# forloop.rindex:: Number of items remaining in the loop
|
38
|
-
# (length - index) where 1 is the last item.
|
39
|
-
# forloop.rindex0:: Number of items remaining in the loop
|
40
|
-
# where 0 is the last item.
|
41
|
-
# forloop.first:: Returns true if the item is the first item.
|
42
|
-
# forloop.last:: Returns true if the item is the last item.
|
43
|
-
#
|
44
|
-
class For < Block
|
45
|
-
Syntax = /(\w+)\s+in\s+(#{Expression}+)\s*(reversed)?/
|
46
|
-
|
47
|
-
def initialize(tag_name, markup, tokens)
|
48
|
-
if markup =~ Syntax
|
49
|
-
@variable_name = $1
|
50
|
-
@collection_name = $2
|
51
|
-
@name = "#{$1}-#{$2}"
|
52
|
-
@reversed = $3
|
53
|
-
@attributes = {}
|
54
|
-
markup.scan(TagAttributes) do |key, value|
|
55
|
-
@attributes[key] = value
|
56
|
-
end
|
57
|
-
else
|
58
|
-
raise SyntaxError.new("Syntax Error in 'for loop' - Valid syntax: for [item] in [collection]")
|
59
|
-
end
|
60
|
-
|
61
|
-
super
|
62
|
-
end
|
63
|
-
|
64
|
-
def render(context)
|
65
|
-
context.registers[:for] ||= Hash.new(0)
|
66
|
-
|
67
|
-
collection = context[@collection_name]
|
68
|
-
collection = collection.to_a if collection.is_a?(Range)
|
69
|
-
|
70
|
-
return '' unless collection.respond_to?(:each)
|
71
|
-
|
72
|
-
from = if @attributes['offset'] == 'continue'
|
73
|
-
context.registers[:for][@name].to_i
|
74
|
-
else
|
75
|
-
context[@attributes['offset']].to_i
|
76
|
-
end
|
77
|
-
|
78
|
-
limit = context[@attributes['limit']]
|
79
|
-
to = limit ? limit.to_i + from : nil
|
80
|
-
|
81
|
-
|
82
|
-
segment = slice_collection_using_each(collection, from, to)
|
83
|
-
|
84
|
-
return '' if segment.empty?
|
85
|
-
|
86
|
-
segment.reverse! if @reversed
|
87
|
-
|
88
|
-
result = []
|
89
|
-
|
90
|
-
length = segment.length
|
91
|
-
|
92
|
-
# Store our progress through the collection for the continue flag
|
93
|
-
context.registers[:for][@name] = from + segment.length
|
94
|
-
|
95
|
-
context.stack do
|
96
|
-
segment.each_with_index do |item, index|
|
97
|
-
context[@variable_name] = item
|
98
|
-
context['forloop'] = {
|
99
|
-
'name' => @name,
|
100
|
-
'length' => length,
|
101
|
-
'index' => index + 1,
|
102
|
-
'index0' => index,
|
103
|
-
'rindex' => length - index,
|
104
|
-
'rindex0' => length - index -1,
|
105
|
-
'first' => (index == 0),
|
106
|
-
'last' => (index == length - 1) }
|
107
|
-
|
108
|
-
result << render_all(@nodelist, context)
|
109
|
-
end
|
110
|
-
end
|
111
|
-
result
|
112
|
-
end
|
113
|
-
|
114
|
-
def slice_collection_using_each(collection, from, to)
|
115
|
-
segments = []
|
116
|
-
index = 0
|
117
|
-
yielded = 0
|
118
|
-
collection.each do |item|
|
119
|
-
|
120
|
-
if to && to <= index
|
121
|
-
break
|
122
|
-
end
|
123
|
-
|
124
|
-
if from <= index
|
125
|
-
segments << item
|
126
|
-
end
|
127
|
-
|
128
|
-
index += 1
|
129
|
-
end
|
130
|
-
|
131
|
-
segments
|
132
|
-
end
|
133
|
-
end
|
134
|
-
|
135
|
-
Template.register_tag('for', For)
|
136
|
-
end
|