Skip to content

Commit 651c202

Browse files
committed
Insert anchor to requirements section programmatically.
1 parent d2b9909 commit 651c202

File tree

19 files changed

+151
-34
lines changed

19 files changed

+151
-34
lines changed

src/antsibull_docs/data/docsite/ansible-docsite/plugin.rst.j2

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,14 @@ Aliases: @{ ', '.join(doc['aliases'] | sort) }@
158158
.. Requirements
159159

160160
{% if doc['requirements'] -%}
161-
.. _@{ rst_requirements_ref(plugin_name, plugin_type) }@:
162-
163161
Requirements
164162
------------
163+
164+
.. ansible-requirements-anchor::
165+
166+
fqcn: @{ plugin_name }@
167+
plugin_type: @{ plugin_type }@
168+
165169
{% if plugin_type in ('module', 'module_util') %}
166170
The below requirements are needed on the host that executes this @{ plugin_type }@.
167171
{% else %}

src/antsibull_docs/data/docsite/ansible-docsite/role.rst.j2

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ Synopsis
130130
{% if ep_doc['requirements'] -%}
131131
Requirements
132132
^^^^^^^^^^^^
133+
134+
.. ansible-requirements-anchor::
135+
136+
fqcn: @{ plugin_name }@
137+
plugin_type: role
138+
entrypoint: @{ entry_point }@
139+
133140
The below requirements are needed on the remote host and/or the local controller node.
134141

135142
{% for req in ep_doc['requirements'] %}

