'Utility: Cut a TOC at a specified depth.'

def _toctree_prune(self, node, depth, maxdepth, collapse=False):

    for subnode in node.children[:]:
        if isinstance(subnode, (addnodes.compact_paragraph, nodes.list_item)):
            self._toctree_prune(subnode, depth, maxdepth, collapse)
        elif isinstance(subnode, nodes.bullet_list):
            if ((maxdepth > 0) and (depth > maxdepth)):
                subnode.parent.replace(subnode, [])
            elif (collapse and (depth > 1) and ('iscurrent' not in subnode.parent)):
                subnode.parent.remove(subnode)
            else:
                self._toctree_prune(subnode, (depth + 1), maxdepth, collapse)
