Class: Showoff::Compiler::TableOfContents

Inherits:
Object
  • Object
show all
Defined in:
lib/showoff/compiler/table_of_contents.rb

Overview

adds table of content generation to the compiler

Class Method Summary collapse

Class Method Details

.generate!(doc) ⇒ Nokogiri::HTML::DocumentFragment

Render a table of contents

Parameters:

  • doc (Nokogiri::HTML::DocumentFragment)

    The presentation document

Returns:

  • (Nokogiri::HTML::DocumentFragment)

    The presentation DOM with the table of contents rendered.

See Also:



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
42
43
44
45
46
47
48
49
50
# File 'lib/showoff/compiler/table_of_contents.rb', line 14

def self.generate!(doc)
  container = doc.search('p').find {|p| p.text == '~~~TOC~~~' }
  return doc unless container

  section = nil
  toc = Nokogiri::XML::Node.new('ol', doc)
  toc.set_attribute('id', 'toc')

  doc.search('div.slide:not(.toc)').each do |slide|
    next if slide.search('.content').first.classes.include? 'cover'

    heads = slide.search('div.content h1:not(.section_title)')
    title = heads.empty? ? slide['data-title'] : heads.first.text
    href  = "##{slide['id']}"

    entry = Nokogiri::XML::Node.new('li', doc)
    entry.add_class('tocentry')
    link  = Nokogiri::XML::Node.new('a', doc)
    link.set_attribute('href', href)
    link.content = title
    entry.add_child(link)

    if (section and slide['data-section'] == section['data-section'])
      section.add_child(entry)
    else
      section = Nokogiri::XML::Node.new('ol', doc)
      section.add_class('major')
      section.set_attribute('data-section', slide['data-section'])
      entry.add_child(section)
      toc.add_child(entry)
    end

  end
  container.replace(toc)

  doc
end