o
    NiՏ                     @   s   d Z dZddlmZmZ ddlmZ G dd deZG dd deZG d	d
 d
eZ	G dd deZ
G dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd dejZdS )z&
Transforms for resolving references.
reStructuredText    )nodesutils)	Transformc                   @      e Zd ZdZdZdd ZdS )PropagateTargetsa  
    Propagate empty internal targets to the next element.

    Given the following nodes::

        <target ids="internal1" names="internal1">
        <target anonymous="1" ids="id1">
        <target ids="internal2" names="internal2">
        <paragraph>
            This is a test.

    `PropagateTargets` propagates the ids and names of the internal
    targets preceding the paragraph to the paragraph itself::

        <target refid="internal1">
        <target anonymous="1" refid="id1">
        <target refid="internal2">
        <paragraph ids="internal2 id1 internal1" names="internal2 internal1">
            This is a test.
    i  c                 C   s  | j tjD ]}t|jtjs|ds|ds|dr qt|dks*J d|j	dd}t|tj
rC|j	ddd	}t|tj
s6|d u sVt|tjtjfrWt|tjsWq|d
 |d
  |d |d  t|dsqi |_t|dsyi |_|d
 D ]}|| j j|< ||j|< q}|d D ]}||j|< q|jt|di  |jt|di  |d
 d |d< g |d
< g |d< | j | qd S )Nrefidrefurirefnamer   z&error: block-level target has childrenT)ascendF)r   descendidsnamesexpect_referenced_by_nameexpect_referenced_by_id)documentfindallr   target
isinstanceparentTextElementhasattrlen	next_nodesystem_message	Invisible
Targetableextendr   r   r   updategetattr
note_refid)selfr   r   idname r$   R/home/ubuntu/.local/lib/python3.10/site-packages/docutils/transforms/references.pyapply(   sT   




zPropagateTargets.applyN__name__
__module____qualname____doc__default_priorityr&   r$   r$   r$   r%   r      s    r   c                   @   r   )AnonymousHyperlinksa  
    Link anonymous references to targets.  Given::

        <paragraph>
            <reference anonymous="1">
                internal
            <reference anonymous="1">
                external
        <target anonymous="1" ids="id1">
        <target anonymous="1" ids="id2" refuri="http://external">

    Corresponding references are linked via "refid" or resolved via "refuri"::

        <paragraph>
            <reference anonymous="1" refid="id1">
                text
            <reference anonymous="1" refuri="http://external">
                external
        <target anonymous="1" ids="id1">
        <target anonymous="1" ids="id2" refuri="http://external">
    i  c           
      C   sJ  g }g }| j tjD ]}|dr|| q| j tjD ]}|dr+|| qt|t|krj| j j	dt|t|f }| j 
|}|D ]}tj|j|j|d}| j 
|}|| || qKd S t||D ]3\}}	d|	_	 |	dr|	d |d< d|_n|	d s| j j|	d  }	qv|	d d	 |d< | j | 	 qod S )
N	anonymousz]Anonymous hyperlink mismatch: %s references but %s targets.
See "backrefs" attribute for IDs.r      Tr	   r   r   r   )r   r   r   	referencegetappendr   r   reportererrorset_idproblematic	rawsourceadd_backrefreplace_selfzip
referencedr   resolvedr   r    )
r!   anonymous_refsanonymous_targetsnodemsgmsgidrefprbprbidr   r$   r$   r%   r&   s   sP   






zAnonymousHyperlinks.applyNr'   r$   r$   r$   r%   r-   Y   s    r-   c                   @   sD   e Zd ZdZdZdd Zdd Zdd Zd	d
 Zdd Z	dd Z
dS )IndirectHyperlinksaN  
    a) Indirect external references::

           <paragraph>
               <reference refname="indirect external">
                   indirect external
           <target id="id1" name="direct external"
               refuri="http://indirect">
           <target id="id2" name="indirect external"
               refname="direct external">

       The "refuri" attribute is migrated back to all indirect targets
       from the final direct target (i.e. a target not referring to
       another indirect target)::

           <paragraph>
               <reference refname="indirect external">
                   indirect external
           <target id="id1" name="direct external"
               refuri="http://indirect">
           <target id="id2" name="indirect external"
               refuri="http://indirect">

       Once the attribute is migrated, the preexisting "refname" attribute
       is dropped.

    b) Indirect internal references::

           <target id="id1" name="final target">
           <paragraph>
               <reference refname="indirect internal">
                   indirect internal
           <target id="id2" name="indirect internal 2"
               refname="final target">
           <target id="id3" name="indirect internal"
               refname="indirect internal 2">

       Targets which indirectly refer to an internal target become one-hop
       indirect (their "refid" attributes are directly set to the internal
       target's "id"). References which indirectly refer to an internal
       target become direct internal references::

           <target id="id1" name="final target">
           <paragraph>
               <reference refid="id1">
                   indirect internal
           <target id="id2" name="indirect internal 2" refid="id1">
           <target id="id3" name="indirect internal" refid="id1">
    i  c                 C   s,   | j jD ]}|js| | | | qd S N)r   indirect_targetsr=   resolve_indirect_targetresolve_indirect_referencesr!   r   r$   r$   r%   r&      s
   
