Skip to content

Commit edbfdc4

Browse files
author
Nathaniel Wesley Filardo
committed
Allow explicit markup in toctree directives
This adds support for :ref:`` text in toctree listings in particular, but is reasonably generic. My website, for example, now contains something like .. toctree:: _ :ref:`foo <bar>` baz which renders as one might expect.
1 parent de070a3 commit edbfdc4

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

sphinx/directives/other.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from docutils.parsers.rst.directives.admonitions import BaseAdmonition
1414
from docutils.parsers.rst.directives.misc import Class
1515
from docutils.parsers.rst.directives.misc import Include as BaseInclude
16+
from docutils.statemachine import ViewList
1617

1718
from sphinx import addnodes
1819
from sphinx.locale import versionlabels, _
@@ -57,8 +58,14 @@ def run(self):
5758
self.options.setdefault('name', nodes.fully_normalize_name(caption))
5859

5960
ret = []
61+
# Children of the internal toctree node; these will be rewritten by
62+
# traversals (and so having other references into these will also
63+
# get rewritten) but, nicely, are not rendered directly due to the
64+
# way that the environment code deals with toctrees.
65+
others = []
6066
# (title, ref) pairs, where ref may be a document, or an external link,
61-
# and title may be None if the document's title is to be used
67+
# or a node. title may be None if the document's title is to be used
68+
# and must be None if a node is given as a ref.
6269
entries = []
6370
includefiles = []
6471
all_docnames = env.found_docs.copy()
@@ -67,7 +74,12 @@ def run(self):
6774
for entry in self.content:
6875
if not entry:
6976
continue
70-
if glob and ('*' in entry or '?' in entry or '[' in entry):
77+
if entry.startswith("_ "):
78+
node = nodes.paragraph()
79+
self.state.nested_parse(ViewList([entry[2:]]), 0, node)
80+
others.append(node)
81+
entries.append((None, node))
82+
elif glob and ('*' in entry or '?' in entry or '[' in entry):
7183
patname = docname_join(env.docname, entry)
7284
docnames = sorted(patfilter(all_docnames, patname))
7385
for docname in docnames:
@@ -119,6 +131,7 @@ def run(self):
119131
subnode['includehidden'] = 'includehidden' in self.options
120132
subnode['numbered'] = self.options.get('numbered', 0)
121133
subnode['titlesonly'] = 'titlesonly' in self.options
134+
subnode.children = others
122135
set_source_info(self, subnode)
123136
wrappernode = nodes.compound(classes=['toctree-wrapper'])
124137
wrappernode.append(subnode)

sphinx/environment.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1376,7 +1376,14 @@ def _entries_from_toctree(toctreenode, parents,
13761376
for (title, ref) in refs:
13771377
try:
13781378
refdoc = None
1379-
if url_re.match(ref):
1379+
if isinstance(ref, nodes.Node):
1380+
# Strip off the outer paragraph and stuff its
1381+
# children into a compact one here.
1382+
para = addnodes.compact_paragraph('', '')
1383+
para.children = ref.children[0].children
1384+
item = nodes.list_item('', para)
1385+
toc = nodes.bullet_list('', item)
1386+
elif url_re.match(ref):
13801387
if title is None:
13811388
title = ref
13821389
reference = nodes.reference('', '', internal=False,
@@ -1487,6 +1494,9 @@ def _entries_from_toctree(toctreenode, parents,
14871494
# set the target paths in the toctrees (they are not known at TOC
14881495
# generation time)
14891496
for refnode in newnode.traverse(nodes.reference):
1497+
if 'anchorname' not in refnode:
1498+
# This one is already resolved; just leave it be
1499+
continue
14901500
if not url_re.match(refnode['refuri']):
14911501
refnode['refuri'] = builder.get_relative_uri(
14921502
docname, refnode['refuri']) + refnode['anchorname']

0 commit comments

Comments
 (0)