src/sphinx_antsibull_ext/directives.py

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
AnsibleAttribute,
3333
AnsibleOption,
3434
AnsiblePlugin,
35+
AnsibleRequirementsAnchor,
3536
AnsibleReturnValue,
3637
AnsibleRoleEntrypoint,
3738
)
@@ -219,6 +220,48 @@ def _run(
219220
return [indexnode]
220221

221222

223+
def _plugin_name(fqcn: str, plugin_type: str) -> str:
224+
if plugin_type in ("module", "role"):
225+
return f"{fqcn} {plugin_type}"
226+
return f"{fqcn} {plugin_type} plugin"
227+
228+
229+
class _RequirementsAnchor(YAMLDirective[AnsibleRequirementsAnchor]):
230+
schema = AnsibleRequirementsAnchor
231+
232+
def _run(
233+
self, content_str: str, content: AnsibleRequirementsAnchor
234+
) -> list[nodes.Node]:
235+
section = self.state.parent
236+
titles = [child for child in section.children if isinstance(child, nodes.title)]
237+
if len(titles) != 1:
238+
raise self.error(
239+
f"Cannot find single title for section {section} - found {titles}"
240+
)
241+
title = titles[0]
242+
self.state.document.note_explicit_target(title)
243+
std = t.cast(StandardDomain, self.env.get_domain("std"))
244+
rst_id = (
245+
f"ansible_collections.{content.fqcn}_{content.plugin_type}_requirements"
246+
)
247+
ref_title = (
248+
f"Requirements of the {_plugin_name(content.fqcn, content.plugin_type)}"
249+
)
250+
if content.role_entrypoint is not None and content.plugin_type == "role":
251+
rst_id = (
252+
f"ansible_collections.{content.fqcn}_role"
253+
f"-{content.role_entrypoint}_requirements"
254+
)
255+
ref_title = f"{ref_title}, {content.role_entrypoint} entrypoint"
256+
std.note_hyperlink_target(
257+
rst_id,
258+
self.env.docname,
259+
title["ids"][0],
260+
ref_title,
261+
)
262+
return []
263+
264+
222265
def _percent_encode(s):
223266
return _urllib_quote(s, safe="")
224267

@@ -241,7 +284,7 @@ def _run(self, content_str: str, content: AnsibleAttribute) -> list[nodes.Node]:
241284
rst_id,
242285
self.env.docname,
243286
html_id,
244-
f"{content.name} attribute of {content.fqcn} {content.plugin_type}",
287+
f"{content.name} attribute of {_plugin_name(content.fqcn, content.plugin_type)}",
245288
)
246289
permalink = nodes.raw(
247290
"",
@@ -306,7 +349,7 @@ def _run(self, content_str: str, content: AnsibleOption) -> list[nodes.Node]:
306349
rst_id,
307350
self.env.docname,
308351
html_id,
309-
f"{key} option of {content.fqcn} {content.plugin_type}",
352+
f"{key} option of {_plugin_name(content.fqcn, content.plugin_type)}",
310353
)
311354
permalink = nodes.raw(
312355
"",
@@ -341,7 +384,7 @@ def _run(self, content_str: str, content: AnsibleReturnValue) -> list[nodes.Node
341384
rst_id,
342385
self.env.docname,
343386
html_id,
344-
f"{key} return value of {content.fqcn} {content.plugin_type}",
387+
f"{key} return value of {_plugin_name(content.fqcn, content.plugin_type)}",
345388
)
346389
permalink = nodes.raw(
347390
"",
@@ -357,6 +400,7 @@ def _run(self, content_str: str, content: AnsibleReturnValue) -> list[nodes.Node
357400
"ansible-links": _Links,
358401
"ansible-plugin": _Plugin,
359402
"ansible-role-entrypoint": _RoleEntrypoint,
403+
"ansible-requirements-anchor": _RequirementsAnchor,
360404
"ansible-attribute": _Attribute,
361405
"ansible-option": _Option,
362406
"ansible-return-value": _ReturnValue,

src/sphinx_antsibull_ext/schemas/ansible_plugin.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ class AnsibleRoleEntrypoint(p.BaseModel):
2828
short_description: t.Optional[str] = None
2929

3030

31+
class AnsibleRequirementsAnchor(p.BaseModel):
32+
fqcn: str
33+
plugin_type: str
34+
role_entrypoint: t.Optional[str] = None
35+
36+
3137
class AnsibleAttribute(p.BaseModel):
3238
fqcn: str
3339
plugin_type: str

tests/functional/baseline-default/collections/ns/col2/foo2_module.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,14 @@ Synopsis
6868
6969
.. Requirements
7070
71-
.. _ansible_collections.ns.col2.foo2_module_requirements:
72-
7371
Requirements
7472
------------
73+
74+
.. ansible-requirements-anchor::
75+
76+
fqcn: ns.col2.foo2
77+
plugin_type: module
78+
7579
The below requirements are needed on the host that executes this module.
7680

7781
- Foo.

tests/functional/baseline-default/collections/ns/col2/foo3_module.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,14 @@ Synopsis
6363
6464
.. Requirements
6565
66-
.. _ansible_collections.ns.col2.foo3_module_requirements:
67-
6866
Requirements
6967
------------
68+
69+
.. ansible-requirements-anchor::
70+
71+
fqcn: ns.col2.foo3
72+
plugin_type: module
73+
7074
The below requirements are needed on the host that executes this module.
7175

7276
- Foo.

tests/functional/baseline-default/collections/ns2/col/foo_module.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,14 @@ Aliases: foo_redirect
7070

7171
.. Requirements
7272
73-
.. _ansible_collections.ns2.col.foo_module_requirements:
74-
7573
Requirements
7674
------------
75+
76+
.. ansible-requirements-anchor::
77+
78+
fqcn: ns2.col.foo
79+
plugin_type: module
80+
7781
The below requirements are needed on the host that executes this module.
7882

7983
- Foo on remote.

tests/functional/baseline-default/collections/ns2/col/foo_vars.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,14 @@ Synopsis
6868
6969
.. Requirements
7070
71-
.. _ansible_collections.ns2.col.foo_vars_requirements:
72-
7371
Requirements
7472
------------
73+
74+
.. ansible-requirements-anchor::
75+
76+
fqcn: ns2.col.foo
77+
plugin_type: vars
78+
7579
The below requirements are needed on the local controller node that executes this vars.
7680

7781
- Enabled in Ansible's configuration.

tests/functional/baseline-no-breadcrumbs/collections/ns/col2/foo2_module.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,14 @@ Synopsis
6868
6969
.. Requirements
7070
71-
.. _ansible_collections.ns.col2.foo2_module_requirements:
72-
7371
Requirements
7472
------------
73+
74+
.. ansible-requirements-anchor::
75+
76+
fqcn: ns.col2.foo2
77+
plugin_type: module
78+
7579
The below requirements are needed on the host that executes this module.
7680

7781
- Foo.

tests/functional/baseline-no-breadcrumbs/collections/ns/col2/foo3_module.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,14 @@ Synopsis
6363
6464
.. Requirements
6565
66-
.. _ansible_collections.ns.col2.foo3_module_requirements:
67-
6866
Requirements
6967
------------
68+
69+
.. ansible-requirements-anchor::
70+
71+
fqcn: ns.col2.foo3
72+
plugin_type: module
73+
7074
The below requirements are needed on the host that executes this module.
7175

7276
- Foo.

0 commit comments

Comments
 (0)