
    Kip$              	         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Zd"d#d	Zd"d
Zd"dZd"dZedk(  rdZ ed        ed ee       d        ed eej'                  dd             d        ed ee       d        ed ee       d        ed        ed        e eed              ed        e eej'                  dd      d              ed        e eed              ed        e eed              ed       d Z	  e ed!e d!             yy# e$ rKZ ee        eej,                         ej,                  D ]  Z eej1                                 Y dZ[ydZ[ww xY w)$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                N   d}|s0| j                   r$t        j                  d| j                   dg      g}|rd| j                  d    d}nL| j                  r!d| j                  d    d| j                   d	}n| j
                  sd| j                  d    d
}|rt        ||      y )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   msgs      q/home/jay/workspace/scripts/.codegraph-venv/lib/python3.12/site-packages/docutils/utils/math/tex2mathml_extern.py_check_resultr   #   s     Cv}}??2v}}zlKL)&++a.)9C			*6;;q>*: ;$$*$5$5#6a9]])&++a.)99NO
W--     c                
   g d}|rdnd}t        | |      }d|v r|j                  dd      } t        j                  || dd      }d|j                  v rJ|j                  |j                  j                  d	      d
z   |j                  j                  d       |_        nI|j                  |j                  j                  d      d
z   |j                  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   s         r   r   r   4   s    
D )1$bKi2HX$$Z=	^^D	+/d<F FMM!fmm&8&8&Ea&G&,mm&8&8&FH fmm&8&8&DQ&F&,mm&8&8&EG&>{m Lw( )r   c                ~  	 g d}t         t        | |      z  } d	t        j                  || dd      }|j                  r7dj                  	fd|j                  j                         D              |_        t        |       g d}t        j                  ||j                  dd      }|j                  j                  d      }|d	k7  rD|j                  j                  d
|      dz   }|j                  || j                  dd      |_        nI|j                  |j                  j                  d      |j                  j                  d      dz    |_        |j                  r|j                  }n|j                  }dj                  	fd|j                         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  D   K   | ]  }|j                        r|  y wN
startswith.0line
error_tagss     r   	<genexpr>zlatexml.<locals>.<genexpr>s   s&      #CD&*ooj&A $( #C    )latexmlpostr/   z--nonumbersectionsz--format=html5z--omitdoctypez--noscanz--nocrossrefz--nographicimagesz--nopictureimagesz--nodefaultresourcesr0   z<table class="ltx_equationgroupz</table>   ltx_equationgroupzborderless align-center<mathr$      c              3  D   K   | ]  }|j                        r|  y wr3   r4   r6   s     r   r:   zlatexml.<locals>.<genexpr>   s$      ?"&//*"=  $ ?r;   )document_templater   r&   r'   r   join
splitlinesr   r   r(   r%   )
r)   r*   args1result1args2result2startstop_msg_sourcer9   s
            @r   r.   r.   X   s   E "N9h$GGI1JnnU),0t=G~~ #CGNN4M4M4O #C C'E nnU'..,0t=G NN ABE{~~"":u5a7 d3;;!:< !(;(;G(D(/(;(;I(Fq(HJ ~~nnnnYY ?0F0F0H ? ?GN'>>r   c                @   g d}t        j                  |t        | |      dd      }|j                  |j                  j	                  d      |j                  j	                  d      dz    |_        g }|j
                  r|j
                  j                         }|t        j                  d|d         t        j                  dd	j                  |d
d             t        j                  dd	j                  |dd       dg      gz  }t        ||       |j                  S )zQConvert LaTeX math code to MathML with pandoc__.

    __ https://pandoc.org/
    )pandocr   z--from=latexTr   r@   r$   rA   r	   r   r1         Nr
   r   r   )r&   r'   r   r   r(   r   rE   r   r   literal_blockrD   r   )r)   r*   r   r   r   liness         r   rN   rN      s    
D ^^Dy((K+/d<F MM&--"4"4W"="(--"4"4Y"?"ACFM G}}((*EOOBa1''DIIeAaj,ABOOB		%)(<-7L: 	
 &'*==r   c                    g d}t        | |      } 	 t        j                  || ddd      }|j
                  |j
                  j                  d      |j
                  j                  d      dz    |_        |r!|j
                  j                  d	d
      |_        dj                  d |j                  j                         D              |_	        t        |       |j
                  S # t        $ r}t	        |      d}~ww xY w)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$   rA   z<math xmlns=z<math display="block" xmlns=r1   c              3  N   K   | ]  }|j                  d       r
|dd dz     yw)z****    Nr   r4   )r7   r8   s     r   r:   zttm.<locals>.<genexpr>   s.      ;"&!%!9 #12hn ;s   #%)r   r&   r'   UnicodeEncodeErrorr   r   r(   r%   rD   r   rE   r   )r)   r*   r   r   errs        r   rT   rT      s    D y(3II/3$)57 MM&--"4"4W"="(--"4"4Y"?"ACFM--n.LNII ;*0--*B*B*D; ;FM &==  ns   C$ $	C=-C88C=__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)returnstr)__doc__
__future__r   __docformat__r&   docutilsr   docutils.utils.mathr   r   rC   r   r   r.   rN   rT   __name__exampleprintr%   buggyrY   r   nodeastext r   r   <module>rm      s   #"   9  #% ."!)H?DBF z6G 
   
Igg&'q
)*	E#gooh9:;1
=>	K	'*+1
-.	HVG_%Q
'(	&M	
	''D
)*	-	#gooh1D
AB	
	)Gd
+,	
	&4
()	
%&E	! 	fqq\"#G J  !c
ckkKK 	!D$++- 	!!s   D3 3F8AE>>F