'Toc can have up to 5 levels, 0 - 4 inclusive.
This function will add items to their appropriate
depth in the TOC tree. If the specified depth is
invalid (item would not have a valid parent) add
it to the next valid level above the specified
level.'

def get_toc(self):

    n_toc = TOC()
    t_l0 = None
    t_l1 = None
    t_l2 = None
    t_l3 = None
    for (level, (href, id, text)) in self.toc:
        if (level == u'0'):
            t_l0 = n_toc.add_item(href, id, text)
            t_l1 = None
            t_l2 = None
            t_l3 = None
        elif (level == u'1'):
            if (t_l0 is None):
                t_l0 = n_toc
            t_l1 = t_l0.add_item(href, id, text)
            t_l2 = None
            t_l3 = None
        elif (level == u'2'):
            if (t_l1 is None):
                if (t_l0 is None):
                    t_l1 = n_toc
                else:
                    t_l1 = t_l0
            t_l2 = t_l1.add_item(href, id, text)
            t_l3 = None
        elif (level == u'3'):
            if (t_l2 is None):
                if (t_l1 is None):
                    if (t_l0 is None):
                        t_l2 = n_toc
                    else:
                        t_l2 = t_l0
                else:
                    t_l2 = t_l1
            t_l3 = t_l2.add_item(href, id, text)
        else:
            if (t_l3 is None):
                if (t_l2 is None):
                    if (t_l1 is None):
                        if (t_l0 is None):
                            t_l3 = n_toc
                        else:
                            t_l3 = t_l0
                    else:
                        t_l3 = t_l1
                else:
                    t_l3 = t_l2
            t_l3.add_item(href, id, text)
    return n_toc
