|
1 | 1 | """
|
2 | 2 | Sphinx event handler implementation.
|
3 | 3 | """
|
4 |
| - |
5 |
| -from typing import Any, Final, Optional |
| 4 | +from typing import Any, Final, Optional, Iterator, cast |
6 | 5 |
|
7 | 6 | from docutils import nodes
|
8 | 7 | from sphinx import addnodes
|
@@ -74,20 +73,42 @@ def doctree_read(app: Sphinx, doctree: nodes.document):
|
74 | 73 | tab_docnames: list[str] = getattr(app.env, INLINE_TAB_DOCNAMES)
|
75 | 74 | if app.env.docname in tab_docnames:
|
76 | 75 | 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 |
77 | 88 |
|
78 | 89 | # Generate the tab-based TOC (includes headings from within tabs)
|
79 | 90 | updated_tocs: nodes.list_item = sectiondata_to_toc(
|
80 | 91 | app.env.docname,
|
81 | 92 | collect_sections(app.env, doctree, app.env.docname, doctree),
|
82 | 93 | )
|
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) |
84 | 106 | 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}" |
86 | 108 | )
|
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 |
91 | 112 |
|
92 | 113 |
|
93 | 114 | def html_page_context(
|
@@ -328,11 +349,12 @@ def sectiondata_to_toc(docname: str, secdata: SectionData) -> nodes.list_item:
|
328 | 349 | # Add a reference for this section (including tabs and regular sections)
|
329 | 350 | # Only skip adding reference if this is a section root with no meaningful name
|
330 | 351 | 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 |
331 | 353 | logger.debug(
|
332 | 354 | 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}" |
334 | 356 | )
|
335 |
| - list_item.append(make_compact_reference(secdata.id, secdata.name, docname)) |
| 357 | + list_item.append(make_compact_reference(section_id, secdata.name, docname)) |
336 | 358 |
|
337 | 359 | if len(secdata.children) > 0:
|
338 | 360 | logger.debug(
|
@@ -369,7 +391,7 @@ def make_compact_reference(
|
369 | 391 | "",
|
370 | 392 | section_name,
|
371 | 393 | refuri=docname,
|
372 |
| - anchorname=f"#{section_id}", |
| 394 | + anchorname=f"#{section_id}" if section_id else "", |
373 | 395 | internal=True,
|
374 | 396 | )
|
375 | 397 | )
|
|
0 commit comments