
    7 f                       d Z ddlmZ ddlZddlmZmZ ddlmZm	Z	 ddl
Z
ddlmZ ddlmZ ddlmZmZ dd	lmZ er dd
lmZ ddlmZmZ ddlmZ ddlmZ ddlmZ  ej        e           Z! G d de          Z"ddZ#ddZ$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)TYPE_CHECKINGAny)nodesutils)__)SphinxPostTransform)loggingrst)split_explicit_title)Sequence)Nodesystem_message)Inliner)Sphinx)RoleFunctionc                  &    e Zd ZdZdZddZ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  kwargsr   returnNonec                    | j         j        sd S | j                            t          j                  D ]}|                     |           d S N)configextlinks_detect_hardcoded_linksdocumentfindallr   	reference	check_uri)selfr   refnodes      I/var/www/equiseq/venv/lib/python3.11/site-packages/sphinx/ext/extlinks.pyrunzExternalLinksChecker.run6   sT    {: 	F},,U_== 	$ 	$GNN7####	$ 	$    r!   nodes.referencec                   d|v sd|vrdS |d         }|                                 }| j        j        j                                        D ]\  }\  }}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                              |	|||           !d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)astextappr   extlinksitemsrecompileescapereplacematch	groupdictgetr   r   loggerwarning)r    r!   urititlealiasbase_uri_captionuri_patternr6   msgr)   replacements               r"   r   zExternalLinksChecker.check_uri=   s   
   HG$;$;Fh  +/8?+C+I+I+K+K 	H 	H'E'Hh*RYx%8%8%@%@%W%WXXK%%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$   N)r   r   r   r   )r!   r%   r   r   )__name__
__module____qualname____doc__default_priorityr#   r    r$   r"   r   r   -   sT          $ $ $ $H H H H H Hr$   r   namestrbase_urlcaptionr   r   c                    	 ddfd}|S )NrH   typrJ   rawtexttextlinenointinlinerr   optionsdict | NonecontentSequence[str]r   'tuple[list[Node], list[system_message]]c                    t          j        |          }t          |          \  }}}	|	z  }
|s
|
}n|	z  }t          j        ||d|
          }|gg fS )NF)r'   r(   )r   unescaper   r   r   )rN   rO   rP   rQ   rS   rT   rV   has_explicit_titler<   partfull_urlpnoderK   rL   s               r"   rolezmake_link_role.<locals>.roleb   ss     ~d##*>t*D*D'E4d?! 	' $uuXNNNw{r$   )NrH   )rN   rJ   rO   rJ   rP   rJ   rQ   rR   rS   r   rT   rU   rV   rW   r   rX   rH   )rI   rK   rL   r_   s    `` r"   make_link_roler`   \   s:     VX        Kr$   r/   r   r   c           	         | j         j                                        D ]-\  }\  }}|                     |t	          |||                     .d S r   )r   r0   r1   add_roler`   )r/   rI   rK   rL   s       r"   setup_link_rolesrc   r   s]    %(Z%8%>%>%@%@ D D!!xT>$'BBCCCCD Dr$   dict[str, Any]c                    |                      di d           |                      ddd           |                     dt                     |                     t                     t
          j        ddS )Nr0   envr   Fzbuilder-initedT)versionparallel_read_safe)add_config_valueconnectrc   add_post_transformr   sphinx__display_version__)r/   s    r"   setuprn   w   so    R///:E5IIIKK "2333/0001NNNr$   )rI   rJ   rK   rJ   rL   rJ   r   r   )r/   r   r   r   )r/   r   r   rd   )&rF   
__future__r   r2   typingr   r   docutilsr   r   rl   sphinx.localer   !sphinx.transforms.post_transformsr	   sphinx.utilr
   r   sphinx.util.nodesr   collections.abcr   docutils.nodesr   r   docutils.parsers.rst.statesr   sphinx.applicationr   sphinx.util.typingr   	getLoggerrC   r9   r   r`   rc   rn   rH   r$   r"   <module>r|      s   & # " " " " " 				 % % % % % % % % ! ! ! ! ! ! ! !        A A A A A A $ $ $ $ $ $ $ $ 2 2 2 2 2 2 0((((((33333333333333))))))//////		8	$	$,H ,H ,H ,H ,H. ,H ,H ,H^   ,D D D D
O O O O O Or$   