mirror of
https://github.com/kemko/liquid.git
synced 2026-01-03 08:45:42 +03:00
Compare commits
1 Commits
reduce-blo
...
for-loop-f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a61903da54 |
@@ -7,6 +7,8 @@ AllCops:
|
|||||||
|
|
||||||
Metrics/BlockNesting:
|
Metrics/BlockNesting:
|
||||||
Max: 3
|
Max: 3
|
||||||
|
Exclude:
|
||||||
|
- 'lib/liquid/block_body.rb'
|
||||||
|
|
||||||
Metrics/ModuleLength:
|
Metrics/ModuleLength:
|
||||||
Enabled: false
|
Enabled: false
|
||||||
|
|||||||
@@ -15,35 +15,38 @@ module Liquid
|
|||||||
def parse(tokenizer, parse_context)
|
def parse(tokenizer, parse_context)
|
||||||
parse_context.line_number = tokenizer.line_number
|
parse_context.line_number = tokenizer.line_number
|
||||||
while token = tokenizer.shift
|
while token = tokenizer.shift
|
||||||
next if token.empty?
|
unless token.empty?
|
||||||
case
|
case
|
||||||
when token.start_with?(TAGSTART)
|
when token.start_with?(TAGSTART)
|
||||||
whitespace_handler(token, parse_context)
|
whitespace_handler(token, parse_context)
|
||||||
unless token =~ FullToken
|
if token =~ FullToken
|
||||||
raise_missing_tag_terminator(token, parse_context)
|
tag_name = $1
|
||||||
|
markup = $2
|
||||||
|
# fetch the tag from registered blocks
|
||||||
|
if tag = registered_tags[tag_name]
|
||||||
|
new_tag = tag.parse(tag_name, markup, tokenizer, parse_context)
|
||||||
|
@blank &&= new_tag.blank?
|
||||||
|
@nodelist << new_tag
|
||||||
|
else
|
||||||
|
# end parsing if we reach an unknown tag and let the caller decide
|
||||||
|
# determine how to proceed
|
||||||
|
return yield tag_name, markup
|
||||||
|
end
|
||||||
|
else
|
||||||
|
raise_missing_tag_terminator(token, parse_context)
|
||||||
|
end
|
||||||
|
when token.start_with?(VARSTART)
|
||||||
|
whitespace_handler(token, parse_context)
|
||||||
|
@nodelist << create_variable(token, parse_context)
|
||||||
|
@blank = false
|
||||||
|
else
|
||||||
|
if parse_context.trim_whitespace
|
||||||
|
token.lstrip!
|
||||||
|
end
|
||||||
|
parse_context.trim_whitespace = false
|
||||||
|
@nodelist << token
|
||||||
|
@blank &&= !!(token =~ /\A\s*\z/)
|
||||||
end
|
end
|
||||||
tag_name = $1
|
|
||||||
markup = $2
|
|
||||||
# fetch the tag from registered blocks
|
|
||||||
unless tag = registered_tags[tag_name]
|
|
||||||
# end parsing if we reach an unknown tag and let the caller decide
|
|
||||||
# determine how to proceed
|
|
||||||
return yield tag_name, markup
|
|
||||||
end
|
|
||||||
new_tag = tag.parse(tag_name, markup, tokenizer, parse_context)
|
|
||||||
@blank &&= new_tag.blank?
|
|
||||||
@nodelist << new_tag
|
|
||||||
when token.start_with?(VARSTART)
|
|
||||||
whitespace_handler(token, parse_context)
|
|
||||||
@nodelist << create_variable(token, parse_context)
|
|
||||||
@blank = false
|
|
||||||
else
|
|
||||||
if parse_context.trim_whitespace
|
|
||||||
token.lstrip!
|
|
||||||
end
|
|
||||||
parse_context.trim_whitespace = false
|
|
||||||
@nodelist << token
|
|
||||||
@blank &&= !!(token =~ /\A\s*\z/)
|
|
||||||
end
|
end
|
||||||
parse_context.line_number = tokenizer.line_number
|
parse_context.line_number = tokenizer.line_number
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -171,9 +171,7 @@ module Liquid
|
|||||||
if scope.nil?
|
if scope.nil?
|
||||||
@environments.each do |e|
|
@environments.each do |e|
|
||||||
variable = lookup_and_evaluate(e, key, raise_on_not_found: raise_on_not_found)
|
variable = lookup_and_evaluate(e, key, raise_on_not_found: raise_on_not_found)
|
||||||
# When lookup returned a value OR there is no value but the lookup also did not raise
|
unless variable.nil?
|
||||||
# then it is the value we are looking for.
|
|
||||||
if !variable.nil? || @strict_variables && raise_on_not_found
|
|
||||||
scope = e
|
scope = e
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -7,12 +7,6 @@ class String # :nodoc:
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Symbol # :nodoc:
|
|
||||||
def to_liquid
|
|
||||||
to_s
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class Array # :nodoc:
|
class Array # :nodoc:
|
||||||
def to_liquid
|
def to_liquid
|
||||||
self
|
self
|
||||||
|
|||||||
@@ -201,14 +201,12 @@ module Liquid
|
|||||||
|
|
||||||
# Replace occurrences of a string with another
|
# Replace occurrences of a string with another
|
||||||
def replace(input, string, replacement = ''.freeze)
|
def replace(input, string, replacement = ''.freeze)
|
||||||
replacement = replacement.to_s
|
input.to_s.gsub(string.to_s, replacement.to_s)
|
||||||
input.to_s.gsub(string.to_s) { replacement }
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Replace the first occurrences of a string with another
|
# Replace the first occurrences of a string with another
|
||||||
def replace_first(input, string, replacement = ''.freeze)
|
def replace_first(input, string, replacement = ''.freeze)
|
||||||
replacement = replacement.to_s
|
input.to_s.sub(string.to_s, replacement.to_s)
|
||||||
input.to_s.sub(string.to_s) { replacement }
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# remove a substring
|
# remove a substring
|
||||||
|
|||||||
@@ -30,11 +30,11 @@ module Liquid
|
|||||||
end
|
end
|
||||||
|
|
||||||
def render(context)
|
def render(context)
|
||||||
context.registers[:cycle] ||= {}
|
context.registers[:cycle] ||= Hash.new(0)
|
||||||
|
|
||||||
context.stack do
|
context.stack do
|
||||||
key = context.evaluate(@name)
|
key = context.evaluate(@name)
|
||||||
iteration = context.registers[:cycle][key].to_i
|
iteration = context.registers[:cycle][key]
|
||||||
result = context.evaluate(@variables[iteration])
|
result = context.evaluate(@variables[iteration])
|
||||||
iteration += 1
|
iteration += 1
|
||||||
iteration = 0 if iteration >= @variables.size
|
iteration = 0 if iteration >= @variables.size
|
||||||
|
|||||||
@@ -120,7 +120,7 @@ module Liquid
|
|||||||
private
|
private
|
||||||
|
|
||||||
def collection_segment(context)
|
def collection_segment(context)
|
||||||
offsets = context.registers[:for] ||= {}
|
offsets = context.registers[:for] ||= Hash.new(0)
|
||||||
|
|
||||||
from = if @from == :continue
|
from = if @from == :continue
|
||||||
offsets[@name].to_i
|
offsets[@name].to_i
|
||||||
@@ -129,7 +129,7 @@ module Liquid
|
|||||||
end
|
end
|
||||||
|
|
||||||
collection = context.evaluate(@collection_name)
|
collection = context.evaluate(@collection_name)
|
||||||
collection = collection.to_a if collection.is_a?(Range)
|
collection = collection.step(1).to_a if collection.is_a?(Range)
|
||||||
|
|
||||||
limit = context.evaluate(@limit)
|
limit = context.evaluate(@limit)
|
||||||
to = limit ? limit.to_i + from : nil
|
to = limit ? limit.to_i + from : nil
|
||||||
|
|||||||
@@ -362,10 +362,8 @@ class StandardFiltersTest < Minitest::Test
|
|||||||
def test_replace
|
def test_replace
|
||||||
assert_equal '2 2 2 2', @filters.replace('1 1 1 1', '1', 2)
|
assert_equal '2 2 2 2', @filters.replace('1 1 1 1', '1', 2)
|
||||||
assert_equal '2 2 2 2', @filters.replace('1 1 1 1', 1, 2)
|
assert_equal '2 2 2 2', @filters.replace('1 1 1 1', 1, 2)
|
||||||
assert_equal "\\& \\& \\& \\&", @filters.replace('1 1 1 1', '1', "\\&")
|
|
||||||
assert_equal '2 1 1 1', @filters.replace_first('1 1 1 1', '1', 2)
|
assert_equal '2 1 1 1', @filters.replace_first('1 1 1 1', '1', 2)
|
||||||
assert_equal '2 1 1 1', @filters.replace_first('1 1 1 1', 1, 2)
|
assert_equal '2 1 1 1', @filters.replace_first('1 1 1 1', 1, 2)
|
||||||
assert_equal '\\& 1 1 1', @filters.replace_first("1 1 1 1", '1', "\\&")
|
|
||||||
assert_template_result '2 1 1 1', "{{ '1 1 1 1' | replace_first: '1', 2 }}"
|
assert_template_result '2 1 1 1', "{{ '1 1 1 1' | replace_first: '1', 2 }}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -48,6 +48,10 @@ HERE
|
|||||||
|
|
||||||
def test_for_with_variable_range
|
def test_for_with_variable_range
|
||||||
assert_template_result(' 1 2 3 ', '{%for item in (1..foobar) %} {{item}} {%endfor%}', "foobar" => 3)
|
assert_template_result(' 1 2 3 ', '{%for item in (1..foobar) %} {{item}} {%endfor%}', "foobar" => 3)
|
||||||
|
assert_template_result(' 1.0 2.0 3.0 ', '{%for item in foobar %} {{item}} {%endfor%}', "foobar" => (1..3.0))
|
||||||
|
assert_template_result(' 1.0 2.0 3.0 ', '{%for item in foobar %} {{item}} {%endfor%}', "foobar" => (1.0..3))
|
||||||
|
assert_template_result(' 1.0 2.0 3.0 ', '{%for item in foobar %} {{item}} {%endfor%}', "foobar" => (1.0..3.0))
|
||||||
|
assert_template_result(' 1.5 2.5 ', '{%for item in foobar %} {{item}} {%endfor%}', "foobar" => (1.5..3))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_for_with_hash_value_range
|
def test_for_with_hash_value_range
|
||||||
|
|||||||
@@ -261,15 +261,6 @@ class TemplateTest < Minitest::Test
|
|||||||
assert_equal 'Liquid error: undefined variable d', t.errors[2].message
|
assert_equal 'Liquid error: undefined variable d', t.errors[2].message
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_nil_value_does_not_raise
|
|
||||||
Liquid::Template.error_mode = :strict
|
|
||||||
t = Template.parse("some{{x}}thing")
|
|
||||||
result = t.render!({ 'x' => nil }, strict_variables: true)
|
|
||||||
|
|
||||||
assert_equal 0, t.errors.count
|
|
||||||
assert_equal 'something', result
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_undefined_variables_raise
|
def test_undefined_variables_raise
|
||||||
t = Template.parse("{{x}} {{y}} {{z.a}} {{z.b}} {{z.c.d}}")
|
t = Template.parse("{{x}} {{y}} {{z.a}} {{z.b}} {{z.c.d}}")
|
||||||
|
|
||||||
|
|||||||
@@ -89,8 +89,4 @@ class VariableTest < Minitest::Test
|
|||||||
def test_multiline_variable
|
def test_multiline_variable
|
||||||
assert_equal 'worked', Template.parse("{{\ntest\n}}").render!('test' => 'worked')
|
assert_equal 'worked', Template.parse("{{\ntest\n}}").render!('test' => 'worked')
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_symbol
|
|
||||||
assert_template_result 'bar', '{{ foo }}', 'foo' => :bar
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user