Skip to content

Commit

Permalink
Allow :ref: role to be used with definitions and fields
Browse files Browse the repository at this point in the history
Since these nodes have a natural title, there is no reason not to
support the :ref: role.
  • Loading branch information
jbms committed Sep 1, 2022
1 parent 5e9550c commit b78e342
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGES
Expand Up @@ -17,6 +17,7 @@ Features added
parameter lists and the declaration.
- #10755: linkcheck: Check the source URL of raw directives that use the ``url``
option.
- #10781: Allow :rst:role:`ref` role to be used with definitions and fields.

Bugs fixed
----------
Expand Down
19 changes: 14 additions & 5 deletions sphinx/domains/std.py
Expand Up @@ -757,12 +757,21 @@ def process_doc(self, env: "BuildEnvironment", docname: str, document: nodes.doc
if not sectname:
continue
else:
toctree = next(node.findall(addnodes.toctree), None)
if toctree and toctree.get('caption'):
sectname = toctree.get('caption')
if (isinstance(node, (nodes.definition_list,
nodes.field_list)) and
node.children):
node = cast(nodes.Element, node.children[0])
if isinstance(node, (nodes.field, nodes.definition_list_item)):
node = cast(nodes.Element, node.children[0])
if isinstance(node, (nodes.term, nodes.field_name)):
sectname = clean_astext(node)
else:
# anonymous-only labels
continue
toctree = next(node.findall(addnodes.toctree), None)
if toctree and toctree.get('caption'):
sectname = toctree.get('caption')
else:
# anonymous-only labels
continue
self.labels[name] = docname, labelid, sectname

def add_program_option(self, program: str, name: str, docname: str, labelid: str) -> None:
Expand Down
40 changes: 40 additions & 0 deletions tests/test_domain_std.py
Expand Up @@ -445,3 +445,43 @@ def test_labeled_rubric(app):
domain = app.env.get_domain("std")
assert 'label' in domain.labels
assert domain.labels['label'] == ('index', 'label', 'blah blah blah')


def test_labeled_definition(app):
text = (".. _label1:\n"
"\n"
"Foo blah *blah* blah\n"
" Definition\n"
"\n"
".. _label2:\n"
"\n"
"Bar blah *blah* blah\n"
" Definition\n"
"\n")
restructuredtext.parse(app, text)

domain = app.env.get_domain("std")
assert 'label1' in domain.labels
assert domain.labels['label1'] == ('index', 'label1', 'Foo blah blah blah')
assert 'label2' in domain.labels
assert domain.labels['label2'] == ('index', 'label2', 'Bar blah blah blah')


def test_labeled_field(app):
text = (".. _label1:\n"
"\n"
":Foo blah *blah* blah:\n"
" Definition\n"
"\n"
".. _label2:\n"
"\n"
":Bar blah *blah* blah:\n"
" Definition\n"
"\n")
restructuredtext.parse(app, text)

domain = app.env.get_domain("std")
assert 'label1' in domain.labels
assert domain.labels['label1'] == ('index', 'label1', 'Foo blah blah blah')
assert 'label2' in domain.labels
assert domain.labels['label2'] == ('index', 'label2', 'Bar blah blah blah')

0 comments on commit b78e342

Please sign in to comment.