Sha256: 927aee8611305dfa0c3a904f4d870b13fe77a22e31d64f0ba8572958cbcb5f51

Contents?: true

Size: 839 Bytes

Versions: 29

Compression:

Stored size: 839 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

29 entries across 29 versions & 1 rubygems

Version Path
govuk_tech_docs-4.1.1 lib/govuk_tech_docs/table_of_contents/headings_builder.rb
govuk_tech_docs-4.1.0 lib/govuk_tech_docs/table_of_contents/headings_builder.rb
govuk_tech_docs-4.0.0 lib/govuk_tech_docs/table_of_contents/headings_builder.rb
govuk_tech_docs-3.5.0 lib/govuk_tech_docs/table_of_contents/headings_builder.rb
govuk_tech_docs-3.4.5 lib/govuk_tech_docs/table_of_contents/headings_builder.rb
govuk_tech_docs-3.4.0 lib/govuk_tech_docs/table_of_contents/headings_builder.rb
govuk_tech_docs-3.3.1 lib/govuk_tech_docs/table_of_contents/headings_builder.rb
govuk_tech_docs-3.3.0 lib/govuk_tech_docs/table_of_contents/headings_builder.rb
govuk_tech_docs-3.2.1 lib/govuk_tech_docs/table_of_contents/headings_builder.rb
govuk_tech_docs-3.2.0 lib/govuk_tech_docs/table_of_contents/headings_builder.rb
govuk_tech_docs-3.1.0 lib/govuk_tech_docs/table_of_contents/headings_builder.rb
govuk_tech_docs-3.0.1 lib/govuk_tech_docs/table_of_contents/headings_builder.rb
govuk_tech_docs-3.0.0 lib/govuk_tech_docs/table_of_contents/headings_builder.rb
govuk_tech_docs-2.4.3 lib/govuk_tech_docs/table_of_contents/headings_builder.rb
govuk_tech_docs-2.4.2 lib/govuk_tech_docs/table_of_contents/headings_builder.rb
govuk_tech_docs-2.4.1 lib/govuk_tech_docs/table_of_contents/headings_builder.rb
govuk_tech_docs-2.4.0 lib/govuk_tech_docs/table_of_contents/headings_builder.rb
govuk_tech_docs-2.3.0 lib/govuk_tech_docs/table_of_contents/headings_builder.rb
govuk_tech_docs-2.2.2 lib/govuk_tech_docs/table_of_contents/headings_builder.rb
govuk_tech_docs-2.2.1 lib/govuk_tech_docs/table_of_contents/headings_builder.rb