def _entries_from_toctree(toctreenode, parents, subtree=False):
        refs = [(e[0], e[1]) for e in toctreenode['entries']]
        entries = []
        for (title, ref) in refs:
            refdoc = None
            if url_re.match(ref):
                raise NotImplementedError('Not going to implement this (url)')
            elif ref == 'env':
                raise NotImplementedError('Not going to implement this (env)')
            else:
                if ref in parents:
                    env.warn(
                        ref, 'circular toctree references '
                        'detected, ignoring: %s <- %s' %
                        (ref, ' <- '.join(parents)))
                    continue
                refdoc = ref
                toc = env.tocs[ref].deepcopy()
                env.process_only_nodes(toc, builder, ref)
                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:
                # empty toc means: no titles will show up in the toctree
                env.warn_node(
                    'toctree contains reference to document %r that '
                    'doesn\'t have a title: no link will be generated' % ref,
                    toctreenode)

            # delete everything but the toplevel title(s)
            # and toctrees
            for toplevel in toc:
                # nodes with length 1 don't have any children anyway
                if len(toplevel) > 1:
                    subtrees = toplevel.traverse(addnodes.toctree)
                    toplevel[1][:] = subtrees

            # resolve all sub-toctrees
            for subtocnode in toc.traverse(addnodes.toctree):
                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)

            entries.extend(toc.children)
        if not subtree:
            ret = nodes.bullet_list()
            ret += entries
            return [ret]
        return entries
Exemple #2
0
    def _entries_from_toctree(toctreenode, parents, subtree=False):
        refs = [(e[0], e[1]) for e in toctreenode['entries']]
        entries = []
        for (title, ref) in refs:
            refdoc = None
            if url_re.match(ref):
                raise NotImplementedError('Not going to implement this (url)')
            elif ref == 'env':
                raise NotImplementedError('Not going to implement this (env)')
            else:
                if ref in parents:
                    env.warn(ref, 'circular toctree references '
                             'detected, ignoring: %s <- %s' %
                             (ref, ' <- '.join(parents)))
                    continue
                refdoc = ref
                toc = env.tocs[ref].deepcopy()
                env.process_only_nodes(toc, builder, ref)
                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:
                # empty toc means: no titles will show up in the toctree
                env.warn_node(
                    'toctree contains reference to document %r that '
                    'doesn\'t have a title: no link will be generated'
                    % ref, toctreenode)

            # delete everything but the toplevel title(s)
            # and toctrees
            for toplevel in toc:
                # nodes with length 1 don't have any children anyway
                if len(toplevel) > 1:
                    subtrees = toplevel.traverse(addnodes.toctree)
                    toplevel[1][:] = subtrees

            # resolve all sub-toctrees
            for subtocnode in toc.traverse(addnodes.toctree):
                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)

            entries.extend(toc.children)
        if not subtree:
            ret = nodes.bullet_list()
            ret += entries
            return [ret]
        return entries
def resolve_toctree(env, docname, builder, toctree, collapse=False):
    def _toctree_add_classes(node):
        for subnode in node.children:
            if isinstance(subnode, (addnodes.compact_paragraph,
                                    nodes.list_item, nodes.bullet_list)):
                _toctree_add_classes(subnode)
            elif isinstance(subnode, nodes.reference):
                # for <a>, identify which entries point to the current
                # document and therefore may not be collapsed
                if subnode['refuri'] == docname:
                    list_item = subnode.parent.parent
                    if not subnode['anchorname']:

                        # give the whole branch a 'current' class
                        # (useful for styling it differently)
                        branchnode = subnode
                        while branchnode:
                            branchnode['classes'].append('current')
                            branchnode = branchnode.parent
                    # mark the list_item as "on current page"
                    if subnode.parent.parent.get('iscurrent'):
                        # but only if it's not already done
                        return
                    while subnode:
                        subnode['iscurrent'] = True
                        subnode = subnode.parent

                    # Now mark all siblings as well and also give the
                    # innermost expansion an extra class.
                    list_item['classes'].append('active')
                    for node in list_item.parent.children:
                        node['classes'].append('relevant')

    def _entries_from_toctree(toctreenode, parents, subtree=False):
        refs = [(e[0], e[1]) for e in toctreenode['entries']]
        entries = []
        for (title, ref) in refs:
            refdoc = None
            if url_re.match(ref):
                raise NotImplementedError('Not going to implement this (url)')
            elif ref == 'env':
                raise NotImplementedError('Not going to implement this (env)')
            else:
                if ref in parents:
                    env.warn(
                        ref, 'circular toctree references '
                        'detected, ignoring: %s <- %s' %
                        (ref, ' <- '.join(parents)))
                    continue
                refdoc = ref
                toc = env.tocs[ref].deepcopy()
                env.process_only_nodes(toc, builder, ref)
                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:
                # empty toc means: no titles will show up in the toctree
                env.warn_node(
                    'toctree contains reference to document %r that '
                    'doesn\'t have a title: no link will be generated' % ref,
                    toctreenode)

            # delete everything but the toplevel title(s)
            # and toctrees
            for toplevel in toc:
                # nodes with length 1 don't have any children anyway
                if len(toplevel) > 1:
                    subtrees = toplevel.traverse(addnodes.toctree)
                    toplevel[1][:] = subtrees

            # resolve all sub-toctrees
            for subtocnode in toc.traverse(addnodes.toctree):
                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)

            entries.extend(toc.children)
        if not subtree:
            ret = nodes.bullet_list()
            ret += entries
            return [ret]
        return entries

    tocentries = _entries_from_toctree(toctree, [])
    if not tocentries:
        return None

    newnode = addnodes.compact_paragraph('', '')
    newnode.extend(tocentries)
    newnode['toctree'] = True

    _toctree_add_classes(newnode)

    for refnode in newnode.traverse(nodes.reference):
        if not url_re.match(refnode['refuri']):
            refnode.parent.parent['classes'].append('ref-' + refnode['refuri'])
            refnode['refuri'] = builder.get_relative_uri(
                docname, refnode['refuri']) + refnode['anchorname']

    return newnode
