Class: MediaWikiHTMLGenerator::TocGenerator

Inherits:
MediaWikiHTMLGenerator show all
Defined in:
lib/mediacloth/mediawikihtmlgenerator.rb

Overview

AST walker that generates a table of contents, containing links to all section headings in the page.

Defined Under Namespace

Classes: TocNode

Instance Attribute Summary

Attributes inherited from MediaWikiHTMLGenerator

#html

Attributes inherited from MediaWikiWalker

#params, #template_handler

Instance Method Summary collapse

Methods inherited from MediaWikiHTMLGenerator

anchor_for, escape

Methods inherited from MediaWikiWalker

#link_handler, #link_handler=

Instance Method Details

#parse(ast) ⇒ Object



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/mediacloth/mediawikihtmlgenerator.rb', line 276

def parse(ast)
    @html = ''
    @text_generator = TextGenerator.new

    root = TocNode.new
    root_stack = [root]

    parse_branch = lambda do |ast| 
        ast.children.each do |child|
            if child.class == SectionAST
                root_stack.pop while child.level <= ((sec = root_stack.last.section) ? sec.level : 0)

                node = TocNode.new
                node.section = child
                root_stack.last.add_child(node)

                root_stack.push node
            end
            parse_branch.call(child)
        end
    end
    parse_branch.call(ast)

    @html += parse_section(root)
    @html = "<div class=\"wikitoc\">\n<div class=\"wikitoctitle\">Contents</div>#{@html}\n</div>\n" if @html != ''
end