
    -Ph>                       d Z ddlmZ 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	Z	ddl
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Zdd
lm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"m#Z# ddl$m%Z% er*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l0m1Z1 ddl2m3Z3  ej4        e5          Z6 ej7        dd          Z8 G d de          Z9 G d de          Z:dZ; ej<        d          Z= ej<        d           Z> ej<        d!          Z?dVd&Z@dWd+ZAdXd3ZBdYd6ZCdZd8ZDd[d=ZEd\d@ZFd\dAZGd]dEZHd^dGZId_dLZJd`dOZKdadQZLdbdSZMdcdUZNdS )dz*Render math in HTML via dvipng or dvisvgm.    )annotations N)sha1)Path)CalledProcessError)TYPE_CHECKING)nodes)package_dir)SphinxError)___)logging)get_node_equation_numberwrap_displaymath)read_png_depthwrite_png_depth)LaTeXRenderer)Element)Sphinx)Builder)Config)_StrPath)ExtensionMetadata)HTML5Translator	templatesimgmathc                  (     e Zd ZdZ	 dd fd
Z xZS )MathExtErrorzMath extension errorNmsgstrstderr
str | NonestdoutreturnNonec                r    |r|d|z   z  }|r|d|z   z  }t                                          |           d S )Nz

[stderr]
z

[stdout]
)super__init__)selfr   r!   r#   	__class__s       R/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/sphinx/ext/imgmath.pyr(   zMathExtError.__init__1   sQ      	+>F**C 	+>F**C    NN)r   r    r!   r"   r#   r"   r$   r%   )__name__
__module____qualname__categoryr(   __classcell__)r*   s   @r+   r   r   .   sN        %H IM          r,   r   c                      e Zd ZdZdS )InvokeErrorzerrors on invoking converters.N)r.   r/   r0   __doc__r   r,   r+   r4   r4   ;   s        ((((r,   r4   )pngsvgz\[\d+ depth=(-?\d+)\]z.*, depth=(.*)ptz<!-- DEPTH=(-?\d+) -->filenamestr | os.PathLike[str]r$   
int | Nonec                    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 )z4Read 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_depthrH   F   s    	h	)	)	) Q 	 	D$**400 	)w}}Q''((                         s   AA0"A00A47A4r   depthrC   r%   c                    t          | dd          5 }|                    d|z             ddd           dS # 1 swxY w Y   dS )z7Write the depth to SVG file as a comment at end of filear<   r=   z
<!-- DEPTH=%s -->N)r@   write)r8   rI   rE   s      r+   write_svg_depthrM   R   s    	hg	.	.	. /!	%-.../ / / / / / / / / / / / / / / / / /s   9= =image_formatr    mathconfigr   confdirr   c                x   |j         t          |j         dz            |j        | dk    rdnd|d}|j        rd}nd}|j        D ]L}dD ]G}||z  ||z   z  }|                                r&t                                          ||          c c S HMt          t          g                              |d	z   |          S )
zGenerate LaTeX macro.g333333?r6    z,dvips,tightpage)fontsizebaselineskippreamble	tightpagerO   zpreview.texztemplate.tex).jinja_trX   )imgmath_font_sizeroundimgmath_latex_preambleimgmath_use_previewtemplates_pathexistsr   render)	rN   rO   rP   rQ   	variablestemplate_nametemplate_dirtemplate_suffixtemplates	            r+   generate_latex_macrorf   X   s    ,f6<==1 (500RR6H I ! '%&- C C/ 	C 	CO-1PQH   C$--h	BBBBBBBC	C
 .)**11-(2JIVVVr,   builderr   c                z    t          | d          s%t          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)hasattrr   tempfilemkdtempri   )rg   s    r+   ensure_tempdirrm   w   s9     7.// <#'(8(:(:#;#; ##r,   latexc                   t          |          }|dz  }t          |dd          5 }|                    |            ddd           n# 1 swxY w Y   t          j                            |j        j                  }|j        j        g}|dk    r|                    d           |	                    |j        j
                   |                    d           	 t          j        |d|dd	
           |dv r|dz  S |dz  S # t          $ r?}t                              t!          d          |j        j                   t"          |d}~wt$          $ r#}d}t'          ||j        |j                  |d}~ww xY w)z%Compile LaTeX macros for math to DVI.zmath.texwr<   r=   Ntectonicz--interaction=nonstopmodeTascii)capture_outputcwdcheckr>   >   xelatexrq   zmath.xdvzmath.dvizYLaTeX command %r cannot be run (needed for math display), check the imgmath_latex settingzlatex exited with error)rm   r@   rL   ospathbasenamerP   imgmath_latexappendextendimgmath_latex_args
