o
    Ni$                  
   @   s  d Z ddlZddlmZ ddlmZmZ dZg fddZd&d	d
Z	d&ddZ
d&ddZd&ddZedkrdZed ede
e d edeedd d ede	e d edee d ed ed ee
edd ed  eeedddd ed! ee	edd ed" eeedd ed# d$Zzeed%e d% W dS  ey Z zee eej ejD ]Zee  qW Y dZ[dS dZ[ww dS )'zWrappers for TeX->MathML conversion by external tools

This module is provisional:
the API is not settled and may change with any minor Docutils version.
    N)nodes)	MathErrorwrap_math_codez;\documentclass{article}
\begin{document}
%s
\end{document}
c                 C   s   d}|s| j rtjd| j dgdg}|rd| jd  d}n| jr/d| jd  d| j d	}n| js;d| jd  d
}|rCt||dd S )N pre-wrapclasseszTeX to MathML converter `r   z	` failed:zTeX to MathMl converter `z` exited with Errno .z` returned no MathML.details)stderrr   	paragraphargs
returncodestdoutr   )resultr   msg r   Y/home/ubuntu/.local/lib/python3.10/site-packages/docutils/utils/math/tex2mathml_extern.py_check_result   s   

r   Fc                 C   s   g d}|rdnd}t | |}d|v r|dd} tj|| ddd}d|jv r:|j|jd	d
 |jd |_n|j|jdd
 |jd |_t| d| d|j dS )zbConvert LaTeX math code to MathML with blahtexml__.

    __ http://gva.noekeon.org/blahtexml/
    )	blahtexml--mathmlz
--indentedz	--spacingmoderatez--mathml-encodingrawz--other-encodingr   z--doctype-xhtml+mathmlz--annotate-TeXz display="block"r   z{align*}z	{aligned}Tinputcapture_outputtextz<error>z	<message>	   z
</message>z<markup>z	</markup>z0<math xmlns="http://www.w3.org/1998/Math/MathML"z>
</math>)r   replace
subprocessrunr   findr   r   )	math_codeas_blockr   mathml_args_wrappedr   r   r   r   r   0   s&   




r   c           	      C   s  g d}t t| | } tj|| ddd}|jr%ddd |j D |_t| g d}tj||jddd}|j	d}|d	krW|j	d
|d }|j|| 
dd|_n|j|j	d|j	dd  |_|jrp|j}n|j}ddd | D |_t| |jS )zConvert LaTeX math code to MathML with LaTeXML__.

    Comprehensive macro support but **very** slow.

    __ http://dlmf.nist.gov/LaTeXML/
    )latexml-z--preload=amsmathz--preload=amssymbz--inputencoding=utf8--Tr   
c                 s   2    | ]}| d s| ds| dr|V  qdS zError:zWarning:zFatal:N
startswith.0liner   r   r   	<genexpr>n       zlatexml.<locals>.<genexpr>)latexmlpostr)   z--nonumbersectionsz--format=html5z--omitdoctypez--noscanz--nocrossrefz--nographicimagesz--nopictureimagesz--nodefaultresourcesr*   z<table class="ltx_equationgroupz</table>   ltx_equationgroupzborderless align-center<mathr      c                 s   r,   r-   r.   r0   r   r   r   r3      r4   )document_templater   r!   r"   r   join
splitlinesr   r   r#   r    )	r$   r%   args1result1args2result2startstop_msg_sourcer   r   r   r(   T   s6   
r(   c              
   C   s   g d}t j|t| |ddd}|j|jd|jdd  |_g }|jrZ|j }|t	d|d  |t
dd	|d
d  |tj	dd	|dd dgd t||d |jS )zQConvert LaTeX math code to MathML with pandoc__.

    __ https://pandoc.org/
    )pandocr   z--from=latexTr   r9   r   r:   r   r   r+         Nr   r   r
   )r!   r"   r   r   r#   r   r=   appendr   r   literal_blockr<   r   )r$   r%   r   r   r   linesr   r   r   rE      s"   	
 rE   c              
   C   s   g d}t | |} ztj|| dddd}W n ty% } zt|d}~ww |j|jd|jdd  |_|rB|jd	d
|_ddd |j	
 D |_	t| |jS )z{Convert LaTeX math code to MathML with TtM__.

    Aged, limited, but fast.

    __ http://silas.psfc.mit.edu/tth/mml/
    )ttmz-Lz-rTz
ISO-8859-1)r   r   r   encodingNr9   r   r:   z<math xmlns=z<math display="block" xmlns=r+   c                 s   s*    | ]}| d r|dd d V  qdS )z****    Nr	   r.   r0   r   r   r   r3      s
    zttm.<locals>.<genexpr>)r   r!   r"   UnicodeEncodeErrorr   r   r#   r    r<   r   r=   r   )r$   r%   r   r   errr   r   r   rK      s.   



rK   __main__uR   \frac{\partial \sin^2(\alpha)}{\partial \vec r}\varpi \mathbb{R} \, \text{Grüße}z<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>test external mathml converters</title>
</head>
<body>
<p>Test external converters</p>
<p>
z	latexml: ,zttm: mathbbmathbfzblahtexml: zpandoc: r	   z</p>z<p>latexml:</p>T)r%   z<p>ttm:</p>z<p>blahtexml:</p>z<p>pandoc:</p>z</main>
</body>
</html>z
\sinc \phy$)F)__doc__r!   docutilsr   docutils.utils.mathr   r   r;   r   r   r(   rE   rK   __name__exampleprintr    buggyrO   r   nodeastextr   r   r   r   <module>   sN   

$
E
 #	