zIndirectHyperlinks.applyc                 C   sH  | d}|d u r|d }n| jj |}|s-| jjjD ]	}||r% d S q| | d S | jj| }|j|d t|t	j
r]|js]|dr]t|drS| | d S d|_| | |`|drp|d |d< d|v ro|d= n(|dr|d |d< | j| n|d r||d< | j| n| | d S |d ur|d= d|_d S )Nr
   r   r"   multiply_indirectr0   r	   r   )r2   r   nameidstransformerunknown_reference_resolversnonexistent_indirect_targetr   note_referenced_byr   r   r   r=   r   circular_indirect_referencerM   rI   r    )r!   r   r
   reftarget_idresolver_function	reftargetr$   r$   r%   rI      sT   









z*IndirectHyperlinks.resolve_indirect_targetc                 C   s0   |d | j jv r| |d d S | |d d S )Nr
   z>which is a duplicate, and cannot be used as a unique referencezwhich does not exist)r   rN   indirect_target_errorrK   r$   r$   r%   rQ     s   z.IndirectHyperlinks.nonexistent_indirect_targetc                 C   s   |  |d d S )Nzforming a circular reference)rW   rK   r$   r$   r%   rS   	  s   z.IndirectHyperlinks.circular_indirect_referencec                 C   s   d}g }|d rd|d d  }|d D ]}| | jj|g  q|d D ]}| | jj|g  q&|d rB|d|d d  7 }| jjjd||d |f |d	}| j|}t	|D ]}	t
j|	j|	j|d
}
| j|
}|| |	|
 q]d|_d S )N r   z"%s" r   r   z	(id="%s")z7Indirect hyperlink target %s refers to target "%s", %s.r
   	base_noder/   r0   )r   r   refnamesr2   refidsr4   r5   r6   r   uniqr   r7   r8   r9   r:   r=   )r!   r   explanationnamingreflistr#   r"   rA   rB   rC   rD   rE   r$   r$   r%   rW     s2   


z(IndirectHyperlinks.indirect_target_errorc           	      C   s(  | drd}| jj}n| drd}d }nd S || }|d D ]6}| jj|g }|r2|j|d |D ]!}|jr:q4|d= |||< |rG|| d|_t|tj	rU| 
| q4q |d D ]6}| jj|g }|rm|j|d |D ]!}|jruqo|d= |||< |r|| d|_t|tj	r| 
| qoq[d S )	Nr   r	   r   r#   r
   r0   r   rL   )r   r   r    r[   r2   rR   r=   r   r   r   rJ   r\   )	r!   r   attnamecall_methodattvalr#   r`   rC   r"   r$   r$   r%   rJ   #  sR   





z.IndirectHyperlinks.resolve_indirect_referencesN)r(   r)   r*   r+   r,   r&   rI   rQ   rS   rW   rJ   r$   r$   r$   r%   rF      s    2,rF   c                   @   r   )ExternalTargetsa  
    Given::

        <paragraph>
            <reference refname="direct external">
                direct external
        <target id="id1" name="direct external" refuri="http://direct">

    The "refname" attribute is replaced by the direct "refuri" attribute::

        <paragraph>
            <reference refuri="http://direct">
                direct external
        <target id="id1" name="direct external" refuri="http://direct">
    i  c                 C   s~   | j tjD ]5}|dr<|d }|d D ]%}| j j|g }|r(|j|d |D ]}|jr0q*|d= ||d< d|_q*qqd S )Nr	   r   ra   r
   r0   )	r   r   r   r   r   r[   r2   rR   r=   )r!   r   r	   r#   r`   rC   r$   r$   r%   r&   _  s    
zExternalTargets.applyNr'   r$   r$   r$   r%   re   K  s    re   c                   @   s    e Zd ZdZdd Zdd ZdS )InternalTargetsi  c                 C   s6   | j tjD ]}|ds|ds| | qd S )Nr	   r   )r   r   r   r   r   resolve_reference_idsrK   r$   r$   r%   r&   s  s
   