subprocessrunOSErrorloggerwarningr   r4   r   r   r!   r#   )	rn   rg   tempdirr8   rE   imgmath_latex_namecommandexcr   s	            r+   compile_mathr      s   W%%G#H	hg	.	.	. !	               ))'.*FGG
 ~+,GZ''2333NN7>4555NN:ADgTG	
 	
 	
 	
 !888Z''Z'' # # #<  N(	
 	
 	
 s" A A A'3
CJ77S@As;   A		AA!D <D 
E5:EE5E00E5r   	list[str]nametuple[str, str]c                @   	 t          j        | ddd          }|j        |j        fS # t          $ r=}t
                              t          d          || d         |           t          |d}~wt          $ r&}| d}t          ||j        |j                  |d}~ww xY w)z*Convert DVI file to specific image format.Trr   )rs   ru   r>   zS%s command %r cannot be run (needed for math display), check the imgmath_%s settingr   Nz exited with error)r~   r   r#   r!   r   r   r   r   r4   r   r   )r   r   retr   r   s        r+   convert_dvi_to_imager      s    AnWTPWXXXz3:%% 
# 
# 
#9  AJ	
 	
 	
 s" A A A)))3
CJ77S@As    %( 
B8A**B7!BBdvipathout_pathc                   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|S )	zConvert DVI file to PNG image.dvipng-oz-Ttightz-z9z--depthNr?   )rP   imgmath_dvipngr|   imgmath_dvipng_argsr]   r{   r   
splitlinesdepth_rerB   rC   rD   r   
r   rg   r   r   r   r#   r!   rI   rF   rG   s
             r+   convert_dvi_to_pngr      s    D~,dHdGUSGNN7>5666~) "y!!!NN7)'488NFFE~) %%'' 	 	DnnT**G GMM!,,--%000
 Lr,   c                   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|S )zConvert DVI file to SVG image.dvisvgmr   Nr?   d   gzGR@)rP   imgmath_dvisvgmr|   imgmath_dvisvgm_argsr{   r   r]   r   depthsvg_rerB   r[   floatrD   rM   r   s
             r+   convert_dvi_to_svgr      s    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
 Lr,   r)   r   "tuple[_StrPath | None, int | None]c                   | j         j        j                                        }|t          vrd}t          |          t          ||| j         j        | j         j                  }t          |	                                d          
                                 d| }| j         j        | j         j        z  dz  |z  }|j                            dd           |                                r/|dk    rt!          |          }n|d	k    rt#          |          }||fS t%          | j         d
          }t%          | j         d          }	|s|	rdS 	 t'          || j                   }
n# t(          $ r d| j         _        Y dS w xY w	 |dk    rt-          |
| j         |          }n|d	k    rt/          |
| j         |          }n# t(          $ r d| j         _        Y dS w xY w||fS )a{  Render the LaTeX math expression *math* using latex and dvipng or
    dvisvgm.

    Return the image absolute filename 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.
    2imgmath_image_format must be either "png" or "svg"F)usedforsecurity.rO   T)parentsexist_okr6   r7   _imgmath_warned_latex _imgmath_warned_image_translatorr-   )rg   rP   imgmath_image_formatlowerSUPPORT_FORMATr   rf   rQ   r   encode	hexdigestoutdirimagedirparentmkdiris_filer   rH   rj   r   r4   r   r   r   r   )r)   rO   rN   unsupported_format_msgrn   r8   generated_pathrI   latex_failedtrans_failedr   s              r+   render_mathr      s    " <&;AACCL>))!U1222 dDL/1E E
 666@@BBSS\SS  \(4<+@@6IHTNt<<< %5  ">22EEU""">22Eu$$ 4<)@AAL4<)KLLL | zudl33   -1*zz
