o
    i8                     @  s   d Z ddlm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ZG dd dejZdS )z&
Transforms for resolving references.
    )annotationsreStructuredText)nodesutils)	Transformc                   @     e Zd ZdZdZd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  returnNonec                 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  t|jtjrt|tjr|j| q|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figurecaptionremove
note_refid)selfr   r   idname r*   d/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/docutils/transforms/references.pyapply*   s^   




zPropagateTargets.applyNr	   r
   __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  r	   r
   c           	      C  s8  dd | j tjD }dd | j tjD }t|t|krV| j jdt|t|f }| j |}|D ]}tj	|j
|j
|d}| j |}|| || q7d S t||D ]>\}}|dsi|drjq[d|_	 |dr}|d |d< d|_n|d	 s| j j|d  }qm|d	 d
 |d< | j | 	 q[d S )Nc                 S     g | ]	}| d r|qS 	anonymousget.0noder*   r*   r+   
<listcomp>}       z-AnonymousHyperlinks.apply.<locals>.<listcomp>c                 S  r5   r6   r8   r:   r*   r*   r+   r=      r>   z]Anonymous hyperlink mismatch: %s references but %s targets.
See "backrefs" attribute for IDs.r   r   r   Tr   r   )r   r   r   	referencer   r   reportererrorset_idproblematic	rawsourceadd_backrefreplace_selfzipr   
referencedresolvedr   r&   )	r'   anonymous_refsanonymous_targetsmsgmsgidrefprbprbidr   r*   r*   r+   r,   |   sL   


zAnonymousHyperlinks.applyNr-   r.   r*   r*   r*   r+   r4   b   s    r4   c                   @  sP   e Zd ZdZdZdddZdddZdd	d
ZdddZdddZ	dddZ
dS )IndirectHyperlinksab  
    a) Indirect external references::

           <paragraph>
               <reference refname="indirect-external">
                   indirect external
           <target ids="id1" names="direct-external"
               refuri="http://indirect">
           <target ids="id2" names="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 ids="id1" names="direct-external"
               refuri="http://indirect">
           <target ids="id2" names="indirect-external"
               refuri="http://indirect">

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

    b) Indirect internal references::

           <target ids="id1" names="final-target">
           <paragraph>
               <reference refname="indirect-internal">
                   indirect internal
           <target ids="id2" names="indirect-internal-2"
               refname="final target">
           <target ids="id3" names="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 ids="id1" names="final-target">
           <paragraph>
               <reference refid="id1">
                   indirect internal
           <target ids="id2" names="indirect-internal-2" refid="id1">
           <target ids="id3" names="indirect-internal" refid="id1">
    i  r	   r
   c                 C  s,   | j jD ]}|js| | | | qd S N)r   indirect_targetsrJ   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_indirect   r   r   T)r9   r   nameidstransformerunknown_reference_resolversnonexistent_indirect_targetr   note_referenced_byr   r   r   rJ   r   circular_indirect_referencerY   rU   r&   )r'   r   r   reftarget_idresolver_function	reftargetr*   r*   r+   rU      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   r[   indirect_target_errorrW   r*   r*   r+   r^     s   z.IndirectHyperlinks.nonexistent_indirect_targetc                 C  s   |  |d d S )Nzforming a circular reference)rd   rW   r*   r*   r+   r`     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?   T)r    r   refnamesr9   refidsrA   rB   rC   r   uniqr   rD   rE   rF   rG   rJ   )r'   r   explanationnamingreflistr)   r(   rM   rN   rO   rP   rQ   r*   r*   r+   rd     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   Tr   rX   )r   r   r&   rh   r9   r_   rJ   r   r   r   rV   ri   )	r'   r   attnamecall_methodattvalr)   rm   rO   r(   r*   r*   r+   rV   ,  sR   





