class Amber::Render::TocItem
TOC ITEM
A tree of TocItems composes the table of contents outline.
Attributes
anchor[R]
children[R]
level[R]
text[R]
Public Class Methods
new(heading='h0', text=nil, anchor=nil)
click to toggle source
# File lib/amber/render/table_of_contents.rb, line 204 def initialize(heading='h0', text=nil, anchor=nil) @level = heading[1].to_i if heading.is_a?(String) @text = text @anchor = anchor @children = [] end
Public Instance Methods
add_heading(heading, heading_text, heading_anchor)
click to toggle source
# File lib/amber/render/table_of_contents.rb, line 211 def add_heading(heading, heading_text, heading_anchor) self.parent_for(heading).children << TocItem.new(heading, heading_text, heading_anchor) end
parent_for(heading)
click to toggle source
Returns the appropriate TocItem
for appending a new item at a particular heading level.
# File lib/amber/render/table_of_contents.rb, line 261 def parent_for(heading) heading = heading[1].to_i if heading.is_a?(String) if children.any? && children.last.level < heading children.last.parent_for(heading) else self end end
populate_node(node, options)
click to toggle source
generates nokogiri html node tree from this toc
# File lib/amber/render/table_of_contents.rb, line 218 def populate_node(node, options) @children.each do |item| li = node.document.create_element("li") li.add_child(li.document.create_element("a", item.text, :href => "#{options[:href_base]}##{item.anchor}")) if item.children.any? ul = li.document.create_element(options[:tag]) item.populate_node(ul, options) li.add_child(ul) end node.add_child(li) end end
to_html(options={})
click to toggle source
generates html string from this toc
# File lib/amber/render/table_of_contents.rb, line 234 def to_html(options={}) html = [] tag = options[:tag] indent = options[:indent] || 0 str = options[:indent_str] || " " html << '%s<%s>' % [(str*indent), tag] @children.each do |item| html << '%s<li>' % (str*(indent+1)) html << '%s<a href="%s#%s">%s</a>' % [str*(indent+2), options[:href_base], item.anchor, item.text] if item.children.any? html << item.to_html({ :indent => indent+2, :indent_str => str, :tag => tag, :href_base => options[:href_base] }) end html << '%s</li>' % (str*(indent+1)) end html << '%s</%s>' % [(str*indent), tag] html.join("\n") end