B
    .ÛKcV9  ã               @   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   ú1lib/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baselineskipZpreambleZ	tightpagerD   zpreview.tex_tztemplate.tex_t)Úimgmath_font_sizer8   ÚroundÚimgmath_latex_preambleÚimgmath_use_previewÚtemplates_pathr   ÚjoinÚexistsr   Zrender)rC   rD   rE   rF   Z	variablesZtemplate_nameZtemplate_dirÚtemplater   r   r'   Úgenerate_latex_macroP   s    
rO   )Úbuilderr"   c             C   s   t | dƒst ¡ | _| jS )zîCreate 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mkdtemprQ   )rP   r   r   r'   Úensure_tempdirh   s    

rT   )ÚlatexrP   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)rT   r   rL   r5   r@   rE   Úimgmath_latexÚextendÚimgmath_latex_argsÚappendÚ
subprocessÚrunr   ÚOSErrorÚloggerÚwarningr   r.   r   r   r    r!   )rU   rP   Ztempdirr2   r:   ÚcommandÚexcr   r   r'   Úcompile_mathu   s"    


re   )rc   Ú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.TrW   )r!   r    rY   r3   zS%s command %r cannot be run (needed for math display), check the imgmath_%s settingr   Nz%s exited with error)r^   r_   r   r!   r    r`   ra   rb   r   r.   r   r   )rc   rf   Úretrd   r   r   r'   Úconvert_dvi_to_image‘   s    
rh   )ÚdvipathrP   Úout_pathr"   c       
      C   sš   d}|j jd|dddg}| |j j¡ |j jr8| d¡ | | ¡ t||ƒ\}}d}|j jr–x8| ¡ 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_argsrJ   r]   rh   Ú
splitlinesÚdepth_rer7   r8   r9   r   )
ri   rP   rj   rf   rc   r!   r    r>   r;   r<   r   r   r'   Úconvert_dvi_to_pngŸ   s     



rp   c       
      C   sŽ   d}|j jd|g}| |j j¡ | | ¡ t||ƒ\}}d}|j jrŠxD| ¡ D ]8}t 	|¡}	|	rNt
t|	 d¡ƒd d ƒ}t||ƒ P qNW |S )zConvert DVI file to SVG image.Údvisvgmz-oNr4   éd   gáz®GR@)rE   Úimgmath_dvisvgmr[   Úimgmath_dvisvgm_argsr]   rh   rJ   rn   Údepthsvg_rer7   rH   Úfloatr9   rA   )
ri   rP   rj   rf   rc   r!   r    r>   r;   r<   r   r   r'   Úconvert_dvi_to_svg¶   s    


rw   )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r t|ƒ}||fS t| j dƒsÀt| j dƒrÄd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)rP   rE   Úimgmath_image_formatÚlowerÚSUPPORT_FORMATr   rO   rF   r   ÚencodeZ	hexdigestr   rL   ÚoutdirÚimagedirr   ÚdirnameÚisfiler   r=   rR   re   r.   ry   rp   rw   rz   )r%   rD   rC   rU   r2   Úgenerated_pathr>   ri   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   Úbase64Z	b64encodeÚreadÚdecoder   )rC   rƒ   r:   Zencodedr   r   r'   Úrender_maths_to_base64	  s     

r‰   )Úapprd   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 )NrQ   rD   )rR   rP   ÚshutilZrmtreerQ   Ú	ExceptionrE   Úimgmath_embedr   rL   r   r€   )rŠ   rd   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   )rP   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Ú	walkaboutra   rb   r   ÚSkipNodeÚbodyr]   r~   r’   rP   rE   r   r{   r|   r‰   r   Úrelpathr   ÚreplaceÚsepr“   )r%   r   Úrendered_pathr>   rd   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: %sZ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Ÿ   ra   rb   r   r    r¡   r]   Zstarttagr   Zadd_permalink_refr   r~   r’   rP   rE   r   r{   r|   r‰   r   r¢   r   r£   r¤   r“   )r%   r   rU   r¥   r>   rd   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   Zhtmlrl   rk   rs   rq   rZ   rU   rJ   Frm   z-gammaz1.5z-DZ110z-bgZTransparentrt   z
--no-fontsr\   rI   rB   r   TrG   é   r   zbuild-finished)ÚversionZparallel_read_safe)	Zadd_html_math_rendererrª   r¬   Zadd_config_valueÚboolZconnectrŽ   ÚsphinxZ__display_version__)rŠ   r   r   r'   Úsetupo  s&    
r±   )rB   )Qr/   r†   Úrer‹   r^   rS   Ú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   Z	getLoggerr(   ra   rL   rK   Ú__all__r   r.   r}   Úcompilero   ru   r6   r,   r8   r=   rA   rO   rT   re   rh   rp   rw   r„   r‰   rŒ   rŽ   r“   rD   rª   Z
math_blockr¬   r±   r   r   r   r'   Ú<module>   sb   



;
&