5  &wnMMEEU""&wnMME   8<5zz 5  s$   E# #E=<E=9F; ;GGr   c                   t          |d          5 }|                                }d d d            n# 1 swxY w Y   t          j        |                              d          }| dk    rd| S | dk    rd| S d}t          |          )	Nrbr<   r=   r6   zdata:image/png;base64,r7   zdata:image/svg+xml;base64,r   )r@   readbase64	b64encodedecoder   )rN   r   rE   contentencodedr   s         r+   render_maths_to_base64r   .  s    	nd	#	# q&&((              w''...@@Gu1111u5G555Q
-
.
..s   266appr   r   	Exceptionc                   |rd S t          | j        d          rOt          j        t                    5  t          j        | j        j                   d d d            n# 1 swxY w Y   | j        j        j	        rbt          j        t                    5  t          j        | j        j
        | j        j        z  dz             d d d            d S # 1 swxY w Y   d S d S )Nri   rO   )rj   rg   
contextlibsuppressr   shutilrmtreeri   rP   imgmath_embedr   r   )r   r   s     r+   clean_up_filesr   :  sm   
 s{.// 8 ++ 	8 	8M#+6777	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 {' N  ++ 	N 	NM#+,s{/CCfLMMM	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	NN Ns#   AA"%A"/CCCnoder   c                    | j         j        j        r=d|                     |                                                                           dS dS )Nz alt=""rS   )rg   rP   imgmath_add_tooltipsr   astextstrip)r)   r   s     r+   get_tooltipr   I  sH    |/ >=DKKMM2288::====2r,   
nodes.mathc           	        	 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|V| j                            d|                     |                                                                           d           n| j        j        j        r4| j        j        j                                        }t)          ||          }nNt*          j                            |          }	t1          | j        j        d|	          }
|
                                }|d	| d
dnd}| j                            d| dt7          | |           | d           t          j
        )N$WARNING   typelevelbackrefssourcezdisplay latex %r: %s<span class="math"></span>rO   z style="vertical-align: dzpx"rS   z<img class="math" src="r   z/>)r   r   r   r    r	   system_message	walkaboutr   r   r   SkipNodebodyr{   r   r   rg   rP   r   r   r   r   rw   rx   ry   r   imgpathas_posixr   )r)   r   rendered_pathrI   r   r   smrN   img_srcbnamerelative_pathaligns               r+   html_visit_mathr   O  s   	&*4t{{}}1Ds1JKKuu & & &#hh!iq2dkkmm
 
 
 	Tr0114;;==#FFFn#%& 	M$++dkkmm"<"<"B"B"D"DMMM	
 	
 	
 	
 <, 	/<.CIIKKL,\=IIGGG$$]33E !5vuEEM#,,..G<A<M8E688888SU	RgRRD$0G0GRRRR	
 	
 	
 .s   +. 
CBCCnodes.math_blockc           	        |                     d|                     d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           |V| j                            d|                     |                                                                           d           n| j        j        j        r4| j        j        j                                        }	t5          |	|          }
nNt6          j                            |          }t=          | j        j        d	|          }|                                 }
| j                            d|
 dtC          | |           d           t          j        )Nzno-wrapnowrapFr   r   r   zinline latex %r: %sdivrO   )CLASSz<p>numberz<span class="eqno">(%s)zLink to this equationr   r   z</span></p>
