U
    Âmœd$  ã                   @   sÞ   d 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	Z	ddl
mZmZ ddlZddlmZmZ ddlmZ G d	d
„ d
ejejƒZdd„ Zi g fdd„Zeedœe_G dd„ deƒZddd„Zdd„ Zdd„ Zdd„ ZdS )aš  
A role and directive to display mathtext in Sphinx
==================================================

.. warning::
    In most cases, you will likely want to use one of `Sphinx's builtin Math
    extensions
    <https://www.sphinx-doc.org/en/master/usage/extensions/math.html>`__
    instead of this one.

Mathtext may be included in two ways:

1. Inline, using the role::

     This text uses inline math: :mathmpl:`\alpha > \beta`.

   which produces:

     This text uses inline math: :mathmpl:`\alpha > \beta`.

2. Standalone, using the directive::

     Here is some standalone math:

     .. mathmpl::

         \alpha > \beta

   which produces:

     Here is some standalone math:

     .. mathmpl::

         \alpha > \beta

Options
-------

The ``mathmpl`` role and directive both support the following options:

    fontset : str, default: 'cm'
        The font set to use when displaying math. See :rc:`mathtext.fontset`.

    fontsize : float
        The font size, in points. Defaults to the value from the extension
        configuration option defined below.

Configuration options
---------------------

The mathtext extension has the following configuration options:

    mathmpl_fontsize : float, default: 10.0
        Default font size, in points.

    mathmpl_srcset : list of str, default: []
        Additional image sizes to generate when embedding in HTML, to support
        `responsive resolution images
        <https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images>`__.
        The list should contain additional x-descriptors (``'1.5x'``, ``'2x'``,
        etc.) to generate (1x is the default and always included.)

