'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(self, docname, builder, toctree, prune=True, maxdepth=0, titles_only=False, collapse=False, includehidden=False):

    if (toctree.get('hidden', False) and (not includehidden)):
        return None
    toctree_ancestors = self.get_toctree_ancestors(docname)
    def _toctree_add_classes(node, depth):
        "Add    'toctree-l%d'    and    'current'    classes    to    the    toctree."
        for subnode in node.children:
            if isinstance(subnode, (addnodes.compact_paragraph, nodes.list_item)):
                subnode['classes'].append(('toctree-l%d' % (depth - 1)))
                _toctree_add_classes(subnode, depth)
            elif isinstance(subnode, nodes.bullet_list):
                _toctree_add_classes(subnode, (depth + 1))
            elif isinstance(subnode, nodes.reference):
                if (subnode['refuri'] == docname):
                    if (not subnode['anchorname']):
                        branchnode = subnode
                        while branchnode:
                            branchnode['classes'].append('current')
                            branchnode = branchnode.parent
                    if subnode.parent.parent.get('iscurrent'):
                        return
                    while subnode:
                        subnode['iscurrent'] = True
                        subnode = subnode.parent
    def _entries_from_toctree(toctreenode, parents, separate=False, subtree=False):
        'Return    TOC    entries    for    a    toctree    node.'
        refs = [(e[0], e[1]) for e in toctreenode['entries']]
        entries = []
        for (title, ref) in refs:
            try:
                refdoc = None
                if url_re.match(ref):
                    if (title is None):
                        title = ref
                    reference = nodes.reference('', '', internal=False, 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 = clean_astext(self.env.titles[ref])
                    reference = nodes.reference('', '', internal=True, refuri=ref, anchorname='', *[nodes.Text(title)])
                    para = addnodes.compact_paragraph('', '', reference)
                    item = nodes.list_item('', para)
                    toc = nodes.bullet_list('', item)
                else:
                    if (ref in parents):
                        logger.warning('circular    toctree    references    detected,    ignoring:    %s    <-    %s', ref, '    <-    '.join(parents), location=ref)
                        continue
                    refdoc = ref
                    toc = self.env.tocs[ref].deepcopy()
                    maxdepth = self.env.metadata[ref].get('tocdepth', 0)
                    if ((ref not in toctree_ancestors) or (prune and (maxdepth > 0))):
                        self._toctree_prune(toc, 2, maxdepth, collapse)
                    self.process_only_nodes(toc)
                    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):
                    logger.warning("toctree    contains    reference    to    document    %r    that    doesn't    have    a    title:    no    link    will    be    generated", ref, location=toctreenode)
            except KeyError:
                logger.warning('toctree    contains    reference    to    nonexisting    document    %r', ref, location=toctreenode)
            else:
                if titles_only:
                    for toplevel in toc:
                        if (len(toplevel) > 1):
                            subtrees = toplevel.traverse(addnodes.toctree)
                            if subtrees:
                                toplevel[1][:] = subtrees
                            else:
                                toplevel.pop(1)
                for subtocnode in toc.traverse(addnodes.toctree):
                    if (not (subtocnode.get('hidden', False) and (not includehidden))):
                        i = (subtocnode.parent.index(subtocnode) + 1)
                        for item in _entries_from_toctree(subtocnode, ([refdoc] + parents), subtree=True):
                            subtocnode.parent.insert(i, item)
                            i += 1
                        subtocnode.parent.remove(subtocnode)
                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)))
    if ((not titles_only) and toctree.get('titlesonly', False)):
        titles_only = True
    if ((not includehidden) and toctree.get('includehidden', False)):
        includehidden = True
    tocentries = _entries_from_toctree(toctree, [], separate=False)
    if (not tocentries):
        return None
    newnode = addnodes.compact_paragraph('', '')
    caption = toctree.attributes.get('caption')
    if caption:
        caption_node = nodes.caption(caption, '', *[nodes.Text(caption)])
        caption_node.line = toctree.line
        caption_node.source = toctree.source
        caption_node.rawsource = toctree['rawcaption']
        if hasattr(toctree, 'uid'):
            caption_node.uid = toctree.uid
            del toctree.uid
        newnode += caption_node
    newnode.extend(tocentries)
    newnode['toctree'] = True
    _toctree_add_classes(newnode, 1)
    self._toctree_prune(newnode, 1, ((prune and maxdepth) or 0), collapse)
    if (len(newnode[(-1)]) == 0):
        return None
    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
