class JsDuck::GuideTocEntry
Manages the single TOC entry (with possible subentries).
Attributes
items[RW]
label[RW]
Public Class Methods
new(parent=nil)
click to toggle source
# File lib/jsduck/guide_toc_entry.rb, line 9 def initialize(parent=nil) @parent = parent @label = "" @items = [] @min_level = 2 end
Public Instance Methods
add(level, id, text)
click to toggle source
Adds entry at the corresponding heading level.
# File lib/jsduck/guide_toc_entry.rb, line 17 def add(level, id, text) if level == @min_level @items << GuideTocEntry.new(self) @items.last.label = "#{prefix} <a href='#!/guide/#{id}'>#{text}</a>\n" else if @items.empty? @items << GuideTocEntry.new(self) end @items.last.add(level-1, id, text) end end
count()
click to toggle source
Total number of headings in TOC
# File lib/jsduck/guide_toc_entry.rb, line 35 def count @items.map {|item| 1 + item.count}.reduce(0, :+) end
prefix()
click to toggle source
Generates the heading counter, like “1.5.4.”
# File lib/jsduck/guide_toc_entry.rb, line 30 def prefix (@parent ? @parent.prefix : "") + "#{@items.length}." end
to_html()
click to toggle source
Converts to nested HTML list.
# File lib/jsduck/guide_toc_entry.rb, line 40 def to_html return if @items.empty? return [ "<ul>", @items.map do |item| "<li>#{item.label} #{item.to_html}</li>" end, "</ul>", ].flatten.compact.join("\n") end