+
    5)i!                         ^ RI t ^ RIt^ RIt^ RIt^ RIHtHtHtHt ^ RI	H
t
 ]P                  ! ]4      t ! R R]]
4      t ! R R4      t]tR# )    N)ListDictTupleLiteral)Enumc                   "    ] tR t^tRtRtRtRtR# )OutputFormatz.Supported output formats for LaTeX conversion.svghtml N)__name__
__module____qualname____firstlineno____doc__SVGHTML__static_attributes__r       ID:\Projects\python\client-ideal-oms_pyapi\app\services\latex_converter.pyr	   r	      s    8
CDr   r	   c                      a  ] tR t^t o RtRtR]P                  R3V 3R lR lltV 3R lR lt	V 3R	 lR
 lt
V 3R lR ltV 3R lR ltV 3R lR ltRV 3R lR lltRtV tR# )LatexConverterzpEfficient LaTeX converter using batched MathJax processing.

Supports both SVG and HTML (CHTML) output formats.
nodez&node_scripts/batch_latex_converter.mjsi  c                ,   < V ^8  d   QhRS[ RS[RS[/# )   script_pathoutput_format
cache_size)strr	   int)format__classdict__s   "r   __annotate__LatexConverter.__annotate__   s)     " "" $" 	"r   c                ,    Wn         W n        RV n        R# )z
Initialize the converter.

Args:
    script_path: Path to the Node.js script
    output_format: Output format - 'svg' or 'html'
    cache_size: Unused in this implementation (kept for compatibility)
N)r   r   _chtml_styles)selfr   r   r   s   &&&&r   __init__LatexConverter.__init__   s     '*!r   c                    < V ^8  d   QhRS[ /# )r   	node_pathr   )r!   r"   s   "r   r#   r$   +   s       r   c                    Wn         V # )z'Use the node path to execute the script)r+   )r'   r+   s   &&r   with_node_pathLatexConverter.with_node_path+   s    "r   c                    < V ^8  d   QhRS[ /# )r   r   )r	   )r!   r"   s   "r   r#   r$   0   s       r   c                    Wn         V # )z#Set the output format (svg or html))r   )r'   r   s   &&r   with_output_format!LatexConverter.with_output_format0   s    *r   c                P   < V ^8  d   QhRS[ RS[S[S[ S[ S[3,          ,          /# )r   html_contentreturn)r   r   r   bool)r!   r"   s   "r   r#   r$   5   s+     # #s #tE#sD.<Q7R #r   c                2   . pRR.pV Ft  w  rE\         P                  ! WA\         P                  4       FF  pVP                  ^ 4      pVP                  ^4      P	                  4       pVP                  WxR34       KH  	  Kv  	  RR.p	V	 Ft  w  rE\         P                  ! WA\         P                  4       FF  pVP                  ^ 4      pVP                  ^4      P	                  4       pVP                  WxR34       KH  	  Kv  	  \        P                  RV 24       V# )zh
Find all LaTeX formulas in HTML content.

Returns:
    List of tuples (placeholder, latex, is_display)
TFz
formulas: )z\$\$(.*?)\$\$z$$)z\\\[(.*?)\\\]z\[\])z#(?<!\$)\$(?!\$)(.*?)(?<!\$)\$(?!\$)$)z\\\((.*?)\\\)z\(\))refinditerDOTALLgroupstripappendloggerdebug)
r'   r5   formulasdisplay_patternspatterndelimmatchplaceholderlatexinline_patternss
   &&        r   find_latex_in_html!LatexConverter.find_latex_in_html5   s      %(

 /NGWBIIF#kk!nA,,.T :; G / :(

 .NGWBIIF#kk!nA,,.U ;< G . 	z(,-r   c                \   < V ^8  d   QhRS[ S[S[S[3,          ,          RS[ S[,          /# )r   rB   r6   )r   r   r   r7   r   )r!   r"   s   "r   r#   r$   Z   s1     ?K ?Kd5d+;&< ?Kd ?Kr   c           
     f   V'       g   . # R. RV P                   P                  RV P                   \        P                  8H  /p\	        V4       F&  w  pw  rEVR,          P                  RVRVRV/4       K(  	   \        P                  ! V P                  V P                  .\        P                  ! V4      RRRR7      p\        P                  ! VP                  4      p\        P                  R	V 24       \!        V\"        4      '       d   TpM'VP%                  R
. 4      pRV9   d   VR,          V n        VP)                  R R7       V#   \        P*                   dj   p	\-        RT	P.                   24       \        P1                  RT	P.                   24       T U
u. uF  p
RRR\3        T	4      /NK  	  Mu up
i up
u Rp	?	# Rp	?	i\        P4                   dV   p	\-        RT	 24       \        P1                  RT	 24       T U
u. uF  p
RRR\3        T	4      /NK  	  Mu up
i up
u Rp	?	# Rp	?	ii ; i)z
Convert multiple LaTeX formulas to SVG or HTML in a single call.

Args:
    formulas: List of tuples (latex_string, is_display)

Returns:
    List of dictionaries with 'svg'/'html' and 'output' or 'error' keys
rB   r!   includeStylesidrH   displayT)inputcapture_outputtextcheckzConverted formulas response: resultsstylesc                     V R ,          # )rO   r   )xs   &r   <lambda>.LatexConverter.convert_batch.<locals>.<lambda>   s    qwr   )keyzError running Node.js script: successFerrorNzError parsing output: )r   valuer	   r   	enumerater?   
subprocessrunr+   r   jsondumpsloadsstdoutr@   rA   
isinstancelistgetr&   sortCalledProcessErrorprintstderrr]   r   JSONDecodeError)r'   rB   
input_datairH   rP   resultresponseoutputse_s   &&         r   convert_batchLatexConverter.convert_batchZ   s    I d((..T//<3D3DD

 $-X#6Az"))a7+  $7#	K^^!1!12jj,#F zz&--0HLL8
CD (D))"",,y"5x')1();D& LL.L/N,, 	K2188*=>LL9!((DEAIJAYwA7JJ## 	K*1#./LL1!56AIJAYwA7JJ	KsO   8CE
 
H0>GF65G=H0H0H0*H+HH+%H0+H0c                    < V ^8  d   QhRS[ /# )r   r6   r,   )r!   r"   s   "r   r#   r$      s     ( (# (r   c                .    V P                   ;'       g    R# )zw
Get the CHTML styles required for HTML output.

Returns:
    CSS styles as a string, or empty string if not available
 )r&   )r'   s   &r   get_chtml_stylesLatexConverter.get_chtml_styles   s     !!''R'r   c                ,   < V ^8  d   QhRS[ RS[RS[ /# )r   r5   	use_cacher6   )r   r7   )r!   r"   s   "r   r#   r$      s'     F F F F Fr   c                   V P                  V4      pV'       g   V# / p. pV F&  w  rgpWx3p	W9  g   K  RWI&   VP                  V	4       K(  	  V'       d   V P                  V4      p
\        WZ4       Fo  w  w  rxpVR,          '       dK   VP	                  R4      ;'       g+    VP	                  R4      ;'       g    VP	                  R4      pWWx3&   Ka  \        RV 24       Kq  	  Tp/ pV F=  w  rpVP	                  Wx34      pV'       g   K"  TpV'       d   RMRpR	V R
V R2pVW&   K?  	  \        VP                  4       \        RR7      pV F  pVP                  WV,          4      pK  	  V P                  \        P                  8X  d]   V P                  '       dK   RV P                   R2pRV9   d   VP                  RV R24      pV# RV9   d   VP                  RRV 24      pV# )a  
Process HTML content and replace all LaTeX formulas with SVGs or HTML.

Args:
    html_content: HTML content with LaTeX formulas
    use_cache: Unused in this implementation (kept for compatibility)

Returns:
    HTML content with LaTeX replaced by SVG or HTML
Nr\   outputr
   r   zFailed to convert: zmath-displayzmath-inlinez<span class="z">z</span>T)r[   reversez<style id="mjx-styles">z</style>z</head>z<head>)rJ   r?   ru   ziprh   rk   sortedkeyslenreplacer   r	   r   r&   )r'   r5   r}   formula_dataunique_formulas_mapunique_formulas_listrt   rH   rP   r[   rU   rp   r   processed_htmlreplacementsrG   
is_displaywrapper_classwrapped_outputsorted_replacements
styles_tags   &&&                  r   process_htmlLatexConverter.process_html   s    ..|< !!!-Ag"C-+/#($++C0	 ".  (()=>G,/0D,N( &)$$#ZZ1\\VZZ5F\\&**U[J\F<B(89/w78 -O & +7'K(,,e-=>Fv$
2<-#0r&!Q,:) ,8 %\%6%6%8c4P.K+33KkAZ[N / !2!22t7I7I7I243E3E2FhOJN*!/!7!7	j\QXCY!Z  ^+!/!7!7F:,BW!Xr   )r&   r+   r   r   N)T)r   r   r   r   r   r+   r	   r   r(   r.   r2   rJ   ru   rz   r   r   __classdictcell__)r"   s   @r   r   r      su     
 I D&2&6&6	" "$ 
 
# #J?K ?KB( (F F Fr   r   )rb   r`   r:   loggingtypingr   r   r   r   enumr   	getLoggerr   r@   r   r	   r   LatexToSVGConverterr   r   r   <module>r      sM      	  - - 			8	$3 Y Yz % r   