z.IndirectHyperlinks.resolve_indirect_referencesNr-   )r/   r0   r1   r2   r3   r,   rU   r^   r`   rd   rV   r*   r*   r*   r+   rR      s    2


,

rR   c                   @  r   )	ExternalTargetsa  
    Given::

        <paragraph>
            <reference refname="direct-external">
                direct external
        <target ids="id1" names="direct-external" refuri="http://direct">

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

        <paragraph>
            <reference refuri="http://direct">
                direct external
        <target ids="id1" names="direct-external" refuri="http://direct">
    i  r	   r
   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   rn   r   T)	r   r   r   r   r   rh   r9   r_   rJ   )r'   r   r   r)   rm   rO   r*   r*   r+   r,   h  s    
zExternalTargets.applyNr-   r.   r*   r*   r*   r+   rr   T  s    rr   c                   @  s$   e Zd ZdZd	ddZd	ddZdS )
InternalTargetsi  r	   r
   c                 C  s6   | j tjD ]}|ds|ds| | qd S )Nr   r   )r   r   r   r   r   resolve_reference_idsrW   r*   r*   r+   r,   |  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 ids="id1" names="direct-internal">

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

            <paragraph>
                <reference refid="id1">
                    direct internal
            <target ids="id1" names="direct-internal">
        r   rn   r   r   TN)r   r[   r9   rh   r_   rJ   )r'   r   r)   r   rm   rO   r*   r*   r+   rt     s   z%InternalTargets.resolve_reference_idsNr-   )r/   r0   r1   r3   r,   rt   r*   r*   r*   r+   rs   x  s    
rs   c                   @  s\   e Zd ZdZdZdZ	 g dZdddZd	d
 ZdddZ	dddZ
dddZd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" ids="id1" refname="footnote">
            <paragraph>
                An unlabeled autonumbered footnote reference:
                <footnote_reference auto="1" ids="id2">
            <footnote auto="1" ids="id3">
                <paragraph>
                    Unlabeled autonumbered footnote.
            <footnote auto="1" ids="footnote" names="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" ids="id1" refid="footnote">
                    2
            <paragraph>
                An unlabeled autonumbered footnote reference:
                <footnote_reference auto="1" ids="id2" refid="id3">
                    1
            <footnote auto="1" ids="id3" backrefs="id2">
                <label>
                    1
                <paragraph>
                    Unlabeled autonumbered footnote.
            <footnote auto="1" ids="footnote" names="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   ♣r	   r
   c                 C  s:   g | _ | jj}| || j_| | |   |   d S rS   )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.
        TrZ   r   re   r   r   r   r   dupnames)r   autofootnotesstrr[   insertr   labelfootnote_refsr9   Textdelattrr   rF   r&   rJ   appendnote_explicit_targetrz   )r'   r   footnoter   r)   rO   r*   r*   r+   r|     s0   
,zFootnotes.number_footnotesc              
   C  s^  d}| j jD ]}|js|drqz| j| }W n\ tyt   t| j}|dkr*dnd}| j jjd| d| d|d	}| j 	|}| j j|d
 D ]%}|jsT|drUqJt
j|j|j|d}	| j 	|	}
||
 ||	 qJY  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   rZ   sre   z0Too many autonumbered footnote references: only z corresponding footnotez available.rf   Nr   r?   r   T)r   autofootnote_refsrJ   r   rz   
IndexErrorr   rA   rB   rC   r   rD   rE   rF   rG   r   r[   r   r&   )r'   r   irO   r   nr   rM   rN   rP   rQ   r(   r   r*   r*   r+   r}     sN   



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.rZ   r   re   zOToo many symbol footnote references: only %s corresponding footnotes available.rf   Nr   r?   r   )r   symbol_footnotesdivmodsymbol_footnote_startr   symbolsr   r   r   r   rC   symbol_footnote_refsr   r   rA   rB   rJ   r   rD   rE   rF   rG   r&   )r'   labelsr   repsindex	labeltextr   rO   rM   rN   rP   rQ   r*   r*   r+   r~   ;  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   	footnotesr   resolve_references	citationscitation_refs)r'   r   r   rm   citationr*   r*   r+   r   `  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   rZ   r   r   r   T)r   rJ   r   rF   )r'   noterm   r(   rO   r*   r*   r+   r   p  s   

zFootnotes.resolve_referencesr-   )r/   r0   r1   r2   r3   rz   r   r,   r|   r}   r~   r   r   r*   r*   r*   r+   ru     s    ;


#
%ru   c                   @  s   e Zd ZdS )#CircularSubstitutionDefinitionErrorN)r/   r0   r1   r*   r*   r*   r+   r   ~  s    r   c                   @  s   e Zd ZdZdZ	 dd ZdS )SubstitutionsaF  
    Given the following ``document`` as input::

        <document>
            <paragraph>
                The
                <substitution_reference refname="biohazard">
                    biohazard
                 symbol is deservedly scary-looking.
            <substitution_definition names="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 names="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'  re   r   z(Undefined substitution referenced: "%s".rf   z;Substitution definition "%s" exceeds the line-length-limit.r?   ltrimtrimr   rZ   rtrimz
ref-originz*Circular substitution definition detected:)linerg   z2Circular substitution definition referenced: "%s".)&r   substitution_defssubstitution_namesr"   settingslistr   r   substitution_referencelowerr9   rA   rB   r   astextrC   rD   rE   rF   rG   r   r   
attributesr   r   rstriplstripdeepcopy
setdefaultr   r   substitution_definitionliteral_blockr   r   childrenReferentialnote_refname)r'   defsnormednestedr   
subreflistrO   rM   r   keynormed_namesubdefrN   rP   rQ   r   r   subdef_copy
nested_refnested_name
ref_originr<   r*   r*   r+   r,     s   













