o
    NiG                     @  s  U d Z ddlmZ ddlZddlZddlmZmZ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 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rd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/m0Z0 ddl1m2Z2 ddl3m4Z4 ddl5m6Z6 ddl7m8Z8 e(d Z9de:d< e;e<Z=e>h dZ?G dd  d eZ@G d!d" d"eZAG d#d$ d$e@ZBdYd)d*ZCG d+d, d,e@ZDG d-d. d.e@ZEG d/d0 d0e@ZFG d1d2 d2e@ZGe
jHe
jIe
jJejKe
jLd3ZMG d4d5 d5e@ZNG d6d7 d7e@ZOG d8d9 d9e@ZPG d:d; d;e@ZQG d<d= d=e@ZRG d>d? d?e@ZSG d@dA dAeZTG dBdC dCee@ZUG dDdE dEe@ZVG dFdG dGe@ZWG dHdI dIe@ZXdZdMdNZYd[dRdSZZd\dWdXZ[dS )]z:Docutils transforms used by Sphinx when reading documents.    )annotationsN)TYPE_CHECKINGAnycast)nodes)	TransformTransformer)ContentsFilter)	Footnotes)SmartQuotes)normalize_language_tag)
smartchars)addnodes)___)logging)new_document)format_date)apply_source_workaroundis_smartquotable)Iterator)Literal	TypeAlias)NodeText)TypeIs)Sphinx)Config)StandardDomain)BuildEnvironment)ExtensionMetadata)versionreleasetodaytranslation progressr   _DEFAULT_SUBSTITUTION_NAMES>   r#   r"   r!   r$   c                   @  s:   e Zd ZdZedddZedddZedd
dZdS )SphinxTransformzA base class of Transforms.

    Compared with ``docutils.transforms.Transform``, this class improves accessibility to
    Sphinx APIs.
    returnr   c                 C     | j jS )z)Reference to the :class:`.Sphinx` object.)envappself r-   N/home/ubuntu/.local/lib/python3.10/site-packages/sphinx/transforms/__init__.pyr*   >      zSphinxTransform.appr   c                 C  s
   | j jjS )z3Reference to the :class:`.BuildEnvironment` object.)documentsettingsr)   r+   r-   r-   r.   r)   C   s   
zSphinxTransform.envr   c                 C  r(   )z)Reference to the :class:`.Config` object.)r)   configr+   r-   r-   r.   r2   H   r/   zSphinxTransform.configN)r'   r   )r'   r   )r'   r   )__name__
__module____qualname____doc__propertyr*   r)   r2   r-   r-   r-   r.   r&   7   s    r&   c                      sB   e Zd ZU dZded< dZded< dd
dZd fddZ  ZS )SphinxTransformerz#
    A transformer for Sphinx.
    nodes.documentr0   NzBuildEnvironment | Noner)   r   r'   Nonec                 C  s
   || _ d S N)r)   )r,   r)   r-   r-   r.   set_environmentV      
z!SphinxTransformer.set_environmentc                   s   t | jtjrt| jjds| jr| j| jj_t   d S z"td}| jr+| j|j_|| j7 }|| _t   W | jd | _d S | jd | _w )Nr)    r   )	
isinstancer0   r   hasattrr1   r)   superapply_transformsr   )r,   r0   	__class__r-   r.   rB   Y   s   

z"SphinxTransformer.apply_transforms)r)   r   r'   r:   )r'   r:   )	r3   r4   r5   r6   __annotations__r)   r<   rB   __classcell__r-   r-   rC   r.   r8   N   s   
 
r8   c                   @  s(   e Zd ZdZdZdddZdddZdS )DefaultSubstitutionszL
    Replace some substitutions if they aren't defined in the document.
       kwargsr   r'   r:   c                 K  sH   t t| jj }| jtjD ]}|d  }|v r!|| | qd S )Nrefname)	_DEFAULT_SUBSTITUTIONSsetr0   substitution_defsfindallr   substitution_referencereplace_self_handle_default_substitution)r,   rI   	to_handlerefnamer-   r-   r.   applyt   s   zDefaultSubstitutions.applyrT   r%   
