Sha256: 5da33c016d8a8fcaa0989a00610be707467c138774a848b6da5f71e704c6b59b

Contents?: true

Size: 867 Bytes

Versions: 1

Compression:

Stored size: 867 Bytes

Contents

module Slippery
  module Processors
    # Take a flat list of elements, and wrap elements between <hr> lines into
    # a sections.
    #
    # @example
    #   HrToSections.new('body', H[:section]).call(doc)
    #
    class HrToSections
      def self.call(doc)
        self.new.call(doc)
      end

      def initialize(wrapper = H[:section], selector = 'body')
        @selector, @wrapper = selector, wrapper
      end

      def call(doc)
        doc.replace(@selector) { |element| hr_to_section(element) }
      end

      def hr_to_section(element)
        sections = [@wrapper]
        element.children.each do |child|
          if child.tag == :hr
            sections << @wrapper.merge_attrs(child)
          else
            sections[-1] = sections.last << child
          end
        end
        element.set_children(sections)
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
slippery-0.0.1 lib/slippery/processors/hr_to_sections.rb