3
T'íc ã @ s d Z ddlZddlZddlZddlmZmZmZmZ ddl m
Z
mZ ddlm
Z
mZ ddlmZ ddlZddlmZ ddlmZ dd lmZ dd
lmZ ddlmZmZ ddlmZ dd
lm Z ej!e"Z#G dd deZ$e%e%e%e dddZ&eddddZ'eee%ef dddZ(dS )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)ÚRemovedInSphinx60Warning)Ú__)ÚSphinxPostTransform)ÚloggingÚrst)Úsplit_explicit_title)ÚRoleFunctionc @ s6 e Zd ZdZdZeddddZejdddd Z dS )
Ú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 jsd S x | jjtjD ]}| j| qW d S )N)ÚconfigÚextlinks_detect_hardcoded_linksÚdocumentÚfindallr Ú referenceÚ check_uri)Úselfr Úrefnode© r ú5/tmp/pip-build-gk9425m9/sphinx/sphinx/ext/extlinks.pyÚrun2 s zExternalLinksChecker.run)r r c C s d|ksd|krdS |d }|j }xØ| jjjj D ]Æ\}\}}tjdk rdtjtj |j
dd}ntjtj |j
dd}|j|}|r4|j j
d r4td
} |j j
d }
||krÖd| dtj | d
|
d}nd| d|
d}tj| |||d q4W dS )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ÚvaluezHhardcoded link %r could be replaced by an extlink (try using %r instead)ú:z:`z <z>`ú`)Úlocation)r# r$ )ZastextÚappr ÚextlinksÚitemsÚsysÚversion_infoÚreÚcompileÚescapeÚreplaceÚmatchÚ groupdictÚgetr