
    G8c$                     
   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
ej&                  ej(                        Zd Zi g fdZeede_         G d de      ZddZd Zd Zd Zy)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                       e Zd Zy)
latex_mathN)__name__
__module____qualname__     >/usr/lib/python3/dist-packages/matplotlib/sphinxext/mathmpl.pyr   r   P   s    r   r   c                 ^    t        j                  | t        j                  j                        S N)r   choicer
   MathTextParser_font_type_mapping)args    r   fontset_choicer   T   s!    S("9"9"L"LMMr   c                     |j                  d      }||dz   d }t        |      }	||	d<   |j                  dd      |	d<   |j                  dt        j                  j
                  j                        |	d<   |	gg fS )N`   latexfontsetcmfontsize)findr   getsetupappconfigmathmpl_fontsize)
rolerawtexttextlinenoinlineroptionscontentir   nodes
             r   	math_roler2   X   s{    SAAaCOEgDDMkk)T2DO{{:#(99#3#3#D#DFD62:r   r    r"   c                   0    e Zd ZdZdZdZdZdZee	dZ
d Zy)MathDirectivezR
    The ``.. mathmpl::`` directive, as documented in the module's docstring.
    Tr   Fr3   c                 2   dj                  | j                        }t        | j                        }||d<   | j                  j                  dd      |d<   | j                  j                  dt        j                  j                  j                        |d<   |gS )N r   r    r!   r"   )
joinr/   r   
block_textr.   r$   r%   r&   r'   r(   )selfr   r1   s      r   runzMathDirective.runq   s{    %$//*W,,**9d;Y<<++J,1II,<,<,M,MOZvr   N)r   r   r   __doc__has_contentrequired_argumentsoptional_argumentsfinal_argument_whitespacer   r   option_specr;   r   r   r   r5   r5   f   s2     K %,57Kr   r5   c                     t        j                  ||d      5  	 t        j                  d|  d||d      }d d d        |S # t        $ r t        j                  d|         d}Y /w xY w# 1 sw Y   S xY w)N)zmathtext.fontsetz	font.size$png)dpiformatz!Could not render math expression r   )mpl
rc_contextr
   math_to_image	Exceptionr	   warn_external)r   filenamer    r"   rE   depths         r   	latex2pngrN   |   s    	W8L	M 	**E7!hC?E L  	!B5'JKE		 Ls(   A+A#A(%A+'A((A++A5c                    t        | j                  t        j                        }| d   }| d   }| d   }dj	                  t        j                  | | | j                               j                         dd        }t        t        j                  j                  j                  dd      }|j                  dd	       || d
z  }t        ||||      }	g }
t        j                  j                   j"                  D ]w  }| d|j%                  dd       d
}t        |||z  ||dt'        |d d       z         |
j)                  t        j                  j                  j*                   d| d|        y |
r@dt        j                  j                  j*                   d| ddj-                  |
      z   dz   }
|rd}nd}|r|	dk7  r	d|	dz   z  }nd}dt        j                  j                  j*                   d| d|
 | | d	S ) Nr   r    r"   zmath-{}i_imagesmathmplT)parentsexist_okz.png)r"   -._d   r   )r"   rE   z	/mathmpl/ zsrcset="z.png, z, z" r7   zclass="center" r   z)style="position: relative; bottom: -%dpx"r   z
<img src="z.png" z/>)
isinstanceparentr   TextElementrF   hashlibmd5encode	hexdigestr   r%   r&   builderoutdirmkdirrN   r'   mathmpl_srcsetreplacefloatappendimgpathr8   )r1   sourceinliner   r    r"   namedestdirdestrM   srcsetsizerL   clsstyles                  r   
latex2htmlrq      s   U%6%67FME9oGJHuggYxj188:;EEGMOD 599$$++Y	BGMM$M.vT]"DeT7X>EF		  // FV1T\\#s34D9%8+WxE$s),,	.yy  (()8*AdVD	F	F UYY..667yfM))F#$&*+ %1*;uqyI**2239TF CxuUG2' (r   c                     t        | j                  j                        D ]+  \  }}|d   dk(  r	 t        |d d        t        d|d       y # t        $ r t        d|d      w xY w)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'   rc   re   
ValueErrorr   )r&   r'   r0   rn   s       r   _config_initedrv      s    SZZ667 E48s?Id3Bi  >th GD DE EE  I!B4( KH HI IIs   AA(c                 
   | t         _        | j                  ddd       | j                  dg d       	 | j                  dt               d }d	 }d
 }d }| j                  t        ||f||f       | j                  dt               | j                  dt               t        j                  dk  r,| j                  dt               | j                  dt               ddd}|S # t
        $ r | j                  dd        Y w xY w)Nr(   g      $@Trc   zconfig-initedzenv-updatedc                     t        | d       S r   )rv   )r&   envs     r   <lambda>zsetup.<locals>.<lambda>   s    N34M r   c                     | j                   j                  d   }| j                  j                  t	        ||             y )Nrh   )document
attributesbodyrf   rq   )r:   r1   rh   s      r   visit_latex_math_htmlz$setup.<locals>.visit_latex_math_html   s/    ))(3		D&12r   c                      y r   r   r:   r1   s     r   depart_latex_math_htmlz%setup.<locals>.depart_latex_math_html       r   c                     t        |j                  t        j                        }|r"| j                  j                  d|d   z         y | j                  j                  d|d   dg       y )Nz$%s$r   z\begin{equation}z\end{equation})rY   rZ   r   r[   r~   rf   extend)r:   r1   ri   s      r   visit_latex_math_latexz%setup.<locals>.visit_latex_math_latex   sZ    DKK):):;IIVd7m34II1"7m/1 2r   c                      y r   r   r   s     r   depart_latex_math_latexz&setup.<locals>.depart_latex_math_latex   r   r   )htmlr   rQ   )r      math)parallel_read_safeparallel_write_safe)r%   r&   add_config_valueconnectrv   r   add_noder   add_roler2   add_directiver5   sphinxversion_info)r&   r   r   r   r   metadatas         r   r%   r%      s    EI+T48)2t4OO^4
32 LL,.DE.0GH  J LLI&i/V#VY'&-0&*4HHOE  OM#MNOs   C# #DD)r!   
   rW   )r<   r\   pathlibr   docutilsr   docutils.parsers.rstr   r   r   sphinx.errorsr   r   
matplotlibrG   r	   r
   matplotlib.rcsetupr   GeneralElementr   r   r2   r.   r5   rN   rq   rv   r%   r   r   r   <module>r      s   ?B    6  5  % 5	 	N
 "	 !/!79	 I ,#(LE$(r   