3
ƽh                 @   s   d dl Z d dlmZ d dlmZ d dlmZmZ d dlZd dl	Z
d dl	mZ d dlmZ edZG dd	 d	ejejZd
d Zi g fddZdeie_G dd deZdddZdd Zdd ZdS )    N)Path)nodes)	Directive
directives)cbook)MathTextParserZBitmapc               @   s   e Zd ZdS )
latex_mathN)__name__
__module____qualname__ r   r   B/tmp/pip-build-7iwl8md4/matplotlib/matplotlib/sphinxext/mathmpl.pyr      s   r   c             C   s   t j| tjS )N)r   choicer   Z_font_type_mapping)argr   r   r   fontset_choice   s    r   c       
      C   sD   |j d}||d d }t|}	||	d< |jdd|	d< |	gg fS )N`   latexfontsetcm)findr   get)
roleZrawtexttextlinenoZinlineroptionscontentir   noder   r   r   	math_role   s    
r    r   c               @   s,   e Zd ZdZdZdZdZdeiZdd Z	dS )MathDirectiveTr   Fr   c             C   s6   dj | j}t| j}||d< | jjdd|d< |gS )N r   r   r   )joinr   r   Z
block_textr   r   )selfr   r   r   r   r   run)   s
    
zMathDirective.runN)
r	   r
   r   Zhas_contentZrequired_argumentsZoptional_argumentsZfinal_argument_whitespacer   Zoption_specr%   r   r   r   r   r!   "   s   r!   r   c             C   s   d|  } t jd|i` t|j r4tj| dd}n>ytj|| dd}W n( tk
rp   tj	d|   d}Y nX W d Q R X |S )Nz$%s$zmathtext.fontsetd   )Zdpiz!Could not render math expression r   )
mplZ
rc_contextr   existsmathtext_parserZ	get_depthZto_png	Exceptionr   Z_warn_external)r   filenamer   depthr   r   r   	latex2png2   s    r-   c             C   s   t | jtj}| d }| d }djtj|| j j dd  }t	t
jjjdd}|jddd || d	 }t|||}|rd
}	nd}	|r|dkrd|d  }
nd
}
dt
jjj d| d|	 |
 dS )Nr   r   zmath-{}
   Z_imagesmathmplT)parentsexist_okz.pngr"   zclass="center" r   z)style="position: relative; bottom: -%dpx"r   z
<img src="z	/mathmpl/z.png" z/>i)
isinstanceparentr   TextElementformathashlibmd5encode	hexdigestr   setupappZbuilderoutdirmkdirr-   Zimgpath)r   sourceinliner   r   namedestdirdestr,   clsstyler   r   r   
latex2htmlB   s      rE   c             C   s   | t _dd }dd }dd }dd }| jt||f||fd	 | jd
t | jd
t tj	dk rx| jdt | jdt ddd}|S )Nc             S   s"   | j jd }| jjt|| d S )Nr>   )document
attributesbodyappendrE   )r$   r   r>   r   r   r   visit_latex_math_html`   s    z$setup.<locals>.visit_latex_math_htmlc             S   s   d S )Nr   )r$   r   r   r   r   depart_latex_math_htmld   s    z%setup.<locals>.depart_latex_math_htmlc             S   sB   t |jtj}|r(| jjd|d   n| jjd|d dg d S )Nz$%s$r   z\begin{equation}z\end{equation})r2   r3   r   r4   rH   rI   extend)r$   r   r?   r   r   r   visit_latex_math_latexh   s    z%setup.<locals>.visit_latex_math_latexc             S   s   d S )Nr   )r$   r   r   r   r   depart_latex_math_latexq   s    z&setup.<locals>.depart_latex_math_latex)htmlr   r/   r      mathT)Zparallel_read_safeZparallel_write_safe)r   rP   )
r:   r;   add_noder   Zadd_roler    Zadd_directiver!   sphinxversion_info)r;   rJ   rK   rM   rN   metadatar   r   r   r:   \   s    	

r:   )r   )r6   Zpathlibr   Zdocutilsr   Zdocutils.parsers.rstr   r   rS   Z
matplotlibr'   r   Zmatplotlib.mathtextr   r)   ZGeneralElementr   r   r    r   r!   r-   rE   r:   r   r   r   r   <module>   s    

