Sha256: ba6f70a6c1ec5ede8d09fafc09d0fec2adf86a771aef1992c37b831b6f02b601

Contents?: true

Size: 852 Bytes

Versions: 6

Compression:

Stored size: 852 Bytes

Contents

module Slacken::Filters
  # Public: Group inline elements and wrap them in a wrapper node.
  #         Wrapper nodes are introduced to group inline nodes in a paragraph.
  class GroupInlines < Slacken::Filter
    def call(component)
      if component.block?
        component.derive(group_component(component))
      else
        component.derive(component.children.map(&method(:call)))
      end
    end

    def valid?(component)
      if component.type.member_of?(:wrapper)
        true
      elsif component.inline?
        false
      else
        component.children.all?(&method(:valid?))
      end
    end

    private

    def group_component(component)
      component.children.map(&method(:call)).chunk(&:inline?).map do |is_inline, group|
        is_inline ? component.class.new(:wrapper, group) : group
      end.flatten
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
slacken-0.1.6 lib/slacken/filters/group_inlines.rb
slacken-0.1.5 lib/slacken/filters/group_inlines.rb
slacken-0.1.4 lib/slacken/filters/group_inlines.rb
slacken-0.1.3 lib/slacken/filters/group_inlines.rb
slacken-0.1.2 lib/slacken/filters/group_inlines.rb
slacken-0.1.1 lib/slacken/filters/group_inlines.rb