Class: Kitabu::Toc

Inherits:
Object
  • Object
show all
Includes:
REXML::StreamListener
Defined in:
lib/kitabu/toc.rb

Instance Method Summary collapse

Constructor Details

#initializeToc



5
6
7
8
9
10
# File 'lib/kitabu/toc.rb', line 5

def initialize
  @toc = ""
  @previous_level = 0
  @tag = nil
  @stack = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object



43
44
# File 'lib/kitabu/toc.rb', line 43

def method_missing(*args)
end

Instance Method Details

#header?(tag = nil) ⇒ Boolean



12
13
14
15
16
17
# File 'lib/kitabu/toc.rb', line 12

def header?(tag=nil)
  tag ||= @tag_name
  return false unless tag.to_s =~ /h[2-6]/
  @tag_name = tag
  return true
end

#in_header?Boolean



19
20
21
# File 'lib/kitabu/toc.rb', line 19

def in_header?
  @in_header
end

#tag_end(name) ⇒ Object



32
33
34
35
36
# File 'lib/kitabu/toc.rb', line 32

def tag_end(name)
  return unless header?(name)
  @in_header = false
  @previous_level = @current_level
end

#tag_start(name, attrs) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/kitabu/toc.rb', line 23

def tag_start(name, attrs)
  @tag_name = name
  return unless header?(name)
  @in_header = true
  @current_level = name.gsub!(/[^2-6]/, '').to_i
  @stack << @current_level
  @id = attrs["id"]
end

#text(str) ⇒ Object



38
39
40
41
# File 'lib/kitabu/toc.rb', line 38

def text(str)
  return unless in_header?
  @toc << %(<div class="level#{@current_level} #{@id}"><a href="##{@id}"><span>#{str}</span></a></div>)
end

#to_sObject



46
47
48
# File 'lib/kitabu/toc.rb', line 46

def to_s
  @toc
end