
    }c3                        d Z ddlZddlZddlZddlZddlZddlmZ ddlmZm	Z	 ddl
mZmZmZmZ ddlmZ ddlmZ ddl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dlmZmZ ddl m!Z!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.  e!j/        e0          Z1 ej2        edd          Z3 G d de          Z4 G d de          Z5dZ6 ej7        d          Z8 ej7        d          Z9 ej7        d          Z:de;de<fd Z=de;d!e<ddfd"Z>	 d=d$e;d%e;d&ed'e;de;f
d(Z?d)ede;fd*Z@d+e;d)ede;fd,ZAd-ee;         d.e;dee;e;f         fd/ZBd0e;d)edee;e<f         fd1ZCd0e;d)edee;e<f         fd2ZDd3e.d%e;dee;e<f         fd4ZEd5ed6eFddfd7ZGd3e.d8ede;fd9ZHd3e.d8ejI        ddfd:ZJd3e.d8ejK        ddfd;ZLd5edee;ef         fd<ZMdS )>z*Render math in HTML via dvipng or dvisvgm.    N)path)PIPECalledProcessError)AnyDictListTuple)nodes)Element)package_dir)Sphinx)Builder)Config)SphinxError)___)loggingsha1)get_node_equation_numberwrap_displaymath)	ensuredir)read_png_depthwrite_png_depth)LaTeXRenderer)HTMLTranslator	templatesimgmathc            	       6     e Zd ZdZddedededdf fdZ xZS )	MathExtErrorzMath extension errorNmsgstderrstdoutreturnc                 r    |r|d|z   z  }|r|d|z   z  }t                                          |           d S )Nz

[stderr]
z

[stdout]
)super__init__)selfr    r!   r"   	__class__s       2lib/python3.11/site-packages/sphinx/ext/imgmath.pyr&   zMathExtError.__init__%   sO     	+>F**C 	+>F**C    NN)__name__
__module____qualname__categorystrr&   __classcell__)r(   s   @r)   r   r   "   sc        %H C  S D          r*   r   c                       e Zd ZdZdS )InvokeErrorzerrors on invoking converters.N)r,   r-   r.   __doc__ r*   r)   r3   r3   -   s        ((((r*   r3   )pngsvgz\[\d+ depth=(-?\d+)\]z.*, depth=(.*)ptz<!-- DEPTH=(-?\d+) -->filenamer#   c                     t          | d          5 }|D ]}t                              |          }|r.t          |                    d                    cddd           S 	 ddd           dS # 1 swxY w Y   dS )z9Read the depth from comment at last line of SVG file
    utf-8encoding   N)opendepthsvgcomment_rematchintgroup)r8   flinematcheds       r)   read_svg_depthrF   8   s     
h	)	)	) Q 	 	D$**400 	)w}}Q''((                         s   AA0"A00A47A4depthc                     t          | dd          5 }|                    d|z             ddd           dS # 1 swxY w Y   dS )z<Write the depth to SVG file as a comment at end of file
    ar:   r;   z
<!-- DEPTH=%s -->N)r>   write)r8   rG   rC   s      r)   write_svg_depthrK   E   s     
hg	.	.	. /!	%-.../ / / / / / / / / / / / / / / / / /s   9= = image_formatmathconfigconfdirc                    |j         t          t          |j         dz                      |j        | dk    rdnd|d}|j        rd}nd}|j        D ]P}t          j        |||          }t          j        |          r$t                      
                    ||          c S Qt          t
                    
                    ||          S )zGenerate LaTeX macro.g333333?r6   rL   z