zInternalTargets.applyc                 C   sj   |d D ].}| j j|}| j j|g }|r|j|d |D ]}|jr%q|r.|d= ||d< d|_qqdS )a  
        Given::

            <paragraph>
                <reference refname="direct internal">
                    direct internal
            <target id="id1" name="direct internal">

        The "refname" attribute is replaced by "refid" linking to the target's
        "id"::

            <paragraph>
                <reference refid="id1">
                    direct internal
            <target id="id1" name="direct internal">
        r   ra   r
   r   r0   N)r   rN   r2   r[   rR   r=   )r!   r   r#   r   r`   rC   r$   r$   r%   rg   x  s   z%InternalTargets.resolve_reference_idsN)r(   r)   r*   r,   r&   rg   r$   r$   r$   r%   rf   o  s    rf   c                   @   sR   e Zd ZdZdZdZ	 g dZdd Zdd Zd	d
 Z	dd Z
dd Zdd ZdS )	Footnotesa  
    Assign numbers to autonumbered footnotes, and resolve links to footnotes,
    citations, and their references.

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

        <document>
            <paragraph>
                A labeled autonumbered footnote reference:
                <footnote_reference auto="1" id="id1" refname="footnote">
            <paragraph>
                An unlabeled autonumbered footnote reference:
                <footnote_reference auto="1" id="id2">
            <footnote auto="1" id="id3">
                <paragraph>
                    Unlabeled autonumbered footnote.
            <footnote auto="1" id="footnote" name="footnote">
                <paragraph>
                    Labeled autonumbered footnote.

    Auto-numbered footnotes have attribute ``auto="1"`` and no label.
    Auto-numbered footnote_references have no reference text (they're
    empty elements). When resolving the numbering, a ``label`` element
    is added to the beginning of the ``footnote``, and reference text
    to the ``footnote_reference``.

    The transformed result will be::

        <document>
            <paragraph>
                A labeled autonumbered footnote reference:
                <footnote_reference auto="1" id="id1" refid="footnote">
                    2
            <paragraph>
                An unlabeled autonumbered footnote reference:
                <footnote_reference auto="1" id="id2" refid="id3">
                    1
            <footnote auto="1" id="id3" backrefs="id2">
                <label>
                    1
                <paragraph>
                    Unlabeled autonumbered footnote.
            <footnote auto="1" id="footnote" name="footnote" backrefs="id1">
                <label>
                    2
                <paragraph>
                    Labeled autonumbered footnote.

    Note that the footnotes are not in the same order as the references.

    The labels and reference text are added to the auto-numbered ``footnote``
    and ``footnote_reference`` elements.  Footnote elements are backlinked to
    their references via "refids" attributes.  References are assigned "id"
    and "refid" attributes.

    After adding labels and reference text, the "auto" attributes can be
    ignored.
    il  N)
*u   †u   ‡   §   ¶#u   ♠u   ♥u   ♦u   ♣c                 C   s:   g | _ | jj}| || j_| | |   |   d S rG   )autofootnote_labelsr   autofootnote_startnumber_footnotesnumber_footnote_referencessymbolize_footnotesresolve_footnotes_and_citations)r!   startnumr$   r$   r%   r&     s   
zFootnotes.applyc                 C   s  | j jD ]}	 t|}|d7 }|| j jvrnq|dtd| |d D ]I}| j j|g D ]>}|t	|7 }|
d t|d t|d   krPdksSJ  J |d d |d< ||d d  | j | d|_q/q%|d s|d	 s|d | | j || | j| q|S )
z
        Assign numbers to autonumbered footnotes.

        For labeled autonumbered footnotes, copy the number over to
        corresponding footnote references.
        Tr0   r   rX   r   r
   r   r   dupnames)r   autofootnotesstrrN   insertr   labelfootnote_refsr2   Textdelattrr   r9   r    r=   r3   note_explicit_targetrm   )r!   rs   footnoterx   r#   rC   r$   r$   r%   ro     s0   
,zFootnotes.number_footnotesc              
   C   s@  d}| j jD ]}|js|drqz| j| }W nM tye   | j jjdt| j |d}| j 	|}| j j|d D ]%}|jsE|drFq;t
j|j|j|d}| j 	|}|| || q;Y  dS w |t
|7 }| j j| }	| j j|	 }
|	|d< | j | t|d d	ksJ |
|d d  d	|_|d	7 }qdS )
z3Assign numbers to autonumbered footnote references.r   r   zUToo many autonumbered footnote references: only %s corresponding footnotes available.rY   Nr
   r/   r   r0   )r   autofootnote_refsr=   r   rm   
