U
    \Sh:                     @  s  d Z ddlm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 ddlmZmZ ddlmZmZ ddlmZ dd	lmZ dd
lm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$ ddl%m&Z& ddl'm(Z(m)Z) erddl*m+Z+ ddl,m-Z- ddl.m/Z/ e"0e1Z2ddddhZ3G dd deZ4G dd deZ5G dd de4Z6d d!d"d#d$Z7G d%d& d&e4Z8G d'd( d(e4Z9G d)d* d*e4Z:G d+d, d,e4Z;ej<ej=ej>ej?ej@d-ZAG d.d/ d/e4ZBG d0d1 d1e4ZCG d2d3 d3e4ZDG d4d5 d5e4ZEG d6d7 d7e4ZFG d8d9 d9e4ZGG d:d; d;eZHG d<d= d=ee4ZIG d>d? d?e4ZJG d@dA dAe4ZKG dBdC dCe4ZLdDdEdFdGdHZMdS )Iz:Docutils transforms used by Sphinx when reading documents.    )annotationsN)TYPE_CHECKINGAny	Generatorcast)nodes)NodeText)	TransformTransformer)ContentsFilter)SmartQuotes)normalize_language_tag)
smartchars)addnodes)Config)___)logging)new_document)format_date)apply_source_workaroundis_smartquotable)Sphinx)StandardDomain)BuildEnvironmentversionreleasetodaytranslation progressc                   @  sF   e Zd ZdZeddddZeddddZed	dd
dZdS )SphinxTransformzA base class of Transforms.

    Compared with ``docutils.transforms.Transform``, this class improves accessibility to
    Sphinx APIs.
    r   returnc                 C  s   | j jS )z)Reference to the :class:`.Sphinx` object.)envappself r'   M/root/rtd-docs/venv/lib/python3.8/site-packages/sphinx/transforms/__init__.pyr$   0   s    zSphinxTransform.appr   c                 C  s
   | j jjS )z3Reference to the :class:`.BuildEnvironment` object.)documentsettingsr#   r%   r'   r'   r(   r#   5   s    zSphinxTransform.envr   c                 C  s   | j jS )z)Reference to the :class:`.Config` object.)r#   configr%   r'   r'   r(   r+   :   s    zSphinxTransform.configN)__name__
__module____qualname____doc__propertyr$   r#   r+   r'   r'   r'   r(   r    )   s   r    c                      sL   e Zd ZU dZded< dZded< ddd	d
dZdd fddZ  ZS )SphinxTransformerz#
    A transformer for Sphinx.
    nodes.documentr)   NzBuildEnvironment | Noner#   r   None)r#   r"   c                 C  s
   || _ d S N)r#   )r&   r#   r'   r'   r(   set_environmentH   s    z!SphinxTransformer.set_environmentr!   c                   s   t | jtjr:t| jjds.| jr.| j| jj_t   nFz6td}| jrT| j|j_|| j7 }|| _t   W 5 | jd | _X d S )Nr#   r    )	
isinstancer)   r   hasattrr*   r#   superapply_transformsr   )r&   r)   	__class__r'   r(   r:   K   s    

z"SphinxTransformer.apply_transforms)	r,   r-   r.   r/   __annotations__r#   r5   r:   __classcell__r'   r'   r;   r(   r1   @   s
   
r1   c                   @  s$   e Zd ZdZdZdddddZdS )	DefaultSubstitutionszL
    Replace some substitutions if they aren't defined in the document.
       r   r3   kwargsr"   c                 K  s   t t| jj }| jtjD ]j}|d }||kr|dkrFt| j}n
