diff options
Diffstat (limited to 'sphinx/util/nodes.py')
-rw-r--r-- | sphinx/util/nodes.py | 23 |
1 files changed, 12 insertions, 11 deletions
diff --git a/sphinx/util/nodes.py b/sphinx/util/nodes.py index b6542f1d3..c16142a2c 100644 --- a/sphinx/util/nodes.py +++ b/sphinx/util/nodes.py @@ -38,7 +38,7 @@ caption_ref_re = explicit_title_re # b/w compat alias class NodeMatcher: - """A helper class for Node.traverse(). + """A helper class for Node.findall(). It checks that the given node is an instance of the specified node-classes and has the specified node-attributes. @@ -47,7 +47,7 @@ class NodeMatcher: and ``reftype`` attributes:: matcher = NodeMatcher(nodes.reference, refdomain='std', reftype='citation') - doctree.traverse(matcher) + doctree.findall(matcher) # => [<reference ...>, <reference ...>, ...] A special value ``typing.Any`` matches any kind of node-attributes. For example, @@ -55,7 +55,7 @@ class NodeMatcher: from typing import Any matcher = NodeMatcher(nodes.reference, refdomain=Any) - doctree.traverse(matcher) + doctree.findall(matcher) # => [<reference ...>, <reference ...>, ...] """ @@ -147,7 +147,7 @@ def apply_source_workaround(node: Element) -> None: logger.debug('[i18n] PATCH: %r to have rawsource: %s', get_full_module_name(node), repr_domxml(node)) # strip classifier from rawsource of term - for classifier in reversed(list(node.parent.traverse(nodes.classifier))): + for classifier in reversed(list(node.parent.findall(nodes.classifier))): node.rawsource = re.sub(r'\s*:\s*%s' % re.escape(classifier.astext()), '', node.rawsource) if isinstance(node, nodes.topic) and node.source is None: @@ -259,7 +259,7 @@ META_TYPE_NODES = ( def extract_messages(doctree: Element) -> Iterable[Tuple[Element, str]]: """Extract translatable messages from a document tree.""" - for node in doctree.traverse(is_translatable): # type: Element + for node in doctree.findall(is_translatable): # type: Element if isinstance(node, addnodes.translatable): for msg in node.extract_original_messages(): yield node, msg @@ -323,7 +323,8 @@ def get_prev_node(node: Node) -> Optional[Node]: def traverse_translatable_index(doctree: Element) -> Iterable[Tuple[Element, List["IndexEntry"]]]: # NOQA """Traverse translatable index node from a document tree.""" - for node in doctree.traverse(NodeMatcher(addnodes.index, inline=False)): # type: addnodes.index # NOQA + matcher = NodeMatcher(addnodes.index, inline=False) + for node in doctree.findall(matcher): # type: addnodes.index if 'raw_entries' in node: entries = node['raw_entries'] else: @@ -353,9 +354,9 @@ def nested_parse_with_titles(state: Any, content: StringList, node: Node) -> str def clean_astext(node: Element) -> str: """Like node.astext(), but ignore images.""" node = node.deepcopy() - for img in node.traverse(nodes.image): + for img in node.findall(nodes.image): img['alt'] = '' - for raw in list(node.traverse(nodes.raw)): + for raw in list(node.findall(nodes.raw)): raw.parent.remove(raw) return node.astext() @@ -420,7 +421,7 @@ def inline_all_toctrees(builder: "Builder", docnameset: Set[str], docname: str, Record all docnames in *docnameset*, and output docnames with *colorfunc*. """ tree = cast(nodes.document, tree.deepcopy()) - for toctreenode in list(tree.traverse(addnodes.toctree)): + for toctreenode in list(tree.findall(addnodes.toctree)): newnodes = [] includefiles = map(str, toctreenode['includefiles']) for includefile in includefiles: @@ -438,7 +439,7 @@ def inline_all_toctrees(builder: "Builder", docnameset: Set[str], docname: str, else: sof = addnodes.start_of_file(docname=includefile) sof.children = subtree.children - for sectionnode in sof.traverse(nodes.section): + for sectionnode in sof.findall(nodes.section): if 'docname' not in sectionnode: sectionnode['docname'] = includefile newnodes.append(sof) @@ -615,7 +616,7 @@ def is_smartquotable(node: Node) -> bool: def process_only_nodes(document: Node, tags: "Tags") -> None: """Filter ``only`` nodes which do not match *tags*.""" - for node in document.traverse(addnodes.only): + for node in document.findall(addnodes.only): try: ret = tags.eval_condition(node['expr']) except Exception as err: |