Module: Reacco::Filters::TOC

Included in:
Readme
Defined in:
lib/reacco/filters/toc.rb

Instance Method Summary collapse

Instance Method Details

#linkify(h) ⇒ Object



21
22
23
24
# File 'lib/reacco/filters/toc.rb', line 21

def linkify(h)
  href = slugify(h.content)
  "<a href='##{href}'>#{h.inner_html}</a>"
end

#make_section(section) ⇒ Object



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
51
# File 'lib/reacco/filters/toc.rb', line 26

def make_section(section)
  h     = section.at_css('h1, h2, h3')
  return ''  unless h
  level = h.name[1]  # 1 | 2 | 3
  return ''  unless %w(1 2 3).include?(level)
  name  = h.content.strip

  out = case level
  when "1"
    [ "<h2>#{linkify h}</h2>",
      section.css('section.h2').map { |s| make_section s }
    ]
  when "2"
    [ "<nav class='level-#{level}'>",
      "<h3>#{linkify h}</h3>",
      "<ul>",
      section.css('section.h3').map { |s| make_section s },
      "</ul>",
      "</nav>"
    ]
  when "3"
    [ "<li>#{linkify h}</li>" ]
  end

  out.flatten.join "\n"
end

#make_toc(contents) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/reacco/filters/toc.rb', line 4

def make_toc(contents)
  aside = Nokogiri::XML::Node.new('aside', contents)
  aside['id'] = 'toc'

  # Header
  title = (h1 = contents.at_css('h1')) && h1.text || File.basename(Dir.pwd)
  aside.inner_html = "#{aside.inner_html}<h1>#{title}</h1>"

  contents.xpath('//body/section').each { |tag|
    aside.inner_html = "#{aside.inner_html}#{make_section tag}"
  }

  contents.at_css('body').add_child aside

  contents
end