Class: JsDuck::GuideTocEntry
- Inherits:
-
Object
- Object
- JsDuck::GuideTocEntry
- Defined in:
- lib/jsduck/guide_toc_entry.rb
Overview
Manages the single TOC entry (with possible subentries).
Instance Attribute Summary collapse
-
#items ⇒ Object
Returns the value of attribute items.
-
#label ⇒ Object
Returns the value of attribute label.
Instance Method Summary collapse
-
#add(level, id, text) ⇒ Object
Adds entry at the corresponding heading level.
-
#count ⇒ Object
Total number of headings in TOC.
-
#initialize(parent = nil) ⇒ GuideTocEntry
constructor
A new instance of GuideTocEntry.
-
#prefix ⇒ Object
Generates the heading counter, like “1.5.4.”.
-
#to_html ⇒ Object
Converts to nested HTML list.
Constructor Details
#initialize(parent = nil) ⇒ GuideTocEntry
Returns a new instance of GuideTocEntry.
9 10 11 12 13 14 |
# File 'lib/jsduck/guide_toc_entry.rb', line 9 def initialize(parent=nil) @parent = parent @label = "" @items = [] @min_level = 2 end |
Instance Attribute Details
#items ⇒ Object
Returns the value of attribute items.
7 8 9 |
# File 'lib/jsduck/guide_toc_entry.rb', line 7 def items @items end |
#label ⇒ Object
Returns the value of attribute label.
7 8 9 |
# File 'lib/jsduck/guide_toc_entry.rb', line 7 def label @label end |
Instance Method Details
#add(level, id, text) ⇒ Object
Adds entry at the corresponding heading level.
17 18 19 20 21 22 23 24 25 26 27 |
# 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 ⇒ Object
Total number of headings in TOC
35 36 37 |
# File 'lib/jsduck/guide_toc_entry.rb', line 35 def count @items.map {|item| 1 + item.count}.reduce(0, :+) end |
#prefix ⇒ Object
Generates the heading counter, like “1.5.4.”
30 31 32 |
# File 'lib/jsduck/guide_toc_entry.rb', line 30 def prefix (@parent ? @parent.prefix : "") + "#{@items.length}." end |
#to_html ⇒ Object
Converts to nested HTML list.
40 41 42 43 44 45 46 47 48 49 50 |
# 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 |