,tightpage)fontsizebaselineskippreamble	tightpagerN   zpreview.tex_tztemplate.tex_t)imgmath_font_sizerA   roundimgmath_latex_preambleimgmath_use_previewtemplates_pathr   joinexistsr   render)rM   rN   rO   rP   	variablestemplate_nametemplate_dirtemplates           r)   generate_latex_macrorb   L   s     ,E&":S"@AABB1'50BRRl I ! )'(- ? ?9WlMBB;x   	? ??))(I>>>>>	? ((//yIIIr*   builderc                 `    t          | d          st          j                    | _        | j        S )zCreate temporary directory.

    use only one tempdir per build -- the use of a directory is cleaner
    than using temporary files, since we can clean up everything at once
    just removing the whole directory (see cleanup_tempdir)
    _imgmath_tempdir)hasattrtempfilemkdtempre   )rc   s    r)   ensure_tempdirri   d   s1     7.// 6#+#3#5#5 ##r*   latexc                    t          |          }t          j        |d          }t          |dd          5 }|                    |            ddd           n# 1 swxY w Y   |j        j        dg}|                    |j        j                   |	                    d           	 t          j        |t          t          |dd	           t          j        |d
          S # t          $ r?}t                              t!          d          |j        j                   t"          |d}~wt$          $ r!}t'          d|j        |j                  |d}~ww xY w)z%Compile LaTeX macros for math to DVI.zmath.texwr:   r;   Nz--interaction=nonstopmodeTascii)r"   r!   cwdcheckr<   zmath.dvizYLaTeX command %r cannot be run (needed for math display), check the imgmath_latex settingzlatex exited with error)ri   r   r[   r>   rJ   rO   imgmath_latexextendimgmath_latex_argsappend
subprocessrunr   OSErrorloggerwarningr   r3   r   r   r!   r"   )rj   rc   tempdirr8   rC   commandexcs          r)   compile_mathr|   q   s   W%%Gy*--H	hg	.	.	. !	               ~+-HIGNN7>4555NN:
WwtDgT '	) 	) 	) 	)y*--- # # #r F G G~3	5 	5 	5 s" W W W4cj#*MMSVVWs5   AA A'8C   
E*:D$$E1EErz   namec                 R   	 t          j        | t          t          dd          }|j        |j        fS # t
          $ r=}t                              t          d          || d         |           t          |d}~wt          $ r$}t          d|z  |j        |j                  |d}~ww xY w)z*Convert DVI file to specific image format.Trm   )r"   r!   ro   r<   zS%s command %r cannot be run (needed for math display), check the imgmath_%s settingr   Nz%s exited with error)rt   ru   r   r"   r!   rv   rw   rx   r   r3   r   r   )rz   r}   retr{   s       r)   convert_dvi_to_imager      s    	[nWT$dU\]]]z3:%% # # #r C D DWQZ	/ 	/ 	/ s" [ [ [1D8#*cjQQWZZ[s    03 
B&8A55B&B!!B&dvipathc                 4   t          |          }t          j        |d          }d}|j        j        d|dddg}|                    |j        j                   |j        j        r|                    d           |                    |            t          ||          \  }}d}|j        j        rg|
                                D ]R}	t                              |	          }
|
r4t          |
                    d	                    }t          ||            nS||fS )
zConvert DVI file to PNG image.zmath.pngdvipng-oz-Ttightz-z9z--depthNr=   )ri   r   r[   rO   imgmath_dvipngrq   imgmath_dvipng_argsrY   rs   r   
splitlinesdepth_rer@   rA   rB   r   r   rc   ry   r8   r}   rz   r"   r!   rG   rD   rE   s              r)   convert_dvi_to_pngr      s   W%%Gy*--HD~,dHdGUSGNN7>5666~) "y!!!NN7)'488NFFE~) %%'' 	 	DnnT**G GMM!,,--%000
 U?r*   c                    t          |          }t          j        |d          }d}|j        j        d|g}|                    |j        j                   |                    |            t          ||          \  }}d}|j        j	        rz|
                                D ]e}	t                              |	          }
