a
    0øaæP  ã                   @   s”  d Z ddlmZmZmZmZmZmZ ddlm	Z	 ddl
mZmZ ddlmZ 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  d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&G dd„ deƒZ'G dd„ de	j%ƒ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.eee/ef d&œd'd(„Z0d)S )*zà
    sphinx.builders.latex.transforms
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    Transforms for LaTeX builder.

    :copyright: Copyright 2007-2022 by the Sphinx team, see AUTHORS.
    :license: BSD, see LICENSE for details.
é    )ÚAnyÚDictÚListÚSetÚTupleÚcast)Únodes)ÚElementÚNode)ÚSubstitutions)Úaddnodes)ÚSphinx)Úcaptioned_literal_blockÚfootnotemarkÚfootnotetextÚmath_referenceÚthebibliography)ÚCitationDomain)ÚSphinxTransform)ÚSphinxPostTransform)ÚNodeMatcher)úmailto:zhttp:zhttps:zftp:c                   @   s0   e Zd ZdZdZejejfZe	ddœdd„Z
dS )ÚFootnoteDocnameUpdaterz5Add docname to footnote and footnote_reference nodes.i¼  N©ÚkwargsÚreturnc                 K   s,   t | jŽ }| j |¡D ]}| jj|d< qd S )NÚdocname)r   ÚTARGET_NODESÚdocumentÚfindallÚenvr   )Úselfr   ÚmatcherÚnode© r$   ú?lib/python3.9/site-packages/sphinx/builders/latex/transforms.pyÚapply"   s    
zFootnoteDocnameUpdater.apply)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Údefault_priorityr   ÚfootnoteÚfootnote_referencer   r   r&   r$   r$   r$   r%   r      s   r   c                   @   s.   e Zd ZdZejd ZdZeddœdd„ZdS )ÚSubstitutionDefinitionsRemoverz4Remove ``substitution_definition node from doctrees.é   ©ZlatexNr   c                 K   s(   t | j tj¡ƒD ]}|j |¡ qd S ©N)Úlistr   r   r   Zsubstitution_definitionÚparentÚremove)r!   r   r#   r$   r$   r%   Úrun/   s    z"SubstitutionDefinitionsRemover.run)	r'   r(   r)   r*   r   r+   Úformatsr   r5   r$   r$   r$   r%   r.   (   s   
r.   c                   @   sv   e Zd ZdZdZdZdZeddœdd„Zdd	œd
d„Z	e
edœdd„Zeeeejejf dœdd„Zdd	œdd„ZdS )ÚShowUrlsTransformz«Expand references to inline text or footnotes.

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

    .. note:: This transform is used for integrated doctree
    é  r0   FNr   c                 K   sB   z4| j j}|j}d|_|  ¡  | jr,|  ¡  W ||_n||_0 d S )NÚ	show_urls)r   ÚsettingsÚ	id_prefixÚexpand_show_urlsÚexpandedÚrenumber_footnotes)r!   r   r:   r;   r$   r$   r%   r5   A   s    
zShowUrlsTransform.run)r   c           	      C   sê   | j j}|du s|dkrd S t| j tj¡ƒD ]¶}| dd¡}| t	¡r.| d¡r^|dd … }| 
¡ |kr.|j |¡}|  |¡}|dkrÄ|  ||¡\}}|j |d |¡ |j |d	 |¡ d
| _q.t d| ¡}|j |d |¡ q.d S )NFÚnoÚrefuriÚ r   é   r,   r/   é   Tz (%s))ZconfigZlatex_show_urlsr2   r   r   r   Ú	referenceÚgetÚ
startswithÚURI_SCHEMESÚastextr3   ÚindexÚget_docname_for_nodeÚcreate_footnoteÚinsertr=   ÚText)	r!   r9   r#   ÚurirI   r   ÚfnZfnrefZtextnoder$   r$   r%   r<   O   s$    


z"ShowUrlsTransform.expand_show_urls©r#   r   c                 C   s@   |r<t |tjƒr | j |d ¡S t |tjƒr4|d S |j}q d S )NÚsourcer   )Ú
isinstancer   r   r    Zpath2docr   Ústart_of_filer3   ©r!   r#   r$   r$   r%   rJ   f   s    z&ShowUrlsTransform.get_docname_for_node)rN   r   r   c                 C   s²   t jdt  |¡|dd}t j|d|d}|d  d¡ |t  dd¡7 }|t  dd|¡7 }| j |¡ t j	dd|d	 d
 |d}|t  d¡7 }| j 
