Class: Gollum::Filter::TOC

Inherits:
Gollum::Filter show all
Defined in:
lib/gollum-lib/filter/toc.rb

Overview

Inserts header anchors and creates TOC

Instance Method Summary collapse

Methods inherited from Gollum::Filter

#initialize

Methods included from Helpers

#trim_leading_slash

Constructor Details

This class inherits a constructor from Gollum::Filter

Instance Method Details

#extract(data) ⇒ Object



3
4
5
# File 'lib/gollum-lib/filter/toc.rb', line 3

def extract(data)
  data
end

#process(data) ⇒ Object



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
# File 'lib/gollum-lib/filter/toc.rb', line 7

def process(data)

  @doc               = Nokogiri::HTML::DocumentFragment.parse(data)
  @toc               = nil
  @anchor_names      = {}
  @current_ancestors = []

  if @markup.sub_page && @markup.parent_page
    @toc = @markup.parent_page.toc_data
  else
    @doc.css('h1,h2,h3,h4,h5,h6').each_with_index do |header, i|
      next if header.content.empty?
      # omit the first H1 (the page title) from the TOC if so configured
      next if (i == 0 && header.name =~ /[Hh]1/) && @markup.wiki && @markup.wiki.h1_title

      anchor_name = generate_anchor_name(header)
      
      add_anchor_to_header header, anchor_name
      add_entry_to_toc     header, anchor_name
    end

    @toc  = @toc.to_xml(@markup.to_xml_opts) if @toc != nil
    data  = @doc.to_xml(@markup.to_xml_opts)
    
  end
 
  @markup.toc = @toc
  data.gsub("[[_TOC_]]") do
    @toc.nil? ? '' : @toc
  end
end