o
    ip$                  
   @  s  d Z ddlmZ 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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.
    )annotationsreStructuredTextN)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   k/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/docutils/utils/math/tex2mathml_extern.py_check_result#   s   

r   Freturnstrc                 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   4   s&   




r   c           	        s  g d}t t| | } d tj|| ddd}|jr)d fdd|j D |_t| g d}tj||jddd}|j	d	}|d
kr[|j	d|d }|j|| 
dd|_n|j|j	d|j	dd  |_|jrt|j}n|j}d f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--)zError:zWarning:zFatal:Tr   
c                 3      | ]
}|  r|V  qd S N
startswith.0line
error_tagsr   r   	<genexpr>s       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                 3  r0   r1   r2   r4   r7   r   r   r9      r:   )document_templater   r%   r&   r   join
splitlinesr   r   r'   r$   )	r(   r)   args1result1args2result2startstop_msg_sourcer   r7   r   r,   X   s8    
r,   c              
   C  s   g d}t j|t| |ddd}|j|jd|jdd  |_g }|jrR|j }|td|d t	dd	
|d
d tjdd	
|dd dgdg7 }t||d |jS )zQConvert LaTeX math code to MathML with pandoc__.

    __ https://pandoc.org/
    )pandocr   z--from=latexTr   r?   r#   r@   r   r   r/         Nr   r	   r   )r%   r&   r   r   r'   r   rC   r   r   literal_blockrB   r   )r(   r)   r   r   r   linesr   r   r   rK      s$   	
rK   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!   encodingNr?   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   r2   r4   r   r   r   r9      s
    zttm.<locals>.<genexpr>)r   r%   r&   UnicodeEncodeErrorr   r   r'   r$   rB   r   rC   r   )r(   r)   r   r   errr   r   r   rP      s.   



rP   __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)r   r   )__doc__
__future__r   __docformat__r%   docutilsr   docutils.utils.mathr   r   rA   r   r   r,   rK   rP   __name__exampleprintr$   buggyrT   r   nodeastextr   r   r   r   <module>   sR   
$
B
!#	

