Sha256: 42a8402cb5632661a57602b72637a5bbccb0c454277fb1a667f1201d5bec0ba6

Contents?: true

Size: 837 Bytes

Versions: 3

Compression:

Stored size: 837 Bytes

Contents

# frozen_string_literal: true

module Polites
  # Modify the AST for a parsed sheet to group list items in a nested structure,
  # rather than a flat structure using levels.
  class ListIndenter
    List = Struct.new(:children)

    # @param [Array<Polites::Node>] items
    # @return [Array<Polites::Node>]
    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

    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
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
polites-0.3.0 lib/polites/list_indenter.rb
polites-0.2.0 lib/polites/list_indenter.rb
polites-0.1.0 lib/polites/list_indenter.rb