é    N)ÚPath)Únodes)Ú	DirectiveÚ
directives)ÚConfigErrorÚExtensionError)Ú_apiÚmathtext)Úvalidate_float_or_Nonec                   @   s   e Zd ZdS )Ú
latex_mathN)Ú__name__Ú
__module__Ú__qualname__© r   r   úU/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/matplotlib/sphinxext/mathmpl.pyr   P   s   r   c                 C   s   t  | tjj¡S ©N)r   Úchoicer	   ZMathTextParserZ_font_type_mapping)Úargr   r   r   Úfontset_choiceT   s    r   c           
      C   sZ   |  d¡}||d d… }t|ƒ}	||	d< | dd¡|	d< | dtjjj¡|	d< |	gg fS )Nú`é   éÿÿÿÿÚlatexÚfontsetÚcmÚfontsize)Úfindr   ÚgetÚsetupÚappÚconfigÚmathmpl_fontsize)
ZroleZrawtextÚtextÚlinenoZinlinerÚoptionsÚcontentÚir   Únoder   r   r   Ú	math_roleX   s    
ÿr(   ©r   r   c                   @   s2   e Zd ZdZdZdZdZdZee	dœZ
dd„ ZdS )	ÚMathDirectivezR
    The ``.. mathmpl::`` directive, as documented in the module's docstring.
    Tr   Fr)   c                 C   sN   d  | j¡}t| jƒ}||d< | j dd¡|d< | j dtjjj	¡|d< |gS )NÚ r   r   r   r   )
Újoinr%   r   Z
block_textr$   r   r   r   r    r!   )Úselfr   r'   r   r   r   Úrunq   s    
ÿzMathDirective.runN)r   r   r   Ú__doc__Zhas_contentZrequired_argumentsZoptional_argumentsZfinal_argument_whitespacer   r
   Zoption_specr.   r   r   r   r   r*   f   s   ÿr*   r   é
   éd   c              	   C   sh   t  ||dœ¡�N ztjd| › d�||dd�}W n( tk
rX   t d| › �¡ d}Y nX W 5 Q R X |S )N)zmathtext.fontsetz	font.sizeú$Zpng)ÚdpiÚformatz!Could not render math expression r   )ÚmplZ
rc_contextr	   Zmath_to_imageÚ	Exceptionr   Zwarn_external)r   Úfilenamer   r   r3   Údepthr   r   r   Ú	latex2png|   s    
   ÿ
r9   c                 C   s†  t | jtjƒ}| d }| d }| d }d t |› |› |› � ¡ ¡ ¡ dd … ¡}t	t
jjjddƒ}|jddd	� ||› d
� }t||||d�}	g }
t
jjjD ]b}|› d| dd¡› d
�}t||| ||dt|d d… ƒ d� |
 t
jjj› d|› d|› �¡ qž|
�r.dt
jjj› d|› d�d |
¡ d }
|�r:d}nd}|�r\|	dk�r\d|	d  }nd}dt
jjj› d|› d|
› |› |› d�	S ) Nr   r   r   zmath-{}iöÿÿÿZ_imagesÚmathmplT)ÚparentsÚexist_okz.png)r   ú-Ú.Ú_r1   r   )r   r3   z	/mathmpl/ú zsrcset="z.png, z, z" r+   zclass="center" r   z)style="position: relative; bottom: -%dpx"r   z
<img src="z.png" z/>)Ú
isinstanceÚparentr   ÚTextElementr4   ÚhashlibÚmd5ÚencodeÚ	hexdigestr   r   r   ZbuilderZoutdirÚmkdirr9   r    Úmathmpl_srcsetÚreplaceÚfloatÚappendZimgpathr,   )r'   ÚsourceÚinliner   r   r   ÚnameZdestdirÚdestr8   ZsrcsetÚsizer7   ÚclsÚstyler   r   r   Ú
latex2htmlˆ   sB    $ÿÿÿÿÿrT   c              	   C   sr   t | jjƒD ]`\}}|d dkr\zt|d d… ƒ W ql tk
rX   td|›d�ƒ‚Y qlX qtd|›d�ƒ‚qd S )Nr   Úxz,Invalid value for mathmpl_srcset parameter: ze. Must be a list of strings with the multiplicative factor followed by an "x".  e.g. ["2.0x", "1.5x"])Ú	enumerater    rI   rK   Ú
ValueErrorr   )r   r    r&   rQ   r   r   r   Ú_config_inited®   s    
ÿ
ÿrX   c                 C   sØ   | t _|  ddd¡ |  dg d¡ z|  dt¡ W n$ tk
rV   |  ddd„ ¡ Y nX 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 )Nr!   g      $@TrI   zconfig-initedzenv-updatedc                 S   s
   t | d ƒS r   )rX   )r   Úenvr   r   r   Ú<lambda>Ç   ó    zsetup.<locals>.<lambda>c                 S   s"   | j jd }| j t||ƒ¡ d S )NrM   )ZdocumentÚ
attributesÚbodyrL   rT   )r-   r'   rM   r   r   r   Úvisit_latex_math_htmlÊ   s    z$setup.<locals>.visit_latex_math_htmlc                 S   s   d S r   r   ©r-   r'   r   r   r   Údepart_latex_math_htmlÎ   s    z%setup.<locals>.depart_latex_math_htmlc                 S   sB   t |jtjƒ}|r(| j d|d  ¡ n| j d|d dg¡ d S )Nz$%s$r   z\begin{equation}z\end{equation})rA   rB   r   rC   r]   rL   Úextend)r-   r'   rN   r   r   r   Úvisit_latex_math_latexÒ   s    þz%setup.<locals>.visit_latex_math_latexc                 S   s   d S r   r   r_   r   r   r   Údepart_latex_math_latexÛ   s    z&setup.<locals>.depart_latex_math_latex)Úhtmlr   r:   )r   é   Úmath)Zparallel_read_safeZparallel_write_safe)r   r   Zadd_config_valueÚconnectrX   r   Úadd_noder   Zadd_roler(   Zadd_directiver*   ÚsphinxÚversion_info)r   r^   r`   rb   rc   Úmetadatar   r   r   r   À   s,    	þ

r   )r   r0   r1   )r/   rD   Úpathlibr   Zdocutilsr   Zdocutils.parsers.rstr   r   ri   Zsphinx.errorsr   r   Z
matplotlibr5   r   r	   Zmatplotlib.rcsetupr
   ZGeneralZElementr   r   r(   r$   r*   r9   rT   rX   r   r   r   r   r   Ú<module>   s,   A ÿ

ÿ
&