
    Riz;                       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  ejd                  e3      Z4 ejj                  dd      Z6 G d de      Z7 G d de      Z8dZ9 ejt                  d      Z; ejt                  d      Z< ejt                  d       Z=d/d!Z>d0d"Z?	 	 	 	 	 	 	 	 	 	 d1d#Z@d2d$ZAd3d%ZBd4d&ZCd4d'ZD	 	 	 	 	 	 	 	 d5d(ZEd6d)ZFd7d*ZGd8d+ZHd9d,ZId:d-ZJd;d.ZKy)<z*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)Config)_StrPath)ExtensionMetadata)HTML5Translator	templatesimgmathc                  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       J/home/agent/.friday_env/lib/python3.12/site-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.   r/   __doc__r   r'   r&   r3   r3   :   s    (r'   r3   )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)z4Read 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_depthrE   E   sj    	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)z7Write the depth to SVG file as a comment at end of filear8   r9   z
<!-- DEPTH=%s -->N)r<   write)rA   depthrB   s      r&   write_svg_depthrJ   Q   s7    	hg	. /!	%-./ / /s   -6c                ~   |j                   t        |j                   dz        |j                  | dk(  rdnd|d}|j                  rd}nd}|j                  D ]B  }dD ];  }||z  ||z   z  }|j                         st               j                  ||      c c S  D t        t        g      j                  |d	z   |      S )
zGenerate LaTeX macro.g333333?r5    z,dvips,tightpage)fontsizebaselineskippreamble	tightpagemathzpreview.texztemplate.tex).jinja_trR   )imgmath_font_sizeroundimgmath_latex_preambleimgmath_use_previewtemplates_pathexistsr   render)	image_formatrQ   configconfdir	variablestemplate_nametemplate_dirtemplate_suffixtemplates	            r&   generate_latex_macrorc   W   s     ,,f66<=11 (50R6HI !!%&-- C/ 	CO-1PQH $--h	BB	CC .)*11-(2JIVVr'   c                  t        t        j                  d            }|dz  }t        |dd      5 }|j	                  |        ddd       t
        j                  j                  |j                        }|j                  g}|dk7  r|j                  d	       |j                  |j                         |j                  d       	 t        j                  |d
|d
d       |dv r|dz  S |dz  S # 1 sw Y   xY w# t        $ r5}t        j!                  t#        d      |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-sphinx-imgmath)suffixzmath.texwr8   r9   Ntectonicz--interaction=nonstopmodeTascii)capture_outputcwdcheckr:   >   xelatexrg   zmath.xdvzmath.dvizYLaTeX command %r cannot be run (needed for math display), check the imgmath_latex settingzlatex exited with error)r   tempfilemkdtempr<   rH   ospathbasenameimgmath_latexappendextendimgmath_latex_args
subprocessrunOSErrorloggerwarningr   r3   r   r   r#   r$   )	latexr\   tempdirrA   rB   imgmath_latex_namecommandexcr"   s	            r&   compile_mathr   v   sV   8##+<=>G#H	hg	. !	 ))&*>*>?
 ##$GZ'23NN6,,-NN:ADgTG	
 !88Z''Z''- .  #<   	
 s" A'3

CJJ7S@As5   C."C: )C: .C7:	E(0D33E(?$E##E(c                D   	 t        j                  | ddd      }|j                  |j                  fS # t        $ r0}t
        j                  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.Trh   )ri   rk   r:   zS%s command %r cannot be run (needed for math display), check the imgmath_%s settingr   Nz exited with error)rv   rw   r$   r#   rx   ry   rz   r   r3   r   r   )r~   nameretr   r"   s        r&   convert_dvi_to_imager      s    AnnWTPWXzz3::%% 
#9 AJ	
 s" A()3

