U
    \ªShR  ã                   @  s”  d Z ddlmZ ddl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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.d(d)d*œd+d,„Z/d-S ).zTransforms for LaTeX builder.é    )Úannotations)ÚAnyÚ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dddœdd„Z	dS )	ÚFootnoteDocnameUpdaterz5Add docname to footnote and footnote_reference nodes.i¼  r   ÚNone©Ú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#   úS/root/rtd-docs/venv/lib/python3.8/site-packages/sphinx/builders/latex/transforms.pyÚapply"   s    
zFootnoteDocnameUpdater.applyN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__Údefault_priorityr   ÚfootnoteÚfootnote_referencer   r%   r#   r#   r#   r$   r      s   r   c                   @  s.   e Zd ZdZejd ZdZdddœdd„Zd	S )
ÚSubstitutionDefinitionsRemoverz7Remove ``substitution_definition`` nodes from doctrees.é   ©Úlatexr   r   r   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.runN)r&   r'   r(   r)   r   r*   Úformatsr5   r#   r#   r#   r$   r-   (   s   
r-   c                   @  sj   e Zd ZdZdZdZdZdddœdd	„Zdd
œdd„Zdddœdd„Z	ddd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
    é  r/   Fr   r   r   c                 K  s<   z.| jj}|j }d|_ |  ¡  | jr,|  ¡  W 5 ||_ X d S )NÚ	show_urls)Ú	id_prefixr   ÚsettingsÚ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k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))Ú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"   ÚurirJ   r   ÚfnZfnrefZtextnoder#   r#   r$   r<   O   s$    


z"ShowUrlsTransform.expand_show_urlsr   Ústr©r"   r   c                 C  sˆ   |r@t |tjƒr$| j |d ¡p"dS t |tjƒr8|d S |j}q z|d }W n" tk
rn   t	t
dƒƒd ‚Y nX t	t
dƒj|dƒ‚d S )NÚsourcerA   r   zFailed to get a docname!z.Failed to get a docname for source {source!r}!)rS   )Ú
isinstancer   r   r   Zpath2docr	   Ústart_of_filer3   Ú	TypeErrorÚ
ValueErrorr   Úformat)r    r"   rS   r#   r#   r$   rK   f   s    
ÿz&ShowUrlsTransform.get_docname_for_nodez/tuple[nodes.footnote, nodes.footnote_reference])rO   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   )rY   Úrefidr   )r   rE   rN   r+   ÚappendÚlabelZ	paragraphr   Znote_autofootnoter,   Znote_autofootnote_refZadd_backref)r    rO   r   rE   r+   Zfootnote_refr#   r#   r$   rL   v   s    
 ÿz!ShowUrlsTransform.create_footnotec                 C  sî   t | jƒ}| j |¡ d}|jD ]È}|d7 }t|ƒ|jkr$q>q$ttj|d ƒ}| 	t dt|ƒ¡¡ ||d k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   rZ   r   r\   r]   )ÚFootnoteCollectorr   Ú	walkaboutÚauto_footnotesrQ   Úused_footnote_numbersr   r   r_   Úreplace_selfr4   rI   r^   Úfootnote_refsrN   )r    Ú	collectorÚnumr+   Z	old_labelr   Úrefr#   r#   r$   r>   ˆ   s"    


 z$ShowUrlsTransform.renumber_footnotesN)r&   r'   r(   r)   r*   r6   r=   r5   r<   rK   rL   r>   r#   r#   r#   r$   r7   4   s   r7   c                      sh   e Zd ZdZdddœ‡ fdd„Zdddœd	d