|¡ | |d	 d
 ¡ ||fS )NrA   T)r@   Z	nolinkurlr/   )Úautor   Únamesú#z[#]_Úidsr   )rU   Úrefidr   )r   rD   rM   r,   ÚappendÚlabelZ	paragraphr   Znote_autofootnoter-   Znote_autofootnote_refZadd_backref)r!   rN   r   rD   r,   Zfootnote_refr$   r$   r%   rK   q   s    ÿz!ShowUrlsTransform.create_footnotec                 C   sî   t | jƒ}| j |¡ d}|jD ]È}|d7 }t|ƒ|jvr$q>q$ttj|d ƒ}| 	t dt|ƒ¡¡ ||d v r‚|d  
| ¡ ¡ |d  t|ƒ¡ |d }|jD ]D}||d kr¢|d d |d kr¢| 
|d ¡ |t t|ƒ¡7 }q¢q d S )Nr   r/   rA   rV   r   rX   rY   )ÚFootnoteCollectorr   Ú	walkaboutÚauto_footnotesÚstrÚused_footnote_numbersr   r   r[   Úreplace_selfr4   rH   rZ   Úfootnote_refsrM   )r!   Z	collectorZnumr,   Z	old_labelr   Úrefr$   r$   r%   r>      s"    


 z$ShowUrlsTransform.renumber_footnotes)r'   r(   r)   r*   r+   r6   r=   r   r5   r<   r
   r_   rJ   r   r   r,   r-   rK   r>   r$   r$   r$   r%   r7   4   s   r7   c                       sn   e Zd ZdZejddœ‡ fdd„Zeddœdd„Zeddœd	d
„Z	ej
ddœdd„Zejddœdd„Z‡  ZS )r\   z9Collect footnotes and footnote references on the documentN)r   r   c                    s$   g | _ tƒ | _g | _tƒ  |¡ d S r1   )r^   Úsetr`   rb   ÚsuperÚ__init__)r!   r   ©Ú	__class__r$   r%   rf   Ÿ   s    zFootnoteCollector.__init__rP   c                 C   s   d S r1   r$   rT   r$   r$   r%   Úunknown_visit¥   s    zFootnoteCollector.unknown_visitc                 C   s   d S r1   r$   rT   r$   r$   r%   Úunknown_departure¨   s    z#FootnoteCollector.unknown_departurec                 C   s6   |  d¡r| j |¡ n|d D ]}| j |¡ q d S )NrU   rV   )rE   r^   rZ   r`   Úadd)r!   r#   Únamer$   r$   r%   Úvisit_footnote«   s    
z FootnoteCollector.visit_footnotec                 C   s   | j  |¡ d S r1   )rb   rZ   rT   r$   r$   r%   Úvisit_footnote_reference²   s    z*FootnoteCollector.visit_footnote_reference)r'   r(   r)   r*   r   r   rf   r
   ri   rj   r,   rm   r-   rn   Ú__classcell__r$   r$   rg   r%   r\   œ   s   r\   c                   @   s(   e Zd ZdZdZdZeddœdd„ZdS )ÚLaTeXFootnoteTransformaT  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.

      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">
              <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  r0   Nr   c                 K   sD   t | j tj¡ƒ}|D ]}|j |¡ qt| j|ƒ}| j |¡ d S r1   )	r2   r   r   r   r,   r3   r4   ÚLaTeXFootnoteVisitorr]   )r!   r   Ú	footnotesr#   Zvisitorr$   r$   r%   r5   ^  s
    zLaTeXFootnoteTransform.run©r'   r(   r)   r*   r+   r6   r   r5   r$   r$   r$   r%   rp   ¶   s
    %rp   c                       s~  e Zd Zejeej ddœ‡ fdd„Zeddœdd„Z	eddœdd	„Z
