# File lib/maruku/toc.rb, line 135
                def create_toc
                        each_element(:header) do |h|
                                h.attributes[:id] ||= h.generate_id
                        end
                
                        stack = []
                
                        # the ancestor section
                        s = Section.new
                        s.section_level = 0

                        stack.push s
        
                        i = 0;
                        while i < @children.size
                                while i < @children.size 
                                        if @children[i].node_type == :header
                                                level = @children[i].level
                                                break if level <= stack.last.section_level+1
                                        end
                                
                                        stack.last.immediate_children.push @children[i]
                                        i += 1
                                end

                                break if i>=@children.size
                        
                                header = @children[i]
                                level = header.level
                        
                                if level > stack.last.section_level
                                        # this level is inside
                                
                                        s2 = Section.new
                                        s2.section_level = level
                                        s2.header_element = header
                                        header.instance_variable_set :@section, s2
                                
                                        stack.last.section_children.push s2
                                        stack.push s2
                                
                                        i+=1
                                elsif level == stack.last.section_level
                                        # this level is a sibling
                                        stack.pop
                                else 
                                        # this level is a parent
                                        stack.pop
                                end
                        
                        end

                        # If there is only one big header, then assume
                        # it is the master
                        if s.section_children.size == 1
                                s = s.section_children.first
                        end
                
                        # Assign section numbers
                        s.numerate
        
                        s
                end