Commit 4b30aab4 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Extract Guide TOC creation to a class.

parent bcebe90e
Loading
Loading
Loading
Loading
+45 −0
Original line number Diff line number Diff line
require 'jsduck/util/html'

module JsDuck

  # Adds Table of Contents section to guide HTML.
  class GuideToc

    # Inserts table of contents at the top of guide HTML by looking
    # for <h2> elements.
    def self.inject(html, guide_name)
      toc = [
        "<div class='toc'>\n",
        "<p><strong>Contents</strong></p>\n",
        "<ol>\n",
      ]

      new_html = []
      i = 0

      html.each_line do |line|
        if line =~ /^<h2>(.*)<\/h2>$/
          i += 1
          text = Util::HTML.strip_tags($1)
          toc << "<li><a href='#!/guide/#{guide_name}-section-#{i}'>#{text}</a></li>\n"
          new_html << "<h2 id='#{guide_name}-section-#{i}'>#{text}</h2>\n"
        else
          new_html << line
        end
      end

      toc << "</ol>\n"
      toc << "</div>\n"

      # Inject TOC below first heading if at least 2 items in TOC
      if i >= 2
        new_html.insert(1, toc)
        new_html.flatten.join
      else
        html
      end
    end

  end

end
+3 −32
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@ require 'jsduck/util/io'
require 'jsduck/util/null_object'
require 'jsduck/logger'
require 'jsduck/grouped_asset'
require 'jsduck/util/html'
require 'jsduck/guide_toc'
require 'jsduck/img/dir'
require 'fileutils'

@@ -67,7 +67,8 @@ module JsDuck
    def format_guide(guide)
      @formatter.doc_context = {:filename => guide[:filename], :linenr => 0}
      @formatter.images = Img::Dir.new(guide["url"], "guides/#{guide["name"]}")
      html = add_toc(guide, @formatter.format(Util::IO.read(guide[:filename])))
      html = @formatter.format(Util::IO.read(guide[:filename]))
      html = GuideToc.inject(html, guide['name'])

      # Report unused images (but ignore the icon files)
      @formatter.images.get("icon.png")
@@ -115,36 +116,6 @@ module JsDuck
      end
    end

    # Creates table of contents at the top of guide by looking for <h2> elements in HTML.
    def add_toc(guide, html)
      toc = [
        "<div class='toc'>\n",
        "<p><strong>Contents</strong></p>\n",
        "<ol>\n",
      ]
      new_html = []
      i = 0
      html.each_line do |line|
        if line =~ /^<h2>(.*)<\/h2>$/
          i += 1
          text = Util::HTML.strip_tags($1)
          toc << "<li><a href='#!/guide/#{guide['name']}-section-#{i}'>#{text}</a></li>\n"
          new_html << "<h2 id='#{guide['name']}-section-#{i}'>#{text}</h2>\n"
        else
          new_html << line
        end
      end
      toc << "</ol>\n"
      toc << "</div>\n"
      # Inject TOC at below first heading if at least 2 items in TOC
      if i >= 2
        new_html.insert(1, toc)
        new_html.flatten.join
      else
        html
      end
    end

    # Returns HTML listing of guides
    def to_html(style="")
      html = @groups.map do |group|