| j| }|dkrx|sxt	| jj
pltd| jjd}|t| qd S )Nrefnamer   r   z	%b %d, %Y)language)default_substitutionssetr)   Zsubstitution_defsfindallr   Zsubstitution_reference_calculate_translation_progressr+   r   Z	today_fmtr   rD   replace_selfr	   )r&   rB   Z	to_handlerefrC   textr'   r'   r(   applye   s    
zDefaultSubstitutions.applyNr,   r-   r.   r/   default_priorityrL   r'   r'   r'   r(   r?   ^   s   r?   r2   str)r)   r"   c                 C  sV   z| d }W n t k
r(   td Y S X |d }|d }|dkrJtdS || dS )Ntranslation_progressz)could not calculate translation progress!total
translatedr   zno translated elements!z.2%)KeyErrorr   )r)   rP   rQ   rR   r'   r'   r(   rH   w   s    rH   c                   @  s$   e Zd ZdZdZdddddZdS )	MoveModuleTargetszv
    Move module targets that are the first thing in a section to the section
    title.

    XXX Python specific
    r@   r   r3   rA   c                 K  sn   t | jtjD ]V}|d s qd|kr|jjtjkr|j|dkr|d |jd dd< |j	| qd S )NidsZismod   r   )
listr)   rG   r   targetparentr<   sectionindexremover&   rB   noder'   r'   r(   rL      s    zMoveModuleTargets.applyNrM   r'   r'   r'   r(   rT      s   rT   c                   @  s$   e Zd ZdZdZdddddZdS )	HandleCodeBlocksz5
    Several code block related transformations.
    r@   r   r3   rA   c                 K  s8   | j tjD ]$}tdd |jD r||j qd S )Nc                 s  s   | ]}t |tjV  qd S r4   )r7   r   doctest_block).0childr'   r'   r(   	<genexpr>   s     z)HandleCodeBlocks.apply.<locals>.<genexpr>)r)   rG   r   Zblock_quoteallchildrenrI   r]   r'   r'   r(   rL      s
    zHandleCodeBlocks.applyNrM   r'   r'   r'   r(   r_      s   r_   c                   @  s$   e Zd ZdZdZdddddZdS )	AutoNumberingzO
    Register IDs of tables, figures and literal_blocks to assign numbers.
    r@   r   r3   rA   c                 K  sT   | j jd }| jtjD ]4}||r||d k	r|d g kr| j| qd S )NZstdrU   )	r#   domainsr)   rG   r   ZElementZis_enumerable_nodeZget_numfig_titleZnote_implicit_target)r&   rB   domainr^   r'   r'   r(   rL      s    

zAutoNumbering.applyNrM   r'   r'   r'   r(   rf      s   rf   c                   @  s$   e Zd ZdZdZdddddZdS )	SortIdszA
    Sort section IDs so that the "id[0-9]+" one comes last.
    i  r   r3   rA   c                 K  s\   | j tjD ]H}t|d dkr|d d dr|d dd  |d d g |d< qd S )NrU   rV   r   id)r)   rG   r   rZ   len
startswithr]   r'   r'   r(   rL      s    "zSortIds.applyNrM   r'   r'   r'   r(   ri      s   ri   )zliteral-blockzdoctest-blockrawr[   imagec                   @  s$   e Zd ZdZdZdddddZdS )	ApplySourceWorkaroundz0
    Update source and rawsource attributes
    
   r   r3   rA   c                 K  s2   | j  D ]"}t|tjtjtjfr
t| q
d S r4   )r)   rG   r7   r   ZTextElementrn   Ztopicr   r]   r'   r'   r(   rL      s    zApplySourceWorkaround.applyNrM   r'   r'   r'   r(   ro      s   ro   c                   @  s$   e Zd ZdZdZdddddZdS )	AutoIndexUpgraderz[
    Detect old style (4 column based indices) and automatically upgrade to new style.
    r@   r   r3   rA   c                 K  s   | j tjD ]r}d|krtdd |d D rtd|d  }tj||d t|d D ]$\}}t	|dkrZ|d |d |< qZqd S )Nentriesc                 s  s   | ]}t |d kV  qdS )   N)rk   )ra   entryr'   r'   r(   rc      s     z*AutoIndexUpgrader.apply.<locals>.<genexpr>zG4 column based index found. It might be a bug of extensions you use: %r)locationrs   r4   )
r)   rG   r   r[   anyr   loggerwarning	enumeraterk   )r&   rB   r^   msgirt   r'   r'   r(   rL      s    zAutoIndexUpgrader.applyNrM   r'   r'   r'   r(   rq      s   rq   c                   @  s$   e Zd ZdZdZdddddZdS )	ExtraTranslatableNodesz!
    Make nodes translatable
    rp   r   r3   rA   c                   sX   | j jfddt D   s&d S ddd fdd}| j|D ]}d|d	< qFd S )
