B
    .Kc                 @   s   d Z ddlZddlmZmZmZ ddlmZ ddlZddl	m
Z
 ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ dZejjeZeejddddZeejddddZe
eeeeef eddddZ e
eeef dddZ!dS )zAllow `MathJax`_ to be used to display math in Sphinx's HTML writer.

This requires the MathJax JavaScript library on your webserver/computer.

.. _MathJax: https://www.mathjax.org/
    N)AnyDictcast)nodes)Sphinx)
MathDomain)ExtensionError)_)get_node_equation_number)HTMLTranslatorz;https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js)selfnodereturnc             C   sZ   | j | j|dddd | j | jjjd | |  | jjjd  d  tj	d S )Nspan zmath notranslate nohighlight)CLASSr      z</span>)
bodyappendstarttagbuilderconfigmathjax_inlineencodeastextr   SkipNode)r   r    r   1lib/python3.7/site-packages/sphinx/ext/mathjax.pyhtml_visit_math   s    0r   c             C   sz  | j | j|ddd |d rH| j | |  | j d tj|d rt| |}| j d|  | |t	d | j d	 | j | j
jjd
  dd | dD }t|dkr| j d xft|D ]Z\}}| |}d|kr| j d| d  n| j | |t|d k r| j d qW t|dkrN| j d | j | j
jjd  | j d tjd S )NZdivzmath notranslate nohighlight)r   Znowrapz</div>numberz<span class="eqno">(%s)zPermalink to this equationz</span>r   c             S   s   g | ]}|  r|qS r   )strip).0Zprtr   r   r   
<listcomp>2   s    z*html_visit_displaymath.<locals>.<listcomp>z

r   z \begin{align}\begin{aligned}z\\z\begin{split}z\end{split}z\end{aligned}\end{align} z</div>
)r   r   r   r   r   r   r   r
   Zadd_permalink_refr	   r   r   mathjax_displaysplitlen	enumerate)r   r   r   partsipartr   r   r   html_visit_displaymath$   s4    


r*   )apppagenametemplatenamecontext	event_argr   c             C   s  | j jdks| j jdkrd S | jjs,tdtt| j	d}| j
jdksV||r| jjr| jjtkrttd dt| jj }| jd d|d	 | jjrd
t| jj }| jd |d i }| jjr|| jj d|krd|kr| jjrd|d< nd|d< | j| jjf| d S )NhtmlmathjaxzGmathjax_path config value must be set for the mathjax extension to workmathalwayszimathjax_config/mathjax2_config does not work for the current MathJax version, use mathjax3_config insteadzMathJax.Hub.Config(%s)ztext/x-mathjax-config)typer   zwindow.MathJax = %s)r   asyncZdefer)r   formatZmath_renderer_namer   mathjax_pathr   r   r   envZ
get_domainregistryZhtml_assets_policyZhas_equationsmathjax2_configMATHJAX_URLloggerZwarningjsondumpsZadd_js_filemathjax3_configmathjax_optionsupdate)r+   r,   r-   r.   r/   domainr   optionsr   r   r   install_mathjaxD   s.    
rD   )r+   r   c             C   s   |  dtd ftd f | dtd | di d | dddgd | dd	d
gd | dd d | ddd d | dd d | dt tjddS )Nr1   r7   r0   r@   r   z\(z\)r#   z\[z\]mathjax_configr:   c             S   s   | j S )N)rE   )cr   r   r   <lambda>q       zsetup.<locals>.<lambda>r?   zhtml-page-contextT)versionZparallel_read_safe)	Zadd_html_math_rendererr   r*   Zadd_config_valuer;   ZconnectrD   sphinxZ__display_version__)r+   r   r   r   setupg   s    
rK   )"__doc__r=   typingr   r   r   Zdocutilsr   rJ   Zsphinx.applicationr   Zsphinx.domains.mathr   Zsphinx.errorsr   Zsphinx.localer	   Zsphinx.util.mathr
   Zsphinx.writers.htmlr   r;   utilZloggingZ	getLogger__name__r<   r2   r   Z
math_blockr*   strrD   rK   r   r   r   r   <module>   s"    "