Skip to content

Commit

Permalink
PEP 676: Use docutils.nodes.Node.findall (#2363)
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner authored Feb 25, 2022
1 parent 99bb6aa commit 9422588
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
12 changes: 10 additions & 2 deletions generate_rss.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import email.utils
from pathlib import Path

import docutils
from docutils import frontend
from docutils import nodes
from docutils import utils
Expand All @@ -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]] = {}

Expand Down Expand Up @@ -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 ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def get_doc_context(self, docname: str, body: str, _metatags: str) -> dict:
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:
Expand Down

0 comments on commit 9422588

Please sign in to comment.