Class: TOC

Inherits:
Object
  • Object
show all
Includes:
Kramdown::Parser::Html::Constants, Kramdown::Utils::Html
Defined in:
lib/toc.rb

Constant Summary collapse

DISPATCHER =
Hash.new {|h,k| h[k] = "convert_#{k}"}

Instance Method Summary collapse

Instance Method Details

#convert_children(element, index) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/toc.rb', line 13

def convert_children(element, index)
    result = ""
    index += 2
    element.children.each do |inner_element|
        result += send(DISPATCHER[inner_element.type], inner_element, index)
    end
    result
end

#convert_text(element, index) ⇒ Object



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

def convert_text(element, index)
    escape_html(element.value, :text)
end

#convert_toc(element, index) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/toc.rb', line 26

def convert_toc(element, index)
    # result = Hash.new
    # result['href'] = element.attr[:id]
    # result['title'] = element.value.children.first.value
    #
    # if (element.children.length > 1)
    #     result['items'] = self.inner(element, ind)
    # end

    # return result

    title = element.value.children.first.value
    result = (" " * index)  + "<li><a href=\"##{element.attr[:id]}\">#{title}</a>"
    if (element.children.length > 1)
        result += "\n<ul>\n"
        result += "#{self.convert_children(element, index)}"
        result += (" " * index) + "</ul>"
    end

    result += "</li>\n"
    result
end

#to_html(element) ⇒ Object

转换文件



50
51
52
53
54
55
56
# File 'lib/toc.rb', line 50

def to_html(element)
    return nil if element.children.length == 0
    result = "<ul class='toc'>"
    result += self.convert_children(element, 0)
    result += "</ul>"
    result
end