Class: Amber::Render::TocItem
- Inherits:
-
Object
- Object
- Amber::Render::TocItem
- Defined in:
- lib/amber/render/table_of_contents.rb
Overview
TOC ITEM
A tree of TocItems composes the table of contents outline.
Instance Attribute Summary collapse
-
#anchor ⇒ Object
readonly
Returns the value of attribute anchor.
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#level ⇒ Object
readonly
Returns the value of attribute level.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
Instance Method Summary collapse
- #add_heading(heading, heading_text, heading_anchor) ⇒ Object
-
#initialize(heading = 'h0', text = nil, anchor = nil) ⇒ TocItem
constructor
A new instance of TocItem.
-
#parent_for(heading) ⇒ Object
Returns the appropriate TocItem for appending a new item at a particular heading level.
-
#populate_node(node, options) ⇒ Object
generates nokogiri html node tree from this toc.
-
#to_html(options = {}) ⇒ Object
generates html string from this toc.
Constructor Details
#initialize(heading = 'h0', text = nil, anchor = nil) ⇒ TocItem
Returns a new instance of TocItem.
203 204 205 206 207 208 |
# File 'lib/amber/render/table_of_contents.rb', line 203 def initialize(heading='h0', text=nil, anchor=nil) @level = heading[1].to_i if heading.is_a?(String) @text = text @anchor = anchor @children = [] end |
Instance Attribute Details
#anchor ⇒ Object (readonly)
Returns the value of attribute anchor.
201 202 203 |
# File 'lib/amber/render/table_of_contents.rb', line 201 def anchor @anchor end |
#children ⇒ Object (readonly)
Returns the value of attribute children.
201 202 203 |
# File 'lib/amber/render/table_of_contents.rb', line 201 def children @children end |
#level ⇒ Object (readonly)
Returns the value of attribute level.
201 202 203 |
# File 'lib/amber/render/table_of_contents.rb', line 201 def level @level end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
201 202 203 |
# File 'lib/amber/render/table_of_contents.rb', line 201 def text @text end |
Instance Method Details
#add_heading(heading, heading_text, heading_anchor) ⇒ Object
210 211 212 |
# File 'lib/amber/render/table_of_contents.rb', line 210 def add_heading(heading, heading_text, heading_anchor) self.parent_for(heading).children << TocItem.new(heading, heading_text, heading_anchor) end |
#parent_for(heading) ⇒ Object
Returns the appropriate TocItem for appending a new item at a particular heading level.
260 261 262 263 264 265 266 267 |
# File 'lib/amber/render/table_of_contents.rb', line 260 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) ⇒ Object
generates nokogiri html node tree from this toc
217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/amber/render/table_of_contents.rb', line 217 def populate_node(node, ) @children.each do |item| li = node.document.create_element("li") li.add_child(li.document.create_element("a", item.text, :href => "#{[:href_base]}##{item.anchor}")) if item.children.any? ul = li.document.create_element([:tag]) item.populate_node(ul, ) li.add_child(ul) end node.add_child(li) end end |
#to_html(options = {}) ⇒ Object
generates html string from this toc
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/amber/render/table_of_contents.rb', line 233 def to_html(={}) html = [] tag = [:tag] indent = [:indent] || 0 str = [: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), [:href_base], item.anchor, item.text] if item.children.any? html << item.to_html({ :indent => indent+2, :indent_str => str, :tag => tag, :href_base => [:href_base] }) end html << '%s</li>' % (str*(indent+1)) end html << '%s</%s>' % [(str*indent), tag] html.join("\n") end |