Class: Kitabu::Toc
- Inherits:
-
Object
show all
- Includes:
- REXML::StreamListener
- Defined in:
- lib/kitabu/toc.rb
Instance Method Summary
collapse
Constructor Details
#initialize ⇒ Toc
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
12
13
14
15
16
17
|
# File 'lib/kitabu/toc.rb', line 12
def (tag=nil)
tag ||= @tag_name
return false unless tag.to_s =~ /h[2-6]/
@tag_name = tag
return true
end
|
19
20
21
|
# File 'lib/kitabu/toc.rb', line 19
def
end
|
#tag_end(name) ⇒ Object
32
33
34
35
36
|
# File 'lib/kitabu/toc.rb', line 32
def tag_end(name)
return unless (name)
= 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 (name)
= 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
@toc << %(<div class="level#{@current_level} #{@id}"><a href="##{@id}"><span>#{str}</span></a></div>)
end
|
#to_s ⇒ Object
46
47
48
|
# File 'lib/kitabu/toc.rb', line 46
def to_s
@toc
end
|