|
rGt          t          |
                    d                    dz  dz            }t!          ||            nf||fS )zConvert DVI file to SVG image.zmath.svgdvisvgmr   Nr=   d   gzGR@)ri   r   r[   rO   imgmath_dvisvgmrq   imgmath_dvisvgm_argsrs   r   rY   r   depthsvg_rer@   rW   floatrB   rK   r   s              r)   convert_dvi_to_svgr      s   W%%Gy*--HD~-tX>GNN7>6777NN7)'488NFFE~) %%'' 	 	D!''--G eGMM!$4$455;eCDD%000
 U?r*   r'   c                 <   | j         j        j                                        }|t          vrt          d          t          ||| j         j        | j         j                  }t          |	                                          
                                d|}t          j        | j         j        d|          }t          j        | j         j        | j         j        d|          }t          j        |          r/|dk    rt%          |          }n|dk    rt'          |          }||fS t)          | j         d          st)          | j         d          rdS 	 t+          || j                   }n# t,          $ r d	| j         _        Y dS w xY w	 |dk    rt1          || j                   \  }	}n|dk    rt3          || j                   \  }	}n# t,          $ r d	| j         _        Y dS w xY wt7          t          j        |                     t;          j        |	|           ||fS )
a  Render the LaTeX math expression *math* using latex and dvipng or
    dvisvgm.

    Return the filename relative to the built document and the "depth",
    that is, the distance of image bottom and baseline in pixels, if the
    option to use preview_latex is switched on.

    Error handling may seem strange, but follows a pattern: if LaTeX or dvipng
    (dvisvgm) aren't available, only a warning is generated (since that enables
    people on machines without these programs to at least build the rest of the
    docs successfully).  If the programs are there, however, they may not fail
    since that indicates a problem in the math source.
    z2imgmath_image_format must be either "png" or "svg".rN   r6   r7   _imgmath_warned_latex _imgmath_warned_image_translatorr+   T)rc   rO   imgmath_image_formatlowerSUPPORT_FORMATr   rb   rP   r   encode	hexdigest	posixpathr[   imgpathr   outdirimagedirisfiler   rF   rf   r|   r3   r   r   r   r   r   dirnameshutilmove)
r'   rN   rM   rj   r8   relfnoutfnrG   r   r   s
             r)   render_mathr      s<    <&;AACCL>) QOPPP !%!%!4!%!57 7E
 u||~~..88::::LLIHN4</BBEIdl)4<+@&(SSE{5 5  	*"5))EEU" 	*"5))Ee| t|455 t|?@@zudl33   -1*zz
5  	G/FFNGUUU" 	G/FFNGU   8<5zz
 dl5!!"""
K%<s$   E+ +FF	=G G! G!appr{   c                     |rd S t          | j        d          sd S 	 t          j        | j        j                   d S # t
          $ r Y d S w xY w)Nre   )rf   rc   r   rmtree_mathpng_tempdir	Exception)r   r{   s     r)   cleanup_tempdirr     sk    
 3; 233 ck233333   s   = 
A
Anodec                     | j         j        j        r<d|                     |                                                                          z  S dS )Nz	 alt="%s"rL   )rc   rO   imgmath_add_tooltipsr   astextstrip)r'   r   s     r)   get_tooltipr     sC    |/ @T[[77==????2r*   c           	         	 t          | d|                                z   dz             \  }}n# t          $ r}t          |          }t	          j        |ddg |                                          }|                    |            t                              t          d          |                                |           t          j
        |d }~ww xY w|U| j                            d|                     |                                                                          z             n>d|z  t          | |          z   }|	|d| z  z  }| j                            |d	z              t          j
        )
N$WARNING   typelevelbackrefssourcezdisplay latex %r: %sz<span class="math">%s</span>z<img class="math" src="%s"z style="vertical-align: %dpx"z/>)r   r   r   r0   r
   system_message	walkaboutrw   rx   r   SkipNodebodyrs   r   r   r   )r'   r   fnamerG   r{   r    smcs           r)   html_visit_mathr     ss   &"4t{{}})<s)BCCuu & & &#hh!#IQ+-dkkmmE E E
Tr0114;;==#FFFn#%&  #	7T[[]]3399;;< 	= 	= 	= 	= *E1[t5L5LL 	<0UF;;A	T"""
.s   +. 
CBCCc           	         |d         r|                                 }n#t          |                                 d d          }	 t          | |          \  }}n# t          $ r}t	          |          }t          j        |ddg |                                           }|                    |            t          	                    t          d          |                                 |           t
          j        |d }~ww xY w| j                            |                     |dd	                     | j                            d
           |d         rjt          | |          }| j                            d|z             |                     |t#          d                     | j                            d           |U| j                            d|                     |                                                                           z             n1| j                            d|z  t)          | |          z   dz              t
          j        )NnowrapFr   r   r   zinline latex %r: %sdivrN   )CLASSz<p>numberz<span class="eqno">(%s)zPermalink to this equationz</span>z'<span class="math">%s</span></p>
