B
    \0                 @   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	 ddl
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mZ dd
lmZ ddlmZmZ ddlmZmZmZ ddl m!Z!m"Z" ddl#m$Z$ e%e&Z'G dd deZ(G dd deZ)dZ*dZ+dZ,dZ-e.dZ/dd Z0dd Z1dd Z2dd Z3d d! Z4d"d# Z5d$d% Z6d&d' Z7d(d) Z8d*d+ Z9d,d- Z:d.d/ Z;dS )0z
    sphinx.ext.imgmath
    ~~~~~~~~~~~~~~~~~~

    Render math in HTML via dvipng or dvisvgm.

    :copyright: Copyright 2007-2019 by the Sphinx team, see AUTHORS.
    :license: BSD, see LICENSE for details.
    N)sha1)path)PopenPIPE)nodes)	text_type)SphinxError)___)logging)get_node_equation_numberwrap_displaymath)	ensuredirENOENTcd)read_png_depthwrite_png_depth)sys_encodingc               @   s   e Zd ZdZdddZdS )MathExtErrorzMath extension errorNc             C   s@   |r|d| td 7 }|r0|d| td 7 }t| | d S )Nz

[stderr]
replacez

[stdout]
)decoder   r   __init__)selfmsgstderrstdout r   1lib/python3.7/site-packages/sphinx/ext/imgmath.pyr   /   s
    zMathExtError.__init__)NN)__name__
__module____qualname__categoryr   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"   8   s   r"   )pngsvgz
\documentclass[12pt]{article}
\usepackage[utf8x]{inputenc}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{amsfonts}
\usepackage{anyfontsize}
\usepackage{bm}
\pagestyle{empty}
zA
\begin{document}
\fontsize{%d}{%d}\selectfont %s
\end{document}
z|
\usepackage[active]{preview}
\begin{document}
\begin{preview}
\fontsize{%s}{%s}\selectfont %s
\end{preview}
\end{document}
s   \[\d+ depth=(-?\d+)\]c             C   sP   |j }tt|d }t|j }|jr:|t||| f 7 }n|t||| f 7 }|S )zGenerate LaTeX macro.g333333?)imgmath_font_sizeintroundDOC_HEADimgmath_latex_preambleimgmath_use_previewDOC_BODY_PREVIEWDOC_BODY)mathconfigZfontsizeZbaselineskiplatexr   r   r   generate_latex_macro\   s    
r1   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tempfileZmkdtempr2   )builderr   r   r   ensure_tempdirk   s    

r6   c       
      C   s   t |}t|d}t|dd}||  W dQ R X |jjdg}||jj	 |
d t|` yt|ttd}W nF tk
r } z(|jtkr ttd|jj tW dd}~X Y nX W dQ R X | \}}	|jdkrtd	|	|t|d
S )z%Compile LaTeX macros for math to DVI.zmath.texwzutf-8Nz--interaction=nonstopmode)r   r   zYLaTeX command %r cannot be run (needed for math display), check the imgmath_latex settingr   zlatex exited with errorzmath.dvi)r6   r   joincodecsopenwriter/   imgmath_latexextendimgmath_latex_argsappendr   r   r   OSErrorerrnor   loggerwarningr
   r"   communicate
returncoder   )
r0   r5   tempdirfilenamefcommandperrr   r   r   r   r   compile_mathy   s(    




 
rL   c          
   C   s   yt | ttd}W nJ tk
r\ } z,|jtkr0 ttd|| d | tW dd}~X Y nX |	 \}}|j
dkrtd| ||||fS )z*Convert DVI file to specific image format.)r   r   zS%s command %r cannot be run (needed for math display), check the imgmath_%s settingr   Nz%s exited with error)r   r   r@   rA   r   rB   rC   r
   r"   rD   rE   r   )rI   namerJ   rK   r   r   r   r   r   convert_dvi_to_image   s    


rN   c             C   s   t |}t|d}d}|jjd|dddg}||jj |jjrL|d ||  t	||\}}d}|jjrx8|
 D ],}	t|	}
|
rzt|
d	}t|| P qzW ||fS )
zConvert DVI file to PNG image.zmath.pngdvipngz-oz-TZtightz-z9z--depthN   )r6   r   r8   r/   imgmath_dvipngr=   imgmath_dvipng_argsr+   r?   rN   
splitlinesdepth_rematchr'   groupr   )dvipathr5   rF   rG   rM   rI   r   r   depthlineZmatchedr   r   r   convert_dvi_to_png   s$    



rZ   c             C   sP   t |}t|d}d}|jjd|g}||jj ||  t|| |dfS )zConvert DVI file to SVG image.zmath.svgdvisvgmz-oN)	r6   r   r8   r/   imgmath_dvisvgmr=   imgmath_dvisvgm_argsr?   rN   )rW   r5   rF   rG   rM   rI   r   r   r   convert_dvi_to_svg   s    

r^   c       
      C   sN  | j jj }|tkrtdt|| j j}dt|d	 |f }t
| j jd|}t| j j| j jd|}t|rt|}||fS t| j dst| j drdS yt|| j }W n tk
r   d| j _dS X y8|d	krt|| j \}	}n|d
kr
t|| j \}	}W n tk
r*   d| j _dS X tt| t|	| ||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.

    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"z%s.%szutf-8r.   _imgmath_warned_latex _imgmath_warned_image_translator)NNTr$   r%   )r5   r/   imgmath_image_formatlowerSUPPORT_FORMATr   r1   r   encodeZ	hexdigest	posixpathr8   imgpathr   ZoutdirZimagedirisfiler   r3   rL   r"   r_   rZ   r^   r`   r   dirnameshutilZmove)