nodes.Textc                 C  sn   |dkrt t| jS |dkr.| jj }rt |S | jjp"td}t t|| jj	dS t t
| j|S )Nr$   r#   z	%b %d, %Y)language)r   r   _calculate_translation_progressr0   r2   r#   	today_fmtr   r   rW   getattr)r,   rT   textrY   r-   r-   r.   rQ   {   s   
z1DefaultSubstitutions._handle_default_substitutionNrI   r   r'   r:   )rT   r%   r'   rV   )r3   r4   r5   r6   default_priorityrU   rQ   r-   r-   r-   r.   rG   l   s
    
rG   r0   r9   r'   strc                 C  sT   z| d }W n t y   td Y S w |d }|d }|dkr$tdS || dS )Ntranslation_progressz)could not calculate translation progress!total
translatedr   zno translated elements!z.2%)KeyErrorr   )r0   r_   r`   ra   r-   r-   r.   rX      s   rX   c                   @     e Zd ZdZdZ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
    rH   rI   r   r'   r:   c                 K  sp   t | jtjD ],}|d sq	d|v r5t|jtju r5|j|dkr5|d |jd dd< |j	| q	d S )Nidsismod   r   )
listr0   rN   r   targettypeparentsectionindexremover,   rI   noder-   r-   r.   rU      s   zMoveModuleTargets.applyNr\   r3   r4   r5   r6   r]   rU   r-   r-   r-   r.   rd      s    rd   c                   @  rc   )HandleCodeBlocksz5
    Several code block related transformations.
    rH   rI   r   r'   r:   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 r;   )r?   r   doctest_block).0childr-   r-   r.   	<genexpr>       z)HandleCodeBlocks.apply.<locals>.<genexpr>)r0   rN   r   block_quoteallchildrenrP   ro   r-   r-   r.   rU      s
   zHandleCodeBlocks.applyNr\   rq   r-   r-   r-   r.   rr          rr   c                   @  rc   )AutoNumberingzO
    Register IDs of tables, figures and literal_blocks to assign numbers.
    rH   rI   r   r'   r:   c                 K  sR   | j jj}| jtjD ]}||r&||d ur&|d g kr&| j	| qd S )Nre   )
r)   domainsstandard_domainr0   rN   r   Elementis_enumerable_nodeget_numfig_titlenote_implicit_target)r,   rI   domainrp   r-   r-   r.   rU      s   
zAutoNumbering.applyNr\   rq   r-   r-   r-   r.   r|      r{   r|   c                   @  rc   )SortIdszA
    Sort section IDs so that the "id[0-9]+" one comes last.
    i  rI   r   r'   r:   c                 K  s\   | j tjD ]$}t|d dkr+|d d dr+|d dd  |d d g |d< qd S )Nre      r   id)r0   rN   r   rl   len
startswithro   r-   r-   r.   rU      s
   ""zSortIds.applyNr\   rq   r-   r-   r-   r.   r      r{   r   )zliteral-blockzdoctest-blockrawrm   imagec                   @  rc   )ApplySourceWorkaroundz0
    Update source and rawsource attributes
    
   rI   r   r'   r:   c                 K  s4   | j  D ]}t|tjtjB tjB rt| qd S r;   )r0   rN   r?   r   TextElementr   topicr   ro   r-   r-   r.   rU      s
   zApplySourceWorkaround.applyNr\   rq   r-   r-   r-   r.   r      r{   r   c                   @  rc   )AutoIndexUpgraderz[
    Detect old style (4 column based indices) and automatically upgrade to new style.
    rH   rI   r   r'   r:   c                 K  s   | j tjD ]<}d|v rCtdd |d D rCtd|d  }tj||d t|d D ]\}}t	|dkrBg |d R |d |< q-qd S )Nentriesc                 s  s    | ]	}t |d kV  qdS )   N)r   )rt   entryr-   r-   r.   rv   	  rw   z*AutoIndexUpgrader.apply.<locals>.<genexpr>zG4 column based index found. It might be a bug of extensions you use: %r)locationr   )
r0   rN   r   rm   anyr   loggerwarning	enumerater   )r,   rI   rp   msgir   r-   r-   r.   rU     s   zAutoIndexUpgrader.applyNr\   rq   r-   r-   r-   r.   r      r{   r   c                   @  rc   )ExtraTranslatableNodesz!
    Make nodes translatable
    r   rI   r   r'   r:   c                   sZ   t | jjtfddt D   sd S d fdd}| j|D ]}d	|d
< q$d S )Nc                 3  s     | ]\}}| v r|V  qd S r;   r-   )rt   kv)targetsr-   r.   rv      s    z/ExtraTranslatableNodes.apply.<locals>.<genexpr>rp   r   r'   TypeIs[nodes.Element]c                   s
   t |  S r;   )r?   )rp   )target_nodesr-   r.   is_translatable_node$  r=   z:ExtraTranslatableNodes.apply.<locals>.is_translatable_nodeTtranslatable)rp   r   r'   r   )	frozensetr2   gettext_additional_targetstupleTRANSLATABLE_NODESitemsr0   rN   )r,   rI   r   rp   r-   )r   r   r.   rU     s   
zExtraTranslatableNodes.applyNr\   rq   r-   r-   r-   r.   r     r{   r   c                   @  s$   e Zd ZdZejd Zd
ddZd	S )UnreferencedFootnotesDetectorz9
    Detect unreferenced footnotes and emit warnings
    rg   rI   r   r'   r:   c                 K  s   | j jD ]$}|d s(|d r(tjtd|d r|d d n|d d dd|d q| j jD ]}|d s>tjtd	dd|d q-| j jD ]}|d sX|d rXtjtd
dd|d qCd S )Nbackrefsnamesz Footnote [%s] is not referenced.r   dupnamesrS   footnote)rj   subtyper   zFootnote [*] is not referenced.zFootnote [#] is not referenced.)r0   	footnotesr   r   r   symbol_footnotesautofootnotesro   r-   r-   r.   rU   2  s:   z#UnreferencedFootnotesDetector.applyNr\   )r3   r4   r5   r6   r
   r]   rU   r-   r-   r-   r.   r   +  s    
r   c                   @  rc   )DoctestTransformz.Set "doctest" style to each doctest_block node  rI   r   r'   r:   c                 K  s&   | j tjD ]	}|d d qd S )Nclassesdoctest)r0   rN   r   rs   appendro   r-   r-   r.   rU   Y  s   zDoctestTransform.applyNr\   rq   r-   r-   r-   r.   r   T      r   c                   @  rc   )FilterSystemMessagesz&Filter system messages from a doctree.i  rI   r   r'   r:   c                 K  sT   | j jrdnd}t| jtjD ]}|d |k r'td|	  |j
| qd S )Nrg      levelz%s [filtered system message])r2   keep_warningsrh   r0   rN   r   system_messager   debugastextrk   rn   )r,   rI   filterlevelrp   r-   r-   r.   rU   c  s   zFilterSystemMessages.applyNr\   rq   r-   r-   r-   r.   r   ^  r   r   c                   @  s    e Zd ZdZejZd	ddZdS )
SphinxContentsFilterzx
    Used with BuildEnvironment.add_toc_from() to discard cross-file links
    within table-of-contents link nodes.
    rp   nodes.imager'   r:   c                 C  s   t jr;   )r   SkipNode)r,   rp   r-   r-   r.   visit_images  s   z SphinxContentsFilter.visit_imageN)rp   r   r'   r:   )r3   r4   r5   r6   r	    ignore_node_but_process_childrenvisit_pending_xrefr   r-   r-   r-   r.   r   k  s    r   c                      s:   e Zd ZdZdZd fddZdd
dZdddZ  ZS )SphinxSmartQuoteszr
    Customized SmartQuotes to avoid transform for some extra node types.

    refs: sphinx.parsers.RSTParser
    i  rI   r   r'   r:   c                   s$   |   sd S | jj| _t   d S r;   )is_availabler2   smartquotes_actionrA   rU   r,   rI   rC   r-   r.   rU     s   
zSphinxSmartQuotes.applyboolc                 C  s   | j jdg }| j jdg }| jjjdu rdS | j jdu r!dS | jjj	|v r*dS | j j
|v r2dS | jjd }tdd t|D S )Nbuilders	languagesFlanguage_codec                 s  s    | ]}|t jv V  qd S r;   )r   quotes)rt   tagr-   r-   r.   rv     s    z1SphinxSmartQuotes.is_available.<locals>.<genexpr>)r2   smartquotes_excludesgetr0   r1   smart_quotessmartquotesr*   builderrT   rW   r)   r   r   )r,   r   r   rW   r-   r-   r.   r     s   zSphinxSmartQuotes.is_availabletxtnodes
list[Text]Iterator[tuple[str, str]]c                 c  sD    |D ]}t |rtddt|}d|fV  qd| fV  qd S )Nz(?<=\x00)([-\\\'".`])z\\\1plainliteral)r   resubr^   r   )r,   r   txtnoder[   r-   r-   r.   
get_tokens  s   zSphinxSmartQuotes.get_tokensr\   )r'   r   )r   r   r'   r   )	r3   r4   r5   r6   r]   rU   r   r   rF   r-   r-   rC   r.   r   w  s    
	r   c                   @  rc   )DoctreeReadEventz!Emit :event:`doctree-read` event.ip  rI   r   r'   r:   c                 K  s   | j d| j d S )Nzdoctree-read)r*   emitr0   r   r-   r-   r.   rU     s   zDoctreeReadEvent.applyNr\   rq   r-   r-   r-   r.   r     r   r   c                   @  rc   )GlossarySorterz.Sort glossaries that have the ``sorted`` flag.r   rI   r   r'   r:   c                 K  sH   | j tjD ]}|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 )NNFDr   )unicodedata	normalizer   r   termr   lower)itemr-   r-   r.   <lambda>  s    z&GlossarySorter.apply.<locals>.<lambda>key)r0   rN   r   glossaryr   r   definition_listr   )r,   rI   r   r   r-   r-   r.   rU     s   zGlossarySorter.applyNr\   rq   r-   r-   r-   r.   r     r{   r   c                   @  rc   )%ReorderConsecutiveTargetAndIndexNodesa  Index nodes interspersed between target nodes prevent other
    Transformations from combining those target nodes,
    e.g. ``PropagateTargets``.  This transformation reorders them:

    Given the following ``document`` as input::

        <document>
            <target ids="id1" ...>
            <index entries="...1...">
            <target ids="id2" ...>
            <target ids="id3" ...>
            <index entries="...2...">
            <target ids="id4" ...>

    The transformed result will be::

        <document>
            <index entries="...1...">
            <index entries="...2...">
            <target ids="id1" ...>
            <target ids="id2" ...>
            <target ids="id3" ...>
            <target ids="id4" ...>
       rI   r   r'   r:   c                 K  s    | j tjD ]}t| qd S r;   )r0   rN   r   ri   _reorder_index_target_nodes)r,   rI   ri   r-   r-   r.   rU     s   
