def outline node, opts = {}
return unless node.sections?
sectnumlevels = opts[:sectnumlevels] || (node.document.attr 'sectnumlevels', 3).to_i
toclevels = opts[:toclevels] || (node.document.attr 'toclevels', 2).to_i
result = []
sections = node.sections
slevel = (first_section = sections[0]).level
slevel = 1 if slevel == 0 && first_section.special
result << %(<ul 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 << '</ul>'
result * EOL
end