Skip to content

Commit 77c9950

Browse files
authored
Merge branch 'master' into admin-guide-nav-restructure
2 parents 991478c + 245bbc4 commit 77c9950

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

extensions/sphinx_inline_tabs/events.py

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
"""
22
Sphinx event handler implementation.
33
"""
4-
5-
from typing import Any, Final, Optional
4+
from typing import Any, Final, Optional, Iterator, cast
65

76
from docutils import nodes
87
from sphinx import addnodes
@@ -74,20 +73,42 @@ def doctree_read(app: Sphinx, doctree: nodes.document):
7473
tab_docnames: list[str] = getattr(app.env, INLINE_TAB_DOCNAMES)
7574
if app.env.docname in tab_docnames:
7675
logger.debug(f"{LOG_PREFIX} doctree_read: {app.env.docname} has tabs")
76+
logger.debug(
77+
f"{LOG_PREFIX} doctree_read({app.env.docname}): app.env.tocs[app.env.docname]={app.env.tocs[app.env.docname]}"
78+
)
79+
80+
# check if this toc has a toctree node. if so, get a reference to it
81+
toctree_nodes: Iterator[addnodes.toctree] = app.env.tocs[app.env.docname].findall(addnodes.toctree)
82+
toctree_node: Optional[addnodes.toctree] = None
83+
for node in toctree_nodes:
84+
if node is not None and toctree_node is None:
85+
toctree_node = node
86+
logger.debug(f"{LOG_PREFIX} doctree_read: {app.env.docname} has a toctree node: {node}")
87+
break
7788

7889
# Generate the tab-based TOC (includes headings from within tabs)
7990
updated_tocs: nodes.list_item = sectiondata_to_toc(
8091
app.env.docname,
8192
collect_sections(app.env, doctree, app.env.docname, doctree),
8293
)
83-
94+
95+
# if there was a toctree node, insert it at the start of the updated toc nodes
96+
if toctree_node is not None:
97+
toctree_bullet_list: nodes.bullet_list = nodes.bullet_list()
98+
toctree_bullet_list.append(toctree_node)
99+
for child_list in cast("nodes.Element", updated_tocs[1]).children:
100+
toctree_bullet_list.append(child_list)
101+
updated_tocs[1] = toctree_bullet_list
102+
103+
# ensure the new toctree is a child of a bullet list
104+
tocs_bullet_list: nodes.bullet_list = nodes.bullet_list()
105+
tocs_bullet_list.append(updated_tocs)
84106
logger.debug(
85-
f"{LOG_PREFIX} doctree_read({app.env.docname}): updated_tocs[0][1]={updated_tocs}"
107+
f"{LOG_PREFIX} doctree_read({app.env.docname}): tocs_bullet_list={tocs_bullet_list}"
86108
)
87-
if len(app.env.tocs[app.env.docname][0]) == 1:
88-
app.env.tocs[app.env.docname][0].append(updated_tocs)
89-
else:
90-
app.env.tocs[app.env.docname][0][1] = updated_tocs
109+
110+
# replace the document's toc with the one we just generated
111+
app.env.tocs[app.env.docname] = tocs_bullet_list
91112

92113

93114
def html_page_context(
@@ -328,11 +349,12 @@ def sectiondata_to_toc(docname: str, secdata: SectionData) -> nodes.list_item:
328349
# Add a reference for this section (including tabs and regular sections)
329350
# Only skip adding reference if this is a section root with no meaningful name
330351
if secdata.name and not (secdata.is_section_root and secdata.name == secdata.id):
352+
section_id: str = "" if secdata.level == 1 and secdata.tab_counter == 0 else secdata.id
331353
logger.debug(
332354
f"{LOG_PREFIX} sectiondata_to_toc(): "
333-
f"({docname}): [{secdata.level}/{secdata.tab_counter}] add reference for {secdata.name}<{secdata.id}>"
355+
f"({docname}): [{secdata.level}/{secdata.tab_counter}] add reference for {secdata.name}<{secdata.id}>: {section_id}"
334356
)
335-
list_item.append(make_compact_reference(secdata.id, secdata.name, docname))
357+
list_item.append(make_compact_reference(section_id, secdata.name, docname))
336358

337359
if len(secdata.children) > 0:
338360
logger.debug(
@@ -369,7 +391,7 @@ def make_compact_reference(
369391
"",
370392
section_name,
371393
refuri=docname,
372-
anchorname=f"#{section_id}",
394+
anchorname=f"#{section_id}" if section_id else "",
373395
internal=True,
374396
)
375397
)

0 commit comments

Comments
 (0)