Compare commits

..

5 Commits

Author SHA1 Message Date
Thierry Joyal
3f439f73ba Add osx 2019-12-13 09:47:29 -05:00
Thierry Joyal
5face68cc8 Experiment with ruby 2.7 2019-12-13 09:13:32 -05:00
Mike Angell
57c9cf64eb Allow render to handle with and for correctly (#1193)
* Allow render to handle with and for correctly

* code improvements
2019-10-23 04:12:46 +10:00
Alessandro Diogo Brückheimer
e83b1e4159 Add ForceEqualSignAlignment to .rubocop.yml (#1190)
* Add ForceEqualSignAlignment to .rubocop.yml

* Revert ForceEqualSignAlignment cop

* Update method alignment

* Undo addition of whitespace to improve readability

* Fix missing alignment
2019-10-21 21:18:48 +10:00
Mike Angell
3784020a8d [New Feature] Add forloop inside render tag when using for syntax (#1191)
* Add forloop to render for syntax

* Remove forloop guard
2019-10-17 23:06:13 +10:00
68 changed files with 375 additions and 334 deletions

View File

@@ -892,7 +892,7 @@ Lint/FormatParameterMismatch:
Enabled: true Enabled: true
Lint/HandleExceptions: Lint/HandleExceptions:
Enabled: true AllowComments: true
Lint/ImplicitStringConcatenation: Lint/ImplicitStringConcatenation:
Description: Checks for adjacent string literals on the same line, which could Description: Checks for adjacent string literals on the same line, which could

View File

@@ -1,11 +1,15 @@
language: ruby language: ruby
cache: bundler cache: bundler
os: [osx, linux]
rvm: rvm:
- 2.4 - 2.4
- 2.5 - 2.5
- &latest_ruby 2.6 - &latest_ruby 2.6
- 2.7 - 2.7
- 2.7.0-preview1
- 2.7.0-preview2
- 2.7.0-preview3
- ruby-head - ruby-head
matrix: matrix:
@@ -15,6 +19,9 @@ matrix:
name: Profiling Memory Usage name: Profiling Memory Usage
allow_failures: allow_failures:
- rvm: ruby-head - rvm: ruby-head
- rvm: 2.7
- rvm: 2.7.0-preview2
- rvm: 2.7.0-preview3
branches: branches:
only: only:

View File

@@ -38,6 +38,7 @@ module Liquid
@left = left @left = left
@operator = operator @operator = operator
@right = right @right = right
@child_relation = nil @child_relation = nil
@child_condition = nil @child_condition = nil
end end

View File

@@ -7,8 +7,10 @@ module Liquid
def initialize(options = {}) def initialize(options = {})
@template_options = options ? options.dup : {} @template_options = options ? options.dup : {}
@locale = @template_options[:locale] ||= I18n.new @locale = @template_options[:locale] ||= I18n.new
@warnings = [] @warnings = []
self.depth = 0 self.depth = 0
self.partial = false self.partial = false
end end
@@ -20,6 +22,7 @@ module Liquid
def partial=(value) def partial=(value)
@partial = value @partial = value
@options = value ? partial_options : @template_options @options = value ? partial_options : @template_options
@error_mode = @options[:error_mode] || Template.error_mode @error_mode = @options[:error_mode] || Template.error_mode
end end

View File

@@ -9,6 +9,7 @@ module Liquid
file_system = (context.registers[:file_system] ||= Liquid::Template.file_system) file_system = (context.registers[:file_system] ||= Liquid::Template.file_system)
source = file_system.read_template_file(template_name) source = file_system.read_template_file(template_name)
parse_context.partial = true parse_context.partial = true
partial = Liquid::Template.parse(source, parse_context) partial = Liquid::Template.parse(source, parse_context)

View File

@@ -78,9 +78,12 @@ module Liquid
return if input.nil? return if input.nil?
input_str = input.to_s input_str = input.to_s
length = Utils.to_integer(length) length = Utils.to_integer(length)
truncate_string_str = truncate_string.to_s truncate_string_str = truncate_string.to_s
l = length - truncate_string_str.length l = length - truncate_string_str.length
l = 0 if l < 0 l = 0 if l < 0
input_str.length > length ? input_str[0...l].concat(truncate_string_str) : input_str input_str.length > length ? input_str[0...l].concat(truncate_string_str) : input_str
end end
@@ -88,8 +91,10 @@ module Liquid
return if input.nil? return if input.nil?
wordlist = input.to_s.split wordlist = input.to_s.split
words = Utils.to_integer(words) words = Utils.to_integer(words)
l = words - 1 l = words - 1
l = 0 if l < 0 l = 0 if l < 0
wordlist.length > l ? wordlist[0..l].join(" ").concat(truncate_string.to_s) : input wordlist.length > l ? wordlist[0..l].join(" ").concat(truncate_string.to_s) : input
end end

View File

@@ -51,8 +51,8 @@ module Liquid
iteration += 1 iteration += 1
iteration = 0 if iteration >= @variables.size iteration = 0 if iteration >= @variables.size
context.registers[:cycle][key] = iteration
context.registers[:cycle][key] = iteration
output output
end end

View File

@@ -105,9 +105,11 @@ module Liquid
p = Parser.new(markup) p = Parser.new(markup)
@variable_name = p.consume(:id) @variable_name = p.consume(:id)
raise SyntaxError, options[:locale].t("errors.syntax.for_invalid_in") unless p.id?('in') raise SyntaxError, options[:locale].t("errors.syntax.for_invalid_in") unless p.id?('in')
collection_name = p.expression collection_name = p.expression
@name = "#{@variable_name}-#{collection_name}"
@collection_name = Expression.parse(collection_name) @collection_name = Expression.parse(collection_name)
@name = "#{@variable_name}-#{collection_name}"
@reversed = p.id?('reversed') @reversed = p.id?('reversed')
while p.look(:id) && p.look(:colon, 1) while p.look(:id) && p.look(:colon, 1)

View File

@@ -2,7 +2,8 @@
module Liquid module Liquid
class Render < Tag class Render < Tag
SYNTAX = /(#{QuotedString}+)(\s+(?:with|for)\s+(#{QuotedFragment}+))?(\s+(?:as)\s+(#{VariableSegment}+))?/o FOR = 'for'
SYNTAX = /(#{QuotedString}+)(\s+(with|#{FOR})\s+(#{QuotedFragment}+))?(\s+(?:as)\s+(#{VariableSegment}+))?/o
disable_tags "include" disable_tags "include"
@@ -14,11 +15,13 @@ module Liquid
raise SyntaxError, options[:locale].t("errors.syntax.render") unless markup =~ SYNTAX raise SyntaxError, options[:locale].t("errors.syntax.render") unless markup =~ SYNTAX
template_name = Regexp.last_match(1) template_name = Regexp.last_match(1)
variable_name = Regexp.last_match(3) with_or_for = Regexp.last_match(3)
variable_name = Regexp.last_match(4)
@alias_name = Regexp.last_match(5) @alias_name = Regexp.last_match(6)
@variable_name_expr = variable_name ? Expression.parse(variable_name) : nil @variable_name_expr = variable_name ? Expression.parse(variable_name) : nil
@template_name_expr = Expression.parse(template_name) @template_name_expr = Expression.parse(template_name)
@for = (with_or_for == FOR)
@attributes = {} @attributes = {}
markup.scan(TagAttributes) do |key, value| markup.scan(TagAttributes) do |key, value|
@@ -48,6 +51,7 @@ module Liquid
inner_context.template_name = template_name inner_context.template_name = template_name
inner_context.partial = true inner_context.partial = true
inner_context['forloop'] = forloop if forloop inner_context['forloop'] = forloop if forloop
@attributes.each do |key, value| @attributes.each do |key, value|
inner_context[key] = context.evaluate(value) inner_context[key] = context.evaluate(value)
end end
@@ -57,7 +61,7 @@ module Liquid
} }
variable = @variable_name_expr ? context.evaluate(@variable_name_expr) : nil variable = @variable_name_expr ? context.evaluate(@variable_name_expr) : nil
if variable.is_a?(Array) if @for && variable.respond_to?(:each) && variable.respond_to?(:count)
forloop = Liquid::ForloopDrop.new(template_name, variable.count, nil) forloop = Liquid::ForloopDrop.new(template_name, variable.count, nil)
variable.each { |var| render_partial_func.call(var, forloop) } variable.each { |var| render_partial_func.call(var, forloop) }
else else

View File

@@ -27,7 +27,6 @@ module Liquid
to = @attributes.key?('limit') ? from + context.evaluate(@attributes['limit']).to_i : nil to = @attributes.key?('limit') ? from + context.evaluate(@attributes['limit']).to_i : nil
collection = Utils.slice_collection(collection, from, to) collection = Utils.slice_collection(collection, from, to)
length = collection.length length = collection.length
cols = context.evaluate(@attributes['cols']).to_i cols = context.evaluate(@attributes['cols']).to_i

View File

@@ -252,6 +252,7 @@ class ErrorHandlingTest < Minitest::Test
begin begin
Liquid::Template.file_system = TestFileSystem.new Liquid::Template.file_system = TestFileSystem.new
template = Liquid::Template.parse("Argument error:\n{% include 'product' %}", line_numbers: true) template = Liquid::Template.parse("Argument error:\n{% include 'product' %}", line_numbers: true)
page = template.render('errors' => ErrorDrop.new) page = template.render('errors' => ErrorDrop.new)
ensure ensure

View File

@@ -214,4 +214,22 @@ class RenderTagTest < Minitest::Test
assert_template_result("Product: Draft 151cm first index:1 Product: Element 155cm last index:2 ", assert_template_result("Product: Draft 151cm first index:1 Product: Element 155cm last index:2 ",
"{% render 'product' for products %}", "products" => [{ 'title' => 'Draft 151cm' }, { 'title' => 'Element 155cm' }]) "{% render 'product' for products %}", "products" => [{ 'title' => 'Draft 151cm' }, { 'title' => 'Element 155cm' }])
end end
def test_render_tag_for_drop
Liquid::Template.file_system = StubFileSystem.new(
'loop' => "{{ value.foo }}",
)
assert_template_result("123",
"{% render 'loop' for loop as value %}", "loop" => TestEnumerable.new)
end
def test_render_tag_with_drop
Liquid::Template.file_system = StubFileSystem.new(
'loop' => "{{ value }}",
)
assert_template_result("TestEnumerable",
"{% render 'loop' with loop as value %}", "loop" => TestEnumerable.new)
end
end end