B
    dV9                 @   s  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mZ ddlmZm	Z	 ddl
mZmZmZmZ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 ddl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%m&Z& ddl'm(Z( ddl)m*Z*m+Z+ ddl,m-Z- ddl.m/Z/ e"0e1Z2e3eddZ4dZ5G dd deZ6G dd deZ7dZ8e9dZ:e9dZ;e9dZ<e=ee> dd d!Z?e=e>dd"d#d$Z@dJe=e=ee=e=d&d'd(ZAee=d)d*d+ZBe=ee=d,d-d.ZCee= e=ee=e=f d/d0d1ZDe=ee=ee> d2d3d4ZEe=ee=ee> d2d5d6ZFe/e=eee= ee> f d7d8d9ZGe=ee= e=d:d;d<ZHeeIdd=d>d?ZJe/ee=d@dAdBZKe/ejLdd@dCdDZMe/ejNdd@dEdFZOeee=ef dGdHdIZPdS )Kz*Render math in HTML via dvipng or dvisvgm.    N)path)PIPECalledProcessError)AnyDictListOptionalTuple)nodes)Element)package_dir)Sphinx)Builder)Config)SphinxError)___)loggingsha1)get_node_equation_numberwrap_displaymath)	ensuredir)read_png_depthwrite_png_depth)LaTeXRenderer)HTMLTranslatorZ	templatesimgmath c                   s6   e Zd ZdZdeee ee dd fddZ  ZS )MathExtErrorzMath extension errorN)msgstderrstdoutreturnc                s0   |r|d| 7 }|r |d| 7 }t  | d S )Nz

[stderr]
z

[stdout]
)super__init__)selfr   r    r!   )	__class__r   _/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/sphinx/ext/imgmath.pyr$   '   s
    zMathExtError.__init__)NN)__name__
__module____qualname__categorystrr   r$   __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'   r.   1   s   r.   )pngsvgz\[\d+ depth=(-?\d+)\]z.*, depth=(.*)ptz<!-- DEPTH=(-?\d+) -->)filenamer"   c          	   C   sF   t | dd0}x|D ]}qW t|}|r8t|dS dS Q R X dS )z9Read the depth from comment at last line of SVG file
    zutf-8)encoding   N)opendepthsvgcomment_rematchintgroup)r2   flinematchedr   r   r'   read_svg_depth<   s    

r=   )r2   depthr"   c          	   C   s,   t | ddd}|d|  W dQ R X dS )z<Write the depth to SVG file as a comment at end of file
    azutf-8)r3   z
<!-- DEPTH=%s -->N)r5   write)r2   r>   r:   r   r   r'   write_svg_depthI   s    rA    )image_formatmathconfigconfdirr"   c             C   s   |j tt|j d |j| dkr$dnd|d}|jr:d}nd}x4|jD ]*}t|||}t|rFt	 
||S qFW t	t
||S )zGenerate LaTeX macro.g333333?r0   rB   z
,tightpage)ZfontsizeZbaselineskippreambleZ	tightpagerD   zpreview.tex_tztemplate.tex_t)imgmath_font_sizer8   roundimgmath_latex_preambleimgmath_use_previewtemplates_pathr   joinexistsr   render)rC   rD   rE   rF   	variablesZtemplate_nameZtemplate_dirtemplater   r   r'   generate_latex_macroP   s    
rR   )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mkdtemprT   )rS   r   r   r'   ensure_tempdirh   s    

rX   )latexrS   r"   c          
   C   s   t |}t|d}t|ddd}||  W dQ R X |jjdg}||jj |	d y"t
j|tt|ddd	 t|d
S  tk
r } zttd|jj t|W dd}~X Y n4 tk
r } ztd|j|j|W dd}~X Y nX dS )z%Compile LaTeX macros for math to DVI.zmath.texwzutf-8)r3   Nz--interaction=nonstopmodeTascii)r!   r    cwdcheckr3   zmath.dvizYLaTeX command %r cannot be run (needed for math display), check the imgmath_latex settingzlatex exited with error)rX   r   rM   r5   r@   rE   imgmath_latexextendimgmath_latex_argsappend
subprocessrunr   OSErrorloggerwarningr   r.   r   r   r    r!   )rY   rS   tempdirr2   r:   commandexcr   r   r'   compile_mathu   s"    


rj   )rh   namer"   c          
   C   s   y t j| ttddd}|j|jfS  tk
