Sha256: c4f7c44e4c0f2eda8c4bb787857a98b6bad1f2e28463c79582400919db73800a

Contents?: true

Size: 1 KB

Versions: 17

Compression:

Stored size: 1 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(el)
      el.attr.fetch('id', generate_id(el.options[:raw_text]))
    end

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

    def find_headers(parent)
      headers = []

      if parent.type == :header
        headers << build_header(parent)
      elsif parent.type == :html_element
        parent.children.each do |child|
          if child.type == :header
            headers << build_header(child)
          elsif child.children.size > 0
            headers << find_headers(child)
          end
        end
      end

      headers
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
govspeak-5.6.0 lib/govspeak/header_extractor.rb
govspeak-5.5.0 lib/govspeak/header_extractor.rb
govspeak-5.4.0 lib/govspeak/header_extractor.rb
govspeak-5.3.0 lib/govspeak/header_extractor.rb
govspeak-5.2.2 lib/govspeak/header_extractor.rb
govspeak-5.2.1 lib/govspeak/header_extractor.rb
govspeak-5.2.0 lib/govspeak/header_extractor.rb
govspeak-5.1.0 lib/govspeak/header_extractor.rb
govspeak-5.0.3 lib/govspeak/header_extractor.rb
govspeak-5.0.2 lib/govspeak/header_extractor.rb
govspeak-5.0.1 lib/govspeak/header_extractor.rb
govspeak-5.0.0 lib/govspeak/header_extractor.rb
govspeak-4.0.0 lib/govspeak/header_extractor.rb
govspeak-3.6.2 lib/govspeak/header_extractor.rb
govspeak-3.6.1 lib/govspeak/header_extractor.rb
govspeak-3.6.0 lib/govspeak/header_extractor.rb
govspeak-3.5.2 lib/govspeak/header_extractor.rb