class Polites::ListIndenter

Modify the AST for a parsed sheet to group list items in a nested structure, rather than a flat structure using levels.

Constants

List

Public Instance Methods

call(items) click to toggle source

@param [Array<Polites::Node>] items @return [Array<Polites::Node>]

# File lib/polites/list_indenter.rb, line 11
def call(items)
  items
    .chunk { |i| i.is_a?(Block::List) }.to_a
    .inject([]) do |acc, (k, contents)|
    acc + (k ? [List.new(indent(contents))] : contents)
  end
end

Private Instance Methods

indent(items) click to toggle source
# File lib/polites/list_indenter.rb, line 21
def indent(items)
  items
    .chunk { |item| item.level > items.first.level }
    .inject([]) do |acc, (indented, subitems)|
    if indented
      acc.last.children << List.new(indent(subitems))
      acc
    else
      acc + subitems
    end
  end
end