Class: Showoff::Compiler::TableOfContents
- Inherits:
-
Object
- Object
- Showoff::Compiler::TableOfContents
- Defined in:
- lib/showoff/compiler/table_of_contents.rb
Overview
adds table of content generation to the compiler
Class Method Summary collapse
-
.generate!(doc) ⇒ Nokogiri::HTML::DocumentFragment
Render a table of contents.
Class Method Details
.generate!(doc) ⇒ Nokogiri::HTML::DocumentFragment
Render a table of contents
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 || next if .search('.content').first.classes.include? 'cover' heads = .search('div.content h1:not(.section_title)') title = heads.empty? ? ['data-title'] : heads.first.text href = "##{['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 ['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', ['data-section']) entry.add_child(section) toc.add_child(entry) end end container.replace(toc) doc end |