Class: Gollum::Filter::TOC
- Inherits:
-
Gollum::Filter
- Object
- Gollum::Filter
- Gollum::Filter::TOC
- Defined in:
- lib/gollum-lib/filter/toc.rb
Overview
Inserts header anchors and creates TOC
Instance Method Summary collapse
Methods inherited from Gollum::Filter
Methods included from Helpers
Constructor Details
This class inherits a constructor from Gollum::Filter
Instance Method Details
#extract(d) ⇒ Object
3 |
# File 'lib/gollum-lib/filter/toc.rb', line 3 def extract(d) d; end |
#process(data) ⇒ Object
5 6 7 8 9 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 42 43 44 |
# File 'lib/gollum-lib/filter/toc.rb', line 5 def process(data) doc = Nokogiri::HTML::DocumentFragment.parse(data) toc = nil doc.css('h1,h2,h3,h4,h5,h6').each do |h| # must escape " h_name = h.content.gsub(' ','-').gsub('"','%22') level = h.name.gsub(/[hH]/,'').to_i # Add anchors h.add_child(%Q{<a class="anchor" id="#{h_name}" href="##{h_name}"></a>}) # Build TOC toc ||= Nokogiri::XML::DocumentFragment.parse('<div class="toc"><div class="toc-title">Table of Contents</div></div>') tail ||= toc.child tail_level ||= 0 while tail_level < level node = Nokogiri::XML::Node.new('ul', doc) tail = tail.add_child(node) tail_level += 1 end while tail_level > level tail = tail.parent tail_level -= 1 end node = Nokogiri::XML::Node.new('li', doc) # % -> %25 so anchors work on Firefox. See issue #475 node.add_child(%Q{<a href="##{h_name}">#{h.content}</a>}) tail.add_child(node) end toc = toc.to_xml(@markup.to_xml_opts) if toc != nil data = doc.to_xml(@markup.to_xml_opts) @markup.toc = toc data.gsub("[[_TOC_]]") do toc.nil? ? '' : toc end end |