From a9909c615a38202355a5dac1d5b9163bdf90ce01 Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Sat, 12 Aug 2023 07:00:25 +0100 Subject: [PATCH 1/5] Fix TocTree manipulation on Sphinx 7.2+ --- src/pydata_sphinx_theme/toctree.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/pydata_sphinx_theme/toctree.py b/src/pydata_sphinx_theme/toctree.py index c1b8ff770..3a0d13ce4 100644 --- a/src/pydata_sphinx_theme/toctree.py +++ b/src/pydata_sphinx_theme/toctree.py @@ -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 from .utils import traverse_or_findall @@ -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)] + 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] @@ -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: From 6b2c4cb2e287dc5312b42f6487bf4ff8b1b0d02f Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Mon, 14 Aug 2023 15:59:01 +0100 Subject: [PATCH 2/5] guard import --- src/pydata_sphinx_theme/toctree.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/pydata_sphinx_theme/toctree.py b/src/pydata_sphinx_theme/toctree.py index 3a0d13ce4..b9f79fecd 100644 --- a/src/pydata_sphinx_theme/toctree.py +++ b/src/pydata_sphinx_theme/toctree.py @@ -11,7 +11,7 @@ from sphinx import addnodes from sphinx.addnodes import toctree as toctree_node from sphinx.application import Sphinx -from sphinx.environment.adapters.toctree import _get_toctree_ancestors, TocTree +from sphinx.environment.adapters.toctree import TocTree from .utils import traverse_or_findall @@ -422,6 +422,8 @@ def index_toctree( toctree = TocTree(app.env) if sphinx.version_info[:2] >= (7, 2): + from sphinx.environment.adapters.toctree import _get_toctree_ancestors + ancestors = [*_get_toctree_ancestors(app.env.toctree_includes, pagename)] else: ancestors = toctree.get_toctree_ancestors(pagename) From 33ce157d6fab3bdcef2b57424bc2d7eb18e9532f Mon Sep 17 00:00:00 2001 From: Daniel McCloy Date: Mon, 14 Aug 2023 10:04:15 -0500 Subject: [PATCH 3/5] nested import needed in both funcs --- src/pydata_sphinx_theme/toctree.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/pydata_sphinx_theme/toctree.py b/src/pydata_sphinx_theme/toctree.py index b9f79fecd..64b7bc111 100644 --- a/src/pydata_sphinx_theme/toctree.py +++ b/src/pydata_sphinx_theme/toctree.py @@ -65,6 +65,8 @@ 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) if sphinx.version_info[:2] >= (7, 2): + from sphinx.environment.adapters.toctree import _get_toctree_ancestors + active_header_page = [*_get_toctree_ancestors(app.env.toctree_includes, pagename)] else: active_header_page = toctree.get_toctree_ancestors(pagename) From 9a11328ddd9440a9eed50b86b059c921bbeb4378 Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Mon, 14 Aug 2023 16:06:25 +0100 Subject: [PATCH 4/5] lint --- src/pydata_sphinx_theme/toctree.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/pydata_sphinx_theme/toctree.py b/src/pydata_sphinx_theme/toctree.py index 64b7bc111..6f2807e1c 100644 --- a/src/pydata_sphinx_theme/toctree.py +++ b/src/pydata_sphinx_theme/toctree.py @@ -67,7 +67,9 @@ def generate_header_nav_html(n_links_before_dropdown: int = 5) -> str: if sphinx.version_info[:2] >= (7, 2): from sphinx.environment.adapters.toctree import _get_toctree_ancestors - active_header_page = [*_get_toctree_ancestors(app.env.toctree_includes, pagename)] + active_header_page = [ + *_get_toctree_ancestors(app.env.toctree_includes, pagename) + ] else: active_header_page = toctree.get_toctree_ancestors(pagename) if active_header_page: From 68e1c40d2b6f5ae863211782b1831e72a5f9359f Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Mon, 14 Aug 2023 16:07:31 +0100 Subject: [PATCH 5/5] whitespace --- src/pydata_sphinx_theme/toctree.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pydata_sphinx_theme/toctree.py b/src/pydata_sphinx_theme/toctree.py index 6f2807e1c..1d35e2a0c 100644 --- a/src/pydata_sphinx_theme/toctree.py +++ b/src/pydata_sphinx_theme/toctree.py @@ -66,7 +66,7 @@ def generate_header_nav_html(n_links_before_dropdown: int = 5) -> str: # Will be empty if there is no active page (root_doc, or genindex etc) if sphinx.version_info[:2] >= (7, 2): from sphinx.environment.adapters.toctree import _get_toctree_ancestors - + active_header_page = [ *_get_toctree_ancestors(app.env.toctree_includes, pagename) ]