Sha256: fe935bc3a7aa2830055f17b7c085734d6df90d09bc0b0571d5cc4f4e1236e37b

Contents?: true

Size: 787 Bytes

Versions: 5

Compression:

Stored size: 787 Bytes

Contents

module GovukTechDocs
  module TableOfContents
    class HeadingsBuilder
      def initialize(html)
        @html = html
      end

      def headings
        heading_elements.map do |element|
          Heading.new(
            element_name: element.node_name,
            text: element.content,
            attributes: convert_nokogiri_attr_objects_to_hashes(element.attributes)
          )
        end
      end

    private

      def page
        @_page ||= Nokogiri::HTML(@html)
      end

      def heading_elements
        page.search('h1, h2, h3, h4, h5, h6')
      end

      def convert_nokogiri_attr_objects_to_hashes(attributes)
        attributes.tap do |hash|
          hash.each do |k, v|
            hash[k] = v.value
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
govuk_tech_docs-1.3.1 lib/govuk_tech_docs/table_of_contents/headings_builder.rb
govuk_tech_docs-1.3.0 lib/govuk_tech_docs/table_of_contents/headings_builder.rb
govuk_tech_docs-1.2.0 lib/govuk_tech_docs/table_of_contents/headings_builder.rb
govuk_tech_docs-1.1.0 lib/govuk_tech_docs/table_of_contents/headings_builder.rb
govuk_tech_docs-1.0.0 lib/govuk_tech_docs/table_of_contents/headings_builder.rb