class JsDuck::GuideToc

Adds Table of Contents section to guide HTML.

Public Class Methods

new(html, guide_name, max_level=2) click to toggle source
# 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

Public Instance Methods

inject!() click to toggle source

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

# 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

Private Instance Methods

include_to_toc?(level) click to toggle source
# File lib/jsduck/guide_toc.rb, line 46
def include_to_toc?(level)
  (@min_level..@max_level).include?(level)
end
inject_toc!() click to toggle source

Injects TOC below first heading if at least 2 items in TOC

# File lib/jsduck/guide_toc.rb, line 55
def inject_toc!
  return if @toc.count < 2

  @new_html.insert(1, [
    "<div class='toc'>\n",
      "<p><strong>Contents</strong></p>\n",
      @toc.to_html,
    "</div>\n",
  ])
end
title_to_id(title) click to toggle source
# File lib/jsduck/guide_toc.rb, line 50
def title_to_id(title)
  @guide_name + "-section-" + CGI::escape(title.downcase.gsub(/ /, "-"))
end