PEP 676: Use `docutils.nodes.Node.findall` (#2363)

This commit is contained in:
Adam Turner 2022-02-25 18:38:24 +00:00 committed by GitHub
parent 99bb6aa4b9
commit 94225885de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 3 deletions

View File

@ -6,6 +6,7 @@ import datetime
import email.utils
from pathlib import Path
import docutils
from docutils import frontend
from docutils import nodes
from docutils import utils
@ -19,6 +20,14 @@ def _format_rfc_2822(dt: datetime.datetime) -> str:
return email.utils.format_datetime(dt, usegmt=True)
# Monkeypatch nodes.Node.findall for forwards compatability
if docutils.__version_info__ < (0, 18):
def findall(self, *args, **kwargs):
return iter(self.traverse(*args, **kwargs))
nodes.Node.findall = findall
entry.formatRFC2822 = feed.formatRFC2822 = _format_rfc_2822
line_cache: dict[Path, dict[str, str]] = {}
@ -62,8 +71,7 @@ def parse_rst(text: str) -> nodes.document:
def pep_abstract(full_path: Path) -> str:
"""Return the first paragraph of the PEP abstract"""
text = full_path.read_text(encoding="utf-8")
# TODO replace .traverse with .findall when Sphinx updates to docutils>=0.18.1
for node in parse_rst(text).traverse(nodes.section):
for node in parse_rst(text).findall(nodes.section):
if node.next_node(nodes.title).astext() == "Abstract":
return node.next_node(nodes.paragraph).astext().strip().replace("\n", " ")
return ""

View File

@ -38,7 +38,7 @@ class FileBuilder(StandaloneHTMLBuilder):
toc_tree = self.env.tocs[docname].deepcopy()
if len(toc_tree[0]) > 1:
toc_tree = toc_tree[0][1] # don't include document title
for node in toc_tree.traverse(nodes.reference):
for node in toc_tree.findall(nodes.reference):
node["refuri"] = node["anchorname"] or '#' # fix targets
toc = self.render_partial(toc_tree)["fragment"]
else: