o
    Ni<                     @  s@  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
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  ddl!m"Z"m#Z# ddl$m%Z% erd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l0m1Z1 e2e3Z4e5eddZ6G dd deZ7G dd deZ8dZ9e:dZ;e:d Z<e:d!Z=ddd&d'Z>ded+d,Z?	-dfdgd4d5Z@dhd8d9ZAdid;d<ZBdjdAdBZCdkdEdFZDdkdGdHZEdldLdMZFdmdOdPZGdndUdVZHdodYdZZIdpd\d]ZJdqd_d`ZKdrdbdcZLdS )sz*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)	ensuredir)read_png_depthwrite_png_depth)LaTeXRenderer)Element)Sphinx)Builder)Config)ExtensionMetadata)HTML5Translator	templatesimgmathc                      s&   e Zd ZdZ	dd fd
dZ  ZS )MathExtErrorzMath extension errorNmsgstrstderr
str | NonestdoutreturnNonec                   s0   |r|d| 7 }|r|d| 7 }t  | d S )Nz

[stderr]
z

[stdout]
)super__init__)selfr   r   r!   	__class__r   F/home/ubuntu/.local/lib/python3.10/site-packages/sphinx/ext/imgmath.pyr%   1   s
   zMathExtError.__init__NN)r   r   r   r    r!   r    r"   r#   )__name__
__module____qualname__categoryr%   __classcell__r   r   r'   r)   r   .   s    r   c                   @  s   e Zd ZdZdS )InvokeErrorzerrors on invoking converters.N)r+   r,   r-   __doc__r   r   r   r)   r0   ;   s    r0   )pngsvgz\[\d+ depth=(-?\d+)\]z.*, depth=(.*)ptz<!-- DEPTH=(-?\d+) -->filenamer   r"   
int | Nonec                 C  sh   t | dd$}|D ]}q	t|}|r!t|dW  d   S 	 W d   dS 1 s-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)r4   flinematchedr   r   r)   read_svg_depthF   s   
$rB   depthr=   r#   c                 C  sB   t | ddd}|d|  W d   dS 1 sw   Y  dS )z<Write the depth to SVG file as a comment at end of file
    ar6   r7   z
<!-- DEPTH=%s -->N)r:   write)r4   rC   r?   r   r   r)   write_svg_depthS   s   "rF    image_formatmathconfigr   confdirstr | os.PathLike[str]c           	      C  s   |j tt|j d |j| dkrdnd|d}|jrd}nd}|jD ] }dD ]}t|||| }t|rAt	 
||    S q&q"t	t
|d	 |S )
zGenerate LaTeX macro.g333333?r2   rG   z,dvips,tightpage)fontsizebaselineskippreamble	tightpagerI   zpreview.texztemplate.tex).jinja_trQ   )imgmath_font_sizer=   roundimgmath_latex_preambleimgmath_use_previewtemplates_pathr   joinexistsr   render)	rH   rI   rJ   rK   	variablestemplate_nametemplate_dirtemplate_suffixtemplater   r   r)   generate_latex_macroZ   s"   


r`   builderr   c                 C  s   t | ds
t | _| 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mkdtemprb   )ra   r   r   r)   ensure_tempdirw   s   

rf   latexc           	   
   C  s&  t |}t|d}t|ddd}||  W d   n1 s!w   Y  t|jj}|jjg}|dkr;|d |	|jj
 |d ztj|d|dd	d
 |dv r]t|dW S t|dW S  ty| } zttd|jj t|d}~w ty } zd}t||j|j|d}~ww )z%Compile LaTeX macros for math to DVI.zmath.texwr6   r7   Ntectonicz--interaction=nonstopmodeTascii)capture_outputcwdcheckr8   >   xelatexri   zmath.xdvzmath.dvizYLaTeX command %r cannot be run (needed for math display), check the imgmath_latex settingzlatex exited with error)rf   r   rX   r:   rE   basenamerJ   imgmath_latexappendextendimgmath_latex_args
subprocessrunOSErrorloggerwarningr   r0   r   r   r   r!   )	rg   ra   tempdirr4   r?   imgmath_latex_namecommandexcr   r   r   r)   compile_math   s:   



r}   r{   	list[str]nametuple[str, str]c              
   C  s   zt j| dddd}|j|jfW S  ty+ } zttd|| d | t|d}~w t	yA } zt
d| |j|j|d}~ww )z*Convert DVI file to specific image format.Trj   )rk   rm   r8   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   rv   rw   rx   r   r0   r   r   )r{   r   retr|   r   r   r)   convert_dvi_to_image   s   

r   dvipathout_pathc           
      C  s   d}|j jd|dddg}||j j |j jr|d ||  t||\}}d}|j jrK| D ]}t	|}	|	rJt
|	d}t||  |S q2|S )	zConvert DVI file to PNG image.dvipng-oz-Ttightz-z9z--depthNr9   )rJ   imgmath_dvipngrr   imgmath_dvipng_argsrV   rq   r   
splitlinesdepth_rer<   r=   r>   r   
r   ra   r   r   r{   r!   r   rC   r@   rA   r   r   r)   convert_dvi_to_png   s$   



r   c           
      C  s   d}|j jd|g}||j j ||  t||\}}d}|j jrE| D ]}t	|}	|	rDt
t|	dd d }t||  |S q&|S )zConvert DVI file to SVG image.dvisvgmr   Nr9   d   gzGR@)rJ   imgmath_dvisvgmrr   imgmath_dvisvgm_argsrq   r   rV   r   depthsvg_rer<   rT   floatr>   rF   r   r   r   r)   convert_dvi_to_svg   s    


r   r&   r   tuple[str | None, int | None]c           	      C  sj  | j jj }|tvrd}t|t||| j j| j j}t|	 dd
  d| }t| j j| j jd|}tt| t|r[|dkrOt|}||fS |dkrWt|}||fS t| j dsgt| j d	rid
S zt|| j }W n ty   d| j _Y d
S w z&|dkrt|| j |}n|dkrt|| j |}W ||fS W ||fS W ||fS  ty   d| j _Y d
S 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.rI   r2   r3   _imgmath_warned_latex _imgmath_warned_image_translatorr*   T)ra   rJ   imgmath_image_formatlowerSUPPORT_FORMATr   r`   rK   r   encode	hexdigestr   rX   outdirimagedirr   dirnameisfiler   rB   rc   r}   r0   r   r   r   r   )	r&   rI   rH   unsupported_format_msgrg   r4   generated_pathrC   r   r   r   r)   render_math   sV   

