Class: Amber::Render::TocItem

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(heading = 'h0', text = nil, anchor = nil) ⇒ TocItem

Returns a new instance of TocItem.



204
205
206
207
208
209
# 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

Instance Attribute Details

#anchorObject (readonly)

Returns the value of attribute anchor.



202
203
204
# File 'lib/amber/render/table_of_contents.rb', line 202

def anchor
  @anchor
end

#childrenObject (readonly)

Returns the value of attribute children.



202
203
204
# File 'lib/amber/render/table_of_contents.rb', line 202

def children
  @children
end

#levelObject (readonly)

Returns the value of attribute level.



202
203
204
# File 'lib/amber/render/table_of_contents.rb', line 202

def level
  @level
end

#textObject (readonly)

Returns the value of attribute text.



202
203
204
# File 'lib/amber/render/table_of_contents.rb', line 202

def text
  @text
end

Instance Method Details

#add_heading(heading, heading_text, heading_anchor) ⇒ Object



211
212
213
# 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) ⇒ Object

Returns the appropriate TocItem for appending a new item at a particular heading level.



261
262
263
264
265
266
267
268
# 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) ⇒ Object

generates nokogiri html node tree from this toc



218
219
220
221
222
223
224
225
226
227
228
229
# 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 = {}) ⇒ Object

generates html string from this toc



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# 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