Class: JsDuck::GuideTocEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/jsduck/guide_toc_entry.rb

Overview

Manages the single TOC entry (with possible subentries).

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#itemsObject

Returns the value of attribute items.



7
8
9
# File 'lib/jsduck/guide_toc_entry.rb', line 7

def items
  @items
end

#labelObject

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

#countObject

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

#prefixObject

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_htmlObject

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