Nc                   s   g | ]\}}| kr|qS r'   r'   )ra   kv)targetsr'   r(   
<listcomp>   s      z0ExtraTranslatableNodes.apply.<locals>.<listcomp>r   boolr^   r"   c                   s   t | t S r4   )r7   tupler^   )target_nodesr'   r(   is_translatable_node   s    z:ExtraTranslatableNodes.apply.<locals>.is_translatable_nodeTZtranslatable)r+   Zgettext_additional_targetsTRANSLATABLE_NODESitemsr)   rG   )r&   rB   r   r^   r'   )r   r   r(   rL      s    zExtraTranslatableNodes.applyNrM   r'   r'   r'   r(   r|      s   r|   c                   @  s$   e Zd ZdZdZdddddZdS )	UnreferencedFootnotesDetectorz9
    Detect unreferenced footnotes and emit warnings
       r   r3   rA   c                   s   | j jD ]F  d g krq d d | j jkrtjtd d d dd d q| j jD ]4 t fdd| j jD sXtjtd	dd d qXd S )
Nnamesr   z Footnote [%s] is not referenced.rJ   Zfootnote)typesubtyperu   c                 3  s   | ]}|d   d  kV  qdS )autoNr'   )ra   rJ   r   r'   r(   rc     s     z6UnreferencedFootnotesDetector.apply.<locals>.<genexpr>zFootnote [#] is not referenced.)	r)   Z	footnotesZfootnote_refsrw   rx   r   Zautofootnotesrv   Zautofootnote_refsr&   rB   r'   r   r(   rL     s      
 z#UnreferencedFootnotesDetector.applyNrM   r'   r'   r'   r(   r     s   r   c                   @  s$   e Zd ZdZdZdddddZdS )	DoctestTransformz.Set "doctest" style to each doctest_block node  r   r3   rA   c                 K  s&   | j tjD ]}|d d qd S )Nclassesdoctest)r)   rG   r   r`   appendr]   r'   r'   r(   rL   !  s    zDoctestTransform.applyNrM   r'   r'   r'   r(   r     s   r   c                   @  s$   e Zd ZdZdZdddddZdS )	FilterSystemMessagesz&Filter system messages from a doctree.  r   r3   rA   c                 K  sT   | j jrdnd}t| jtjD ],}|d |k r"td|	  |j
| q"d S )N      levelz%s [filtered system message])r+   Zkeep_warningsrW   r)   rG   r   Zsystem_messagerw   debugastextrY   r\   )r&   rB   Zfilterlevelr^   r'   r'   r(   rL   *  s
    zFilterSystemMessages.applyNrM   r'   r'   r'   r(   r   &  s   r   c                   @  s&   e Zd ZdZejZdddddZdS )SphinxContentsFilterzx
    Used with BuildEnvironment.add_toc_from() to discard cross-file links
    within table-of-contents link nodes.
    znodes.imager3   r   c                 C  s
   t jd S r4   )r   ZSkipNode)r&   r^   r'   r'   r(   visit_image9  s    z SphinxContentsFilter.visit_imageN)r,   r-   r.   r/   r   Z ignore_node_but_process_childrenZvisit_pending_xrefr   r'   r'   r'   r(   r   2  s   r   c                      sJ   e Zd ZdZdZddd fddZdd	d
dZdddddZ  ZS )SphinxSmartQuoteszr
    Customized SmartQuotes to avoid transform for some extra node types.

    refs: sphinx.parsers.RSTParser
    i  r   r3   rA   c                   s$   |   sd S | jj| _t   d S r4   )is_availabler+   Zsmartquotes_actionr9   rL   r   r;   r'   r(   rL   E  s    
