
    O&i                     6   d Z ddlZddlZddlmZmZmZmZ ddlm	Z	m
Z
 ddlmZmZ ddlmZ ddlZddlmZ ddlmZ dd	lmZ dd
lmZmZ ddlmZ ddlmZ  ej        e          Z  G d de          Z!de"de"de"defdZ#deddfdZ$dedee"ef         fdZ%dS )a  Extension to save typing and prevent hard-coding of base URLs in reST files.

This adds a new config value called ``extlinks`` that is created like this::

   extlinks = {'exmpl': ('https://example.invalid/%s.html', caption), ...}

Now you can use e.g. :exmpl:`foo` in your documents.  This will create a
link to ``https://example.invalid/foo.html``.  The link caption depends on
the *caption* value given:

- If it is ``None``, the caption will be the full URL.
- If it is a string, it must contain ``%s`` exactly once.  In this case the
  caption will be *caption* with the role content substituted for ``%s``.

You can also give an explicit caption, e.g. :exmpl:`Foo <foo>`.

Both, the url string and the caption string must escape ``%`` as ``%%``.
    N)AnyDictListTuple)nodesutils)Nodesystem_message)Inliner)Sphinx)__)SphinxPostTransform)loggingrst)split_explicit_title)RoleFunctionc                   @    e Zd ZdZdZdeddfdZdej        ddfdZ	dS )	ExternalLinksCheckerz
    For each external link, check if it can be replaced by an extlink.

    We treat each ``reference`` node without ``internal`` attribute as an external link.
    i  kwargsreturnNc                     | j         j        sd S | j                            t          j                  D ]}|                     |           d S N)configextlinks_detect_hardcoded_linksdocumentfindallr   	reference	check_uri)selfr   refnodes      `/home/geonatureadmin/si_en_reseau/tutos/venv/lib/python3.11/site-packages/sphinx/ext/extlinks.pyrunzExternalLinksChecker.run0   sT    {: 	F},,U_== 	$ 	$GNN7####	$ 	$    r    c                 p   d|v sd|vrdS |d         }|                                 }| j        j        j                                        D ]j\  }\  }}t
          j        dk     r;t          j        t          j	        |          
                    dd                    }n:t          j        t          j	        |          
                    dd                    }|                    |          }|r|                                                    d          rd	|                                d         vrt          d
          }	|                                                    d          }
||k    rd| dt          j	        |           d|
 d}n	d| d|
 d}t                               |	|||           ldS )z
        If the URI in ``refnode`` has a replacement in ``extlinks``,
        emit a warning with a replacement suggestion.
        internalrefuriN)      z\%sz(?P<value>.+)%svalue/zHhardcoded link %r could be replaced by an extlink (try using %r instead):z:`z <z>``)location)astextappr   extlinksitemssysversion_inforecompileescapereplacematch	groupdictgetr   r   loggerwarning)r   r    urititlealiasbase_uri_captionuri_patternr9   msgr*   replacements               r!   r   zExternalLinksChecker.check_uri7   s   
   HG$;$;Fh  +/8?+C+I+I+K+K 	H 	H'E'Hh&(( !j8)<)<)D)DV_)])]^^ j8)<)<)D)DT?)[)[\\%%c**EH!!%%g..H 5??,,W555  2 3 3))--g66%<<"Me"M"Msz%/@/@"M"ME"M"M"MKK"7e"7"7u"7"7"7KsCwGGG-	H 	Hr#   )
__name__
__module____qualname____doc__default_priorityr   r"   r   r   r    r#   r!   r   r   '   st          $C $D $ $ $ $!H !HT !H !H !H !H !H !Hr#   r   namebase_urlcaptionr   c                 p   	 dz   nW# t           t          f$ rC t                              t	          d                                         dd          dz   Y nw xY w^	 dz   nW# t           t          f$ rC t                              t	          d                                         dd          dz   Y nw xY wi g fdt          dt          d	t          d
t          dt          dt          dt          t                   dt          t          t                   t          t                   f         ffd}|S )Ndummyztextlinks: Sphinx-6.0 will require base URL to contain exactly one '%s' and all other '%' need to be escaped as '%%'.%z%%r)   z|extlinks: Sphinx-6.0 will require a caption string to contain exactly one '%s' and all other '%' need to be escaped as '%%'.typrawtexttextlinenoinlineroptionscontentr   c                     t          j        |          }t          |          \  }}}	|	z  }
|s
|
}n|	z  }t          j        ||d|
          }|gg fS )NF)r%   r&   )r   unescaper   r   r   )rR   rS   rT   rU   rV   rW   rX   has_explicit_titler?   partfull_urlpnoderM   rN   s               r!   rolezmake_link_role.<locals>.roleq   ss     ~d##*>t*D*D'E4d?! 	' $uuXNNNw{r#   )	TypeError
ValueErrorr<   r=   r   r8   strintr   r   r   r   r	   r
   )rL   rM   rN   r_   s    `` r!   make_link_rolerd   [   s   67z" 6 6 6r 5 6 6 	7 	7 	7 ##C..5	6
 	8g:& 	8 	8 	8NN2 9 : : ; ; ; ooc40047GGG		8 02 #  3  (,<@IT
D$889       Ks#   
 AAA$A* *AB>=B>r0   c           	          | j         j                                        D ]-\  }\  }}|                     |t	          |||                     .d S r   )r   r1   r2   add_rolerd   )r0   rL   rM   rN   s       r!   setup_link_rolesrg      s]    %(Z%8%>%>%@%@ D D!!xT>$'BBCCCCD Dr#   c                     |                      di d           |                      ddd           |                     dt                     |                     t                     t
          j        ddS )Nr1   envr   Fzbuilder-initedT)versionparallel_read_safe)add_config_valueconnectrg   add_post_transformr   sphinx__display_version__)r0   s    r!   setuprq      so    R///:E5IIIKK "2333/0001NNNr#   )&rI   r5   r3   typingr   r   r   r   docutilsr   r   docutils.nodesr	   r
   docutils.parsers.rst.statesr   ro   sphinx.applicationr   sphinx.localer   !sphinx.transforms.post_transformsr   sphinx.utilr   r   sphinx.util.nodesr   sphinx.util.typingr   	getLoggerrF   r<   r   rb   rd   rg   rq   rK   r#   r!   <module>r}      s   & 
			 



 ) ) ) ) ) ) ) ) ) ) ) ) ! ! ! ! ! ! ! ! / / / / / / / / / / / / / /  % % % % % %       A A A A A A $ $ $ $ $ $ $ $ 2 2 2 2 2 2 + + + + + +		8	$	$1H 1H 1H 1H 1H. 1H 1H 1Hh# # #c #l # # # #LD& DT D D D D
Ov O$sCx. O O O O O Or#   