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  (
@@ -92,51 +94,6 @@ def _run(self, content_str: str, content: AnsibleLinks) -> list[nodes.Node]:
9294        return  [node ]
9395
9496
95- class  AnsibleDomain (domains .Domain ):
96-     name  =  "ansible" 
97- 
98-     object_types : dict [str , domains .ObjType ] =  {
99-         "plugin" : domains .ObjType (_ ("plugin" ), "plugin" , searchprio = - 1 ),
100-         "role_entrypoint" : domains .ObjType (
101-             _ ("role entrypoint" ), "role_entrypoint" , searchprio = - 1 
102-         ),
103-     }
104- 
105-     @property  
106-     def  objects (self ) ->  dict [tuple [str , str ], tuple [str , str ]]:
107-         return  self .data .setdefault (
108-             "objects" , {}
109-         )  # (objtype, name) -> docname, labelid 
110- 
111-     def  note_object (
112-         self , objtype : str , name : str , labelid : str , location : t .Any  =  None 
113-     ) ->  None :
114-         if  (objtype , name ) in  self .objects :
115-             docname  =  self .objects [objtype , name ][0 ]
116-             logger .warning (
117-                 f"Duplicate { objtype } { name } { docname }  ,
118-                 location = location ,
119-             )
120-         self .objects [objtype , name ] =  (self .env .docname , labelid )
121- 
122-     def  merge_domaindata (self , docnames : list [str ], otherdata : dict ) ->  None :
123-         """Merge in data regarding *docnames* from a different domaindata 
124-         inventory (coming from a subprocess in parallel builds). 
125-         """ 
126- 
127-     def  resolve_any_xref (
128-         self ,
129-         env : BuildEnvironment ,
130-         fromdocname : str ,
131-         builder : Builder ,
132-         target : str ,
133-         node : addnodes .pending_xref ,
134-         contnode : Element ,
135-     ) ->  list [tuple [str , Element ]]:
136-         """Resolve the pending_xref *node* with the given *target*.""" 
137-         return  []
138- 
139- 
14097class  _Plugin (YAMLDirective [AnsiblePlugin ]):
14198    schema  =  AnsiblePlugin 
14299
@@ -240,16 +197,12 @@ def _run(
240197        title  =  titles [0 ]
241198        self .state .document .note_explicit_target (title )
242199        std  =  t .cast (StandardDomain , self .env .get_domain ("std" ))
243-         rst_id  =  (
244-             f"ansible_collections. { content .fqcn } _ { content .plugin_type } _requirements" 
200+         rst_id  =  get_requirements_ref (
201+             content .fqcn ,  content .plugin_type ,  content . role_entrypoint 
245202        )
246203        plugin_name  =  _plugin_name (content .fqcn , content .plugin_type )
247204        ref_title  =  f"Requirements of the { plugin_name }  
248205        if  content .role_entrypoint  is  not None  and  content .plugin_type  ==  "role" :
249-             rst_id  =  (
250-                 f"ansible_collections.{ content .fqcn }  
251-                 f"-{ content .role_entrypoint }  
252-             )
253206            ref_title  =  f"{ ref_title } { content .role_entrypoint }  
254207        std .note_hyperlink_target (
255208            rst_id ,
@@ -269,9 +222,8 @@ class _Attribute(YAMLDirective[AnsibleAttribute]):
269222
270223    def  _run (self , content_str : str , content : AnsibleAttribute ) ->  list [nodes .Node ]:
271224        html_id  =  f"attribute-{ _percent_encode (content .name )}  
272-         rst_id  =  (
273-             f"ansible_collections.{ content .fqcn } { content .plugin_type }  
274-             f"__attribute-{ content .name }  
225+         rst_id  =  get_attribute_ref (
226+             content .fqcn , content .plugin_type , content .role_entrypoint , content .name 
275227        )
276228        node  =  ansible_attribute (
277229            "" , content .name , classes = ["ansible-option-title" ], ids = [html_id ]
@@ -308,17 +260,16 @@ def _compile_ids(
308260    role_entrypoint : str  |  None ,
309261    full_keys : list [list [str ]],
310262    prefix_type : str ,
263+     get_ref : t .Callable [[str , str , str  |  None , list [str ]], str ],
311264) ->  tuple [dict [str , tuple [str , str , str ]], list [str ]]:
312-     rst_id_prefix  =  f"ansible_collections.{ fqcn } { plugin_type } { prefix_type }  
313265    html_id_prefix  =  f"{ prefix_type }  
314266    if  role_entrypoint  is  not None :
315-         rst_id_prefix  +=  f"{ role_entrypoint }  
316267        html_id_prefix  +=  f"{ role_entrypoint }  
317268    rst_ids  =  {}
318269    html_ids  =  []
319270    for  full_key  in  full_keys :
320271        html_id  =  html_id_prefix  +  "/" .join ([_percent_encode (k ) for  k  in  full_key ])
321-         rst_id  =  rst_id_prefix   +   "/" . join ([ massage_rst_label ( k )  for   k   in   full_key ] )
272+         rst_id  =  get_ref ( fqcn ,  plugin_type ,  role_entrypoint ,  full_key )
322273        html_ids .append (html_id )
323274        rst_ids [rst_id ] =  (html_id , "." .join (full_key ), "." .join (full_key [1 :]))
324275    return  rst_ids , _make_unique (html_ids )
@@ -334,6 +285,7 @@ def _run(self, content_str: str, content: AnsibleOption) -> list[nodes.Node]:
334285            content .role_entrypoint ,
335286            content .full_keys ,
336287            "parameter" ,
288+             get_option_ref ,
337289        )
338290        node  =  ansible_option (
339291            "" ,
@@ -391,6 +343,7 @@ def _run(self, content_str: str, content: AnsibleReturnValue) -> list[nodes.Node
391343            content .role_entrypoint ,
392344            content .full_keys ,
393345            "return" ,
346+             get_return_value_ref ,
394347        )
395348        node  =  ansible_return_value (
396349            "" ,
@@ -445,6 +398,5 @@ def setup_directives(app):
445398    """ 
446399    Setup directives for a Sphinx app object. 
447400    """ 
448-     app .add_domain (AnsibleDomain )
449401    for  name , directive  in  DIRECTIVES .items ():
450402        app .add_directive (name , directive )
0 commit comments