B
    d                 @   s  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eZ G dd deZ!e"e"e"edddZ#eddddZ$eee"ef 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 ``%%``.
    N)AnyDictListTuple)nodesutils)Nodesystem_message)Inliner)Sphinx)__)SphinxPostTransform)loggingrst)split_explicit_title)RoleFunctionc               @   s6   e Zd ZdZdZeddddZej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  N)kwargsreturnc             K   s2   | j jsd S x | jtjD ]}| | qW d S )N)configextlinks_detect_hardcoded_linksZdocumentfindallr   	reference	check_uri)selfr   refnode r   `/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/sphinx/ext/extlinks.pyrun0   s    zExternalLinksChecker.run)r   r   c             C   s  d|ksd|krdS |d }|  }x| jjj D ]\}\}}tjdk rdtt	|
dd}ntt	|
dd}||}|r4| dr4d	| d kr4td
}	| d}
||krd| dt	| d|
 d}nd| d|
 d}tj|	|||d q4W d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>.+)z%svalue/zHhardcoded link %r could be replaced by an extlink (try using %r instead):z:`z <z>``)location)Zastextappr   extlinksitemssysversion_inforecompileescapereplacematch	groupdictgetr   r   loggerwarning)r   r   urititlealiasZbase_uriZ_captionZuri_patternr1   msgr#   replacementr   r   r   r   7   s$    

 zExternalLinksChecker.check_uri)
__name__
__module____qualname____doc__Zdefault_priorityr   r   r   r   r   r   r   r   r   r   '   s   r   )namebase_urlcaptionr   c                s   y d  W n6 t tfk
rB   ttd  ddd  Y nX d k	ryd  W n6 t tfk
r   ttd ddd Y nX i g ftttttt	t
t tt
t t
t f d fd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%%z%sz|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                sV   t |}t|\}}}	 |	 }
|s:d kr2|
}n|	 }tj||d|
d}|gg fS )NF)r   r    )r   unescaper   r   r   )rD   rE   rF   rG   rH   rI   rJ   Zhas_explicit_titler7   partfull_urlZpnode)r@   rA   r   r   roleq   s    
zmake_link_role.<locals>.role)	TypeError
ValueErrorr4   r5   r   r0   strintr
   r   r   r   r   r	   )r?   r@   rA   rN   r   )r@   rA   r   make_link_role[   s    $rS   )r(   r   c             C   s6   x0| j j D ] \}\}}| |t||| qW d S )N)r   r)   r*   Zadd_rolerS   )r(   r?   r@   rA   r   r   r   setup_link_roles   s    rT   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connectrT   Zadd_post_transformr   sphinxZ__display_version__)r(   r   r   r   setup   s
    
rY   )&r>   r-   r+   typingr   r   r   r   Zdocutilsr   r   Zdocutils.nodesr   r	   Zdocutils.parsers.rst.statesr
   rX   Zsphinx.applicationr   Zsphinx.localer   Z!sphinx.transforms.post_transformsr   Zsphinx.utilr   r   Zsphinx.util.nodesr   Zsphinx.util.typingr   	getLoggerr;   r4   r   rQ   rS   rT   rY   r   r   r   r   <module>   s$   
4&