
    -PhQ                    >   d 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mZmZmZ ddlmZ dd	lmZ dd
lmZ ddlmZ erddlmZ ddlmZmZ ddlmZ ddl m!Z! dZ" G d de          Z# G d de          Z$ G d de          Z% G d dej&                  Z' G d de          Z( G d dej&                  Z) G d de          Z* G d d e          Z+ G d! d"e          Z, G d# d$e          Z- G d% d&e          Z. G d' d(e          Z/d/d-Z0d.S )0zTransforms for LaTeX builder.    )annotations)TYPE_CHECKINGcast)nodes)Substitutions)addnodes)captioned_literal_blockfootnotemarkfootnotetextmath_referencethebibliography)__)SphinxTransform)SphinxPostTransform)NodeMatcher)Any)ElementNode)Sphinx)ExtensionMetadata)mailto:zhttp:zhttps:zftp:c                  :    e Zd ZdZdZej        ej        fZd	dZ	dS )
FootnoteDocnameUpdaterz5Add docname to footnote and footnote_reference nodes.i  kwargsr   returnNonec                z    t          | j         }|                    | j                  D ]}| j        j        |d<   d S )Ndocname)r   TARGET_NODESfindalldocumentenvr   )selfr   matchernodes       `/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/sphinx/builders/latex/transforms.pyapplyzFootnoteDocnameUpdater.apply(   sF    t01OODM22 	/ 	/D"h.DOO	/ 	/    Nr   r   r   r   )
__name__
__module____qualname____doc__default_priorityr   footnotefootnote_referencer   r'    r(   r&   r   r   "   sC        ??NE$<=L/ / / / / /r(   r   c                  2    e Zd ZdZej        dz   ZdZd
dZd	S )SubstitutionDefinitionsRemoverz7Remove ``substitution_definition`` nodes from doctrees.   latexr   r   r   r   c                    t          | j                            t          j                            D ]}|j                            |           d S N)listr!   r    r   substitution_definitionparentremove)r#   r   r%   s      r&   runz"SubstitutionDefinitionsRemover.run5   sO    ..u/LMMNN 	% 	%DKt$$$$	% 	%r(   Nr)   )r*   r+   r,   r-   r   r.   formatsr=   r1   r(   r&   r3   r3   .   sC        AA %59G% % % % % %r(   r3   c                  F    e Zd ZdZdZdZdZdd	Zdd
ZddZ	ddZ
ddZdS )ShowUrlsTransformzExpand references to inline text or footnotes.

    For more information, see :confval:`latex_show_urls`.

    .. note:: This transform is used for integrated doctree
      r5   Fr   r   r   r   c                    	 | j         j        }|j        }d|_        |                                  | j        r|                                  ||_        d S # ||_        w xY w)N	show_urls)r!   settings	id_prefixexpand_show_urlsexpandedrenumber_footnotes)r#   r   rD   rE   s       r&   r=   zShowUrlsTransform.runH   ss    	+ M2H *I!,H!!###} *''))) "+HH****s   A	A 	Ac                   | j         j        }|du s|dk    rd S t          | j                            t
          j                            D ]'}|                    dd          }|                    t                    r|
                    d          }|                                |k    r|j                            |          }|                     |          }|dk    r]|                     ||          \  }}|j                            |dz   |           |j                            |dz   |           d	| _        t          j        d
|z            }|j                            |dz   |           )d S )NFnorefuri r   r/   r4      Tz (%s))configlatex_show_urlsr9   r!   r    r   	referenceget
startswithURI_SCHEMESremoveprefixastextr;   indexget_docname_for_nodecreate_footnoteinsertrG   Text)	r#   rC   r%   urirV   r   fnfnreftextnodes	            r&   rF   z"ShowUrlsTransform.expand_show_urlsV   sh   K/	d!2!2F..u??@@ 	@ 	@D((8R((C~~k** @&&y11;;==C'' K--d33E"77==G J..$($8$8g$F$F	E**519b999**519e<<<(,#(:gm#<#<**519h???	@ 	@r(   r%   r   strc                   |rgt          |t          j                  r"| j                            |d                   pdS t          |t
          j                  r|d         S |j        }|g	 |d         }n+# t          $ r t          t          d                    d w xY wt          d          |z  }t          |          )NsourcerL   r   zFailed to get a docname!z&Failed to get a docname for source %r!)
isinstancer   r!   r"   path2docr   start_of_filer;   	TypeError
ValueErrorr   )r#   r%   ra   msgs       r&   rW   z&ShowUrlsTransform.get_docname_for_nodel   s     	#$// #x((h88>B>D("899 #I&{  	#	G(^FF 	G 	G 	GR :;;<<$F	G9::VCoos   +A4 4(Br[   r   /tuple[nodes.footnote, nodes.footnote_reference]c                D   t          j        dt          j        |          |d          }t          j        |d|          }|d                             d           |t          j        dd          z  }|t          j        dd|          z  }| j                            |           t          j	        dd|d	         d
         |          }|t          j        d          z  }| j        
                    |           |                    |d	         d
                    ||fS )NrL   T)rK   	nolinkurlr4   )autor   names#z[#]_idsr   )rk   refidr   )r   rP   rZ   r/   appendlabel	paragraphr!   note_autofootnoter0   note_autofootnote_refadd_backref)r#   r[   r   rP   r/   footnote_refs         r&   rX   z!ShowUrlsTransform.create_footnote|   s    OB
3tTTT	>#Aw???  %%%EKC(((EOBI666''111/(5/!"4g
 
 
 	
3'++L999\%03444%%r(   c           	        t          | j                  }| j                            |           d}|j        D ]@}	 |dz  }t	          |          |j        vrnt          d|d                   }|                    t          j	        dt	          |                               ||d         v r-|d         
                    |                                           |d                             t	          |                     |d         }|j        D ]e}||d         k    rW|d         d         |d	         k    r?|
                    |d                    |t          j        t	          |                    z  }fBd S )
Nr   Tr4   znodes.labelrL   rl   r   rn   ro   )FootnoteCollectorr!   	walkaboutauto_footnotesr_   used_footnote_numbersr   replace_selfr   rq   r<   rU   rp   footnote_refsrZ   )r#   	collectornumr/   	old_labelr   refs          r&   rH   z$ShowUrlsTransform.renumber_footnotes   s{   %dm44		***!0 	0 	0Hqs889#BBB ]HQK88I""5;r3s88#<#<===HW---!(()9)9););<<<W$$SXX... y)G . 0 0c)n,,%1Cs7|1S1SJJs1v&&&5:c#hh///C0!	0 	0r(   Nr)   )r   r   )r%   r   r   r_   )r[   r_   r   r_   r   rh   )r*   r+   r,   r-   r.   r>   rG   r=   rF   rW   rX   rH   r1   r(   r&   r@   r@   :   s          G H+ + + +@ @ @ @,    & & & &&0 0 0 0 0 0r(   r@   c                  D     e Zd ZdZd fdZdd	Zdd
ZddZddZ xZ	S )rx   z9Collect footnotes and footnote references on the documentr!   nodes.documentr   r   c                    g | _         t                      | _        g | _        t	                                          |           d S r8   )rz   setr{   r}   super__init__)r#   r!   	__class__s     r&   r   zFootnoteCollector.__init__   s=    46/2uu"=?"""""r(   r%   r   c                    d S r8   r1   r#   r%   s     r&   unknown_visitzFootnoteCollector.unknown_visit       r(   c                    d S r8   r1   r   s     r&   unknown_departurez#FootnoteCollector.unknown_departure   r   r(   nodes.footnotec                    |                     d          r| j                            |           d S |d         D ]}| j                            |           d S )Nrk   rl   )rQ   rz   rp   r{   add)r#   r%   names      r&   visit_footnotez FootnoteCollector.visit_footnote   sj    88F 	5&&t,,,,,W 5 5*..t44445 5r(   nodes.footnote_referencec                :    | j                             |           d S r8   )r}   rp   r   s     r&   visit_footnote_referencez*FootnoteCollector.visit_footnote_reference   s    !!$'''''r(   )r!   r   r   r   r%   r   r   r   r%   r   r   r   r%   r   r   r   )
r*   r+   r,   r-   r   r   r   r   r   __classcell__r   s   @r&   rx   rx      s        CC# # # # # #      5 5 5 5( ( ( ( ( ( ( (r(   rx   c                  "    e Zd ZdZdZdZd
dZd	S )LaTeXFootnoteTransforma  Convert footnote definitions and references to appropriate form to LaTeX.

    * Replace footnotes on restricted zone (e.g. headings) by footnotemark node.
      In addition, append a footnotetext node after the zone.

      Before::

          <section>
              <title>
                  headings having footnotes
                  <footnote_reference>
                      1
              <footnote ids="id1">
                  <label>
                      1
                  <paragraph>
                      footnote body

      After::

          <section>
              <title>
                  headings having footnotes
                  <footnotemark refid="id1">
                      1
              <footnotetext ids="id1">
                  <label>
                      1
                  <paragraph>
                      footnote body

    * Integrate footnote definitions and footnote references to single footnote node

      Before::

          blah blah blah
          <footnote_reference refid="id1">
              1
          blah blah blah ...

          <footnote ids="id1">
              <label>
                  1
              <paragraph>
                  footnote body

      After::

          blah blah blah
          <footnote ids="id1">
              <label>
                  1
              <paragraph>
                  footnote body
          blah blah blah ...

    * Replace second and subsequent footnote references which refers same footnote definition
      by footnotemark node.  Additionally, the footnote definition node is marked as
      "referred".

      Before::

          blah blah blah
          <footnote_reference refid="id1">
              1
          blah blah blah
          <footnote_reference refid="id1">
              1
          blah blah blah ...

          <footnote ids="id1">
              <label>
                  1
              <paragraph>
                  footnote body

      After::

          blah blah blah
          <footnote ids="id1" referred=True>
              <label>
                  1
              <paragraph>
                  footnote body
          blah blah blah
          <footnotemark refid="id1">
              1
          blah blah blah ...

    * Remove unreferenced footnotes

      Before::

          <footnote ids="id1">
              <label>
                  1
              <paragraph>
                  Unreferenced footnote!

      After::

          <!-- nothing! -->

    * Move footnotes in a title of table or thead to head of tbody

      Before::

          <table>
              <title>
                  title having footnote_reference
                  <footnote_reference refid="id1">
                      1
              <tgroup>
                  <thead>
                      <row>
                          <entry>
                              header having footnote_reference
                              <footnote_reference refid="id2">
                                  2
                  <tbody>
                      <row>
                      ...

          <footnote ids="id1">
              <label>
                  1
              <paragraph>
                  footnote body

          <footnote ids="id2">
              <label>
                  2
              <paragraph>
                  footnote body

      After::

          <table>
              <title>
                  title having footnote_reference
                  <footnotemark refid="id1">
                      1
              <tgroup>
                  <thead>
                      <row>
                          <entry>
                              header having footnote_reference
                              <footnotemark refid="id2">
                                  2
                  <tbody>
                      <footnotetext ids="id1">
                          <label>
                              1
                          <paragraph>
                              footnote body

                      <footnotetext ids="id2">
                          <label>
                              2
                          <paragraph>
                              footnote body
                      <row>
                      ...
    iX  r5   r   r   r   r   c                   t          | j                            t          j                            }|D ]}|j                            |           t          | j        |          }| j                            |           d S r8   )	r9   r!   r    r   r/   r;   r<   LaTeXFootnoteVisitorry   )r#   r   	footnotesr%   visitors        r&   r=   zLaTeXFootnoteTransform.runm  sv    ..u~>>??	 	% 	%DKt$$$$&t}i@@(((((r(   Nr)   r*   r+   r,   r-   r.   r>   r=   r1   r(   r&   r   r      sB        c cJ G) ) ) ) ) )r(   r   c                       e Zd Zd& fdZd'd
Zd'dZd(dZd(dZd)dZd)dZ	d*dZ
d*dZd+dZd+dZd,dZd,dZd-dZd-dZd.dZd/d!Zd/d"Zd0d$Zd1d%Z xZS )2r   r!   r   r   list[nodes.footnote]r   r   c                    i | _         || _        g | _        g | _        d | _        t                                          |           d S r8   )appearedr   pendingstable_footnotes
restrictedr   r   )r#   r!   r   r   s      r&   r   zLaTeXFootnoteVisitor.__init__w  sI     @B/8.057*."""""r(   r%   r   c                    d S r8   r1   r   s     r&   r   z"LaTeXFootnoteVisitor.unknown_visit  r   r(   c                    d S r8   r1   r   s     r&   r   z&LaTeXFootnoteVisitor.unknown_departure  r   r(   r   c                &    | j         	|| _         d S d S r8   )r   r   s     r&   restrictzLaTeXFootnoteVisitor.restrict  s    ?""DOOO #"r(   c                   | j         |k    rd | _         |j                            |          }t          | j                  D ]A\  }}t          dg|j        R d|d         i}|j                            ||z   dz   |           Bg | _        d S d S )NrL   rn   r4   )r   r;   rV   	enumerater   r   childrenrY   )r#   r%   posir/   fntexts         r&   
unrestrictzLaTeXFootnoteVisitor.unrestrict  s    ?d"""DO+##D))C(77 8 88%bR8+<RRR(5/RR""37Q;7777DMMM #"r(   nodes.figurec                0    |                      |           d S r8   r   r   s     r&   visit_figurez!LaTeXFootnoteVisitor.visit_figure      dr(   c                0    |                      |           d S r8   r   r   s     r&   depart_figurez"LaTeXFootnoteVisitor.depart_figure      r(   
nodes.termc                0    |                      |           d S r8   r   r   s     r&   
visit_termzLaTeXFootnoteVisitor.visit_term  r   r(   c                0    |                      |           d S r8   r   r   s     r&   depart_termz LaTeXFootnoteVisitor.depart_term  r   r(   nodes.captionc                0    |                      |           d S r8   r   r   s     r&   visit_captionz"LaTeXFootnoteVisitor.visit_caption  r   r(   c                0    |                      |           d S r8   r   r   s     r&   depart_captionz#LaTeXFootnoteVisitor.depart_caption  r   r(   nodes.titlec                    t          |j        t          j        t          j        z            r|                     |           d S d S r8   )rb   r;   r   sectiontabler   r   s     r&   visit_titlez LaTeXFootnoteVisitor.visit_title  sA    dk5=5;#>?? 	 MM$	  	 r(   c                   t          |j        t          j                  r|                     |           d S t          |j        t          j                  r3| xj        | j        z  c_        g | _        |                     |           d S d S r8   )rb   r;   r   r   r   r   r   r   r   s     r&   depart_titlez!LaTeXFootnoteVisitor.depart_title  s    dk5=11 	"OOD!!!!!U[11 	"  DM1  DMOOD!!!!!	" 	"r(   nodes.theadc                0    |                      |           d S r8   r   r   s     r&   visit_theadz LaTeXFootnoteVisitor.visit_thead  r   r(   c                h    | xj         | j        z  c_         g | _        |                     |           d S r8   )r   r   r   r   s     r&   depart_theadz!LaTeXFootnoteVisitor.depart_thead  s7    -r(   nodes.tablec                    t          |                    t          j                            }t	          | j                  D ]3}t          dg|j        R d|d         i}|                    d|           4g | _        d S )NrL   rn   r   )	nextr    r   tbodyreversedr   r   r   rY   )r#   r%   r   r/   r   s        r&   depart_tablez!LaTeXFootnoteVisitor.depart_table  s    T\\%+..// !566 	$ 	$H!"Nx'8NNNhuoNNFLLF####!r(   r   c                0    |                      |           d S r8   r   r   s     r&   r   z#LaTeXFootnoteVisitor.visit_footnote  r   r(   c                0    |                      |           d S r8   r   r   s     r&   depart_footnotez$LaTeXFootnoteVisitor.depart_footnote  r   r(   r   c                x   |                                                                 }|d         }||f| j        v rB| j        ||f         }d|d<   t          d||d                   }|                    |           n|                     |          }| j        rHt          d||d                   }|                    |           | j                            |           nD| j	        
                    |           |                    |           |                    |            || j        ||f<   t          j        )Nr   TreferredrL   ro   )ro   )rU   stripr   r
   r|   get_footnote_by_referencer   r   rp   r   r<   ry   r   SkipNode)r#   r%   numberr   r/   marks         r&   r   z-LaTeXFootnoteVisitor.visit_footnote_reference  s7   $$&&y/V--}Wf_5H#'HZ F$w-@@@Dd####55d;;H )#Bd7mDDD!!$'''$$X....%%h///!!(+++""4(((-5DM'6/*nr(   c                    |d         }| j         D ]*}||d         k    r|d         d         |d         k    r|c S +t          t          d          |z            )Nr   rn   r   ro   z1No footnote was found for given reference node %r)r   rf   r   )r#   r%   r   r/   s       r&   r   z.LaTeXFootnoteVisitor.get_footnote_by_reference  sl     y/ 	  	 H(9---(5/!2DW2U2UOPPSWWXXXr(   )r!   r   r   r   r   r   r   )r%   r   r   r   )r%   r   r   r   )r%   r   r   r   )r%   r   r   r   )r%   r   r   r   )r%   r   r   r   )r%   r   r   r   r   r   )r%   r   r   r   )r*   r+   r,   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   s   @r&   r   r   v  s       # # # # # #      # # # #                            " " " "      
" " " "         .Y Y Y Y Y Y Y Yr(   r   c                  "    e Zd ZdZdZdZd
dZd	S )BibliographyTransformaN  Gather bibliography entries to tail of document.

    Before::

        <document>
            <paragraph>
                blah blah blah
            <citation>
                ...
            <paragraph>
                blah blah blah
            <citation>
                ...
            ...

    After::

        <document>
            <paragraph>
                blah blah blah
            <paragraph>
                blah blah blah
            ...
            <thebibliography>
                <citation>
                    ...
                <citation>
                    ...
    i  r5   r   r   r   r   c                   t                      }t          | j                            t          j                            D ]!}|j                            |           ||z  }"t          |          dk    r| xj        |z  c_        d S d S )Nr   )	r   r9   r!   r    r   citationr;   r<   len)r#   r   	citationsr%   s       r&   r=   zBibliographyTransform.run  s    #%%	..u~>>?? 	 	DKt$$$IIy>>AMMY&MMMM r(   Nr)   r   r1   r(   r&   r   r     s?         < G' ' ' ' ' 'r(   r   c                  "    e Zd ZdZdZdZd
dZd	S )CitationReferenceTransformzReplace pending_xref nodes for citation by citation_reference.

    To handle citation reference easily on LaTeX writer, this converts
    pending_xref nodes to citation_reference.
       r5   r   r   r   r   c                J   | j         j        j        }t          t          j        dd          }|                    | j                  D ]Z}|j        	                    |d         d          \  }}}|r1t          j        ddg|j        R ||d}|                    |           [d S )Nr   r   )	refdomainreftype	reftarget)rL   rL   r   rL   )r   refname)r"   domainscitation_domainr   r   pending_xrefr    r!   r   rQ   r   citation_referencer   r|   )	r#   r   domainr$   r%   r   labelid_citation_refs	            r&   r=   zCitationReferenceTransform.run   s    !1!Z
 
 
 OODM22 	0 	0D"("2"6"6tK7H+"V"VGWa 0$7 !]   4;W      !!,///	0 	0r(   Nr)   r   r1   r(   r&   r   r     s?          G0 0 0 0 0 0r(   r   c                  "    e Zd ZdZdZdZd
dZd	S )MathReferenceTransformzReplace pending_xref nodes for math by math_reference.

    To handle math reference easily on LaTeX writer, this converts pending_xref
    nodes to math_reference.
    r   r5   r   r   r   r   c                T   | j         j        j        j        d         }| j                            t          j                  D ]f}|d         dk    rX|d         dv rN|                    |d         d          \  }}|r-t          d||d         	          }|
                    |           gd S )
Nobjectsr   mathr   >   eqnumrefr   )NNrL   )r   target)r"   r   math_domaindatar!   r    r   r   rQ   r   r|   )r#   r   	equationsr%   r   r   refnodes          r&   r=   zMathReferenceTransform.run8  s    H$05i@	M))(*?@@ 	/ 	/DK F**tIBR/R/R&]]4+<lKK
 /,GD4E  G %%g...	/ 	/r(   Nr)   r   r1   r(   r&   r   r   .  s?          G	/ 	/ 	/ 	/ 	/ 	/r(   r   c                  "    e Zd ZdZdZdZd
dZd	S )LiteralBlockTransformzEReplace container nodes for literal_block by captioned_literal_block.rA   r5   r   r   r   r   c                    t          t          j        d          }|                    | j                  D ]1}t          dg|j        R i |j        }|                    |           2d S )NT)literal_blockrL   )	r   r   	containerr    r!   r	   r   
attributesr|   )r#   r   r$   r%   newnodes        r&   r=   zLiteralBlockTransform.runJ  su    eoTBBBOODM22 	' 	'D-bT4=TTTDOTTGg&&&&	' 	'r(   Nr)   r   r1   r(   r&   r  r  D  s9        OOG' ' ' ' ' 'r(   r  c                  "    e Zd ZdZdZdZd
dZd	S )DocumentTargetTransformz5Add :doc label to the first section of each document.rA   r5   r   r   r   r   c                    | j                             t          j                  D ]>}|                    t
          j                  }|r|d                             d           ?d S )Nrn   z:doc)r!   r    r   rd   	next_noder   r   rp   )r#   r   r%   r   s       r&   r=   zDocumentTargetTransform.runW  sb    M))(*@AA 	. 	.DnnU]33G .%%f---	. 	.r(   Nr)   r   r1   r(   r&   r  r  Q  s9        ??G. . . . . .r(   r  c                  "    e Zd ZdZdZdZd
dZd	S )IndexInSectionTitleTransforma  Move index nodes in section title to outside of the title.

    LaTeX index macro is not compatible with some handling of section titles
    such as uppercasing done on LaTeX side (cf. fncychap handling of ``\chapter``).
    Moving the index node to after the title node fixes that.

    Before::

        <section>
            <title>
                blah blah <index entries=[...]/>blah
            <paragraph>
                blah blah blah
            ...

    After::

        <section>
            <title>
                blah blah blah
            <index entries=[...]/>
            <paragraph>
                blah blah blah
            ...
    rA   r5   r   r   r   r   c                v   t          | j                            t          j                            D ]}t          |j        t          j                  ret          |                    t          j
                            D ]8\  }}|                    |           |j                            |dz   |           9d S )Nr4   )r9   r!   r    r   titlerb   r;   r   r   r   rV   r<   rY   )r#   r   r%   r   rV   s        r&   r=   z IndexInSectionTitleTransform.run|  s    ..u{;;<< 	5 	5D$+u}55 5 )$,,x~*F*F G G 5 5HAuKK&&&K&&q1ue4444	5 	5r(   Nr)   r   r1   r(   r&   r  r  ^  s?         4 G5 5 5 5 5 5r(   r  appr   r   r   c                   |                      t                     |                     t                     |                     t                     |                     t
                     |                     t                     |                     t                     |                     t                     |                     t                     |                     t                     |                     t                     ddddS )NbuiltinT)versionparallel_read_safeparallel_write_safe)add_transformr   add_post_transformr3   r   r   r  r  r   r  r   r@   )r  s    r&   setupr    s    ,---9:::0111566623337888122201111222,--- "#  r(   N)r  r   r   r   )1r-   
__future__r   typingr   r   docutilsr   docutils.transforms.referencesr   sphinxr   sphinx.builders.latex.nodesr	   r
   r   r   r   sphinx.localer   sphinx.transformsr   !sphinx.transforms.post_transformsr   sphinx.util.nodesr   r   docutils.nodesr   r   sphinx.applicationr   sphinx.util.typingr   rS   r   r3   r@   NodeVisitorrx   r   r   r   r   r   r  r  r  r  r1   r(   r&   <module>r-     s   # # " " " " " " & & & & & & & &       8 8 8 8 8 8                          - - - - - - A A A A A A ) ) ) ) ) ) 5,,,,,,,,))))))4444444	/ 	/ 	/ 	/ 	/_ 	/ 	/ 	/	% 	% 	% 	% 	%%8 	% 	% 	%m0 m0 m0 m0 m0+ m0 m0 m0`( ( ( ( () ( ( (4o) o) o) o) o)0 o) o) o)dqY qY qY qY qY5, qY qY qYh)' )' )' )' )'/ )' )' )'X0 0 0 0 0!4 0 0 00/ / / / /0 / / /,
' 
' 
' 
' 
'/ 
' 
' 
'
. 
. 
. 
. 
.1 
. 
. 
.$5 $5 $5 $5 $5#6 $5 $5 $5N     r(   