
    7 f=<                       d Z ddlm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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  ddl!m"Z"m#Z# ddl$m%Z% er"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/  ej0        e1          Z2 ej3        edd          Z4dZ5 G d de          Z6 G d de          Z7dZ8 ej9        d          Z: ej9        d          Z; ej9        d           Z<dTd%Z=dUd)Z>	 dVdWd1Z?dXd4Z@dYd6ZAdZd;ZBd[d>ZCd[d?ZDd\dCZEd]dEZFd^dJZGd_dMZHd`dOZIdadQZJdbdSZKdS )cz*Render math in HTML via dvipng or dvisvgm.    )annotationsN)sha1)path)CalledProcessError)TYPE_CHECKINGAny)nodes)package_dir)SphinxError)___)logging)get_node_equation_numberwrap_displaymath)	ensuredir)read_png_depthwrite_png_depth)LaTeXRenderer)Element)Sphinx)Builder)Config)HTML5Translator	templatesimgmath c                  (     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       H/var/www/equiseq/venv/lib/python3.11/site-packages/sphinx/ext/imgmath.pyr(   zMathExtError.__init__0   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+) -->filenamer    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 )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_depthrG   E   s     
h	)	)	) Q 	 	D$**400 	)w}}Q''((                         s   AA0"A00A47A4depthrB   r%   c                    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   rH   rD   s      r+   write_svg_depthrL   R   s     
hg	.	.	. /!	%-.../ / / / / / / / / / / / / / / / / /s   9= = image_formatmathconfigr   confdirstr | os.PathLike[str]c                   |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   rM   z,dvips,tightpage)fontsizebaselineskippreamble	tightpagerO   zpreview.tex_tztemplate.tex_t)imgmath_font_sizerB   roundimgmath_latex_preambleimgmath_use_previewtemplates_pathr   joinexistsr   render)rN   rO   rP   rQ   	variablestemplate_nametemplate_dirtemplates           r+   generate_latex_macrord   Y   s     ,E&":S"@AABB1 (500RR6H I ! )'(- ? ?9WlMBB;x   	? ??))(I>>>>>	? ((//yIIIr,   builderr   c                `    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mkdtemprg   )re   s    r+   ensure_tempdirrk   u   s1     7.// 6#+#3#5#5 ##r,   latexc                6   t          |          }t          j        |d          }t          |dd          5 }|                    |            ddd           n# 1 swxY w Y   t          j        |j        j                  }|j        j        g}|dvr|                    d           |	                    |j        j
                   |                    d           	 t          j        |d|dd	
           |dv rt          j        |d          S t          j        |d          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=   )xelatexro   zmath.xdvzmath.dvizYLaTeX command %r cannot be run (needed for math display), check the imgmath_latex settingzlatex exited with error)rk   r   r]   r?   rK   basenamerP   imgmath_latexappendextendimgmath_latex_args
subprocessrunOSErrorloggerwarningr   r4   r   r   r!   r#   )	rl   re   tempdirr8   rD   imgmath_latex_namecommandexcr   s	            r+   compile_mathr      s   W%%Gy*--H	hg	.	.	. !	               w~'CDD
 ~+,G--2333NN7>4555NN:Awt '	) 	) 	) 	)!8889Wj1119Wj111 # # #r F G G~3	5 	5 	5 s" A A A'3
CJ77S@As;   AA A1D$ D$ $
F.:E((F5FF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$}t          d|z  |j        |j                  |d}~ww xY w)z*Convert DVI file to specific image format.Trp   )rq   rs   r=   zS%s command %r cannot be run (needed for math display), check the imgmath_%s settingr   Nz%s exited with error)rz   r{   r#   r!   r|   r}   r~   r   r4   r   r   )r   r   retr   s       r+   convert_dvi_to_imager      s    	[nWTPWXXXz3:%% # # #r C D DWQZ	/ 	/ 	/ s" [ [ [1D8#*cjQQWZZ[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_dvipngrx   imgmath_dvipng_argsr[   rw   r   
splitlinesdepth_rerA   rB   rC   r   
r   re   r   r   r   r#   r!   rH   rE   rF   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_dvisvgmrx   imgmath_dvisvgm_argsrw   r   r[   r   depthsvg_rerA   rY   floatrC   rL   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[str | None, int | None]c                   | j         j        j                                        }|t          vrd}t          |          t          ||| j         j        | j         j                  }t          |	                                d          
                                 d| }t          j        | j         j        | j         j        d|          }t          t          j        |                     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||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   r6   r7   _imgmath_warned_latex _imgmath_warned_image_translatorr-   T)re   rP   imgmath_image_formatlowerSUPPORT_FORMATr   rd   rQ   r   encode	hexdigestr   r]   outdirimagedirr   dirnameisfiler   rG   rh   r   r4   r   r   r   r   )	r)   rO   rN   unsupported_format_msgrl   r8   generated_pathrH   r   s	            r+   render_mathr      s   " <&;AACCL>))!U1222 !%!%!4!%!57 7E
 u||~~u===GGIIZZLZZHYt|2DL4I6S[\\Ndl>**+++{>"" %5  ">22EEU""">22Eu$$ t|455 t|?@@zudl33   -1*zz
5  &wnMMEEU""&wnMME   8<5zz 5  s$   E0 0F
	F
9G G"!G"r   c                   t          |d          5 }t          j        |                                                              d          }d d d            n# 1 swxY w Y   | 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?   base64	b64encodereaddecoder   )rN   r   rD   encodedr   s        r+   render_maths_to_base64r     s    	nd	#	# Fq"16688,,33W3EEF F F F F F F F F F F F F F Fu1111u5G555Q
-
.
..s   ;AAA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	        rpt          j        t                    5  t          j        t          j        | j        j        | j        j        d                     d d d            d S # 1 swxY w Y   d S d S )Nrg   rO   )rh   re   
contextlibsuppressr   shutilrmtreerg   rP   imgmath_embedr   r]   r   r   )r   r   s     r+   clean_up_filesr   )  sq   
 s{.// 8 ++ 	8 	8M#+6777	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 {' W  ++ 	W 	WM$)CK$68LfUUVVV	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	WW Ws#   AA"%A"=CC!$C!noder   c                    | j         j        j        r<d|                     |                                                                          z  S dS )Nz	 alt="%s"rM   )re   rP   imgmath_add_tooltipsr   astextstrip)r)   r   s     r+   get_tooltipr   8  sC    |/ @T[[77==????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|U| j                            d|                     |                                                                          z             n| j        j        j        r4| j        j        j                                        }t)          ||          }nTt+          j        |          }	t+          j        | j        j        d|	          }
|
                    t*          j        d          }d	| d
