'Generate table of contents'

def generate_toc(self, oeb_book, ref_url, output_dir):

    from lxml import etree
    from urllib import unquote
    from calibre.ebooks.oeb.base import element
    with CurrentDir(output_dir):
        def build_node(current_node, parent=None):
            if (parent is None):
                parent = etree.Element('ul')
            elif len(current_node.nodes):
                parent = element(parent, 'ul')
            for node in current_node.nodes:
                point = element(parent, 'li')
                href = relpath(abspath(unquote(node.href)), dirname(ref_url))
                link = element(point, 'a', href=href)
                title = node.title
                if title:
                    title = re.sub('\\s+', '    ', title)
                link.text = title
                build_node(node, point)
            return parent
        wrap = etree.Element('div')
        wrap.append(build_node(oeb_book.toc))
        return wrap
