Skip to content

Commit 7ded70a

Browse files
committed
Make toctree accept 'genindex', 'modindex' and 'search' docnames
Fixes #8438
1 parent b2fe07e commit 7ded70a

File tree

7 files changed

+56
-2
lines changed

7 files changed

+56
-2
lines changed

sphinx/directives/other.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ def run(self) -> List[Node]:
7777
return ret
7878

7979
def parse_content(self, toctree: addnodes.toctree) -> List[Node]:
80+
generated_documents = {'genindex', 'modindex', 'search'}
81+
referencable_docs = self.env.found_docs | generated_documents
8082
suffixes = self.config.source_suffix
8183

8284
# glob target documents
@@ -118,7 +120,7 @@ def parse_content(self, toctree: addnodes.toctree) -> List[Node]:
118120
docname = docname_join(self.env.docname, docname)
119121
if url_re.match(ref) or ref == 'self':
120122
toctree['entries'].append((title, ref))
121-
elif docname not in self.env.found_docs:
123+
elif docname not in referencable_docs:
122124
if excluded(self.env.doc2path(docname, False)):
123125
message = __('toctree contains reference to excluded document %r')
124126
subtype = 'excluded'
@@ -132,6 +134,8 @@ def parse_content(self, toctree: addnodes.toctree) -> List[Node]:
132134
else:
133135
if docname in all_docnames:
134136
all_docnames.remove(docname)
137+
elif docname in generated_documents:
138+
pass
135139
else:
136140
logger.warning(__('duplicated entry found in toctree: %s'), docname,
137141
location=toctree)

sphinx/environment/adapters/toctree.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,18 @@ def _entries_from_toctree(toctreenode: addnodes.toctree, parents: List[str],
139139
item = nodes.list_item('', para)
140140
# don't show subitems
141141
toc = nodes.bullet_list('', item)
142+
elif ref in ('genindex', 'modindex', 'search'):
143+
docname, _, sectionname = self.env.domains['std'].labels[ref]
144+
if not title:
145+
title = sectionname
146+
reference = nodes.reference('', '', internal=True,
147+
refuri=docname,
148+
anchorname='',
149+
*[nodes.Text(title)])
150+
para = addnodes.compact_paragraph('', '', reference)
151+
item = nodes.list_item('', para)
152+
# don't show subitems
153+
toc = nodes.bullet_list('', item)
142154
else:
143155
if ref in parents:
144156
logger.warning(__('circular toctree references '

sphinx/environment/collectors/toctree.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,8 @@ def _walk_doctree(docname: str, doctree: Element, secnum: Tuple[int, ...]) -> No
254254
_walk_doctree(docname, subnode, secnum)
255255
elif isinstance(subnode, addnodes.toctree):
256256
for _title, subdocname in subnode['entries']:
257-
if url_re.match(subdocname) or subdocname == 'self':
257+
if (url_re.match(subdocname) or
258+
subdocname in ('self', 'genindex', 'modindex', 'search')):
258259
# don't mess with those
259260
continue
260261

tests/roots/test-toctree-index/conf.py

Whitespace-only changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
foo
2+
===
3+
4+
:index:`word`
5+
6+
.. py:module:: pymodule
7+
8+
.. py:function:: Timer.repeat(repeat=3, number=1000000)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
test-toctree-index
2+
==================
3+
4+
.. toctree::
5+
6+
foo
7+
8+
9+
.. toctree::
10+
:caption: Indices
11+
12+
genindex
13+
modindex
14+
search
15+

tests/test_environment_toctree.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,3 +346,17 @@ def test_get_toctree_for_includehidden(app):
346346

347347
assert_node(toctree[2],
348348
[bullet_list, list_item, compact_paragraph, reference, "baz"])
349+
350+
351+
@pytest.mark.sphinx('xml', testroot='toctree-index')
352+
def test_toctree_index(app):
353+
app.build()
354+
toctree = app.env.tocs['index']
355+
assert_node(toctree,
356+
[bullet_list, ([list_item, (compact_paragraph, # [0][0]
357+
[bullet_list, (addnodes.toctree, # [0][1][0]
358+
addnodes.toctree)])])]) # [0][1][1]
359+
assert_node(toctree[0][1][1], addnodes.toctree,
360+
caption="Indices", glob=False, hidden=False,
361+
titlesonly=False, maxdepth=-1, numbered=0,
362+
entries=[(None, 'genindex'), (None, 'modindex'), (None, 'search')])

0 commit comments

Comments
 (0)