t7          | |          z   }||d| ddz  }| j                            |dz              t          j
        )N$WARNING   typelevelbackrefssourcezdisplay latex %r: %sz<span class="math">%s</span>rO   /z<img class="math" src=""z style="vertical-align: dzpx"z/>)r   r   r   r    r	   system_message	walkaboutr}   r~   r   SkipNodebodyrw   r   r   re   rP   r   r   r   r   r   ru   r]   imgpathreplacesepr   )r)   r   rendered_pathrH   r   r   smrN   img_srcbnamerelative_pathcs               r+   html_visit_mathr   >  s   &*4t{{}}1Ds1JKKuu & & &#hh!#IQ+-dkkmmE E E
Tr0114;;==#FFFn#%& 	7T[[]]3399;;< 	= 	= 	= 	= <, 	;<.CIIKKL,\=IIGGM-00E Idl&:FEJJM#++DHc::G0g000;tT3J3JJ9UF99999A	T"""
.s   +. 
CBCCnodes.math_blockc           	        |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             n| j        j        j        r4| j        j        j                                        }	t3          |	|          }
nTt5          j        |          }t5          j        | j        j        d|          }|                    t4          j        d          }
| j                            d|
 dtA          | |          z   dz              t
          j        )NnowrapFr   r   r   zinline latex %r: %sdivrO   )CLASSz<p>numberz<span class="eqno">(%s)zLink to this equationz</span>z'<span class="math">%s</span></p>
</div>r   z
<img src="r   z/></p>
</div>)!r   r   r   r   r    r	   r   r   r}   r~   r   r   r   rw   starttagr   add_permalink_refr   r   r   re   rP   r   r   r   r   r   ru   r]   r   r   r   r   )r)   r   rl   r   rH   r   r   r   r   rN   r   r   r   s                r+   html_visit_displaymathr   \  s   H~ = e<<&*477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T[[]]3399;;< 	= 	= 	= 	= <, 	;<.CIIKKL,\=IIGGM-00E Idl&:FEJJM#++DHc::G	0g000;tT3J3JJ)* 	+ 	+ 	+
.s   A 
C< BC77C<dict[str, Any]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ddt          g           |                     dt                     t          j        ddS )Nr   r   r6   htmlr   r   r   r   rv   rl   r[   Fr   )z-gammaz1.5z-D110z-bgTransparentr   z
--no-fontsry   rZ   rM   r   TrX      r   zbuild-finished)versionparallel_read_safe)	add_html_math_rendererr   r   add_config_valueboolconnectr   sphinx__display_version__)r   s    r+   setupr     s   y /6 6=? ? ? /???)8V<<<*Iv>>>'6:::.v>>>.MMM! ! ! /,HHH-r6:::12v>>>/v>>>,b&999%$@@@KK .1111NNNr,   )r8   r    r$   r9   )r8   r    rH   rB   r$   r%   )rM   )
rN   r    rO   r    rP   r   rQ   rR   r$   r    )re   r   r$   r    )rl   r    re   r   r$   r    )r   r   r   r    r$   r   )r   r    re   r   r   r    r$   r9   )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   )Lr5   
__future__r   r   r   rer   rz   ri   hashlibr   osr   r   typingr   r   docutilsr	   r   r
   sphinx.errorsr   sphinx.localer   r   sphinx.utilr   sphinx.util.mathr   r   sphinx.util.osutilr   sphinx.util.pngr   r   sphinx.util.templater   docutils.nodesr   sphinx.applicationr   sphinx.buildersr   sphinx.configr   sphinx.writers.htmlr   	getLoggerr.   r}   r]   r\   __all__r   r4   r   compiler   r   r@   rG   rL   rd   rk   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 ( ( ( ( ( ( ; ; ; ; ; ; ; ; . . . . . . 4III&&&&&&))))))''''''$$$$$$333333		8	$	$;Y??

 
 
 
 
; 
 
 
) ) ) ) )+ ) ) )  2:.//bj,--RZ 9:: 
 
 
 
/ / / / <>J J J J J8
$ 
$ 
$ 
$!A !A !A !AH[ [ [ [   .   *;! ;! ;! ;!|/ / / /W W W W      <$ $ $ $NO O O O O Or,   