r   r   c                 C  sp   t |d}t| jdd}W d    n1 sw   Y  | dkr)d| S | dkr2d| S d}t|)	Nrbr6   r7   r2   zdata:image/png;base64,r3   zdata:image/svg+xml;base64,r   )r:   base64	b64encodereaddecoder   )rH   r   r?   encodedr   r   r   r)   render_maths_to_base64   s   

r   appr   r|   	Exceptionc                 C  s   |rd S t | jdr&tt t| jj W d    n1 s!w   Y  | jjj	rQtt tt
| jj| jjd W d    d S 1 sJw   Y  d S d S )Nrb   rI   )rc   ra   
contextlibsuppressr   shutilrmtreerb   rJ   imgmath_embedr   rX   r   r   )r   r|   r   r   r)   clean_up_files+  s   
"r   noder   c                 C  s$   | j jjrd| |   S dS )Nz	 alt="%s"rG   )ra   rJ   imgmath_add_tooltipsr   astextstrip)r&   r   r   r   r)   get_tooltip:  s   
r   
nodes.mathc              
   C  sB  zt | d|  d \}}W n0 ty? } z$t|}tj|ddg | d}||  tt	d| | tj
|d }~ww |d u rV| jd| |    tj
| jjjrh| jjj }t||}nt|}	t| jjd|	}
|
tjd}d	| d
t| | }|d ur|d| dd7 }| j|d  tj
)N$WARNING   typelevelbackrefssourcezdisplay latex %r: %sz<span class="math">%s</span>rI   /z<img class="math" src=""z style="vertical-align: dzpx"z/>)r   r   r   r   r   system_message	walkaboutrw   rx   r   SkipNodebodyrq   r   r   ra   rJ   r   r   r   r   r   ro   rX   imgpathreplacesepr   )r&   r   rendered_pathrC   r|   r   smrH   img_srcbnamerelative_pathcr   r   r)   html_visit_math@  s8   



r   nodes.math_blockc              
   C  s  |d r	|  }nt|  d d}z	t| |\}}W n0 tyJ } z$t|}tj|ddg |  d}||  t	t
d|  | tj|d }~ww | j| j|ddd	 | jd
 |d r|t| |}| jd|  | |td | jd |d u r| jd| |     tj| jjjr| jjj }	t|	|}
nt|}t| jjd|}|tjd}
| jd|
 dt | | d  tj)NnowrapFr   r   r   zinline latex %r: %sdivrI   )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   rw   rx   r   r   r   rq   starttagr   add_permalink_refr   r   r   ra   rJ   r   r   r   r   r   ro   rX   r   r   r   r   )r&   r   rg   r   rC   r|   r   r   r   rH   r   r   r   r   r   r)   html_visit_displaymath^  sJ   





r   r   c                 C  s   |  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 | dt tjddS )Nr   r   r2   htmlr   r   r   r   rp   rg   rV   Fr   )z-gammaz1.5z-D110z-bgTransparentr   z
--no-fontsrs   rU   rG   r   TrS      r   zbuild-finished)versionparallel_read_safe)	add_html_math_rendererr   r   add_config_valueboolconnectr   sphinx__display_version__)r   r   r   r)   setup  s*   r   )r4   r   r"   r5   )r4   r   rC   r=   r"   r#   )rG   )
rH   r   rI   r   rJ   r   rK   rL   r"   r   )ra   r   r"   r   )rg   r   ra   r   r"   r   )r{   r~   r   r   r"   r   )r   r   ra   r   r   r   r"   r5   )r&   r   rI   r   r"   r   )rH   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   )Mr1   
__future__r   __all__r   r   rer   rt   rd   hashlibr   osr   r   typingr   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.util.typingr   sphinx.writers.html5r   	getLoggerr+   rw   rX   rW   r   r0   r   compiler   r   r;   rB   rF   r`   rf   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r)   <module>   sn    









$



>



'