Sha256: c3173e7665e99cbf97146d8e7c6c81a31fd8f9f903866dcad208fc4ba8ed744f

Contents?: true

Size: 844 Bytes

Versions: 58

Compression:

Stored size: 844 Bytes

Contents

module GovukTechDocs
  module TableOfContents
    class HeadingTreeBuilder
      def initialize(headings)
        @headings = headings
        @tree = HeadingTree.new
        @pointer = @tree
      end

      def tree
        @headings.each do |heading|
          move_to_depth(heading.size)

          @pointer.children << HeadingTree.new(parent: @pointer, heading: heading)
        end

        @tree
      end

    private

      def move_to_depth(depth)
        if depth > @pointer.depth
          @pointer = @pointer.children.last

          if depth > @pointer.depth
            @pointer.children << HeadingTree.new(parent: @pointer)

            move_to_depth(depth)
          end
        end

        if depth < @pointer.depth
          @pointer = @pointer.parent

          move_to_depth(depth)
        end
      end
    end
  end
end

Version data entries

58 entries across 58 versions & 2 rubygems

Version Path
govuk_tech_docs-3.5.0 lib/govuk_tech_docs/table_of_contents/heading_tree_builder.rb
govuk_tech_docs-3.4.5 lib/govuk_tech_docs/table_of_contents/heading_tree_builder.rb
govuk_tech_docs-3.4.0 lib/govuk_tech_docs/table_of_contents/heading_tree_builder.rb
govuk_tech_docs-3.3.1 lib/govuk_tech_docs/table_of_contents/heading_tree_builder.rb
govuk_tech_docs-3.3.0 lib/govuk_tech_docs/table_of_contents/heading_tree_builder.rb
govuk_tech_docs-3.2.1 lib/govuk_tech_docs/table_of_contents/heading_tree_builder.rb
govuk_tech_docs-3.2.0 lib/govuk_tech_docs/table_of_contents/heading_tree_builder.rb
govuk_tech_docs-3.1.0 lib/govuk_tech_docs/table_of_contents/heading_tree_builder.rb
govuk_tech_docs-3.0.1 lib/govuk_tech_docs/table_of_contents/heading_tree_builder.rb
govuk_tech_docs-3.0.0 lib/govuk_tech_docs/table_of_contents/heading_tree_builder.rb
govuk_tech_docs-2.4.3 lib/govuk_tech_docs/table_of_contents/heading_tree_builder.rb
govuk_tech_docs-2.4.2 lib/govuk_tech_docs/table_of_contents/heading_tree_builder.rb
govuk_tech_docs-2.4.1 lib/govuk_tech_docs/table_of_contents/heading_tree_builder.rb
govuk_tech_docs-2.4.0 lib/govuk_tech_docs/table_of_contents/heading_tree_builder.rb
govuk_tech_docs-2.3.0 lib/govuk_tech_docs/table_of_contents/heading_tree_builder.rb
govuk_tech_docs-2.2.2 lib/govuk_tech_docs/table_of_contents/heading_tree_builder.rb
govuk_tech_docs-2.2.1 lib/govuk_tech_docs/table_of_contents/heading_tree_builder.rb
govuk_tech_docs-2.2.0 lib/govuk_tech_docs/table_of_contents/heading_tree_builder.rb
govuk_tech_docs-2.1.1 lib/govuk_tech_docs/table_of_contents/heading_tree_builder.rb
govuk_tech_docs-2.1.0 lib/govuk_tech_docs/table_of_contents/heading_tree_builder.rb