r   r.   Zimage_formatr0   rG   ZrelfnZoutfnrX   rW   rf   r   r   r   render_math   s:    

rj   c             C   sD   |rd S t | jdsd S yt| jj W n tk
r>   Y nX d S )Nr2   )r3   r5   ri   ZrmtreeZ_mathpng_tempdir	Exception)appexcr   r   r   cleanup_tempdir  s    rn   c             C   s$   | j jjr d| |   S dS )Nz	 alt="%s" )r5   r/   imgmath_add_tooltipsrd   astextstrip)r   noder   r   r   get_tooltip  s    
rt   c          
   C   s   yt | d|  d \}}W nh tk
r } zJt|}tj|ddg | d}||  tt	d| | tj
W d d }~X Y nX |d kr| jd| |    n8d| t| | }|d k	r|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="%s"z style="vertical-align: %dpx"z/>)rj   rq   r   r   r   system_message	walkaboutrB   rC   r
   SkipNodebodyr?   rd   rr   rt   )r   rs   fnamerX   rm   r   smcr   r   r   html_visit_math%  s"    

r   c       	   
   C   sT  |d r|  }nt|  d d}yt| |\}}W nh tk
r } zJt|}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| |     n| jd| t| | d  tjd S )NZnowrapFrv   rw   )rx   ry   rz   r{   zinline latex %r: %sZdivr.   )ZCLASSz<p>numberz<span class="eqno">(%s)zPermalink to this equationz</span>z'<span class="math">%s</span></p>
</div>z<img src="%s"z/></p>
</div>)rq   r   rj   r   r   r   r|   r}   rB   rC   r
   r~   r   r?   Zstarttagr   Zadd_permalink_refr	   rd   rr   rt   )	r   rs   r0   r   rX   rm   r   r   r   r   r   r   html_visit_displaymath<  s2    





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t tjddS )NZimgmathra   r$   ZhtmlrQ   rO   r\   r[   r<   r0   r+   FrR   z-gammaz1.5z-DZ110z-bgZTransparentr]   z
--no-fontsr>   r*   ro   rp   Tr&      zbuild-finished)versionZparallel_read_safe)Zadd_html_math_rendererr   r   Zadd_config_valueZconnectrn   sphinxZ__display_version__)rl   r   r   r   setup\  s$    
r   )<r#   r9   re   reri   r4   Zhashlibr   osr   
subprocessr   r   Zdocutilsr   Zsixr   r   Zsphinx.errorsr   Zsphinx.localer	   r
   Zsphinx.utilr   Zsphinx.util.mathr   r   Zsphinx.util.osutilr   r   r   Zsphinx.util.pngr   r   Zsphinx.util.pycompatr   Z	getLoggerr   rB   r   r"   rc   r)   r-   r,   compilerT   r1   r6   rL   rN   rZ   r^   rj   rn   rt   r   r   r   r   r   r   r   <module>
   sL   

	
"9 