Skip to content

Commit a0145df

Browse files
authored
Sphinx extension: fix creation of nodes for options and return values; produce error nodes (#175)
* Make sure that external references (intersphinx) are formatted the same as internal ones. * Format error messages. * Make sure references look the same no matter whether they are resolved by intersphinx, internally, or not at all. * Add changelog fragment. * Reformat.
1 parent e430a08 commit a0145df

File tree

4 files changed

+21
-19
lines changed

4 files changed

+21
-19
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
bugfixes:
2+
- "Fix the way the Sphinx extension creates nodes for options and return values so they look identical for internal references, external (intersphinx) references, and unresolved references (https://github.com/ansible-community/antsibull-docs/pull/175)."
3+
- "Make sure that broken ``:ansopt:`` and ``:ansretval:`` parameters result in correctly rendered error messages (https://github.com/ansible-community/antsibull-docs/pull/175)."

src/sphinx_antsibull_ext/antsibull-minimal.css

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/sphinx_antsibull_ext/css/antsibull-minimal.scss

+1-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ table.ansible-option-table {
341341
}
342342

343343
.ansible-option, .ansible-option-value, .ansible-return-value {
344-
a.reference.internal {
344+
a.reference.internal, a.reference.external {
345345
color: unset;
346346

347347
&:hover {

src/sphinx_antsibull_ext/roles.py

+16-17
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,16 @@ def _create_ref_or_not(
127127
entrypoint: str | None,
128128
ref_parameter: str,
129129
text: str,
130-
) -> tuple[str, list[t.Any]]:
130+
) -> t.Any:
131+
# When successfully resolving *internal* references, Sphinx does **NOT**
132+
# use the node we provide, but simply extracts the text and creates a new
133+
# node. Thus we use nodes.inline so that the result is the same no matter
134+
# whether the reference was internal, not resolved, or external
135+
# (intersphinx).
136+
content = nodes.inline(text, text)
131137
ref = create_ref(plugin_fqcn, plugin_type, entrypoint, ref_parameter)
132138
if ref is None:
133-
return text, []
134-
135-
# The content node will be replaced by Sphinx anyway, so it doesn't matter what kind
136-
# of node we are using...
137-
content = nodes.literal(text, text)
139+
return content
138140

139141
options = {
140142
"reftype": "ref",
@@ -144,13 +146,13 @@ def _create_ref_or_not(
144146
}
145147
refnode = addnodes.pending_xref(text, content, **options)
146148
refnode["reftarget"] = ref
147-
return "", [refnode]
149+
return refnode
148150

149151

150152
# pylint:disable-next=unused-argument
151153
def _create_error(rawtext: str, text: str, error: str) -> tuple[list[t.Any], list[str]]:
152-
node = ... # FIXME
153-
return [node], []
154+
content = nodes.strong(text, error, classes=["error"])
155+
return [content], []
154156

155157

156158
# pylint:disable-next=unused-argument,dangerous-default-value
@@ -182,7 +184,7 @@ def option_role(name, rawtext, text, lineno, inliner, options={}, content=[]):
182184
else:
183185
text = f"{option}={value}"
184186
classes.append("ansible-option-value")
185-
text, subnodes = _create_ref_or_not(
187+
content = _create_ref_or_not(
186188
_create_option_reference,
187189
plugin_fqcn,
188190
plugin_type,
@@ -191,11 +193,8 @@ def option_role(name, rawtext, text, lineno, inliner, options={}, content=[]):
191193
text,
192194
)
193195
if value is None:
194-
content = nodes.strong(rawtext, text, *subnodes)
195-
content = nodes.literal(rawtext, "", content, classes=classes)
196-
else:
197-
content = nodes.literal(rawtext, text, *subnodes, classes=classes)
198-
return [content], []
196+
content = nodes.strong(rawtext, "", content)
197+
return [nodes.literal(rawtext, "", content, classes=classes)], []
199198

200199

201200
# pylint:disable-next=unused-argument,dangerous-default-value
@@ -244,15 +243,15 @@ def return_value_role(name, rawtext, text, lineno, inliner, options={}, content=
244243
text = f"{rv}"
245244
else:
246245
text = f"{rv}={value}"
247-
text, subnodes = _create_ref_or_not(
246+
content = _create_ref_or_not(
248247
_create_return_value_reference,
249248
plugin_fqcn,
250249
plugin_type,
251250
entrypoint,
252251
rv_link,
253252
text,
254253
)
255-
return [nodes.literal(rawtext, text, *subnodes, classes=classes)], []
254+
return [nodes.literal(rawtext, "", content, classes=classes)], []
256255

257256

258257
# pylint:disable-next=unused-argument,dangerous-default-value

0 commit comments

Comments
 (0)