z+ReorderConsecutiveTargetAndIndexNodes.applyNr\   rq   r-   r-   r-   r.   r     s    r   
start_nodenodes.targetr:   c                 C  s   g }| j dddD ]}t|tjtjB r|| q	 t|dk r#dS |d j}||d jkrU||d }||d }|t| d |krWt	|t
d	|||d < dS dS dS )
zSort target and index nodes.

    Find all consecutive target and index nodes starting from ``start_node``,
    and move all index nodes to before the first target node.
    FT)descendsiblingsrg   Nr   r   r   )rN   r?   r   ri   r   rm   r   r   rk   r   	_sort_key)r   nodes_to_reorderrp   rk   	first_idxlast_idxr-   r-   r.   r     s    

r   rp   
nodes.Nodeintc                 C  s6   t | tjrdS t | tjrdS dt| }t|)Nr   r   z+_sort_key called with unexpected node type )r?   r   rm   r   ri   rj   
ValueError)rp   r   r-   r-   r.   r     s   r   r*   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!   parallel_read_safeparallel_write_safe)add_transformr   r   rG   rd   rr   r   r   r|   r   r   r   r   r   r   r   )r*   r-   r-   r.   setup  s&   














r   )r0   r9   r'   r^   )r   r   r'   r:   )rp   r   r'   r   )r*   r   r'   r    )\r6   
__future__r   r   r   typingr   r   r   docutilsr   docutils.transformsr   r   docutils.transforms.partsr	   docutils.transforms.referencesr
   docutils.transforms.universalr   docutils.utilsr   docutils.utils.smartquotesr   sphinxr   sphinx.localer   r   sphinx.utilr   sphinx.util.docutilsr   sphinx.util.i18nr   sphinx.util.nodesr   r   collections.abcr   r   r   docutils.nodesr   r   typing_extensionsr   sphinx.applicationr   sphinx.configr   sphinx.domains.stdr   sphinx.environmentr   sphinx.util.typingr    r%   rE   	getLoggerr3   r   r   rK   r&   r8   rG   rX   rd   rr   r|   r   literal_blockrs   r   rm   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r-   r-   r-   r.   <module>   sz    

	)
4	
"