IndexErrorr4   r5   r   r6   r   r7   r8   r9   r:   rz   rN   r   r    )r!   rs   irC   rx   rA   rB   rD   rE   r"   r}   r$   r$   r%   rp     sF   


z$Footnotes.number_footnote_referencesc              
   C   s  g }| j jD ]4}t| j jt| j\}}| j| |d  }|| |dt	d| | j  jd7  _| j 
| qd}| j jD ]}z|t|| 7 }W nL ty   | j jjdt| |d}| j 
|}	| j j|d D ]%}|jsz|dr{qptj|j|j|	d}
| j 
|
}|| ||
 qpY  dS w | j j| }t|d	 dksJ |d	 d |d< | j | ||d	 d  |d7 }qAdS )
z<Add symbols indexes to "[*]"-style footnotes and references.r0   r   rX   zOToo many symbol footnote references: only %s corresponding footnotes available.rY   Nr   r/   r   )r   symbol_footnotesdivmodsymbol_footnote_startr   symbolsr3   rw   r   rx   r6   symbol_footnote_refsrz   r   r4   r5   r=   r   r7   r8   r9   r:   r    )r!   labelsr}   repsindex	labeltextr   rC   rA   rB   rD   rE   r$   r$   r%   rq   0  sP   



zFootnotes.symbolize_footnotesc                 C   s   | j jD ]}|d D ]}|| j jv r| j j| }| || q
q| j jD ]}|d D ]}|| j jv r>| j j| }| || q*q$dS )za
        Link manually-labeled footnotes and citations to/from their
        references.
        r   N)r   	footnotesry   resolve_references	citationscitation_refs)r!   r}   rx   r`   citationr$   r$   r%   rr   U  s   z)Footnotes.resolve_footnotes_and_citationsc                 C   sz   t |d dks
J |d d }|D ]%}|jrq|d ||d< t |d dks+J ||d d  d|_qd|_d S )Nr   r0   r   r
   r   )r   r=   r{   r9   )r!   noter`   r"   rC   r$   r$   r%   r   e  s   

zFootnotes.resolve_references)r(   r)   r*   r+   r,   rm   r   r&   ro   rp   rq   rr   r   r$   r$   r$   r%   rh     s    ;!%rh   c                   @   s   e Zd ZdS )#CircularSubstitutionDefinitionErrorN)r(   r)   r*   r$   r$   r$   r%   r   s  s    r   c                   @   s   e Zd ZdZdZ	 dd ZdS )SubstitutionsaD  
    Given the following ``document`` as input::

        <document>
            <paragraph>
                The
                <substitution_reference refname="biohazard">
                    biohazard
                 symbol is deservedly scary-looking.
            <substitution_definition name="biohazard">
                <image alt="biohazard" uri="biohazard.png">

    The ``substitution_reference`` will simply be replaced by the
    contents of the corresponding ``substitution_definition``.

    The transformed result will be::

        <document>
            <paragraph>
                The
                <image alt="biohazard" uri="biohazard.png">
                 symbol is deservedly scary-looking.
            <substitution_definition name="biohazard">
                <image alt="biohazard" uri="biohazard.png">
       c              
   C   s   | j j}| j j}i }t| j jdd}t| j tj}|D ]o}d}|d }||v r-|}	n
|	 }
|
|
d }	|	d u rG| j jjd| |d}n||	 }t| |kr\| j jd|	 }|r| j |}tj|j|j|d}| j |}|| || q|j}||}d	|jv sd
|jv r|dkrt||d  tjr||d   ||d < d|jv sd
|jv rt||d krt||d  tjr||d   ||d < | }z-|tjD ]$}||d 	  }|||g v rt|| |	 ||d< || qW ni tym   |j}t|tjr.| j jjdt |j|j|j!|d}|| n=|}|"dr@|d }|"ds6| j jjd| |d}| j |}tj|j|j|d}| j |}|| || Y qw ||j# |j#D ]}t|tj$rd|v r| j %| qwqd S )Nline_length_limiti'  rX   r
   z(Undefined substitution referenced: "%s".rY   z;Substitution definition "%s" exceeds the line-length-limit.r/   ltrimtrimr   r0   rtrimz
ref-originz*Circular substitution definition detected:)linerZ   z2Circular substitution definition referenced: "%s".)&r   substitution_defssubstitution_namesr   settingslistr   r   substitution_referencelowerr2   r4   r5   r   astextr6   r7   r8   r9   r:   r   r   
attributesr   rz   rstriplstripdeepcopy
setdefaultr   r3   substitution_definitionliteral_blockr   r   childrenReferentialnote_refname)r!   defsnormednestedr   
subreflistrC   rA   r
   keynormed_namesubdefrB   rD   rE   r   r   subdef_copy
