Skip to content

Commit

Permalink
fix: sphinx 7
Browse files Browse the repository at this point in the history
  • Loading branch information
agoose77 committed Mar 22, 2024
1 parent 2eb2317 commit 6908845
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
8 changes: 7 additions & 1 deletion sphinx_remove_toctrees/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
logger = logging.getLogger(__name__)


def findall(node):
# findall replaces traverse in docutils v0.18
# note a difference is that findall is an iterator
return getattr(node, "findall", node.traverse)


def remove_toctrees(app, env):
"""Remove toctrees from pages a user provides.
Expand Down Expand Up @@ -37,7 +43,7 @@ def remove_toctrees(app, env):

# Loop through all tocs and remove the ones that match our pattern
for _, tocs in env.tocs.items():
for toctree in tocs.traverse(addnodes.toctree):
for toctree in findall(tocs)(addnodes.toctree):
new_entries = []
for entry in toctree.attributes.get("entries", []):
if entry[1] not in to_remove:
Expand Down
18 changes: 14 additions & 4 deletions sphinx_remove_toctrees/tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
from shutil import copytree

from bs4 import BeautifulSoup
from sphinx.testing.path import path as sphinx_path
from sphinx.testing.util import SphinxTestApp
from sphinx import version_info as sphinx_version_info

pytest_plugins = "sphinx.testing.fixtures"

Expand All @@ -13,8 +12,19 @@

def test_build_html(make_app, tmp_path):
"""Test building the base html template and config."""
copytree(path_test_doc, tmp_path / "test_doc")
app = make_app(srcdir=sphinx_path(tmp_path / "test_doc"))
src_dir = tmp_path / "test_doc"
copytree(path_test_doc, src_dir)

# For compatibility with multiple versions of sphinx, convert pathlib.Path to
# sphinx.testing.path.path here.
if sphinx_version_info >= (7, 2):
app_src_dir = src_dir
else:
from sphinx.testing.path import path

app_src_dir = path(os.fspath(src_dir))

app = make_app(srcdir=app_src_dir)
app.build()
index = tmp_path / "test_doc" / "_build" / "html" / "index.html"
assert index.exists()
Expand Down

0 comments on commit 6908845

Please sign in to comment.