r` } z"ttd|| d | t	|W dd}~X Y n8 t
k
r } ztd| |j|j|W dd}~X Y nX dS )z*Convert DVI file to specific image format.Tr[   )r!   r    r]   r3   zS%s command %r cannot be run (needed for math display), check the imgmath_%s settingr   Nz%s exited with error)rb   rc   r   r!   r    rd   re   rf   r   r.   r   r   )rh   rk   retri   r   r   r'   convert_dvi_to_image   s    
rm   )dvipathrS   out_pathr"   c       
      C   s   d}|j jd|dddg}||j j |j jr8|d ||  t||\}}d}|j jrx8| D ],}t	|}	|	rft
|	d}t|| P qfW |S )	zConvert DVI file to PNG image.dvipngz-oz-TZtightz-z9z--depthNr4   )rE   imgmath_dvipngr_   imgmath_dvipng_argsrK   ra   rm   
splitlinesdepth_rer7   r8   r9   r   )
rn   rS   ro   rk   rh   r!   r    r>   r;   r<   r   r   r'   convert_dvi_to_png   s     



ru   c       
      C   s   d}|j jd|g}||j j ||  t||\}}d}|j jrxD| D ]8}t	|}	|	rNt
t|	dd d }t|| P qNW |S )zConvert DVI file to SVG image.dvisvgmz-oNr4   d   gzGR@)rE   imgmath_dvisvgmr_   imgmath_dvisvgm_argsra   rm   rK   rs   depthsvg_rer7   rI   floatr9   rA   )
rn   rS   ro   rk   rh   r!   r    r>   r;   r<   r   r   r'   convert_dvi_to_svg   s    


r|   )r%   rD   r"   c             C   sP  | j jj }|tkrtdt||| j j| j j}t|	 
  d| }t| j j| j jd|}tt| t|r|dkrt|}n|dkrt|}||fS t| j dst| j drdS yt|| j }W n tk
r   d	| j _dS X y6|dkrt|| j |}n|dkr&t|| j |}W n tk
rF   d	| j _dS X ||fS )
a  Render the LaTeX math expression *math* using latex and dvipng or
    dvisvgm.

    Return the filename relative to the built document and the "depth",
    that is, the distance of image bottom and baseline in pixels, if the
    option to use preview_latex is switched on.
    Also return the temporary and destination files.

    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.
    z2imgmath_image_format must be either "png" or "svg".rD   r0   r1   _imgmath_warned_latex _imgmath_warned_image_translator)NNT)rS   rE   imgmath_image_formatlowerSUPPORT_FORMATr   rR   rF   r   encode	hexdigestr   rM   outdirimagedirr   dirnameisfiler   r=   rU   rj   r.   r~   ru   r|   r   )r%   rD   rC   rY   r2   generated_pathr>   rn   r   r   r'   render_math   sB    




r   )rC   r   r"   c          	   C   s\   t |d}t| jdd}W d Q R X | dkr>d| S | dkrPd| S tdd S )	Nrbzutf-8)r3   r0   zdata:image/png;base64,r1   zdata:image/svg+xml;base64,z2imgmath_image_format must be either "png" or "svg")r5   base64	b64encodereaddecoder   )rC   r   r:   encodedr   r   r'   render_maths_to_base64	  s     

r   )appri   r"   c             C   s   |rd S t | jdr<yt| jj W n tk
r:   Y nX | jjjr|y tt	| jj
| jjd W n tk
rz   Y nX d S )NrT   rD   )rU   rS   shutilrmtreerT   	ExceptionrE   imgmath_embedr   rM   r   r   )r   ri   r   r   r'   clean_up_files  s    
 r   )r%   noder"   c             C   s$   | j jjr d| |   S dS )Nz	 alt="%s"rB   )rS   rE   imgmath_add_tooltipsr   astextstrip)r%   r   r   r   r'   get_tooltip&  s    
r   c          
   C   sB  yt | d|  d \}}W nj tk
r } zLt|}tj|ddg | d}||  tt	d| | tj
|W d d }~X Y nX |d kr| jd| |    n| jjjr| jjj }t||}nt|| jj}	|	tjd}d| d	t| | }
|d k	r(|
d
| dd7 }
| j|
d  tj
d S )N$WARNING   )typelevelbackrefssourcezdisplay latex %r: %sz<span class="math">%s</span>/z<img class="math" src=""z style="vertical-align: dzpx"z/>)r   r   r   r,   r
   system_message	walkaboutre   rf   r   SkipNodebodyra   r   r   rS   rE   r   r   r   r   r   relpathr   replacesepr   )r%   r   rendered_pathr>   ri   r   smrC   img_srcrelative_pathcr   r   r'   html_visit_math,  s,    



r   c          
   C   s  |d r|  }nt|  d d}yt| |\}}W nj tk
r } zLt|}tj|ddg |  d}||  t	t
d|  | tj|W d d }~X Y nX | j| j|ddd	 | jd
 |d rt| |}| jd|  | |td | jd |d kr.| jd| |     nf| jjjrT| jjj }	t|	|}
nt|| jj}|tjd}
| jd|
 dt| | d  tjd S )NZnowrapFr   r   )r   r   r   r   zinline latex %r: %sdivrD   )ZCLASSz<p>numberz<span class="eqno">(%s)zPermalink 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   re   rf   r   r   r   ra   Zstarttagr   Zadd_permalink_refr   r   r   rS   rE   r   r   r   r   r   r   r   r   r   r   )r%   r   rY   r   r>   ri   r   r   r   rC   r   r   r   r   r'   html_visit_displaymathI  s<    





r   )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ddddddg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   r0   htmlrq   rp   rx   rv   r^   rY   rK   Frr   z-gammaz1.5z-DZ110z-bgZTransparentry   z
--no-fontsr`   rJ   rB   r   TrH      r   zbuild-finished)versionZparallel_read_safe)	Zadd_html_math_rendererr   r   Zadd_config_valueboolconnectr   sphinxZ__display_version__)r   r   r   r'   setupo  s&    
r   )rB   )Qr/   r   rer   rb   rV   osr   r   r   typingr   r   r   r   r	   Zdocutilsr
   Zdocutils.nodesr   r   r   Zsphinx.applicationr   Zsphinx.buildersr   Zsphinx.configr   Zsphinx.errorsr   Zsphinx.localer   r   Zsphinx.utilr   r   Zsphinx.util.mathr   r   Zsphinx.util.osutilr   Zsphinx.util.pngr   r   Zsphinx.util.templater   Zsphinx.writers.htmlr   	getLoggerr(   re   rM   rL   __all__r   r.   r   compilert   rz   r6   r,   r8   r=   rA   rR   rX   rj   rm   ru   r|   r   r   r   r   r   rD   r   Z
math_blockr   r   r   r   r   r'   <module>   sb   



;
&