# File lib/maruku/toc.rb, line 133
    def create_toc
      self.header_ids = Hash.new(0)

      each_element(:header) {|h| h.attributes[:id] ||= h.generate_id }


      # The root section
      s = Section.new
      s.section_level = 0

      stack = [s]

      i = 0
      while i < @children.size
        if children[i].node_type == :header
          header = @children[i]
          level = header.level
          s2 = Section.new
          s2.section_level = level
          s2.header_element = header
          header.instance_variable_set :@section, s2
          while level <= stack.last.section_level
            stack.pop
          end
          stack.last.section_children.push s2
          stack.push s2
        else
          stack.last.immediate_children.push @children[i]
        end
        i += 1
      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