
    Fe=<                       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j`                  e1      Z2 ejf                  edd      Z4dZ5 G d de      Z6 G d de      Z7dZ8 ejr                  d      Z: ejr                  d      Z; ejr                  d       Z<d0d!Z=d1d"Z>	 d2	 	 	 	 	 	 	 d3d#Z?d4d$Z@d5d%ZAd6d&ZBd7d'ZCd7d(ZD	 	 	 	 	 	 d8d)ZEd9d*ZFd:d+ZGd;d,ZHd<d-ZId=d.ZJd>d/ZKy)?z*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                  6     e Zd ZdZ	 d	 	 	 	 	 	 	 d fdZ xZS )MathExtErrorzMath extension errorc                L    |r|d|z   z  }|r|d|z   z  }t         |   |       y )Nz

[stderr]
z

[stdout]
)super__init__)selfmsgstderrstdout	__class__s       4/usr/lib/python3/dist-packages/sphinx/ext/imgmath.pyr!   zMathExtError.__init__0   s6     >F**C>F**C    NN)r#   strr$   
str | Noner%   r+   returnNone)__name__
__module____qualname__categoryr!   __classcell__)r&   s   @r'   r   r   -   s9    %H IM *;E	 r(   r   c                      e Zd ZdZy)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+) -->c                    t        | d      5 }|D ]  } t        j                        }|r#t        |j	                  d            cddd       S 	 ddd       y# 1 sw Y   yxY w)z9Read the depth from comment at last line of SVG file
    utf-8encoding   N)opendepthsvgcomment_rematchintgroup)filenameflinematcheds       r'   read_svg_depthrF   E   sl     
h	) Q 	D	 %**40w}}Q'(    s   8AAA#c                r    t        | dd      5 }|j                  d|z         ddd       y# 1 sw Y   yxY w)z<Write the depth to SVG file as a comment at end of file
    ar9   r:   z
<!-- DEPTH=%s -->N)r=   write)rB   depthrC   s      r'   write_svg_depthrK   R   s9     
hg	. /!	%-./ / /s   -6c                   |j                   t        t        |j                   dz              |j                  | dk(  rdnd|d}|j                  rd}nd}|j
                  D ]J  }t        j                  |||      }t        j                  |      s0t               j                  ||      c S  t        t
              j                  ||      S )zGenerate LaTeX macro.g333333?r6    z,dvips,tightpage)fontsizebaselineskippreamble	tightpagemathzpreview.tex_tztemplate.tex_t)imgmath_font_sizer@   roundimgmath_latex_preambleimgmath_use_previewtemplates_pathr   joinexistsr   render)image_formatrR   configconfdir	variablestemplate_nametemplate_dirtemplates           r'   generate_latex_macrorb   Y   s     ,,E&":":S"@AB11 (50R6HI !!'(-- ?99WlMB;;x  ?))(I>>?
 (//yIIr(   c                d    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mkdtemprd   )builders    r'   ensure_tempdirri   u   s,     7./#+#3#3#5 ###r(   c                N   t        |      }t        j                  |d      }t        |dd      5 }|j	                  |        ddd       t        j
                  |j                  j                        }|j                  j                  g}|dvr|j                  d       |j                  |j                  j                         |j                  d       	 t        j                  |d|dd	
       |dv rt        j                  |d      S t        j                  |d      S # 1 sw Y   xY w# t        $ r?}t        j                  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wr9   r:   N)tectonicz--interaction=nonstopmodeTascii)capture_outputcwdcheckr;   )xelatexrl   zmath.xdvzmath.dvizYLaTeX command %r cannot be run (needed for math display), check the imgmath_latex settingzlatex exited with error)ri   r   rX   r=   rI   basenamer\   imgmath_latexappendextendimgmath_latex_args
subprocessrunOSErrorloggerwarningr   r4   r   r   r$   r%   )	latexrh   tempdirrB   rC   imgmath_latex_namecommandexcr#   s	            r'   compile_mathr      sc   W%Gyy*-H	hg	. !	 w~~'C'CD
 ~~++,G-23NN7>>445NN:Awt '	)!8899Wj1199Wj11+ ,  #r F G~~33	5 s" A'3

CJJ7S@As5   D 3D, 
D,  D),	F$5:E//F$;$FF$c                @   	 t        j                  | ddd      }|j                  |j                  fS # t        $ r0}t
        j                  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   )rn   rp   r;   zS%s command %r cannot be run (needed for math display), check the imgmath_%s settingr   Nz%s exited with error)rw   rx   r%   r$   ry   rz   r{   r   r4   r   r   )r   nameretr   s       r'   convert_dvi_to_imager      s    	[nnWTPWXzz3::%% #r C DWQZ	/ s" [1D8#**cjjQWZZ[s    03 	B+A''B3%BBc                   d}|j                   j                  d|dddg}|j                  |j                   j                         |j                   j                  r|j                  d       |j                  |        t        ||      \  }}d}|j                   j                  rU|j                         D ]B  }t        j                  |      }	|	st        |	j                  d            }t        ||        |S  |S )	zConvert DVI file to PNG image.dvipng-oz-Ttightz-z9z--depthNr<   )r\   imgmath_dvipngru   imgmath_dvipng_argsrV   rt   r   
splitlinesdepth_rer?   r@   rA   r   
dvipathrh   out_pathr   r   r%   r$   rJ   rD   rE   s
             r'   convert_dvi_to_pngr      s    D~~,,dHdGUSGNN7>>556~~))y!NN7)'48NFFE~~))%%' 	DnnT*GGMM!,-%0L	 Lr(   c                   d}|j                   j                  d|g}|j                  |j                   j                         |j	                  |        t        ||      \  }}d}|j                   j                  rd|j                         D ]Q  }t        j                  |      }	|	st        t        |	j                  d            dz  dz        }t        ||        |S  |S )zConvert DVI file to SVG image.dvisvgmr   Nr<   d   gzGR@)r\   imgmath_dvisvgmru   imgmath_dvisvgm_argsrt   r   rV   r   depthsvg_rer?   rT   floatrA   rK   r   s
             r'   convert_dvi_to_svgr      s    D~~--tX>GNN7>>667NN7)'48NFFE~~))%%' 	D!''-GeGMM!$45;eCD%0L	 Lr(   c                
   | j                   j                  j                  j                         }|t        vrd}t        |      t        ||| j                   j                  | j                   j                        }t        |j                         d      j                          d| }t        j                  | j                   j                  | j                   j                  d|      }t        t        j                   |             t        j"                  |      r(|dk(  rt%        |      }||fS |dk(  rt'        |      }|fS t)        | j                   d      st)        | j                   d	      ry
	 t+        || j                         }	 |dk(  rt1        || j                   |      }n|dk(  rt3        || j                   |      }|fS # t,        $ r d| j                   _        Y y
w xY w# t,        $ r d| j                   _        Y y
w xY w)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.rR   r6   r7   _imgmath_warned_latex _imgmath_warned_image_translatorr)   T)rh   r\   imgmath_image_formatlowerSUPPORT_FORMATr   rb   r]   r   encode	hexdigestr   rX   outdirimagedirr   dirnameisfiler   rF   re   r   r4   r   r   r   r   )	r"   rR   r[   unsupported_format_msgr|   rB   generated_pathrJ   r   s	            r'   render_mathr      s   " <<&&;;AACL>)!U122 !%!%!4!4!%!5!57E
 u||~u=GGIJ!L>ZHYYt||22DLL4I4I6S[\Ndll>*+{{>"5 ">2E u$$ U"">2Eu$$ t||45t||?@udll35 &wnMEU"&wnME
 5    -1*  8<5s$   1G 9G% G"!G"%HHc                    t        |d      5 }t        j                  |j                               j	                  d      }d d d        | dk(  rd S | dk(  rd S d}t        |      # 1 sw Y   *xY w)	Nrbr9   r:   r6   zdata:image/png;base64,r7   zdata:image/svg+xml;base64,r   )r=   base64	b64encodereaddecoder   )r[   r   rC   encodedr   s        r'   render_maths_to_base64r     s    	nd	# Fq""1668,33W3EFu'y11u+G955Q
-
..F Fs   4A**A3c                &   |ry t        | j                  d      rKt        j                  t              5  t        j                  | j                  j                         d d d        | j                  j                  j                  rut        j                  t              5  t        j                  t        j                  | j                  j                  | j                  j                  d             d d d        y y # 1 sw Y   xY w# 1 sw Y   y xY w)Nrd   rR   )re   rh   
contextlibsuppress	Exceptionshutilrmtreerd   r\   imgmath_embedr   rX   r   r   )appr   s     r'   clean_up_filesr   )  s    
s{{./  + 	8MM#++667	8 {{''   + 	WMM$))CKK$6$68L8LfUV	W 	W (	8 	8	W 	Ws   *C;AD;DDc                    | j                   j                  j                  r0d| j                  |j	                               j                         z  S y)Nz	 alt="%s"rM   )rh   r\   imgmath_add_tooltipsr   astextstrip)r"   nodes     r'   get_tooltipr   8  s;    ||//T[[7==???r(   c           	     :   	 t        | d|j                         z   dz         \  }}|Y| j                  j                  d| j                  |j                               j                         z         t        j                  | j                  j                   j"                  r;| j                  j                   j$                  j'                         }t)        ||      }n`t+        j,                  |      }	t+        j.                  | j                  j0                  d|	      }
|
j3                  t*        j4                  d      }d	| d
t7        | |      z   }||d| ddz  }| j                  j                  |dz          t        j                  # t        $ r}t        |      }t	        j
                  |ddg |j                               }|j                  |        t        j                  t        d      |j                         |       t        j                  |d }~ww xY w)N$WARNING   typelevelbackrefssourcezdisplay latex %r: %sz<span class="math">%s</span>rR   /z<img class="math" src=""z style="vertical-align: dzpx"z/>)r   r   r   r*   r	   system_message	walkaboutrz   r{   r   SkipNodebodyrt   r   r   rh   r\   r   r   r   r   r   rr   rX   imgpathreplacesepr   )r"   r   rendered_pathrJ   r   r#   smr[   img_srcbnamerelative_pathcs               r'   html_visit_mathr   >  s   &*4t{{}1Ds1JKu 		7T[[]399;< 	= .. <<,,<<..CCIIKL,\=IGMM-0E IIdll&:&:FEJM#++DHHc:G%gYa0;tT3JJ+UF1:S99A		T"
