'Resolve a *toctree* node into individual bullet lists with titles
as items, returning None (if no containing titles are found) or
a new node.
If *prune* is True, the tree is pruned to *maxdepth*, or if that is 0,
to the value of the *maxdepth* option on the *toctree* node.
If *titles_only* is True, only toplevel document titles will be in the
resulting tree.
If *collapse* is True, all branches not containing docname will
be collapsed.'

def resolve_toctree(self, docname, builder, toctree, prune=True, maxdepth=0, titles_only=False, collapse=False):

    if toctree.get('hidden', False):
        return None
    def _walk_depth(node, depth, maxdepth):
        'Utility:    Cut    a    TOC    at    a    specified    depth.'
        for subnode in node.children[:]:
            if isinstance(subnode, (addnodes.compact_paragraph, nodes.list_item)):
                subnode['classes'].append(('toctree-l%d' % (depth - 1)))
                _walk_depth(subnode, depth, maxdepth)
            elif isinstance(subnode, nodes.bullet_list):
                if ((maxdepth > 0) and (depth > maxdepth)):
                    subnode.parent.replace(subnode, [])
                else:
                    _walk_depth(subnode, (depth + 1), maxdepth)
                    if (collapse and (depth > 1) and ('current' not in subnode.parent['classes'])):
                        subnode.parent.remove(subnode)
            elif isinstance(subnode, nodes.reference):
                if ((subnode['refuri'] == docname) and (not subnode['anchorname'])):
                    p = subnode
                    while p:
                        p['classes'].append('current')
                        p = p.parent
    def _entries_from_toctree(toctreenode, separate=False, subtree=False):
        'Return    TOC    entries    for    a    toctree    node.'
        refs = [(e[0], str(e[1])) for e in toctreenode['entries']]
        entries = []
        for (title, ref) in refs:
            try:
                if url_re.match(ref):
                    reference = nodes.reference('', '', refuri=ref, anchorname='', *[nodes.Text(title)])
                    para = addnodes.compact_paragraph('', '', reference)
                    item = nodes.list_item('', para)
                    toc = nodes.bullet_list('', item)
                elif (ref == 'self'):
                    ref = toctreenode['parent']
                    if (not title):
                        title = self.titles[ref].astext()
                    reference = nodes.reference('', '', refuri=ref, anchorname='', *[nodes.Text(title)])
                    para = addnodes.compact_paragraph('', '', reference)
                    item = nodes.list_item('', para)
                    toc = nodes.bullet_list('', item)
                else:
                    toc = self.tocs[ref].deepcopy()
                    if (title and toc.children and (len(toc.children) == 1)):
                        child = toc.children[0]
                        for refnode in child.traverse(nodes.reference):
                            if ((refnode['refuri'] == ref) and (not refnode['anchorname'])):
                                refnode.children = [nodes.Text(title)]
                if (not toc.children):
                    self.warn(docname, ("toctree    contains    reference    to    document    %r    that    doesn't    have    a    title:    no    link    will    be    generated" % ref))
            except KeyError:
                self.warn(docname, ('toctree    contains    reference    to    nonexisting    document    %r' % ref))
            else:
                if titles_only:
                    for toplevel in toc:
                        if (len(toplevel) > 1):
                            subtrees = toplevel.traverse(addnodes.toctree)
                            toplevel[1][:] = subtrees
                for toctreenode in toc.traverse(addnodes.toctree):
                    i = (toctreenode.parent.index(toctreenode) + 1)
                    for item in _entries_from_toctree(toctreenode, subtree=True):
                        toctreenode.parent.insert(i, item)
                        i += 1
                    toctreenode.parent.remove(toctreenode)
                if separate:
                    entries.append(toc)
                else:
                    entries.extend(toc.children)
        if ((not subtree) and (not separate)):
            ret = nodes.bullet_list()
            ret += entries
            return [ret]
        return entries
    maxdepth = (maxdepth or toctree.get('maxdepth', (-1)))
    tocentries = _entries_from_toctree(toctree, separate=False)
    if (not tocentries):
        return None
    newnode = addnodes.compact_paragraph('', '', *tocentries)
    newnode['toctree'] = True
    _walk_depth(newnode, 1, ((prune and maxdepth) or 0))
    for refnode in newnode.traverse(nodes.reference):
        if (not url_re.match(refnode['refuri'])):
            refnode['refuri'] = (builder.get_relative_uri(docname, refnode['refuri']) + refnode['anchorname'])
    return newnode