eddœd
d„Zeddœdd„Zejddœdd„Zejddœdd„Zejddœdd„Zejddœdd„Zejddœdd„Zejddœdd„Zejddœdd„Zejddœdd„Zejddœdd„Zejddœd d!„Zejddœd"d#„Zejddœd$d%„Zejddœd&d'„Z ej!ddœd(d)„Z"ej!ejdœd*d+„Z#‡  Z$S ),rq   N)r   rr   r   c                    s0   t ƒ | _|| _g | _g | _d | _tƒ  |¡ d S r1   )rd   Úappearedrr   ÚpendingsÚtable_footnotesÚ
restrictedre   rf   )r!   r   rr   rg   r$   r%   rf   h  s    zLaTeXFootnoteVisitor.__init__rP   c                 C   s   d S r1   r$   rT   r$   r$   r%   ri   p  s    z"LaTeXFootnoteVisitor.unknown_visitc                 C   s   d S r1   r$   rT   r$   r$   r%   rj   s  s    z&LaTeXFootnoteVisitor.unknown_departurec                 C   s   | j d u r|| _ d S r1   )rw   rT   r$   r$   r%   Úrestrictv  s    
zLaTeXFootnoteVisitor.restrictc                 C   sl   | j |krhd | _ |j |¡}t| jƒD ]:\}}tdg|j¢R d|d iŽ}|j || d |¡ q&g | _d S )NrA   rX   r/   )rw   r3   rI   Ú	enumerateru   r   ÚchildrenrL   )r!   r#   ÚposÚir,   Úfntextr$   r$   r%   Ú
unrestrictz  s    
zLaTeXFootnoteVisitor.unrestrictc                 C   s   |   |¡ d S r1   ©rx   rT   r$   r$   r%   Úvisit_figureƒ  s    z!LaTeXFootnoteVisitor.visit_figurec                 C   s   |   |¡ d S r1   ©r~   rT   r$   r$   r%   Údepart_figure†  s    z"LaTeXFootnoteVisitor.depart_figurec                 C   s   |   |¡ d S r1   r   rT   r$   r$   r%   Ú
visit_term‰  s    zLaTeXFootnoteVisitor.visit_termc                 C   s   |   |¡ d S r1   r   rT   r$   r$   r%   Údepart_termŒ  s    z LaTeXFootnoteVisitor.depart_termc                 C   s   |   |¡ d S r1   r   rT   r$   r$   r%   Úvisit_caption  s    z"LaTeXFootnoteVisitor.visit_captionc                 C   s   |   |¡ d S r1   r   rT   r$   r$   r%   Údepart_caption’  s    z#LaTeXFootnoteVisitor.depart_captionc                 C   s"   t |jtjtjfƒr|  |¡ d S r1   )rR   r3   r   ÚsectionÚtablerx   rT   r$   r$   r%   Úvisit_title•  s    z LaTeXFootnoteVisitor.visit_titlec                 C   sL   t |jtjƒr|  |¡ n.t |jtjƒrH|  j| j7  _g | _|  |¡ d S r1   )rR   r3   r   r‡   r~   rˆ   rv   ru   rT   r$   r$   r%   Údepart_title™  s    z!LaTeXFootnoteVisitor.depart_titlec                 C   s   |   |¡ d S r1   r   rT   r$   r$   r%   Úvisit_thead¡  s    z LaTeXFootnoteVisitor.visit_theadc                 C   s$   |  j | j7  _ g | _|  |¡ d S r1   )rv   ru   r~   rT   r$   r$   r%   Údepart_thead¤  s    z!LaTeXFootnoteVisitor.depart_theadc                 C   sR   t | tj¡ƒ}t| jƒD ],}tdg|j¢R d|d iŽ}| d|¡ qg | _d S )NrA   rX   r   )	Únextr   r   ÚtbodyÚreversedrv   r   rz   rL   )r!   r#   rŽ   r,   r}   r$   r$   r%   Údepart_table©  s
    z!LaTeXFootnoteVisitor.depart_tablec                 C   s   |   |¡ d S r1   r   rT   r$   r$   r%   rm   ±  s    z#LaTeXFootnoteVisitor.visit_footnotec                 C   s   |   |¡ d S r1   r   rT   r$   r$   r%   Údepart_footnote´  s    z$LaTeXFootnoteVisitor.depart_footnotec                 C   sÌ   |  ¡  ¡ }|d }| jr\td||d d}| |¡ ||f| jvr²|  |¡}| j |¡ nV||f| jv rˆtd||d d}| |¡ n*|  |¡}| j	 
|¡ | |¡ | | ¡ | j ||f¡ tj‚d S )Nr   rA   rY   )rY   )rH   Ústriprw   r   ra   rt   Úget_footnote_by_referenceru   rZ   rr   r4   r]   rk   r   ZSkipNode)r!   r#   Znumberr   Zmarkr,   r$   r$   r%   rn   ·  s"    




z-LaTeXFootnoteVisitor.visit_footnote_referencec                 C   s@   |d }| j D ],}||d kr|d d |d kr|  S qd S )Nr   rX   r   rY   )rr   )r!   r#   r   r,   r$   r$   r%   r“   Ì  s
    
 
z.LaTeXFootnoteVisitor.get_footnote_by_reference)%r'   r(   r)   r   r   r   r,   rf   r
   ri   rj   r	   rx   r~   Zfigurer€   r‚   Ztermrƒ   r„   Zcaptionr…   r†   Útitler‰   rŠ   Ztheadr‹   rŒ   rˆ   r   rm   r‘   r-   rn   r“   ro   r$   r$   rg   r%   rq   g  s(   	rq   c                   @   s(   e Zd ZdZdZdZeddœ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î  r0   Nr   c                 K   sP   t ƒ }t| j tj¡ƒD ]}|j |¡ ||7 }qt|ƒdkrL|  j|7  _d S )Nr   )	r   r2   r   r   r   Úcitationr3   r4   Úlen)r!   r   Ú	citationsr#   r$   r$   r%   r5   ö  s    
