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

Fix TocTree manipulation on Sphinx 7.2+ #1406

Merged
merged 5 commits into from
Aug 14, 2023
Merged
Changes from 1 commit
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
13 changes: 10 additions & 3 deletions src/pydata_sphinx_theme/toctree.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
from typing import List, Union
from urllib.parse import urlparse

import sphinx
from bs4 import BeautifulSoup
from docutils import nodes
from docutils.nodes import Node
from sphinx import addnodes
from sphinx.addnodes import toctree as toctree_node
from sphinx.application import Sphinx
from sphinx.environment.adapters.toctree import TocTree
from sphinx.environment.adapters.toctree import _get_toctree_ancestors, TocTree
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you'll need to version-guard this import too:

Running Sphinx v5.3.0

Traceback (most recent call last):
  File "/home/docs/checkouts/readthedocs.org/user_builds/pydata-sphinx-theme/envs/1406/lib/python3.9/site-packages/sphinx/config.py", line 350, in eval_config_file
    exec(code, namespace)
  File "/home/docs/checkouts/readthedocs.org/user_builds/pydata-sphinx-theme/checkouts/1406/docs/conf.py", line 14, in <module>
    import pydata_sphinx_theme
  File "/home/docs/checkouts/readthedocs.org/user_builds/pydata-sphinx-theme/envs/1406/lib/python3.9/site-packages/pydata_sphinx_theme/__init__.py", line 15, in <module>
    from . import edit_this_page, logo, pygment, short_link, toctree, translator, utils
  File "/home/docs/checkouts/readthedocs.org/user_builds/pydata-sphinx-theme/envs/1406/lib/python3.9/site-packages/pydata_sphinx_theme/toctree.py", line 14, in <module>
    from sphinx.environment.adapters.toctree import _get_toctree_ancestors, TocTree
ImportError: cannot import name '_get_toctree_ancestors' from 'sphinx.environment.adapters.toctree' (/home/docs/checkouts/readthedocs.org/user_builds/pydata-sphinx-theme/envs/1406/lib/python3.9/site-packages/sphinx/environment/adapters/toctree.py)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, of course!


from .utils import traverse_or_findall

Expand Down Expand Up @@ -63,7 +64,10 @@ def generate_header_nav_html(n_links_before_dropdown: int = 5) -> str:

# Find the active header navigation item so we decide whether to highlight
# Will be empty if there is no active page (root_doc, or genindex etc)
active_header_page = toctree.get_toctree_ancestors(pagename)
if sphinx.version_info[:2] >= (7, 2):
active_header_page = [*_get_toctree_ancestors(app.env.toctree_includes, pagename)]
drammock marked this conversation as resolved.
Show resolved Hide resolved
drammock marked this conversation as resolved.
Show resolved Hide resolved
else:
active_header_page = toctree.get_toctree_ancestors(pagename)
if active_header_page:
# The final list item will be the top-most ancestor
active_header_page = active_header_page[-1]
Expand Down Expand Up @@ -417,7 +421,10 @@ def index_toctree(
kwargs.pop("maxdepth")

toctree = TocTree(app.env)
ancestors = toctree.get_toctree_ancestors(pagename)
if sphinx.version_info[:2] >= (7, 2):
ancestors = [*_get_toctree_ancestors(app.env.toctree_includes, pagename)]
else:
ancestors = toctree.get_toctree_ancestors(pagename)
try:
indexname = ancestors[-startdepth]
except IndexError:
Expand Down
Loading