o
    i                     @   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)MathTextParserBitmapc                   @   s   e Zd ZdS )
latex_mathN)__name__
__module____qualname__ r   r   W/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/matplotlib/sphinxext/mathmpl.pyr	      s    r	   c                 C   s   t | tjS N)r   choicer   _font_type_mapping)argr   r   r   fontset_choice   s   r   c           
      C   sD   | d}||d d }t|}	||	d< |dd|	d< |	gg fS )N`   latexfontsetcm)findr	   get)
rolerawtexttextlineno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}t| j}||d< | jdd|d< |gS )N r   r   r   )joinr"   r	   
block_textr!   r   )selfr   r$   r   r   r   run)   s
   
zMathDirective.runN)
r
   r   r   has_contentrequired_argumentsoptional_argumentsfinal_argument_whitespacer   option_specr+   r   r   r   r   r&   "   s    r&   r   c              	   C   s   d|  } t d|iE t| rtj| dd}nz
tj|| dd}W n ty7   t	d|   d}Y nw W d    |S W d    |S W d    |S 1 sSw   Y  |S )N$%s$zmathtext.fontsetd   )dpiz!Could not render math expression r   )
mpl
rc_contextr   existsmathtext_parser	get_depthto_png	Exceptionr   _warn_external)r   filenamer   depthr   r   r   	latex2png2   s.   






r>   c                 C   s   t | jtj}| d }| d }dt||   dd  }t	t
jjjdd}|jddd || d	 }t|||}|rCd
}	nd}	|rR|dkrRd|d  }
nd
}
dt
jjj d| d|	 |
 dS )Nr   r   zmath-{}i_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/>)
isinstanceparentr   TextElementformathashlibmd5encode	hexdigestr   setupappbuilderoutdirmkdirr>   imgpath)r$   sourceinliner   r   namedestdirdestr=   clsstyler   r   r   
latex2htmlB   s*   rX   c                 C   s   | t _dd }dd }dd }dd }| jt||f||fd	 | d
t | d
t tj	dk r<| dt | dt ddd}|S )Nc                 S   s"   | j jd }| jt|| d S )NrQ   )document
attributesbodyappendrX   )r*   r$   rQ   r   r   r   visit_latex_math_html`   s   z$setup.<locals>.visit_latex_math_htmlc                 S      d S r   r   r*   r$   r   r   r   depart_latex_math_htmld      z%setup.<locals>.depart_latex_math_htmlc                 S   sD   t |jtj}|r| jd|d   d S | jd|d dg d S )Nr1   r   z\begin{equation}z\end{equation})rC   rD   r   rE   r[   r\   extend)r*   r$   rR   r   r   r   visit_latex_math_latexh   s   
z%setup.<locals>.visit_latex_math_latexc                 S   r^   r   r   r_   r   r   r   depart_latex_math_latexq   ra   z&setup.<locals>.depart_latex_math_latex)htmlr   r@   )r      mathT)parallel_read_safeparallel_write_safe)
rK   rL   add_noder	   add_roler%   add_directiver&   sphinxversion_info)rL   r]   r`   rc   rd   metadatar   r   r   rK   \   s    	

rK   )r   )rG   pathlibr   docutilsr   docutils.parsers.rstr   r   rm   
matplotlibr4   r   matplotlib.mathtextr   r7   GeneralElementr	   r   r%   r!   r&   r>   rX   rK   r   r   r   r   <module>   s$    


