Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow :ref: role to be used with definitions and fields #10781

Merged
merged 1 commit into from Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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')