Exemple #4
0
def resolve_toctree(env, docname, builder, toctree, collapse=False):
    def _toctree_add_classes(node):
        for subnode in node.children:
            if isinstance(subnode, (addnodes.compact_paragraph,
                                    nodes.list_item,
                                    nodes.bullet_list)):
                _toctree_add_classes(subnode)
            elif isinstance(subnode, nodes.reference):
                # for <a>, identify which entries point to the current
                # document and therefore may not be collapsed
                if subnode['refuri'] == docname:
                    list_item = subnode.parent.parent
                    if not subnode['anchorname']:

                        # give the whole branch a 'current' class
                        # (useful for styling it differently)
                        branchnode = subnode
                        while branchnode:
                            branchnode['classes'].append('current')
                            branchnode = branchnode.parent
                    # mark the list_item as "on current page"
                    if subnode.parent.parent.get('iscurrent'):
                        # but only if it's not already done
                        return
                    while subnode:
                        subnode['iscurrent'] = True
                        subnode = subnode.parent

                    # Now mark all siblings as well and also give the
                    # innermost expansion an extra class.
                    list_item['classes'].append('active')
                    for node in list_item.parent.children:
                        node['classes'].append('relevant')

    def _entries_from_toctree(toctreenode, parents, subtree=False):
        refs = [(e[0], e[1]) for e in toctreenode['entries']]
        entries = []
        for (title, ref) in refs:
            refdoc = None
            if url_re.match(ref):
                raise NotImplementedError('Not going to implement this (url)')
            elif ref == 'env':
                raise NotImplementedError('Not going to implement this (env)')
            else:
                if ref in parents:
                    env.warn(ref, 'circular toctree references '
                             'detected, ignoring: %s <- %s' %
                             (ref, ' <- '.join(parents)))
                    continue
                refdoc = ref
                toc = env.tocs[ref].deepcopy()
                env.process_only_nodes(toc, builder, ref)
                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:
                # empty toc means: no titles will show up in the toctree
                env.warn_node(
                    'toctree contains reference to document %r that '
                    'doesn\'t have a title: no link will be generated'
                    % ref, toctreenode)

            # delete everything but the toplevel title(s)
            # and toctrees
            for toplevel in toc:
                # nodes with length 1 don't have any children anyway
                if len(toplevel) > 1:
                    subtrees = toplevel.traverse(addnodes.toctree)
                    toplevel[1][:] = subtrees

            # resolve all sub-toctrees
            for subtocnode in toc.traverse(addnodes.toctree):
                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)

            entries.extend(toc.children)
        if not subtree:
            ret = nodes.bullet_list()
            ret += entries
            return [ret]
        return entries

    tocentries = _entries_from_toctree(toctree, [])
    if not tocentries:
        return None

    newnode = addnodes.compact_paragraph('', '')
    newnode.extend(tocentries)
    newnode['toctree'] = True

    _toctree_add_classes(newnode)

    for refnode in newnode.traverse(nodes.reference):
        if not url_re.match(refnode['refuri']):
            refnode.parent.parent['classes'].append('ref-' + refnode['refuri'])
            refnode['refuri'] = builder.get_relative_uri(
                docname, refnode['refuri']) + refnode['anchorname']

    return newnode