Sha256: d7760553d2f773651640e1064823ee44429c12c76a42f00ef861934c6c6c0850

Contents?: true

Size: 1.03 KB

Versions: 34

Compression:

Stored size: 1.03 KB

Contents

module Govspeak
  Header = Struct.new(:text, :level, :id)

  class HeaderExtractor < Kramdown::Converter::Base
    def convert(doc)
      headers = []

      doc.root.children.each do |el|
        if el.type == :header
          headers << build_header(el)
          next
        end

        headers << find_headers(el) if el.type == :html_element
      end

      headers.flatten.compact
    end

  private

    def id(element)
      element.attr.fetch("id", generate_id(element.options[:raw_text]))
    end

    def build_header(element)
      Header.new(element.options[:raw_text], element.options[:level], id(element))
    end

    def find_headers(parent)
      headers = []

      case parent.type
      when :header
        headers << build_header(parent)
      when :html_element
        parent.children.each do |child|
          if child.type == :header
            headers << build_header(child)
          elsif !child.children.empty?
            headers << find_headers(child)
          end
        end
      end

      headers
    end
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
govspeak-8.7.0 lib/govspeak/header_extractor.rb
govspeak-8.6.1 lib/govspeak/header_extractor.rb
govspeak-8.6.0 lib/govspeak/header_extractor.rb
govspeak-8.5.1 lib/govspeak/header_extractor.rb
govspeak-8.5.0 lib/govspeak/header_extractor.rb
govspeak-8.4.1 lib/govspeak/header_extractor.rb
govspeak-8.4.0 lib/govspeak/header_extractor.rb
govspeak-8.3.4 lib/govspeak/header_extractor.rb
govspeak-8.3.3 lib/govspeak/header_extractor.rb
govspeak-8.3.2 lib/govspeak/header_extractor.rb
govspeak-8.3.1 lib/govspeak/header_extractor.rb
govspeak-8.3.0 lib/govspeak/header_extractor.rb
govspeak-8.2.1 lib/govspeak/header_extractor.rb
govspeak-8.2.0 lib/govspeak/header_extractor.rb
govspeak-8.1.0 lib/govspeak/header_extractor.rb
govspeak-8.0.1 lib/govspeak/header_extractor.rb
govspeak-8.0.0 lib/govspeak/header_extractor.rb
govspeak-7.1.1 lib/govspeak/header_extractor.rb
govspeak-7.1.0 lib/govspeak/header_extractor.rb
govspeak-7.0.2 lib/govspeak/header_extractor.rb