Sha256: f1b4a90c4ecaa04466f603126363ee3761c99a2c60c216bdc00d5f59e33064c7

Contents?: true

Size: 1.19 KB

Versions: 21

Compression:

Stored size: 1.19 KB

Contents

module GovukTechDocs
  module TableOfContents
    class HeadingTreeRenderer
      DEFAULT_MAX_LEVEL = Float::INFINITY
      DEFAULT_INDENTATION = ''.freeze
      INDENTATION_INCREMENT = '  '.freeze

      def initialize(heading_tree, max_level: nil)
        @heading_tree = heading_tree
        @max_level = max_level || DEFAULT_MAX_LEVEL
      end

      def html
        render_tree(@heading_tree, level: 0)
      end

    private

      def render_tree(tree, indentation: DEFAULT_INDENTATION, level: nil)
        output = ''

        if tree.heading
          output += indentation + %{<a href="#{tree.heading.href}">#{tree.heading.title}</a>\n}
        end

        if tree.children.any? && level < @max_level
          output += indentation + "<ul>\n"

          tree.children.each do |child|
            output += indentation + INDENTATION_INCREMENT + "<li>\n"
            output += render_tree(
              child,
              indentation: indentation + INDENTATION_INCREMENT * 2,
              level: level + 1
            )
            output += indentation + INDENTATION_INCREMENT + "</li>\n"
          end

          output += indentation + "</ul>\n"
        end

        output
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 2 rubygems

Version Path
govuk_tech_docs-1.9.0.pre.rc2 lib/govuk_tech_docs/table_of_contents/heading_tree_renderer.rb
govuk_tech_docs-1.9.0.pre.gfe lib/govuk_tech_docs/table_of_contents/heading_tree_renderer.rb
govuk_tech_docs-1.8.3 lib/govuk_tech_docs/table_of_contents/heading_tree_renderer.rb
dss_tech_docs-0.1.2 lib/govuk_tech_docs/table_of_contents/heading_tree_renderer.rb
dss_tech_docs-0.1.1 lib/govuk_tech_docs/table_of_contents/heading_tree_renderer.rb
dss_tech_docs-0.1.0 lib/govuk_tech_docs/table_of_contents/heading_tree_renderer.rb
govuk_tech_docs-1.8.2 lib/govuk_tech_docs/table_of_contents/heading_tree_renderer.rb
govuk_tech_docs-1.8.1 lib/govuk_tech_docs/table_of_contents/heading_tree_renderer.rb
govuk_tech_docs-1.8.0 lib/govuk_tech_docs/table_of_contents/heading_tree_renderer.rb
govuk_tech_docs-1.7.0 lib/govuk_tech_docs/table_of_contents/heading_tree_renderer.rb
govuk_tech_docs-1.6.3 lib/govuk_tech_docs/table_of_contents/heading_tree_renderer.rb
govuk_tech_docs-1.6.2 lib/govuk_tech_docs/table_of_contents/heading_tree_renderer.rb
govuk_tech_docs-1.6.1 lib/govuk_tech_docs/table_of_contents/heading_tree_renderer.rb
govuk_tech_docs-1.6.0 lib/govuk_tech_docs/table_of_contents/heading_tree_renderer.rb
govuk_tech_docs-1.5.0 lib/govuk_tech_docs/table_of_contents/heading_tree_renderer.rb
govuk_tech_docs-1.4.0 lib/govuk_tech_docs/table_of_contents/heading_tree_renderer.rb
govuk_tech_docs-1.3.1 lib/govuk_tech_docs/table_of_contents/heading_tree_renderer.rb
govuk_tech_docs-1.3.0 lib/govuk_tech_docs/table_of_contents/heading_tree_renderer.rb
govuk_tech_docs-1.2.0 lib/govuk_tech_docs/table_of_contents/heading_tree_renderer.rb
govuk_tech_docs-1.1.0 lib/govuk_tech_docs/table_of_contents/heading_tree_renderer.rb