zSphinxSmartQuotes.applyr   r!   c                 C  s   | j jdg }| j jdg }| jjjdkr2dS | j jdkrBdS | jjj	|krTdS | j j
|krddS | jjd }tdd t|D S )Nbuilders	languagesFZlanguage_codec                 s  s   | ]}|t jkV  qd S r4   )r   quotes)ra   tagr'   r'   r(   rc   a  s   z1SphinxSmartQuotes.is_available.<locals>.<genexpr>)r+   Zsmartquotes_excludesgetr)   r*   Zsmart_quotesZsmartquotesr$   ZbuildernamerD   r#   rv   r   )r&   r   r   rD   r'   r'   r(   r   N  s    zSphinxSmartQuotes.is_availablez
list[Text]z&Generator[tuple[str, str], None, None])txtnodesr"   c                 c  sB   |D ]8}t |r.tddt|}d|fV  qd| fV  qd S )Nz(?<=\x00)([-\\\'".`])z\\\1plainliteral)r   resubrO   r   )r&   r   ZtxtnoderK   r'   r'   r(   
get_tokensf  s
    zSphinxSmartQuotes.get_tokens)	r,   r-   r.   r/   rN   rL   r   r   r>   r'   r'   r;   r(   r   =  s
   	r   c                   @  s$   e Zd ZdZdZdddddZdS )	DoctreeReadEventz!Emit :event:`doctree-read` event.ip  r   r3   rA   c                 K  s   | j d| j d S )Nzdoctree-read)r$   emitr)   r   r'   r'   r(   rL   w  s    zDoctreeReadEvent.applyNrM   r'   r'   r'   r(   r   s  s   r   c                   @  s$   e Zd ZdZdZdddddZdS )	ManpageLinkz&Find manpage section numbers and namesr   r   r3   rA   c                 K  sb   | j tjD ]N}ddd |jD }d}||dd}t||}|rP| }|j	
| qd S )N c                 S  s    g | ]}t |tjrt|qS r'   )r7   r   r	   rO   )ra   xr'   r'   r(   r     s    z%ManpageLink.apply.<locals>.<listcomp>z7^(?P<path>(?P<page>.+)[\(\.](?P<section>[1-9]\w*)?\)?)$r6   )pathpagerZ   )r)   rG   r   manpagejoinre   r   match	groupdict
attributesupdate)r&   rB   r^   r   patterninforr'   r'   r(   rL     s    zManpageLink.applyNrM   r'   r'   r'   r(   r   {  s   r   c                   @  s$   e Zd ZdZdZdddddZdS )	GlossarySorterz.Sort glossaries that have the ``sorted`` flag.r   r   r3   rA   c                 K  sH   | j tjD ]4}|d rttj|d }t|dd d|d d < qd S )Nsortedr   c                 S  s    t dttj| d   S )NZNFDr   )unicodedata	normalizer   r   termr   lower)itemr'   r'   r(   <lambda>  s   z&GlossarySorter.apply.<locals>.<lambda>)key)r)   rG   r   glossaryr   r   definition_listr   )r&   rB   r   r   r'   r'   r(   rL     s    zGlossarySorter.applyNrM   r'   r'   r'   r(   r     s   r   r   zdict[str, Any])r$   r"   c                 C  s   |  t |  t |  t |  t |  t |  t |  t |  t |  t	 |  t
 |  t |  t |  t |  t |  t ddddS )NbuiltinT)r   Zparallel_read_safeZparallel_write_safe)Zadd_transformro   r|   r?   rT   r_   ri   r   rf   rq   r   r   r   r   r   r   )r$   r'   r'   r(   setup  s&    














r   )Nr/   
__future__r   r   r   typingr   r   r   r   Zdocutilsr   Zdocutils.nodesr   r	   Zdocutils.transformsr
   r   Zdocutils.transforms.partsr   Zdocutils.transforms.universalr   Zdocutils.utilsr   Zdocutils.utils.smartquotesr   Zsphinxr   Zsphinx.configr   Zsphinx.localer   r   Zsphinx.utilr   Zsphinx.util.docutilsr   Zsphinx.util.i18nr   Zsphinx.util.nodesr   r   Zsphinx.applicationr   Zsphinx.domains.stdr   Zsphinx.environmentr   	getLoggerr,   rw   rE   r    r1   r?   rH   rT   r_   rf   ri   Zliteral_blockr`   rm   r[   rn   r   ro   rq   r|   r   r   r   r   r   r   r   r   r   r'   r'   r'   r(   <module>   sl   
		6