zBibliographyTransform.runrs   r$   r$   r$   r%   r•   Õ  s   r•   c                   @   s(   e Zd ZdZdZdZeddœdd„ZdS )ÚCitationReferenceTransformz¹Replace pending_xref nodes for citation by citation_reference.

    To handle citation reference easily on LaTeX writer, this converts
    pending_xref nodes to citation_reference.
    é   r0   Nr   c           	      K   s|   t t| j d¡ƒ}ttjddd}| j |¡D ]H}|j	 
|d d¡\}}}|r.tjddg|j¢R ||dœŽ}| |¡ q.d S )Nr–   rc   )Ú	refdomainÚreftypeÚ	reftarget)rA   rA   r   rA   )r   Zrefname)r   r   r    Ú
get_domainr   r   Úpending_xrefr   r   r˜   rE   r   Zcitation_referencerz   ra   )	r!   r   Údomainr"   r#   r   ZlabelidÚ_Zcitation_refr$   r$   r%   r5   	  s    ÿzCitationReferenceTransform.runrs   r$   r$   r$   r%   r™      s   r™   c                   @   s(   e Zd ZdZdZdZeddœdd„ZdS )ÚMathReferenceTransformz©Replace 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š   r0   Nr   c                 K   sv   | j  d¡jd }| j tj¡D ]P}|d dkr |d dv r | |d d¡\}}|r td||d d	}| 	|¡ q d S )
NZmathZobjectsr›   rœ   )ÚeqZnumrefr   )NNrA   )r   Útarget)
r    rž   Údatar   r   r   rŸ   rE   r   ra   )r!   r   Z	equationsr#   r   r¡   Zrefnoder$   r$   r%   r5     s    zMathReferenceTransform.runrs   r$   r$   r$   r%   r¢     s   r¢   c                   @   s(   e Zd ZdZdZdZeddœdd„ZdS )ÚLiteralBlockTransformzEReplace container nodes for literal_block by captioned_literal_block.r8   r0   Nr   c                 K   sH   t tjdd}| j |¡D ](}tdg|j¢R i |j¤Ž}| |¡ qd S )NT)Zliteral_blockrA   )	r   r   Ú	containerr   r   r   rz   Z
attributesra   )r!   r   r"   r#   Znewnoder$   r$   r%   r5   ,  s    zLiteralBlockTransform.runrs   r$   r$   r$   r%   r¦   '  s   r¦   c                   @   s(   e Zd ZdZdZdZeddœdd„ZdS )ÚDocumentTargetTransformz5Add :doc label to the first section of each document.r8   r0   Nr   c                 K   s6   | j  tj¡D ]"}| tj¡}|r|d  d¡ qd S )NrX   z:doc)r   r   r   rS   Z	next_noder   r‡   rZ   )r!   r   r#   r‡   r$   r$   r%   r5   8  s    zDocumentTargetTransform.runrs   r$   r$   r$   r%   r¨   3  s   r¨   c                   @   s(   e Zd ZdZdZdZeddœ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
            ...
    r8   r0   Nr   c                 K   s`   t | j tj¡ƒD ]H}t|jtjƒrt| t	j
¡ƒD ]$\}}| |¡ |j |d |¡ q4qd S )Nr/   )r2   r   r   r   r”   rR   r3   r‡   ry   r   rI   r4   rL   )r!   r   r#   r|   rI   r$   r$   r%   r5   \  s
    
z IndexInSectionTitleTransform.runrs   r$   r$   r$   r%   r©   ?  s   r©   )Úappr   c                 C   sp   |   t¡ |  t¡ |  t¡ |  t¡ |  t¡ |  t¡ |  t¡ |  t	¡ |  t
¡ |  t¡ ddddœS )NZbuiltinT)ÚversionZparallel_read_safeZparallel_write_safe)Zadd_transformr   Zadd_post_transformr.   r•   r™   r¨   r©   rp   r¦   r¢   r7   )rª   r$   r$   r%   Úsetupe  s    









ýr¬   N)1r*   Útypingr   r   r   r   r   r   Zdocutilsr   Zdocutils.nodesr	   r
   Zdocutils.transforms.referencesr   Zsphinxr   Zsphinx.applicationr   Zsphinx.builders.latex.nodesr   r   r   r   r   Zsphinx.domains.citationr   Zsphinx.transformsr   Z!sphinx.transforms.post_transformsr   Zsphinx.util.nodesr   rG   r   r.   r7   ZNodeVisitorr\   rp   rq   r•   r™   r¢   r¦   r¨   r©   r_   r¬   r$   r$   r$   r%   Ú<module>   s4   
 h 2n+&