forked from oreillymedia/asciidoctor-htmlbook
-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.rb
23 lines (23 loc) · 1.12 KB
/
helpers.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def outline node, opts = {}
return if (sections = node.sections).empty?
sectnumlevels = opts[:sectnumlevels] || (node.document.attr 'sectnumlevels', 3).to_i
toclevels = opts[:toclevels] || (node.document.attr 'toclevels', 2).to_i
result = []
# FIXME the level for special sections should be set correctly in the model
# slevel will only be 0 if we have a book doctype with parts
slevel = (first_section = sections[0]).level
slevel = 1 if slevel == 0 && first_section.special
result << %(<ol class="sectlevel#{slevel}">)
sections.each do |section|
section_num = (section.numbered && !section.caption && section.level <= sectnumlevels) ? %(#{section.sectnum} ) : nil
if section.level < toclevels && (child_toc_level = outline section, :toclevels => toclevels, :secnumlevels => sectnumlevels)
result << %(<li><a href="##{section.id}">#{section_num}#{section.captioned_title}</a>)
result << child_toc_level
result << '</li>'
else
result << %(<li><a href="##{section.id}">#{section_num}#{section.captioned_title}</a></li>)
end
end
result << '</ol>'
result * "\n"
end