</div>z
<img src="r   z/></p>
</div>)"getr   r   r   r   r    r	   r   r   r   r   r   r   r   r{   starttagr   add_permalink_refr   r   r   rg   rP   r   r   r   r   rw   rx   ry   r   r   r   r   )r)   r   rn   r   rI   r   r   r   r   rN   r   r   r   s                r+   html_visit_displaymathr   o  s   xx	488He4455 = e<<	&*477uu & & &#hh!iq2dkkmm
 
 
 	Tr/00$++--EEEn#%& 	IT]]4f]==>>>IUH~ $)$55	2V;<<<tQ'>%?%?@@@	###	Y$++dkkmm"<"<"B"B"D"DYYY	
 	
 	
 	
 <, 	/<.CIIKKL,\=IIGGG$$]33E !5vuEEM#,,..G	WgWWD$0G0GWWWXXX
.s   $A8 8
DBDDr   c           	        |                      dt          d ft          d f           |                     dddt	          t
          h                     |                     dddt	          t
          h                     |                     d	d
dt	          t
          h                     |                     dddt	          t
          h                     |                     dddt	          t          h                     |                     dg ddt	          t          h                     |                     ddgdt	          t          t          h                     |                     dg dt	          t          t          h                     |                     dddt	          t
          h                     |                     dddt	          t          h                     |                     dddt	          t          h                     |                     dddt	          t          h                     | 
                    dt                     t          j        ddS )Nr   )inline_renderersblock_renderersr   r6   html)typesr   r   r   r   rz   rn   r]   Fr   )z-gammaz1.5z-D110z-bgTransparentr   z
--no-fontsr}   r\   rS   r   TrZ      r   zbuild-finished)versionparallel_read_safe)add_html_math_rendererr   r   add_config_value	frozensetr    boollisttuplerC   connectr   sphinx__display_version__)r   s    r+   setupr
    sr   )40/6     /iQTPUFVFVWWW)8V9cUCSCSTTT*IvYPSuEUEUVVV'6C5AQAQRRR.vYPTvEVEVWWW<<<	     iu>V>V     b&	4-0H0H     12vYPSuEUEUVVV/vYPTvEVEVWWW,b&	3%@P@PQQQ%y$?P?PQQQKK .111-"  r,   )r8   r9   r$   r:   )r8   r   rI   rC   r$   r%   )
rN   r    rO   r    rP   r   rQ   r   r$   r    )rg   r   r$   r   )rn   r    rg   r   r$   r   )r   r   r   r    r$   r   )r   r   rg   r   r   r   r$   r:   )r)   r   rO   r    r$   r   )rN   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   )Or5   
__future__r   __all__r   r   rw   os.pathrer   r~   rk   hashlibr   pathlibr   r   typingr   docutilsr	   r  r
   sphinx.errorsr   sphinx.localer   r   sphinx.utilr   sphinx.util.mathr   r   sphinx.util.pngr   r   sphinx.util.templater   docutils.nodesr   sphinx.applicationr   sphinx.buildersr   sphinx.configr   sphinx.util._pathlibr   sphinx.util.typingr   sphinx.writers.html5r   	getLoggerr.   r   joinpathr^   r   r4   r   compiler   r   rA   rH   rM   rf   rm   r   r   r   r   r   r   r   r   r   r   r
  r   r,   r+   <module>r#     s   0 0 " " " " " "
      				  				                   ) ) ) ) ) )                          % % % % % %               G G G G G G G G ; ; ; ; ; ; ; ; . . . . . . 5&&&&&&))))))''''''$$$$$$------444444444444		8	$	$%%k9==
 
 
 
 
; 
 
 
) ) ) ) )+ ) ) )  2:.//bj,--RZ 9:: 	 	 	 	/ / / /W W W W>
$ 
$ 
$ 
$&A &A &A &ARA A A A*   .   *=! =! =! =!@	/ 	/ 	/ 	/N N N N      @% % % %P           r,   