Make TocTreeprocessor extendable and be gentle on badly structured headings

In django-wiki, I want to use an extension of toc, but the current does not allow to be inherited from. This is fixed.

Also, I wrote an external utility function that sorts a list of headers to accommodate this scenario:

    ## Wrongly placed heading
    # Top-level heading

Both are now included in the TOC, with the first heading as a top-level heading. I know this may upset people enforcing a correct structure of headings, but quite frankly, it is done all the time in the real world of busy users :)

Hope it's useful... thanks for keeping python-markdown up and running!