„Zdddœdd„Zdddœdd„Zdddœdd„Z‡  Z	S )r`   z9Collect footnotes and footnote references on the documentúnodes.documentr   )r   r   c                   s$   g | _ tƒ | _g | _tƒ  |¡ d S r1   )rb   Úsetrc   re   ÚsuperÚ__init__)r    r   ©Ú	__class__r#   r$   rl   ¦   s    zFootnoteCollector.__init__r   rR   c                 C  s   d S r1   r#   ©r    r"   r#   r#   r$   Úunknown_visit¬   s    zFootnoteCollector.unknown_visitc                 C  s   d S r1   r#   ro   r#   r#   r$   Úunknown_departure¯   s    z#FootnoteCollector.unknown_departureúnodes.footnotec                 C  s6   |  d¡r| j |¡ n|d D ]}| j |¡ q d S )NrY   rZ   )rF   rb   r^   rc   Úadd)r    r"   Únamer#   r#   r$   Úvisit_footnote²   s    
z FootnoteCollector.visit_footnoteúnodes.footnote_referencec                 C  s   | j  |¡ d S r1   )re   r^   ro   r#   r#   r$   Úvisit_footnote_reference¹   s    z*FootnoteCollector.visit_footnote_reference)
r&   r'   r(   r)   rl   rp   rq   ru   rw   Ú__classcell__r#   r#   rm   r$   r`   £   s   r`   c                   @  s(   e Zd ZdZdZdZdddœ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  r/   r   r   r   c                 K  sD   t | j tj¡ƒ}|D ]}|j |¡ qt| j|ƒ}| j |¡ d S r1   )	r2   r   r   r   r+   r3   r4   ÚLaTeXFootnoteVisitorra   )r    r   Ú	footnotesr"   Úvisitorr#   r#   r$   r5   f  s
    zLaTeXFootnoteTransform.runN©r&   r'   r(   r)   r*   r6   r5   r#   r#   r#   r$   ry   ½   s
    &ry   c                      sV  e Zd Zddddœ‡ fdd„Zdddœd	d
„Zdddœdd„Zdddœdd„Zdddœdd„Zdddœdd„Zdddœdd„Z	dddœdd„Z
dddœdd„Zdddœdd„Zdddœdd „Zd!ddœd"d#„Zd!ddœd$d%„Zd&ddœd'd(„Zd&ddœd)d*„Zd+ddœd,d-„Zd.ddœd/d0„Zd.ddœd1d2„Zd3ddœd4d5„Zd3d.dœd6d7„Z‡  ZS )8rz   ri   zlist[nodes.footnote]r   )r   r{   r   c                   s.   i | _ || _g | _g | _d | _tƒ  |¡ d S r1   )Úappearedr{   ÚpendingsÚtable_footnotesÚ
restrictedrk   rl   )r    r   r{   rm   r#   r$   rl   p  s    zLaTeXFootnoteVisitor.__init__r   rR   c                 C  s   d S r1   r#   ro   r#   r#   r$   rp   x  s    z"LaTeXFootnoteVisitor.unknown_visitc                 C  s   d S r1   r#   ro   r#   r#   r$   rq   {  s    z&LaTeXFootnoteVisitor.unknown_departurer   c                 C  s   | j d kr|| _ d S r1   )r   ro   r#   r#   r$   Úrestrict~  s    
zLaTeXFootnoteVisitor.restrictc                 C  sh   | j |krdd | _ |j |¡}t| jƒD ]6\}}td|jžd|d iŽ}|j || d |¡ q&g | _d S )NrA   r\   r.   )rA   )r   r3   rJ   Ú	enumerater   r   ÚchildrenrM   )r    r"   ÚposÚir+   Úfntextr#   r#   r$   Ú
unrestrict‚  s    
zLaTeXFootnoteVisitor.unrestrictznodes.figurec                 C  s   |   |¡ d S r1   ©r‚   ro   r#   r#   r$   Úvisit_figure‹  s    z!LaTeXFootnoteVisitor.visit_figurec                 C  s   |   |¡ d S r1   ©rˆ   ro   r#   r#   r$   Údepart_figureŽ  s    z"LaTeXFootnoteVisitor.depart_figurez
nodes.termc                 C  s   |   |¡ d S r1   r‰   ro   r#   r#   r$   Ú
visit_term‘  s    zLaTeXFootnoteVisitor.visit_termc                 C  s   |   |¡ d S r1   r‹   ro   r#   r#   r$   Údepart_term”  s    z LaTeXFootnoteVisitor.depart_termznodes.captionc                 C  s   |   |¡ d S r1   r‰   ro   r#   r#   r$   Úvisit_caption—  s    z"LaTeXFootnoteVisitor.visit_captionc                 C  s   |   |¡ d S r1   r‹   ro   r#   r#   r$   Údepart_captionš  s    z#LaTeXFootnoteVisitor.depart_captionznodes.titlec                 C  s"   t |jtjtjfƒr|  |¡ d S r1   )rT   r3   r   ÚsectionÚtabler‚   ro   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   )rT   r3   r   r‘   rˆ   r’   r€   r   ro   r#   r#   r$   Údepart_title¡  s    z!LaTeXFootnoteVisitor.depart_titleznodes.theadc                 C  s   |   |¡ d S r1   r‰   ro   r#   r#   r$   Úvisit_thead©  s    z LaTeXFootnoteVisitor.visit_theadc                 C  s$   |  j | j7  _ g | _|  |¡ d S r1   )r€   r   rˆ   ro   r#   r#   r$   Údepart_thead¬  s    z!LaTeXFootnoteVisitor.depart_theadznodes.tablec                 C  sN   t | tj¡ƒ}t| jƒD ](}td|jžd|d iŽ}| d|¡ qg | _d S )NrA   r\   r   )rA   )	Únextr   r   ÚtbodyÚreversedr€   r   r„   rM   )r    r"   r˜   r+   r‡   r#   r#   r$   Údepart_table±  s
    z!LaTeXFootnoteVisitor.depart_tablerr   c                 C  s   |   |¡ d S r1   r‰   ro   r#   r#   r$   ru   ¹  s    z#LaTeXFootnoteVisitor.visit_footnotec                 C  s   |   |¡ d S r1   r‹   ro   r#   r#   r$   Údepart_footnote¼  s    z$LaTeXFootnoteVisitor.depart_footnoterv   c                 C  sÈ   |  ¡  ¡ }|d }||f| jkrV| j||f }d|d< td||d d}| |¡ nh|  |¡}| jrtd||d d}| |¡ | j |¡ n | j	 
|¡ | |¡ | | ¡ || j||f< tj‚d S )Nr   TZreferredrA   r]   )r]   )rI   Ústripr~   r   rd   Úget_footnote_by_referencer   r   r^   r{   r4   ra   r   ZSkipNode)r    r"   Únumberr   r+   Úmarkr#   r#   r$   rw   ¿  s"    



z-LaTeXFootnoteVisitor.visit_footnote_referencec                 C  sP   |d }| j D ],}||d kr|d d |d kr|  S qttdƒ| ƒ‚d S )Nr   r\   r   r]   z1No footnote was found for given reference node %r)r{   rW   r   )r    r"   r   r+   r#   r#   r$   r   Ö  s
    
 
z.LaTeXFootnoteVisitor.get_footnote_by_reference)r&   r'   r(   rl   rp   rq   r‚   rˆ   rŠ   rŒ   r   rŽ   r   r   r“   r”   r•   r–   rš   ru   r›   rw   r   rx   r#   r#   rm   r$   rz   o  s(   	rz   c                   @  s(   e Zd ZdZdZdZd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î  r/   r   r   r   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.runNr}   r#   r#   r#   r$   r    ß  s   r    c                   @  s(   e Zd ZdZdZdZd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.
    é   r/   r   r   r   c           	      K  sv   t t| j d¡ƒ}ttjddd}| j |¡D ]B}|j	 
|d d¡\}}}|r.tjd|jž||dœŽ}| |¡ q.d S )	Nr¡   rh   )Ú	refdomainÚreftypeÚ	reftarget)rA   rA   r   rA   )r   Zrefname)rA   rA   )r   r   r   Ú
get_domainr   r	   Úpending_xrefr   r   r£   rF   r   Zcitation_referencer„   rd   )	r    r   Údomainr!   r"   r   ZlabelidÚ_Zcitation_refr#   r#   r$   r5     s     ÿzCitationReferenceTransform.runNr}   r#   r#   r#   r$   r¤   
  s   r¤   c                   @  s(   e Zd ZdZdZdZd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¥   r/   r   r   r   c                 K  sv   | j  d¡jd }| j tj¡D ]P}|d dkr |d dkr | |d d¡\}}|r td||d d	}| 	|¡ q d S )
NÚmathÚobjectsr¦   r§   )ÚeqZnumrefr¨   )NNrA   )r   Útarget)
r   r©   Údatar   r   r	   rª   rF   r   rd   )r    r   Z	equationsr"   r   r¬   Zrefnoder#   r#   r$   r5   '  s    zMathReferenceTransform.runNr}   r#   r#   r#   r$   r­     s   r­   c                   @  s(   e Zd ZdZdZdZdddœdd„Zd	S )
ÚLiteralBlockTransformzEReplace container nodes for literal_block by captioned_literal_block.r8   r/   r   r   r   c                 K  s@   t tjdd}| j |¡D ] }td|jž|jŽ}| |¡ qd S )NT)Zliteral_blockrA   )rA   )	r   r   Ú	containerr   r   r   r„   Ú
attributesrd   )r    r   r!   r"   Znewnoder#   r#   r$   r5   6  s    zLiteralBlockTransform.runNr}   r#   r#   r#   r$   r³   1  s   r³   c                   @  s(   e Zd ZdZdZdZdddœdd„Zd	S )
ÚDocumentTargetTransformz5Add :doc label to the first section of each document.r8   r/   r   r   r   c                 K  s6   | j  tj¡D ]"}| tj¡}|r|d  d¡ qd S )Nr\   z:doc)r   r   r	   rU   Z	next_noder   r‘   r^   )r    r   r"   r‘   r#   r#   r$   r5   B  s    zDocumentTargetTransform.runNr}   r#   r#   r#   r$   r¶   =  s   r¶   c                   @  s(   e Zd ZdZdZdZd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   r/   r   r   r   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   ÚtitlerT   r3   r‘   rƒ   r	   rJ   r4   rM   )r    r   r"   r†   rJ   r#   r#   r$   r5   f  s
    
z IndexInSectionTitleTransform.runNr}   r#   r#   r#   r$   r·   I  s   r·   r
   zdict[str, Any])Úappr   c                 C  sp   |   t¡ |  t¡ |  t¡ |  t¡ |  t¡ |  t¡ |  t¡ |  t	¡ |  t
¡ |  t¡ ddddœS )NÚbuiltinT)ÚversionZparallel_read_safeZparallel_write_safe)Zadd_transformr   Zadd_post_transformr-   r    r¤   r¶   r·   ry   r³   r­   r7   )r¹   r#   r#   r$   Úsetupo  s    









ýr¼   N)0r)   Ú
__future__r   Útypingr   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.localer   Zsphinx.transformsr   Z!sphinx.transforms.post_transformsr   Zsphinx.util.nodesr   rH   r   r-   r7   ÚNodeVisitorr`   ry   rz   r    r¤   r­   r³   r¶   r·   r¼   r#   r#   r#   r$   Ú<module>   s8   o 3p+&