..1  &#h!!#IQ+-dkkmE
Tr014;;=#Fnn#%&s   #F	 		HBHHc           	        |d   r|j                         }nt        |j                         d d      }	 t        | |      \  }}| j                  j                  | j                  |dd	             | j                  j                  d
       |d   r`t        | |      }| j                  j                  d|z         | j!                  |t#        d             | j                  j                  d       |Y| j                  j                  d| j%                  |j                               j'                         z         t
        j                  | j(                  j*                  j,                  r;| j(                  j*                  j.                  j1                         }	t3        |	|      }
n`t5        j6                  |      }t5        j8                  | j(                  j:                  d|      }|j=                  t4        j>                  d      }
| j                  j                  d|
 dtA        | |      z   dz          t
        j                  # t        $ r}t	        |      }t        j                  |ddg |j                               }|j                  |        t        j                  t        d      |j                         |       t
        j                  |d }~ww xY w)NnowrapFr   r   r   zinline latex %r: %sdivrR   )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   rz   r{   r   r   r   rt   starttagr   add_permalink_refr   r   r   rh   r\   r   r   r   r   r   rr   rX   r   r   r   r   )r"   r   r|   r   rJ   r   r#   r   r   r[   r   r   r   s                r'   html_visit_displaymathr   \  s8   H~ e<&*47u 	IIT]]4f]=>IIUH~)$5		2V;<tQ'>%?@		#		CT[[]399;< 	= .. <<,,<<..CCIIKL,\=IGMM-0E IIdll&:&:FEJM#++DHHc:G		:gYa0;tT3JJ)* 	+
