1313from  urllib .parse  import  quote  as  _urllib_quote 
1414
1515from  docutils  import  nodes 
16- from  docutils .nodes  import  Element 
17- from  sphinx  import  addnodes , domains 
18- from  sphinx .builders  import  Builder 
16+ from  sphinx  import  addnodes 
1917from  sphinx .domains .std  import  StandardDomain 
20- from  sphinx .environment  import  BuildEnvironment 
21- from  sphinx .locale  import  _ 
2218from  sphinx .util  import  logging 
2319from  sphinx .util .docutils  import  SphinxDirective 
2420from  sphinx .util .nodes  import  make_id 
2521
26- from  antsibull_docs .utils .rst  import  massage_rst_label 
22+ from  antsibull_docs .rst_labels  import  (
23+     get_attribute_ref ,
24+     get_option_ref ,
25+     get_requirements_ref ,
26+     get_return_value_ref ,
27+ )
2728
2829from  .directive_helper  import  YAMLDirective 
30+ from  .domains  import  AnsibleDomain 
2931from  .nodes  import  ansible_attribute , ansible_option , ansible_return_value , link_button 
3032from  .schemas .ansible_links  import  AnsibleLinks 
3133from  .schemas .ansible_plugin  import  (
@@ -93,51 +95,6 @@ def _run(self, content_str: str, content: AnsibleLinks) -> list[nodes.Node]:
9395        return  [node ]
9496
9597
96- class  AnsibleDomain (domains .Domain ):
97-     name  =  "ansible" 
98- 
99-     object_types : dict [str , domains .ObjType ] =  {
100-         "plugin" : domains .ObjType (_ ("plugin" ), "plugin" , searchprio = - 1 ),
101-         "role_entrypoint" : domains .ObjType (
102-             _ ("role entrypoint" ), "role_entrypoint" , searchprio = - 1 
103-         ),
104-     }
105- 
106-     @property  
107-     def  objects (self ) ->  dict [tuple [str , str ], tuple [str , str ]]:
108-         return  self .data .setdefault (
109-             "objects" , {}
110-         )  # (objtype, name) -> docname, labelid 
111- 
112-     def  note_object (
113-         self , objtype : str , name : str , labelid : str , location : t .Any  =  None 
114-     ) ->  None :
115-         if  (objtype , name ) in  self .objects :
116-             docname  =  self .objects [objtype , name ][0 ]
117-             logger .warning (
118-                 f"Duplicate { objtype } { name } { docname }  ,
119-                 location = location ,
120-             )
121-         self .objects [objtype , name ] =  (self .env .docname , labelid )
122- 
123-     def  merge_domaindata (self , docnames : list [str ], otherdata : dict ) ->  None :
124-         """Merge in data regarding *docnames* from a different domaindata 
125-         inventory (coming from a subprocess in parallel builds). 
126-         """ 
127- 
128-     def  resolve_any_xref (
129-         self ,
130-         env : BuildEnvironment ,
131-         fromdocname : str ,
132-         builder : Builder ,
133-         target : str ,
134-         node : addnodes .pending_xref ,
135-         contnode : Element ,
136-     ) ->  list [tuple [str , Element ]]:
137-         """Resolve the pending_xref *node* with the given *target*.""" 
138-         return  []
139- 
140- 
14198class  _Plugin (YAMLDirective [AnsiblePlugin ]):
14299    schema  =  AnsiblePlugin 
143100
@@ -241,16 +198,12 @@ def _run(
241198        title  =  titles [0 ]
242199        self .state .document .note_explicit_target (title )
243200        std  =  t .cast (StandardDomain , self .env .get_domain ("std" ))
244-         rst_id  =  (
245-             f"ansible_collections. { content .fqcn } _ { content .plugin_type } _requirements" 
201+         rst_id  =  get_requirements_ref (
202+             content .fqcn ,  content .plugin_type ,  content . role_entrypoint 
246203        )
247204        plugin_name  =  _plugin_name (content .fqcn , content .plugin_type )
248205        ref_title  =  f"Requirements of the { plugin_name }  
249206        if  content .role_entrypoint  is  not None  and  content .plugin_type  ==  "role" :
250-             rst_id  =  (
251-                 f"ansible_collections.{ content .fqcn }  
252-                 f"-{ content .role_entrypoint }  
253-             )
254207            ref_title  =  f"{ ref_title } { content .role_entrypoint }  
255208        std .note_hyperlink_target (
256209            rst_id ,
@@ -270,9 +223,8 @@ class _Attribute(YAMLDirective[AnsibleAttribute]):
270223
271224    def  _run (self , content_str : str , content : AnsibleAttribute ) ->  list [nodes .Node ]:
272225        html_id  =  f"attribute-{ _percent_encode (content .name )}  
273-         rst_id  =  (
274-             f"ansible_collections.{ content .fqcn } { content .plugin_type }  
275-             f"__attribute-{ content .name }  
226+         rst_id  =  get_attribute_ref (
227+             content .fqcn , content .plugin_type , content .role_entrypoint , content .name 
276228        )
277229        node  =  ansible_attribute (
278230            "" , content .name , classes = ["ansible-option-title" ], ids = [html_id ]
@@ -309,17 +261,16 @@ def _compile_ids(
309261    role_entrypoint : str  |  None ,
310262    full_keys : list [list [str ]],
311263    prefix_type : str ,
264+     get_ref : t .Callable [[str , str , str  |  None , list [str ]], str ],
312265) ->  tuple [dict [str , tuple [str , str , str ]], list [str ]]:
313-     rst_id_prefix  =  f"ansible_collections.{ fqcn } { plugin_type } { prefix_type }  
314266    html_id_prefix  =  f"{ prefix_type }  
315267    if  role_entrypoint  is  not None :
316-         rst_id_prefix  +=  f"{ role_entrypoint }  
317268        html_id_prefix  +=  f"{ role_entrypoint }  
318269    rst_ids  =  {}
319270    html_ids  =  []
320271    for  full_key  in  full_keys :
321272        html_id  =  html_id_prefix  +  "/" .join ([_percent_encode (k ) for  k  in  full_key ])
322-         rst_id  =  rst_id_prefix   +   "/" . join ([ massage_rst_label ( k )  for   k   in   full_key ] )
273+         rst_id  =  get_ref ( fqcn ,  plugin_type ,  role_entrypoint ,  full_key )
323274        html_ids .append (html_id )
324275        rst_ids [rst_id ] =  (html_id , "." .join (full_key ), "." .join (full_key [1 :]))
325276    return  rst_ids , _make_unique (html_ids )
@@ -335,6 +286,7 @@ def _run(self, content_str: str, content: AnsibleOption) -> list[nodes.Node]:
335286            content .role_entrypoint ,
336287            content .full_keys ,
337288            "parameter" ,
289+             get_option_ref ,
338290        )
339291        node  =  ansible_option (
340292            "" ,
@@ -392,6 +344,7 @@ def _run(self, content_str: str, content: AnsibleReturnValue) -> list[nodes.Node
392344            content .role_entrypoint ,
393345            content .full_keys ,
394346            "return" ,
347+             get_return_value_ref ,
395348        )
396349        node  =  ansible_return_value (
397350            "" ,
@@ -446,6 +399,5 @@ def setup_directives(app):
446399    """ 
447400    Setup directives for a Sphinx app object. 
448401    """ 
449-     app .add_domain (AnsibleDomain )
450402    for  name , directive  in  DIRECTIVES .items ():
451403        app .add_directive (name , directive )
0 commit comments