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