Sha256: 2c93da65c17b9cdb23ee3e2bdb76d8c8b444bcc154268918fea89e658439ba6b

Contents?: true

Size: 838 Bytes

Versions: 11

Compression:

Stored size: 838 Bytes

Contents

module GovukTechDocs
  module TableOfContents
    class HeadingsBuilder
      def initialize(html, url)
        @html = html
        @url = url
      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),
            page_url: @url
          )
        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

11 entries across 11 versions & 1 rubygems

Version Path
govuk_tech_docs-2.0.7 lib/govuk_tech_docs/table_of_contents/headings_builder.rb
govuk_tech_docs-2.0.6 lib/govuk_tech_docs/table_of_contents/headings_builder.rb
govuk_tech_docs-2.0.5 lib/govuk_tech_docs/table_of_contents/headings_builder.rb
govuk_tech_docs-2.0.4 lib/govuk_tech_docs/table_of_contents/headings_builder.rb
govuk_tech_docs-2.0.3 lib/govuk_tech_docs/table_of_contents/headings_builder.rb
govuk_tech_docs-2.0.2 lib/govuk_tech_docs/table_of_contents/headings_builder.rb
govuk_tech_docs-2.0.1 lib/govuk_tech_docs/table_of_contents/headings_builder.rb
govuk_tech_docs-2.0.0.pre.test.pre.release.pre.525 lib/govuk_tech_docs/table_of_contents/headings_builder.rb
govuk_tech_docs-2.0.0.pre.test.pre.release.pre.524 lib/govuk_tech_docs/table_of_contents/headings_builder.rb
govuk_tech_docs-2.0.0.test.pre.release.521 lib/govuk_tech_docs/table_of_contents/headings_builder.rb
govuk_tech_docs-2.0.0 lib/govuk_tech_docs/table_of_contents/headings_builder.rb