Class: JsDuck::GuideToc
- Inherits:
-
Object
- Object
- JsDuck::GuideToc
- Defined in:
- lib/jsduck/guide_toc.rb
Overview
Adds Table of Contents section to guide HTML.
Class Method Summary collapse
-
.inject(html, guide_name) ⇒ Object
Inserts table of contents at the top of guide HTML by looking for <h2> elements.
- .title_to_id(title) ⇒ Object
Class Method Details
.inject(html, guide_name) ⇒ Object
Inserts table of contents at the top of guide HTML by looking for <h2> elements.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/jsduck/guide_toc.rb', line 10 def self.inject(html, guide_name) toc = [] new_html = [] html.each_line do |line| if line =~ /^\s*<(h[1-6])>(.*?)<\/h[1-6]>$/ tag = $1 text = Util::HTML.($2) id = guide_name + "-section-" + title_to_id(text) if tag == "h2" toc << "<li><a href='#!/guide/#{id}'>#{text}</a></li>\n" end new_html << "<#{tag} id='#{id}'>#{text}</#{tag}>\n" else new_html << line end end # Inject TOC below first heading if at least 2 items in TOC if toc.length >= 2 new_html.insert(1, [ "<div class='toc'>\n", "<p><strong>Contents</strong></p>\n", "<ol>\n", toc, "</ol>\n", "</div>\n", ]) end new_html.flatten.join end |
.title_to_id(title) ⇒ Object
43 44 45 |
# File 'lib/jsduck/guide_toc.rb', line 43 def self.title_to_id(title) CGI::escape(title.downcase.gsub(/ /, "-")) end |