o $aP@sdZddlmZmZmZmZmZmZddlm Z ddl m Z m Z ddl mZddlmZddlmZddlmZmZmZmZmZdd lmZdd lmZdd lmZdd lm Z d Z!GdddeZ"GdddeZ#GdddeZ$Gddde j%Z&GdddeZ'Gddde j%Z(GdddeZ)GdddeZ*GdddeZ+Gd d!d!eZ,Gd"d#d#eZ-Gd$d%d%eZ.d&ed'ee/effd(d)Z0d*S)+z sphinx.builders.latex.transforms ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Transforms for LaTeX builder. :copyright: Copyright 2007-2021 by the Sphinx team, see AUTHORS. :license: BSD, see LICENSE for details. )AnyDictListSetTuplecast)nodes)ElementNode) Substitutions)addnodes)Sphinx)captioned_literal_block footnotemark footnotetextmath_referencethebibliography)CitationDomain)SphinxTransform)SphinxPostTransform) NodeMatcher)mailto:zhttp:zhttps:zftp:c@s2eZdZdZdZejejfZde ddfddZ dS)FootnoteDocnameUpdaterz5Add docname to footnote and footnote_reference nodes.ikwargsreturnNcKs,t|j}|j|D]}|jj|d<q dS)Ndocname)r TARGET_NODESdocumenttraverseenvr)selfrmatchernoder#B/usr/lib/python3/dist-packages/sphinx/builders/latex/transforms.pyapply"s zFootnoteDocnameUpdater.apply) __name__ __module__ __qualname____doc__default_priorityrfootnotefootnote_referencerrr%r#r#r#r$rs  rc@s0eZdZdZejdZdZdeddfddZdS) SubstitutionDefinitionsRemoverz4Remove ``substitution_definition node from doctrees.latexrrNcKs(t|jtjD]}|j|q dSN)listrrrsubstitution_definitionparentremove)r rr"r#r#r$run/sz"SubstitutionDefinitionsRemover.run) r&r'r(r)r r*formatsrr6r#r#r#r$r-(s  r-c@sveZdZdZdZdZdZdeddfdd Zdd d Z d e de fd dZ de de de ejejffddZdddZdS)ShowUrlsTransformzExpand references to inline text or footnotes. For more information, see :confval:`latex_show_urls`. .. note:: This transform is used for integrated doctree r/FrrNcKsLz!|jj}|j}d|_||jr|W||_dSW||_dS||_w)N show_urls)rsettings id_prefixexpand_show_urlsexpandedrenumber_footnotes)r rr;r<r#r#r$r6As  zShowUrlsTransform.runc Cs|jj}|dus |dkrdSt|jtjD][}|dd}|t rr|dr/|dd}| |krr|j |}| |}|dkrb|||\}}|j |d||j |d |d |_qtd |}|j |d|qdS) NFnorefurirr+r.Tz (%s))configlatex_show_urlsr2rrr referenceget startswith URI_SCHEMESastextr4indexget_docname_for_nodecreate_footnoteinsertr>Text) r r:r"urirLrfnfnreftextnoder#r#r$r=Os(       z"ShowUrlsTransform.expand_show_urlsr"cCsB|rt|tjr|j|dSt|tjr|dS|j}|sdS)Nsourcer) isinstancerrrpath2docr start_of_filer4r r"r#r#r$rMfs  z&ShowUrlsTransform.get_docname_for_noderQrcCstjdt||dd}tj|d|d}|dd|tdd7}|tdd|7}|j|tj dd|d d |d }|td7}|j || |d d ||fS) NrBT)rA nolinkurlr.)autornames#z[#]_idsr)r[refidr) rrGrPr+appendlabel paragraphrnote_autofootnoter,note_autofootnote_ref add_backref)r rQrrGr+ footnote_refr#r#r$rNqs   z!ShowUrlsTransform.create_footnotecCst|j}|j|d}|jD]e} |d7}t||jvrnqttj|d}| tdt|||dvrB|d | |d t||d}|j D]"}||dkrt|dd|dkrt| |d|tt|7}qRqdS) NrTr.rBr\rr^r_)FootnoteCollectorr walkaboutauto_footnotesstrused_footnote_numbersrrra replace_selfr5rKr` footnote_refsrP)r collectornumr+ old_labelrrefr#r#r$r?s,      z$ShowUrlsTransform.renumber_footnotes)rN)r&r'r(r)r*r7r>rr6r=r rjrMrrr+r,rNr?r#r#r#r$r84s " r8csxeZdZdZdejddffdd Zdeddfdd Zdeddfd d Z dej ddfd d Z dej ddfddZ ZS)rgz9Collect footnotes and footnote references on the documentrrNcs$g|_t|_g|_t|dSr1)risetrkrmsuper__init__)r r __class__r#r$rtszFootnoteCollector.__init__r"cCdSr1r#rYr#r#r$ unknown_visitzFootnoteCollector.unknown_visitcCrwr1r#rYr#r#r$unknown_departureryz#FootnoteCollector.unknown_departurecCs8|dr |j|dS|dD]}|j|qdS)Nr[r\)rHrir`rkadd)r r"namer#r#r$visit_footnotes  z FootnoteCollector.visit_footnotecCs|j|dSr1)rmr`rYr#r#r$visit_footnote_referencesz*FootnoteCollector.visit_footnote_reference)r&r'r(r)rrrtr rxrzr+r}r,r~ __classcell__r#r#rur$rgsrgc@*eZdZdZdZdZdeddfddZdS) LaTeXFootnoteTransformaTConvert 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::
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��r/���r���r���Nc�����������������K���sD���t�|�jtj}|D�]}|j|�q t|�j|}|�j|�d�S�r1���) r2���r���r���r���r+���r4���r5���LaTeXFootnoteVisitorrh���)r ���r��� footnotesr"���visitorr#���r#���r$���r6���^��s ��� zLaTeXFootnoteTransform.runr&���r'���r(���r)���r*���r7���r���r6���r#���r#���r#���r$���r������s �����%r���c�����������������������s��e�Zd�Zdejdeej�ddf�fdd Zdeddfdd Z deddfd d Z de ddfd d Z de ddfddZ dejddfddZdejddfddZdejddfddZdejddfddZdejddfddZdejddfddZdejddfddZdejddfddZdejddfd d!Zdejddfd"d#Zdejddfd$d%Zdejddfd&d'Zdejddfd(d)Z dej!ddfd*d+Z"dej!dejfd,d-Z#��Z$S�).r���r���r���r���Nc��������������������s0���t��|�_||�_g�|�_g�|�_d�|�_t�|�d�S�r1���)rr���appearedr���pendingstable_footnotes restrictedrs���rt���)r ���r���r���ru���r#���r$���rt���h��s ���zLaTeXFootnoteVisitor.__init__r"���c�����������������C���rw���r1���r#���rY���r#���r#���r$���rx���p��ry���z"LaTeXFootnoteVisitor.unknown_visitc�����������������C���rw���r1���r#���rY���r#���r#���r$���rz���s��ry���z&LaTeXFootnoteVisitor.unknown_departurec�����������������C���s���|�j�d�u�r ||�_�d�S�d�S�r1���)r���rY���r#���r#���r$���restrictv��s���  zLaTeXFootnoteVisitor.restrictc�����������������C���sp���|�j�|kr6d�|�_�|j|}t|�jD�]\}}tdg|jR�d|d�i}|j||�d�|�qg�|�_d�S�d�S�)NrB���r^���r.���)r���r4���rL��� enumerater���r���childrenrO���)r ���r"���posir+���fntextr#���r#���r$��� unrestrictz��s���   zLaTeXFootnoteVisitor.unrestrictc�����������������C������|��|�d�S�r1���r���rY���r#���r#���r$��� visit_figure�����z!LaTeXFootnoteVisitor.visit_figurec�����������������C���r���r1���r���rY���r#���r#���r$��� depart_figure��r���z"LaTeXFootnoteVisitor.depart_figurec�����������������C���r���r1���r���rY���r#���r#���r$��� visit_term��r���zLaTeXFootnoteVisitor.visit_termc�����������������C���r���r1���r���rY���r#���r#���r$��� depart_term��r���z LaTeXFootnoteVisitor.depart_termc�����������������C���r���r1���r���rY���r#���r#���r$��� visit_caption��r���z"LaTeXFootnoteVisitor.visit_captionc�����������������C���r���r1���r���rY���r#���r#���r$���depart_caption��r���z#LaTeXFootnoteVisitor.depart_captionc�����������������C���s&���t�|jtjtjfr|�|�d�S�d�S�r1���)rV���r4���r���sectiontabler���rY���r#���r#���r$��� visit_title��s���z LaTeXFootnoteVisitor.visit_titlec�����������������C���sR���t�|jtjr|�|�d�S�t�|jtjr'|��j|�j7��_g�|�_|�|�d�S�d�S�r1���)rV���r4���r���r���r���r���r���r���rY���r#���r#���r$��� depart_title��s���z!LaTeXFootnoteVisitor.depart_titlec�����������������C���r���r1���r���rY���r#���r#���r$��� visit_thead��r���z LaTeXFootnoteVisitor.visit_theadc�����������������C���s$���|��j�|�j7��_�g�|�_|�|�d�S�r1���)r���r���r���rY���r#���r#���r$��� depart_thead��s���z!LaTeXFootnoteVisitor.depart_theadc�����������������C���sV���t�|tjd�}t|�jD�]}tdg|jR�d|d�i}|d|�qg�|�_d�S�)Nr���rB���r^���) r2���r���r���tbodyreversedr���r���r���rO���)r ���r"���r���r+���r���r#���r#���r$��� depart_table��s ��� z!LaTeXFootnoteVisitor.depart_tablec�����������������C���r���r1���r���rY���r#���r#���r$���r}�����r���z#LaTeXFootnoteVisitor.visit_footnotec�����������������C���r���r1���r���rY���r#���r#���r$���depart_footnote��r���z$LaTeXFootnoteVisitor.depart_footnotec�����������������C���s���|���}|d�}|�jr.td||d�d}||�||f|�jvr-|�|}|�j|�n+||f|�jv�rDtd||d�d}||�n|�|}|�j |�||�| |��|�j ||f�t j)Nr���rB���r_���)r_���)rK���stripr���r���rl���r���get_footnote_by_referencer���r`���r���r5���rh���r{���r���SkipNode)r ���r"���numberr���markr+���r#���r#���r$���r~�����s$���         z-LaTeXFootnoteVisitor.visit_footnote_referencec�����������������C���s@���|d�}|�j�D�]}||d�kr|d�d�|d�kr|��S�qd�S�)Nr���r^���r���r_���)r���)r ���r"���r���r+���r#���r#���r$���r�����s ���  z.LaTeXFootnoteVisitor.get_footnote_by_reference)%r&���r'���r(���r���r���r���r+���rt���r ���rx���rz���r ���r���r���figurer���r���termr���r���captionr���r���titler���r���theadr���r���r���r���r}���r���r,���r~���r���r���r#���r#���ru���r$���r���g��s*����" r���c�������������������@���r���) 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���Nc�����������������K���sT���t��}t|�jtjD�] }|j|�||7�}q t|dkr(|��j|7��_d�S�d�S�)Nr���) r���r2���r���r���r���citationr4���r5���len)r ���r��� citationsr"���r#���r#���r$���r6�����s���   zBibliographyTransform.runr���r#���r#���r#���r$���r�����s ����r���c�������������������@���r���) 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. ���r/���r���r���Nc����������� ������K���s|���t�t|�jd}ttjddd}|�j|D�]$}|j |d�d\}}}|r;t j ddg|j R�||d}||�qd�S�)Nr���rq���) refdomainreftype reftarget)rB���rB���r���rB���)r���refname)r���r���r��� get_domainr���r ��� pending_xrefr���r���r���rH���r���citation_referencer���rl���) r ���r���domainr!���r"���r���labelid_ citation_refr#���r#���r$���r6��� ��s��� zCitationReferenceTransform.runr���r#���r#���r#���r$���r������ ����r���c�������������������@���r���) 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���r/���r���r���Nc�����������������K���sv���|�j�djd�}|�jtjD�](}|d�dkr8|d�dv�r8||d�d\}}|r8td||d�d }| |�qd�S�) Nmathobjectsr���r���)eqnumrefr���)NNrB���)r���target) r���r���datar���r���r ���r���rH���r���rl���)r ���r��� equationsr"���r���r���refnoder#���r#���r$���r6�����s��� zMathReferenceTransform.runr���r#���r#���r#���r$���r�����r���r���c�������������������@���r���) LiteralBlockTransformzEReplace container nodes for literal_block by captioned_literal_block.r9���r/���r���r���Nc�����������������K���sH���t�tjdd}|�j|D�]}tdg|jR�i�|j}||�q d�S�)NT) literal_blockrB���) r���r��� containerr���r���r���r��� attributesrl���)r ���r���r!���r"���newnoder#���r#���r$���r6���,��s ��� zLiteralBlockTransform.runr���r#���r#���r#���r$���r���'�� ����r���c�������������������@���r���) DocumentTargetTransformz5Add :doc label to the first section of each document.r9���r/���r���r���Nc�����������������K���s6���|�j�tjD�]}|tj}|r|d�d�qd�S�)Nr^���z:doc)r���r���r ���rX��� next_noder���r���r`���)r ���r���r"���r���r#���r#���r$���r6���8��s ��� zDocumentTargetTransform.runr���r#���r#���r#���r$���r���3��r���r���c�������������������@���r���) 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 ... r9���r/���r���r���Nc�����������������K���sd���t�|�jtjD�]&}t|jtjr/tt�|t j D�]\}}| |�|j |d�|�qq d�S�)Nr.���) r2���r���r���r���r���rV���r4���r���r���r ���rL���r5���rO���)r ���r���r"���r���rL���r#���r#���r$���r6���\��s��� z IndexInSectionTitleTransform.runr���r#���r#���r#���r$���r���?��s ����r���appr���c�����������������C���sp���|��t�|�t�|�t�|�t�|�t�|�t�|�t�|�t �|�t �|�t �ddddS�)NbuiltinT)versionparallel_read_safeparallel_write_safe) add_transformr���add_post_transformr-���r���r���r���r���r���r���r���r8���)r���r#���r#���r$���setupe��s���          r���N)1r)���typingr���r���r���r���r���r���docutilsr���docutils.nodesr ���r ���docutils.transforms.referencesr ���sphinxr ���sphinx.applicationr ���sphinx.builders.latex.nodesr���r���r���r���r���sphinx.domains.citationr���sphinx.transformsr���!sphinx.transforms.post_transformsr���sphinx.util.nodesr���rJ���r���r-���r8��� NodeVisitorrg���r���r���r���r���r���r���r���r���rj���r���r#���r#���r#���r$���<module>���s6����          h�2n+  &