def doctree_read(app, doctree):
    secnums = app.builder.env.toc_secnumbers
    for node in doctree.traverse(subfigstart):
        parentloc = node.parent.children.index(node)
        
        subfigendloc = parentloc
        while subfigendloc < len(node.parent.children):
            n = node.parent.children[subfigendloc]
            if isinstance(n, subfigend):
                break
            subfigendloc += 1
        
        if subfigendloc == len(node.parent.children):
            return
        
        between_nodes = node.parent.children[parentloc:subfigendloc]
        subfigend_node = node.parent.children[subfigendloc]
        node['subfigend'] = subfigend_node
        for i, n in enumerate(between_nodes):
            if isinstance(n, nodes.figure):
                children = [n]
                prevnode = between_nodes[i-1]
                if isinstance(prevnode, nodes.target):
                    node.parent.children.remove(prevnode)
                    children.insert(0, prevnode)
                nodeloc = node.parent.children.index(n)
                node.parent.children[nodeloc] = subfig('', *children)
                node.parent.children[nodeloc]['width'] = subfigend_node['width']
                node.parent.children[nodeloc]['mainfigid'] = subfigend_node['ids'][0]
dataset/ETHPy150Open xraypy/xraylarch/doc/sphinx/ext/subfig.py/doctree_read/original