CJJ7S@As    03 	B+A''B3'BBc                  d}|j                   d|dddg}|j                  |j                         |j                  r|j	                  d       |j	                  |        t        ||      \  }}d}|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;   )imgmath_dvipngrt   imgmath_dvipng_argsrW   rs   r   
splitlinesdepth_rer>   r?   r@   r   )
dvipathout_pathr\   r   r~   r$   _stderrrI   rC   rD   s
             r&   convert_dvi_to_pngr      s    D$$dHdGUKGNN6--.!!y!NN7*7D9OFGE!!%%' 	DnnT*GGMM!,-%0L	 Lr'   c                  d}|j                   d|g}|j                  |j                         |j                  |        t	        ||      \  }}d}|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@)imgmath_dvisvgmrt   imgmath_dvisvgm_argsrs   r   rW   r   depthsvg_rer>   rU   floatr@   rJ   )
r   r   r\   r   r~   _stdoutr#   rI   rC   rD   s
             r&   convert_dvi_to_svgr      s    D%%tX6GNN6../NN7*7D9OGVE!!%%' 	D!''-GeGMM!$45;eCD%0L	 Lr'   c               f   |j                   j                         }|t        vrd}t        |      t	        |||| j
                  j                        }t        |j                         d      j                          d| }| j
                  j                  | j
                  j                  z  dz  |z  }|j                  j                  dd       |j                         r(|dk(  rt        |      }||fS |d	k(  rt!        |      }|fS t#        | j
                  d
      }	t#        | j
                  d      }
|	s|
ry	 t%        ||      }	 |dk(  rt+        |||      }n|d	k(  rt-        |||      }|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.rQ   T)parentsexist_okr5   r6   _imgmath_warned_latex _imgmath_warned_image_translatorr(   r\   )imgmath_image_formatlowerSUPPORT_FORMATr   rc   builderr]   r   encode	hexdigestoutdirimagedirparentmkdiris_filer   rE   hasattrr   r3   r   r   r   r   )r!   rQ   r\   r[   unsupported_format_msgr{   rA   generated_pathrI   latex_failedtrans_failedr   s               r&   render_mathr      s     ..446L>)!U122 tVT\\=Q=QRE 6@@B
C1\NS  \\((4<<+@+@@6IHTNt<5 ">2E u$$ U"">2Eu$$ 4<<)@AL4<<)KLL|uV45 &wvNEU"&wvNE
 5    -1*  8<5s$   :E3 'F 3FFF0/F0c                    t        |d      5 }|j                         }d d d        t        j                        j	                  d      }| dk(  rd| S | dk(  rd| S d}t        |      # 1 sw Y   OxY w)	Nrbr8   r9   r5   zdata:image/png;base64,r6   zdata:image/svg+xml;base64,r   )r<   readbase64	b64encodedecoder   )r[   r   rB   contentencodedr   s         r&   render_maths_to_base64r     s    	nd	# q&&(w'...@Gu'y11u+G955Q
-
.. s   A,,A5c                   |ry | j                   j                  rft        j                  t              5  t        j                  | j                  j                  | j                  j                  z  dz         d d d        y y # 1 sw Y   y xY w)NrQ   )
r\   imgmath_embed
contextlibsuppress	Exceptionshutilrmtreer   r   r   )appr   s     r&   clean_up_filesr   )  ss    

zz   + 	NMM#++,,s{{/C/CCfLM	N 	N  	N 	Ns   ABB
c               ~    |j                   r1d| j                  |j                               j                          dS y)Nz alt=""rL   )imgmath_add_tooltipsr   astextstrip)r!   noder\   s      r&   get_tooltipr   4  s6    ""DKKM288:;1==r'   c           	        | j                   j                  }	 t        | d|j                         z   dz   |      \  }}|Z| j                  j                  d| j                  |j                               j!                          d       t        j                  |j"                  r'|j$                  j'                         }t)        ||      }	nPt*        j,                  j/                  |      }
t1        | j                   j2                  d	|
      }|j5                         }	|d
| ddnd}t7        | ||      }| j                  j                  d|	 d| | d       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$r   WARNING   typelevelbackrefssourcezdisplay latex %r: %s<span class="math"></span>rQ   z style="vertical-align: dzpx"rL   z<img class="math" src="r   z/>)r   r\   r   r   r   r)   r	   system_message	walkaboutry   rz   r   SkipNodebodyrs   r   r   r   r   r   r   ro   rp   rq   r   imgpathas_posixr   )r!   r   r\   rendered_pathrI   r   r"   smr[   img_srcbnamerelative_pathaligntooltips                 r&   html_visit_mathr   :  s   \\  F&*#%+F 
u 		!$++dkkm"<"B"B"D!EWM	
 .. !66<<>L,\=IGGG$$]3E !5!5vuEM#,,.G<A<M*E6!*C8SUdD8		27)1WIeWBOP
