Class: JsDuck::GuideToc

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

Overview

Adds Table of Contents section to guide HTML.

Instance Method Summary collapse

Constructor Details

#initialize(html, guide_name, max_level = 2) ⇒ GuideToc

Returns a new instance of GuideToc.



8
9
10
11
12
13
14
15
16
17
# File 'lib/jsduck/guide_toc.rb', line 8

def initialize(html, guide_name, max_level=2)
  @html = html
  @guide_name = guide_name

  @min_level = 2
  @max_level = max_level

  @toc = GuideTocEntry.new
  @new_html = []
end

Instance Method Details

#inject!Object

Inserts table of contents at the top of guide HTML by looking for headings at or below the specified maximum level.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/jsduck/guide_toc.rb', line 21

def inject!
  @html.each_line do |line|
    if line =~ /^\s*<h([1-6])>(.*?)<\/h[1-6]>$/
      level = $1.to_i
      original_text = $2
      text = Util::HTML.strip_tags(original_text)
      id = title_to_id(text)

      if include_to_toc?(level)
        @toc.add(level, id, text)
      end

      @new_html << "<h#{level} id='#{id}'>#{original_text}</h#{level}>\n"
    else
      @new_html << line
    end
  end

  inject_toc!

  @new_html.flatten.join
end