Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAINT: use findall instead of traverse #820

Merged
merged 2 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/sphinx_book_theme/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
)
from .header_buttons.launch import add_launch_buttons
from .header_buttons.source import add_source_buttons
from ._compat import findall
from ._transforms import HandleFootnoteTransform

__version__ = "1.1.2"
Expand Down Expand Up @@ -53,7 +54,7 @@ def add_metadata_to_page(app, pagename, templatename, context, doctree):
# Add a shortened page text to the context using the sections text
if doctree:
description = ""
for section in doctree.traverse(docutil_nodes.section):
for section in findall(doctree, docutil_nodes.section):
description += section.astext().replace("\n", " ")
description = description[:160]
context["page_description"] = description
Expand Down
9 changes: 9 additions & 0 deletions src/sphinx_book_theme/_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from docutils.nodes import Element
from typing import Iterator


def findall(node: Element, *args, **kwargs) -> Iterator[Element]:
# findall replaces traverse in docutils v0.18
# note a difference is that findall is an iterator
impl = getattr(node, "findall", node.traverse)
return iter(impl(*args, **kwargs))
5 changes: 3 additions & 2 deletions src/sphinx_book_theme/_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from sphinx import addnodes as sphinx_nodes
from pydata_sphinx_theme.utils import get_theme_options_dict
from .nodes import SideNoteNode
from ._compat import findall


class HandleFootnoteTransform(SphinxPostTransform):
Expand All @@ -19,10 +20,10 @@ def run(self, **kwargs: Any) -> None:
# Cycle through footnote references, and move their content next to the
# reference. This lets us display the reference in the margin,
# or just below on narrow screens.
for ref_node in self.document.traverse(docutil_nodes.footnote_reference):
for ref_node in findall(self.document, docutil_nodes.footnote_reference):
parent = None
# Each footnote reference should have a single node it points to via `ids`
for foot_node in self.document.traverse(docutil_nodes.footnote):
for foot_node in findall(self.document, docutil_nodes.footnote):
# matching the footnote reference with footnote
if (
len(foot_node.attributes["backrefs"])
Expand Down
Loading