U
    \Sh                     @  s   d Z ddlmZ ddlZddl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eZG dd deZdddddddZ dddddZ!dddd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 ``%%``.
    )annotationsN)Any)nodesutils)Nodesystem_message)Inliner)Sphinx)__)SphinxPostTransform)loggingrst)split_explicit_title)RoleFunctionc                   @  s4   e Zd ZdZdZdddddZddd	d
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  r   None)kwargsreturnc                 K  s.   | j jsd S | jtjD ]}| | qd S N)configextlinks_detect_hardcoded_linksZdocumentfindallr   	reference	check_uri)selfr   refnode r   F/root/rtd-docs/venv/lib/python3.8/site-packages/sphinx/ext/extlinks.pyrun1   s    zExternalLinksChecker.runznodes.reference)r   r   c                 C  s   d|ksd|krdS |d }|  }| jjj D ]\}\}}tt|dd}|	|}|r2|
 dr2d|
 d kr2td}	|
 d}
||krd	| d
t| d|
 d}nd	| d
|
 d}tj|	|||d q2dS )z
        If the URI in ``refnode`` has a replacement in ``extlinks``,
        emit a warning with a replacement suggestion.
        internalrefuriNz%sz(?P<value>.+)value/zHhardcoded link %r could be replaced by an extlink (try using %r instead):z:`z <z>``)location)Zastextappr   extlinksitemsrecompileescapereplacematch	groupdictgetr
   r   loggerwarning)r   r   urititlealiasZbase_uriZ_captionZuri_patternr-   msgr!   replacementr   r   r   r   8   s&    
 zExternalLinksChecker.check_uriN)__name__
__module____qualname____doc__Zdefault_priorityr   r   r   r   r   r   r   (   s   r   strr   )namebase_urlcaptionr   c              
     s,   i g fddddddddd fdd	}|S )
Nr;   intr   dictz	list[str]z'tuple[list[Node], list[system_message]])typrawtexttextlinenoinlineroptionscontentr   c                   sV   t |}t|\}}}	 |	 }
|s:d kr2|
}n|	 }tj||d|
d}|gg fS )NF)r   r    )r   unescaper   r   r   )rA   rB   rC   rD   rE   rF   rG   Zhas_explicit_titler3   partfull_urlZpnoder=   r>   r   r   role]   s    
zmake_link_role.<locals>.roler   )r<   r=   r>   rL   r   rK   r   make_link_roleW   s     $rM   r	   r   )r&   r   c                 C  s2   | j j D ] \}\}}| |t||| qd S r   )r   r'   r(   Zadd_rolerM   )r&   r<   r=   r>   r   r   r   setup_link_rolesm   s    rN   zdict[str, Any]c                 C  s>   |  di d |  ddd | dt | t tjddS )Nr'   envr   Fzbuilder-initedT)versionZparallel_read_safe)Zadd_config_valueconnectrN   Zadd_post_transformr   sphinxZ__display_version__)r&   r   r   r   setupr   s
    
rS   )#r:   
__future__r   r)   typingr   Zdocutilsr   r   Zdocutils.nodesr   r   Zdocutils.parsers.rst.statesr   rR   Zsphinx.applicationr	   Zsphinx.localer
   Z!sphinx.transforms.post_transformsr   Zsphinx.utilr   r   Zsphinx.util.nodesr   Zsphinx.util.typingr   	getLoggerr7   r0   r   rM   rN   rS   r   r   r   r   <module>   s$   
/