zSubstitutions.applyNr.   r*   r*   r*   r+   r     s
    r   c                   @  s2   e Zd ZdZdZ	 dddZd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  r	   r
   c                 C  s$   t j| ||d |jdg | _d S )N)	startnodeclass)r   __init__detailsr9   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   r7   )r   r   r   r   r   r    rh   r9   make_target_footnoter   r@   r   rG   )	r'   notesnodelistr   r   refsr)   r   rO   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   rZ   r   zTARGET_NOTE: autore   )r   )r   r   r    )textClasses)r   r   r   r   rC   	paragraphr@   note_autofootnoter   r   r   footnote_referencer   note_autofootnote_refnote_footnote_refr   r   r   get_trim_footnote_ref_spacer   r   inliner   )r'   r   r   r   r   footnote_namefootnote_idfootnote_paragraphrO   refnoder   rm   r*   r*   r+   r   -  s>   
z TargetNotes.make_target_footnoteNr-   )r/   r0   r1   r2   r3   r   r,   r   r*   r*   r*   r+   r     s    

r   c                   @  r   )	CitationReferenceszResolve <citation_references>.

    The 'use_bibtex'__ configuration setting indicates that citation entries
    are fetched from a BibTeX database by the backend (LaTeX).

    __ https://docutils.sourceforge.io/docs/user/config.html#use-bibtex
    i  r	   r
   c                 C  s`   t | jjdg s
d S | jtjD ]}|js | jj|dr!q|	 r-|
d d|_qd S )N
use_bibtexr   T)r"   r   r   r   r   citation_referencerJ   r[   r9   r   r   r'   r<   r*   r*   r+   r,   _  s   
zCitationReferences.applyNr-   r.   r*   r*   r*   r+   r   R  s    	r   c                   @  r   )	DanglingReferencesu  
    Check for dangling references (incl. footnote & citation) and for
    unreferenced targets.

    Provisional : pending deprecation
      Docutils readers will add separate transforms for resolving
      refnames to refids and for reporting unresolved references
      instead of this transform (to make space for reference-resolving
      transforms added by extensions or applications) in Docutils 1.0.
      This transform will be removed in Docutils 2.0.
    iR  r	   r
   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 )Nr7   r   r   r   r   z(Hyperlink target "%s" is not referenced.rf   )DanglingReferencesVisitorr   r\   r]   walkr   r   r   rI   r9   rA   info)r'   visitorr   rl   r*   r*   r+   r,   ~  s,   
zDanglingReferences.applyNr-   r.   r*   r*   r*   r+   r   n  s    r   c                   @  s6   e Zd ZdZdddZdddZddd	Ze ZZd
S )r   u   Provisional : pending deprecation

    This auxiliary class is used by the `DanglingReferences` transform
    which will no longer be used in Docutils 1.0.
    It will be removed in Docutils 2.0.
    r	   r
   c                 C  s   t j| | || _|| _d S rS   )r   SparseNodeVisitorr   r   r]   )r'   r   r]   r*   r*   r+   r     s   
z"DanglingReferencesVisitor.__init__c                 C  s   d S rS   r*   r   r*   r*   r+   unknown_visit  s   z'DanglingReferencesVisitor.unknown_visitc                 C  s  |j s|ds
d S |d }| jj|}|d ur/|d= ||d< | jj| j|d d|_ d S | jD ]	}||r; d S q2|| jjv rP| jjj	d| d|d}nUd|v sXd	|v rd
}d|vrc|d7 }nd|vrk|d7 }d	|vrt|d7 }n	|
d	s}|d7 }d|v sd|v r|d7 }td|g}ng }| jjj	d| dg|R d|i}| j|}tj|j|j|d}	z|d d }
W n ty   | j|	}
Y nw ||
 ||	 d S )Nr   r   rX   Tz>Duplicate target name, cannot be used as a unique reference: "z".rf   <>z%Did you want to embed a URI or alias?z
Opening bracket missing.z <z7
The embedded reference must be preceded by whitespace.z
Closing bracket missing.zD
The embedded reference must be the last text before the end string.z< z >z9
Whitespace around the embedded reference is not allowed.re   zUnknown target name: "rg   r?   r   r   )rJ   r   r   r[   r9   r   r_   r]   rA   rB   endswithr   r   rC   rD   rE   r   rF   rG   )r'   r<   r   r(   rb   rM   hintr   rN   rP   rQ   r*   r*   r+   visit_reference  sf   





z)DanglingReferencesVisitor.visit_referenceNr-   )	r/   r0   r1   r2   r   r   r   visit_footnote_referencevisit_citation_referencer*   r*   r*   r+   r     s    


4r   N)r2   
__future__r   __docformat__docutilsr   r   docutils.transformsr   r   r4   rR   rr   rs   ru   	Exceptionr   r   r   r   r   r   r   r*   r*   r*   r+   <module>   s&   QA 2$( _}S,