..;  &#h!!#IQ+-dkkmE
Tr/0$++-Enn#%&s   I 	KBKKc                l   | j                  dt        d ft        d f       | j                  ddd       | j                  ddd       | j                  ddd       | j                  d	d
d       | j                  ddd       | j                  dg dd       | j                  ddgd       | j                  dg d       | j                  ddd       | j                  ddd       | j                  ddd       | j                  dddt        g       | j                  dt               t        j                  ddS )Nr   r   r6   htmlr   r   r   r   rs   r|   rV   Fr   )z-gammaz1.5z-D110z-bgTransparentr   z
--no-fontsrv   rU   rM   r   TrS      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     s2   y /6 6=? /?)8V<*Iv>'6:.v>.M! /,H-r6:12v>/v>,b&9%$@KK .111NNr(   )rB   r*   r,   
int | None)rB   r*   rJ   r@   r,   r-   )rM   )
r[   r*   rR   r*   r\   r   r]   zstr | os.PathLike[str]r,   r*   )rh   r   r,   r*   )r|   r*   rh   r   r,   r*   )r   z	list[str]r   r*   r,   ztuple[str, str])r   r*   rh   r   r   r*   r,   r   )r"   r   rR   r*   r,   ztuple[str | None, int | None])r[   r*   r   r*   r,   r*   )r   r   r   r   r,   r-   )r"   r   r   r   r,   r*   )r"   r   r   z
nodes.mathr,   r-   )r"   r   r   znodes.math_blockr,   r-   )r   r   r,   zdict[str, Any])Lr5   
__future__r   r   r   rer   rw   rf   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.   rz   rX   rW   __all__r   r4   r   compiler   r   r>   rF   rK   rb   ri   r   r   r   r   r   r   r   r   r   r   r   r   r(   r'   <module>r	     sv   0 "   	      ) %    %   G ( ; .&)'$3			8	$;Y?

; 
)+ )  2::./bjj,-RZZ 9: 
/ <>J"J!'J #9J CFJ8
$!AH[.*;!
;!
;! #;!|/W<$NOr(   