nested_refnested_name
ref_originr@   r$   r$   r%   r&     s   













zSubstitutions.applyNr'   r$   r$   r$   r%   r   w  s
    r   c                   @   s.   e Zd ZdZdZ	 dd Zdd Zdd Zd	S )
TargetNotesz
    Creates a footnote for each external target in the text, and corresponding
    footnote references after each reference.
    i  c                 C   s$   t j| ||d |jdg | _d S )N)	startnodeclass)r   __init__detailsr2   classes)r!   r   r   r$   r$   r%   r     s   zTargetNotes.__init__c           	      C   s   i }g }| j tjD ];}|dsq|d }g }|D ]}|| j j|g  q|s,q| |d ||}|d |vrF|||d < |	| q| j tj
D ](}|dsVqN|drv| |d |g|}|d |vrv|||d < |	| qN| j| d S )Nr	   r   r.   )r   r   r   r   r   r   r[   r2   make_target_footnoter3   r1   r   r:   )	r!   notesnodelistr   r   refsr#   r}   rC   r$   r$   r%   r&     s<   




zTargetNotes.applyc                 C   sV  ||v r|| }t |d dksJ |d d }n6t }| j|}d| }d|d< |g|d< t }|tjd||d7 }||7 }| j| | j|| |D ]W}t	|tj
rZqQtj|dd}	|	d	  | j7  < | j|	 | j|	 |j|d }
|	g}t| jjs| jr|dtjd
| jd n	|dtd
 |j|
| qQ|S )Nr   r0   r   zTARGET_NOTE: autorX   )r	   )r
   r   r    )textClasses)r   r   r}   r   r6   	paragraphr1   note_autofootnoter|   r   r   footnote_referencer   note_autofootnote_refnote_footnote_refr   r   r   get_trim_footnote_ref_spacer   rw   inlinerz   )r!   r	   r   r   r}   footnote_namefootnote_idfootnote_paragraphrC   refnoder   r`   r$   r$   r%   r   "  s>   
z TargetNotes.make_target_footnoteN)r(   r)   r*   r+   r,   r   r&   r   r$   r$   r$   r%   r     s    r   c                   @   r   )DanglingReferencesze
    Check for dangling references (incl. footnote & citation) and for
    unreferenced targets.
    iR  c                 C   s   t | j| jjj}| j| | jtjD ]0}|jsF|	dr!q|d r,|d d }n|d r7|d d }n|d }| jj
jd| |d qd S )Nr.   r   r   r   r   z(Hyperlink target "%s" is not referenced.rY   )DanglingReferencesVisitorr   rO   rP   walkr   r   r   r<   r2   r4   info)r!   visitorr   r_   r$   r$   r%   r&   P  s,   
zDanglingReferences.applyNr'   r$   r$   r$   r%   r   G  s    r   c                   @   s,   e Zd Zdd Zdd Zdd Ze ZZdS )r   c                 C   s   t j| | || _|| _d S rG   )r   SparseNodeVisitorr   r   rP   )r!   r   rP   r$   r$   r%   r   n  s   
z"DanglingReferencesVisitor.__init__c                 C   s   d S rG   r$   )r!   r@   r$   r$   r%   unknown_visits  s   z'DanglingReferencesVisitor.unknown_visitc           	      C   sL  |j s|ds
d S |d }| jj|}|d u r| jD ]	}||r% d S qt| jjddr9t|t	j
r9d|_ d S || jjv rM| jjjd|d  |d}n| jjjd|d  d|d}| j|}t	j|j|j|d	}z|d
 d }W n ty   | j|}Y nw || || d S |d= ||d< | jj| j|d d|_ d S )Nr
   
use_bibtexFTzBDuplicate target name, cannot be used as a unique reference: "%s".rY   zUnknown target name: "z".r/   r   r   r   rL   )r=   r   r   rN   r2   rP   r   r   r   r   citation_referencer4   r5   r6   r7   r8   r   r9   r:   r   rR   )	r!   r@   r
   r"   rU   rA   rB   rD   rE   r$   r$   r%   visit_referencev  sP   




z)DanglingReferencesVisitor.visit_referenceN)r(   r)   r*   r   r   r   visit_footnote_referencevisit_citation_referencer$   r$   r$   r%   r   l  s
    &r   N)r+   __docformat__docutilsr   r   docutils.transformsr   r   r-   rF   re   rf   rh   	Exceptionr   r   r   r   r   r   r$   r$   r$   r%   <module>   s"   JA 2$( ]}S%