Sha256: a43c890d4df3c13015d51846ce3881ce76be2c85e17663ba8425e32fc6906287
Contents?: true
Size: 1.24 KB
Versions: 11
Compression:
Stored size: 1.24 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}"><span>#{tree.heading.title}</span></a>\n} end if tree.children.any? && level < @max_level output += indentation + "<ul>\n" unless level.zero? 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" unless level.zero? end output end end end end
Version data entries
11 entries across 11 versions & 1 rubygems