diff --git a/lib/liquid.rb b/lib/liquid.rb index e8b3538..0872b30 100644 --- a/lib/liquid.rb +++ b/lib/liquid.rb @@ -48,6 +48,7 @@ require 'liquid/lexer' require 'liquid/parser' require 'liquid/i18n' require 'liquid/drop' +require 'liquid/tablerowloop_drop' require 'liquid/forloop_drop' require 'liquid/extensions' require 'liquid/errors' diff --git a/lib/liquid/tablerowloop_drop.rb b/lib/liquid/tablerowloop_drop.rb new file mode 100644 index 0000000..cda4a1e --- /dev/null +++ b/lib/liquid/tablerowloop_drop.rb @@ -0,0 +1,62 @@ +module Liquid + class TablerowloopDrop < Drop + def initialize(length, cols) + @length = length + @row = 1 + @col = 1 + @cols = cols + @index = 0 + end + + attr_reader :length, :col, :row + + def index + @index + 1 + end + + def index0 + @index + end + + def col0 + @col - 1 + end + + def rindex + @length - @index + end + + def rindex0 + @length - @index - 1 + end + + def first + @index == 0 + end + + def last + @index == @length - 1 + end + + def col_first + @col == 1 + end + + def col_last + @col == @cols + end + + protected + + def increment! + @index += 1 + + if @col == @cols + @col = 1 + @row += 1 + else + @col += 1 + end + end + end +end diff --git a/lib/liquid/tags/table_row.rb b/lib/liquid/tags/table_row.rb index efd4891..acd91e0 100644 --- a/lib/liquid/tags/table_row.rb +++ b/lib/liquid/tags/table_row.rb @@ -28,36 +28,21 @@ module Liquid cols = context.evaluate(@attributes['cols'.freeze]).to_i - row = 1 - col = 0 - result = "