</div>z<img src="%s"z/></p>
</div>)r   r   r   r   r0   r
   r   r   rw   rx   r   r   r   rs   starttagr   add_permalink_refr   r   r   r   )	r'   r   rj   r   rG   r{   r    r   r   s	            r)   html_visit_displaymathr   2  s*   H~ = e<<&"4//uu & & &#hh!#IQ+-dkkmmE E E
Tr/00$++--EEEn#%& 	IT]]4f]==>>>IUH~ $)$55	2V;<<<tQ'C%D%DEEE	### +	CT[[]]3399;;< 	= 	= 	= 	= 		/E1[t5L5LL)* 	+ 	+ 	+
.s   A 
C< BC77C<c                    |                      dt          d ft          d f           |                     ddd           |                     ddd           |                     ddd           |                     d	d
d           |                     ddd           |                     dg dd           |                     ddgd           |                     dg d           |                     ddd           |                     ddd           |                     ddd           |                     dt
                     t          j        ddS )Nr   r   r6   htmlr   r   r   r   rp   rj   rY   Fr   )z-gammaz1.5z-D110z-bgTransparentr   z
--no-fontsrr   rX   rL   r   TrV      zbuild-finished)versionparallel_read_safe)add_html_math_rendererr   r   add_config_valueconnectr   sphinx__display_version__)r   s    r)   setupr   Q  sx   y /6 6=? ? ? /???)8V<<<*Iv>>>'6:::.v>>>.MMM! ! ! /,HHH-r6:::12v>>>/v>>>,b&999KK /2221NNNr*   )rL   )Nr4   r   rer   rt   rg   osr   r   r   typingr   r   r   r	   docutilsr
   docutils.nodesr   r   r   sphinx.applicationr   sphinx.buildersr   sphinx.configr   sphinx.errorsr   sphinx.localer   r   sphinx.utilr   r   sphinx.util.mathr   r   sphinx.util.osutilr   sphinx.util.pngr   r   sphinx.util.templater   sphinx.writers.htmlr   	getLoggerr,   rw   r[   rZ   r   r3   r   compiler   r   r?   r0   rA   rF   rK   rb   ri   r|   r   r   r   r   r   r   r   rN   r   
math_blockr   r   r5   r*   r)   <module>r      s   0 0     				             / / / / / / / / ) ) ) ) ) ) ) ) ) ) ) )       " " " " " "        % % % % % % # # # # # #             % % % % % %         % % % % % % % % G G G G G G G G ( ( ( ( ( ( ; ; ; ; ; ; ; ; . . . . . . . . . . . .		8	$	$;Y??    ;   ) ) ) ) )+ ) ) )  2:.//bj,--RZ 9:: 
S 
S 
 
 
 
/c /# /$ / / / / DFJ Js J"J,2J=@JJMJ J J J0
$G 
$ 
$ 
$ 
$ 
$W Wg W# W W W W8[$s) [3 [5c? [ [ [ [ g %S/    4 g %S/    0;n ;C ;E#s(O ; ; ; ;| i D    n G     . 
 t    , u7G D    >Ov O$sCx. O O O O O Or*   