Skip to content

Commit 6b95f16

Browse files
committed
refactoring caption code
1 parent 7f741f6 commit 6b95f16

File tree

4 files changed

+65
-36
lines changed

4 files changed

+65
-36
lines changed

docs/demo/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ the left sidebar to see how various elements look on this theme.
1616
lists_tables
1717
markdown
1818
example_pandas
19+
index2
1920

2021
.. toctree::
2122
:maxdepth: 3

docs/demo/index2.rst

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
A second index
2+
==============
3+
4+
An index to show off level nesting.
5+
6+
.. toctree::
7+
8+
page2

docs/demo/page2.rst

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
A second page
2+
=============
3+
4+
A demo page to see how levels nest within each other in the theme.

pydata_sphinx_theme/__init__.py

+52-36
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,27 @@
2020

2121
class MyTocTree(TocTree):
2222
def get_toctree_for_subpage(
23-
self, pagename, builder, collapse=True, maxdepth=-1, **kwargs
23+
self, toctree_page, current_page, builder, collapse=True, maxdepth=-1, **kwargs
2424
):
2525
"""Return the global TOC nodetree."""
26-
if pagename in ["genindex", "search"]:
26+
if current_page in ["genindex", "search"]:
2727
return
28-
doctree = self.env.get_doctree(pagename)
29-
toctrees = [] # type: List[Element]
30-
if "includehidden" not in kwargs:
31-
kwargs["includehidden"] = True
32-
if "maxdepth" not in kwargs:
33-
kwargs["maxdepth"] = 0
34-
kwargs["collapse"] = collapse
28+
doctree = self.env.get_doctree(
29+
toctree_page
30+
) # current_page should be the "first active page"
31+
toctrees = []
3532
for toctreenode in doctree.traverse(addnodes.toctree):
36-
toctree = self.resolve(pagename, builder, toctreenode, prune=True, **kwargs)
33+
toctree = self.resolve(
34+
current_page, builder, toctreenode, prune=True, **kwargs
35+
)
3736
if toctree:
3837
toctrees.append(toctree)
3938
if not toctrees:
4039
return None
41-
result = toctrees[0]
40+
toctree_subpage = toctrees[0]
4241
for toctree in toctrees[1:]:
43-
result.extend(toctree.children)
44-
return result
42+
toctree_subpage.extend(toctree.children)
43+
return toctree_subpage
4544

4645

4746
def add_toctree_functions(app, pagename, templatename, context, doctree):
@@ -77,31 +76,48 @@ def get_nav_object(maxdepth=None, collapse=True, subpage_caption=False, **kwargs
7776
return []
7877

7978
if subpage_caption:
80-
if pagename not in [app.env.config.master_doc, "genindex", "search"]:
81-
def is_first_active_page(node):
82-
return isinstance(node, nodes.bullet_list) and node.attributes.get("iscurrent")
83-
84-
active_first_page = list(toctree.traverse(is_first_active_page))[0]
85-
# A path to the active TOC item's first page, relative to the current page
86-
first_page_path = list(active_first_page.traverse(nodes.reference))[0].attributes.get("refuri")
87-
if first_page_path == "":
88-
# First TOC item's first page *is* the active page
89-
first_page_path = Path(pagename).name
90-
else:
91-
first_page_path = Path(first_page_path).with_suffix("")
92-
rel_first_page_path = str(Path(pagename).parent.joinpath(first_page_path))
93-
94-
# We only wish to show a single page's descendants, so we'll keep their captions
95-
subpage_toctree = toc.get_toctree_for_subpage(
96-
rel_first_page_path, app.builder, collapse=collapse, maxdepth=maxdepth, **kwargs
79+
80+
def is_level1_page(node):
81+
return isinstance(
82+
node, nodes.list_item
83+
) and "toctree-l1" in node.attributes.get("classes")
84+
85+
# Grab all of the first-level pages from the index toctree
86+
toctree_root = toc.get_toctree_for(
87+
app.env.config.master_doc,
88+
app.builder,
89+
collapse=collapse,
90+
maxdepth=maxdepth,
91+
**kwargs,
92+
)
93+
first_pages = toctree_root.traverse(is_level1_page)
94+
# For each page in the top toctree, access the page-specific toctree
95+
for first_page in first_pages:
96+
# First reference will be for the top page
97+
first_page_ref = list(first_page.traverse(nodes.reference))[0]
98+
first_page_path = first_page_ref.attributes.get("refuri").rsplit(
99+
".html", 1
100+
)[0]
101+
102+
# Grab the toctree for this page, with links relative from current page
103+
first_page_toctree = toc.get_toctree_for_subpage(
104+
first_page_path,
105+
pagename,
106+
app.builder,
107+
collapse=collapse,
108+
maxdepth=maxdepth - 1,
109+
**kwargs,
97110
)
98-
if subpage_toctree is not None:
111+
112+
if first_page_toctree is not None:
99113
# Find the current page in the top-level children
100-
for item in toctree.children:
101-
if isinstance(item, nodes.bullet_list) and item.attributes.get("iscurrent", []):
102-
# Append that pages' toctree so we get captions
103-
subpage_list = item.children[0]
104-
subpage_list.children = [subpage_list.children[0]] + subpage_toctree.children
114+
for page in toctree.traverse(is_level1_page):
115+
toc_page_ref = list(page.traverse(nodes.reference))[0]
116+
page_uri = toc_page_ref.attributes.get("refuri").rsplit(
117+
".html", 1
118+
)[0]
119+
if page_uri == first_page_path:
120+
page.children = first_page_toctree.children
105121

106122
# toctree has this structure
107123
# <caption>

0 commit comments

Comments
 (0)