B
    c$                 @   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   ;lib/python3.7/site-packages/matplotlib/sphinxext/mathmpl.pyr   P   s   r   c             C   s   t | tjjS )N)r   Z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   F)r   r   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 ytjd|  d||dd}W n( tk
rX   td|   d}Y nX W d 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   r0   depthr   r   r   	latex2png|   s    r6   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 }
xpt
jjjD ]b}| d|dd d
}t||| ||dt|d d  d |
t
jjj d| d|  qW |
r2d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-{}iZ_imagesmathmplT)parentsexist_okz.png)r   -._r.   r   )r   r0   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   TextElementr1   hashlibZmd5encodeZ	hexdigestr   r   r   ZbuilderZoutdirmkdirr6   r   mathmpl_srcsetreplacefloatappendZimgpathr)   )r%   sourceinliner   r   r   nameZdestdirdestr5   Zsrcsetsizer4   clsZstyler   r   r   
latex2html   s4    ( &rN   c          	   C   sv   xpt | jjD ]`\}}|d dkr^yt|d d  W qn tk
rZ   td|dY qnX qtd|dqW 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   rD   rF   
ValueErrorr   )r   r   r$   rL   r   r   r   _config_inited   s    rR   c             C   s   | t _| ddd | dg d y| 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      $@TrD   zconfig-initedzenv-updatedc             S   s
   t | d S )N)rR   )r   envr   r   r   <lambda>       zsetup.<locals>.<lambda>c             S   s"   | j jd }| jt|| d S )NrH   )ZdocumentZ
attributesbodyrG   rN   )r*   r%   rH   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_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})r>   r?   r   r@   rV   rG   extend)r*   r%   rI   r   r   r   visit_latex_math_latex   s    z%setup.<locals>.visit_latex_math_latexc             S   s   d S )Nr   )r*   r%   r   r   r   depart_latex_math_latex   s    z&setup.<locals>.depart_latex_math_latex)Zhtmlr   r7   )r      Zmath)Zparallel_read_safeZparallel_write_safe)r   r   Zadd_config_valueZconnectrR   r   Zadd_noder   Zadd_roler&   Zadd_directiver'   sphinxversion_info)r   rW   rX   rZ   r[   Zmetadatar   r   r   r      s*    	

r   )r   r-   r.   )r,   rA   Zpathlibr   Zdocutilsr   Zdocutils.parsers.rstr   r   r]   Zsphinx.errorsr   r   Z
matplotlibr2   r   r	   Zmatplotlib.rcsetupr
   ZGeneralZElementr   r   r&   r"   r'   r6   rN   rR   r   r   r   r   r   <module>@   s&   	

&