..3  &#h!!iq2dkkm
 	Tr014;;=#Fnn#%&s   %E* *	G;3BG66G;c           	        | j                   j                  }|j                  d|j                  d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       |Z| j                  j!                  d| j+                  |j                               j-                          d       t        j                  |j.                  r'|j0                  j3                         }
t5        |
|      }nPt6        j8                  j;                  |      }t=        | j                   j>                  d
|      }|jA                         }tC        | ||      }| j                  j!                  d| d| d       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)Nzno-wrapnowrapFr   r   r   r   zinline latex %r: %sdivrQ   )CLASSz<p>numberz<span class="eqno">(%s)zLink to this equationr   r   z</span></p>
</div>z
<img src="r   z/></p>
</div>)"r   r\   getr   r   r   r   r)   r	   r   r   ry   rz   r   r   r   rs   starttagr   add_permalink_refr   r   r   r   r   r   r   ro   rp   rq   r   r   r   r   )r!   r   r\   r{   r   _depthr   r"   r   r   r[   r   r   r   r   s                  r&   html_visit_displaymathr   \  sC   \\  Fxx	488He45 e<	& +D% Gv 	IIT]]4f]=>IIUH~)$5		2V;<tQ'>%?@		#		!$++dkkm"<"B"B"D!EEXY	
 .. !66<<>L,\=IGGG$$]3E !5!5vuEM#,,.GdD8		:gYayGH
..?  &#h!!iq2dkkm
 	Tr/0$++-Enn#%&s   &I 	KBKKc           	        | j                  dt        d ft        d f       | j                  dddt	        t
        h             | j                  dddt	        t
        h             | j                  d	d
dt	        t
        h             | j                  dddt	        t
        h             | j                  dddt	        t        h             | j                  dg ddt	        t        h             | j                  ddgdt	        t        t        h             | j                  dg dt	        t        t        h             | j                  dddt	        t
        h             | j                  dddt	        t        h             | j                  dddt	        t        h             | j                  dddt	        t        h             | j                  dt               t        j                  ddS )Nr   )inline_renderersblock_renderersr   r5   html)typesr   r   r   r   rr   r{   rW   Fr   )z-gammaz1.5z-D110z-bgTransparentr   z
--no-fontsru   rV   rL   r   TrT      r   zbuild-finished)versionparallel_read_safe)add_html_math_rendererr   r   add_config_value	frozensetr)   boollisttupler?   connectr   sphinx__display_version__)r   s    r&   setupr    s   )40/6   /iQTPUFVW)8V9cUCST*IvYPSuEUV'6C5AQR.vYPTvEVW<	   iu>V   b&	4-0H   12vYPSuEUV/vYPTvEVW,b&	3%@PQ%y$?PQKK .1--" r'   )rA   zstr | os.PathLike[str]r+   
int | None)rA   r   rI   r?   r+   r,   )
r[   r)   rQ   r)   r\   r   r]   r   r+   r)   )r{   r)   r\   r   r+   r   )r~   z	list[str]r   r)   r+   ztuple[str, str])r   r   r   r   r\   r   r+   r  )r!   r   rQ   r)   r\   r   r+   z"tuple[_StrPath | None, int | None])r[   r)   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+   r   )Lr4   
__future__r   __all__r   r   ro   os.pathrer   rv   rm   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.configr   sphinx.util._pathlibr   sphinx.util.typingr   sphinx.writers.html5r   	getLoggerr-   ry   joinpathrX   r   r3   r   compiler   r   r=   rE   rJ   rc   r   r   r   r   r   r   r   r   r   r   r  r   r'   r&   <module>r     s~   0 "
   	  	      )      %   G ; .&)$-44			8	$%%%k9=
; 
)+ )  2::./bjj,-RZZ 9: 	/WW
W W 	W
 	W>&ARA*.*:!
:!!$:!17:!':!z	/ND'T r'   