
    O&iV9                        d Z ddlZddlZddlZddlZddlZddlmZ ddlmZm	Z	 ddl
mZmZmZmZmZ ddl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m Z  ddl!m"Z"m#Z# ddl$m%Z%m&Z& ddl'm(Z( ddl)m*Z*m+Z+ ddl,m-Z- ddl.m/Z/  e"j0        e1          Z2 ej3        edd          Z4dZ5 G d de          Z6 G d de          Z7dZ8 ej9        d          Z: ej9        d          Z; ej9        d          Z<de=d ee>         fd!Z?de=d"e>d dfd#Z@	 dAd%e=d&e=d'ed(e=d e=f
d)ZAd*ed e=fd+ZBd,e=d*ed e=fd-ZCd.ee=         d/e=d ee=e=f         fd0ZDd1e=d*ed2e=d ee>         fd3ZEd1e=d*ed2e=d ee>         fd4ZFd5e/d&e=d eee=         ee>         f         fd6ZGd%e=d7ee=         d e=fd8ZHd9ed:eId dfd;ZJd5e/d<ed e=fd=ZKd5e/d<ejL        d dfd>ZMd5e/d<ejN        d dfd?ZOd9ed ee=ef         fd@ZPdS )Bz*Render math in HTML via dvipng or dvisvgm.    N)path)PIPECalledProcessError)AnyDictListOptionalTuple)nodes)Element)package_dir)Sphinx)Builder)Config)SphinxError)___)loggingsha1)get_node_equation_numberwrap_displaymath)	ensuredir)read_png_depthwrite_png_depth)LaTeXRenderer)HTMLTranslator	templatesimgmath c            	       P     e Zd ZdZ	 ddedee         dee         ddf fdZ xZS )	MathExtErrorzMath extension errorNmsgstderrstdoutreturnc                 r    |r|d|z   z  }|r|d|z   z  }t                                          |           d S )Nz

[stderr]
z

[stdout]
)super__init__)selfr"   r#   r$   	__class__s       _/home/geonatureadmin/si_en_reseau/tutos/venv/lib/python3.11/site-packages/sphinx/ext/imgmath.pyr(   zMathExtError.__init__'   sQ      	+>F**C 	+>F**C    NN)__name__
__module____qualname__categorystrr	   r(   __classcell__)r*   s   @r+   r!   r!   $   sv        %H OS  (>Fsm	         r,   r!   c                       e Zd ZdZdS )InvokeErrorzerrors on invoking converters.N)r.   r/   r0   __doc__r   r,   r+   r5   r5   1   s        ((((r,   r5   )pngsvgz\[\d+ depth=(-?\d+)\]z.*, depth=(.*)ptz<!-- DEPTH=(-?\d+) -->filenamer%   c                     t          | d          5 }|D ]}t                              |          }|r.t          |                    d                    cddd           S 	 ddd           dS # 1 swxY w Y   dS )z9Read the depth from comment at last line of SVG file
    utf-8encoding   N)opendepthsvgcomment_rematchintgroup)r9   flinematcheds       r+   read_svg_depthrG   <   s     
h	)	)	) Q 	 	D$**400 	)w}}Q''((                         s   AA0"A00A47A4depthc                     t          | dd          5 }|                    d|z             ddd           dS # 1 swxY w Y   dS )z<Write the depth to SVG file as a comment at end of file
    ar;   r<   z
<!-- DEPTH=%s -->N)r?   write)r9   rH   rD   s      r+   write_svg_depthrL   I   s     
hg	.	.	. /!	%-.../ / / / / / / / / / / / / / / / / /s   9= = image_formatmathconfigconfdirc                    |j         t          t          |j         dz                      |j        | dk    rdnd|d}|j        rd}nd}|j        D ]P}t          j        |||          }t          j        |          r$t                      
                    ||          c S Qt          t
                    
                    ||          S )zGenerate LaTeX macro.g333333?r7   rM   z
,tightpage)fontsizebaselineskippreamble	tightpagerO   zpreview.tex_tztemplate.tex_t)imgmath_font_sizerB   roundimgmath_latex_preambleimgmath_use_previewtemplates_pathr   joinexistsr   render)rN   rO   rP   rQ   	variablestemplate_nametemplate_dirtemplates           r+   generate_latex_macrorc   P   s     ,E&":S"@AABB1'500RRl I ! )'(- ? ?9WlMBB;x   	? ??))(I>>>>>	? ((//yIIIr,   builderc                 `    t          | d          st          j                    | _        | j        S )zCreate temporary directory.

    use only one tempdir per build -- the use of a directory is cleaner
    than using temporary files, since we can clean up everything at once
    just removing the whole directory (see cleanup_tempdir)
    _imgmath_tempdir)hasattrtempfilemkdtemprf   )rd   s    r+   ensure_tempdirrj   h   s1     7.// 6#+#3#5#5 ##r,   latexc                    t          |          }t          j        |d          }t          |dd          5 }|                    |            ddd           n# 1 swxY w Y   |j        j        dg}|                    |j        j                   |	                    d           	 t          j        |t          t          |dd	           t          j        |d
          S # t          $ r?}t                              t!          d          |j        j                   t"          |d}~wt$          $ r!}t'          d|j        |j                  |d}~ww xY w)z%Compile LaTeX macros for math to DVI.zmath.texwr;   r<   Nz--interaction=nonstopmodeTascii)r$   r#   cwdcheckr=   zmath.dvizYLaTeX command %r cannot be run (needed for math display), check the imgmath_latex settingzlatex exited with error)rj   r   r\   r?   rK   rP   imgmath_latexextendimgmath_latex_argsappend
subprocessrunr   OSErrorloggerwarningr   r5   r   r!   r#   r$   )rk   rd   tempdirr9   rD   commandexcs          r+   compile_mathr}   u   s   W%%Gy*--H	hg	.	.	. !	               ~+-HIGNN7>4555NN:
WwtDgT '	) 	) 	) 	)y*--- # # #r F G G~3	5 	5 	5 s" W W W4cj#*MMSVVWs5   AA A'8C   
E*:D$$E1EEr{   namec                 R   	 t          j        | t          t          dd          }|j        |j        fS # t
          $ r=}t                              t          d          || d         |           t          |d}~wt          $ r$}t          d|z  |j        |j                  |d}~ww xY w)z*Convert DVI file to specific image format.Trn   )r$   r#   rp   r=   zS%s command %r cannot be run (needed for math display), check the imgmath_%s settingr   Nz%s exited with error)ru   rv   r   r$   r#   rw   rx   ry   r   r5   r   r!   )r{   r~   retr|   s       r+   convert_dvi_to_imager      s    	[nWT$dU\]]]z3:%% # # #r C D DWQZ	/ 	/ 	/ s" [ [ [1D8#*cjQQWZZ[s    03 
B&8A55B&B!!B&dvipathout_pathc                    d}|j         j        d|dddg}|                    |j         j                   |j         j        r|                    d           |                    |            t          ||          \  }}d}|j         j        rg|                                D ]R}t          	                    |          }	|	r4t          |	                    d                    }t          ||            nS|S )	zConvert DVI file to PNG image.dvipng-oz-Ttightz-z9z--depthNr>   )rP   imgmath_dvipngrr   imgmath_dvipng_argsrZ   rt   r   
splitlinesdepth_rerA   rB   rC   r   
r   rd   r   r~   r{   r$   r#   rH   rE   rF   s
             r+   convert_dvi_to_pngr      s    D~,dHdGUSGNN7>5666~) "y!!!NN7)'488NFFE~) %%'' 	 	DnnT**G GMM!,,--%000
 Lr,   c                    d}|j         j        d|g}|                    |j         j                   |                    |            t          ||          \  }}d}|j         j        rz|                                D ]e}t          	                    |          }	|	rGt          t          |	                    d                    dz  dz            }t          ||            nf|S )zConvert DVI file to SVG image.dvisvgmr   Nr>   d   gzGR@)rP   imgmath_dvisvgmrr   imgmath_dvisvgm_argsrt   r   rZ   r   depthsvg_rerA   rX   floatrC   rL   r   s
             r+   convert_dvi_to_svgr      s    D~-tX>GNN7>6777NN7)'488NFFE~) %%'' 	 	D!''--G eGMM!$4$455;eCDD%000
 Lr,   r)   c                    | j         j        j                                        }|t          vrt          d          t          ||| j         j        | j         j                  }t          |	                                          
                                 d| }t          j        | j         j        | j         j        d|          }t          t          j        |                     t          j        |          r/|dk    rt%          |          }n|dk    rt'          |          }||fS t)          | j         d          st)          | j         d          rdS 	 t+          || j                   }n# t,          $ r d	| j         _        Y dS w xY w	 |dk    rt1          || j         |          }n|dk    rt3          || j         |          }n# t,          $ r d	| j         _        Y dS w xY w||fS )
a  Render the LaTeX math expression *math* using latex and dvipng or
    dvisvgm.

    Return the filename relative to the built document and the "depth",
    that is, the distance of image bottom and baseline in pixels, if the
    option to use preview_latex is switched on.
    Also return the temporary and destination files.

    Error handling may seem strange, but follows a pattern: if LaTeX or dvipng
    (dvisvgm) aren't available, only a warning is generated (since that enables
    people on machines without these programs to at least build the rest of the
    docs successfully).  If the programs are there, however, they may not fail
    since that indicates a problem in the math source.
    2imgmath_image_format must be either "png" or "svg".rO   r7   r8   _imgmath_warned_latex _imgmath_warned_image_translatorr-   T)rd   rP   imgmath_image_formatlowerSUPPORT_FORMATr!   rc   rQ   r   encode	hexdigestr   r\   outdirimagedirr   dirnameisfiler   rG   rg   r}   r5   r   r   r   r   )r)   rO   rN   rk   r9   generated_pathrH   r   s           r+   render_mathr      s   $ <&;AACCL>))OPPP !%!%!4!%!57 7E
 u||~~&&0022CC\CCHYt|2DL4I6S[\\Ndl>**+++{>"" %5  ">22EEU""">22Eu$$ t|455 t|?@@zudl33   -1*zz
5  &wnMMEEU""&wnMME   8<5zz 5  s$   E, ,FF
9G GGr   c                    t          |d          5 }t          j        |                                                              d          }d d d            n# 1 swxY w Y   | dk    rd| S | dk    rd| S t          d          )	Nrbr;   r<   r7   zdata:image/png;base64,r8   zdata:image/svg+xml;base64,r   )r?   base64	b64encodereaddecoder!   )rN   r   rD   encodeds       r+   render_maths_to_base64r   	  s    	nd	#	# Fq"16688,,33W3EEF F F F F F F F F F F F F F Fu1111u5G555
K
L
LLs   ;AAAappr|   c                 Z   |rd S t          | j        d          r0	 t          j        | j        j                   n# t
          $ r Y nw xY w| j        j        j        rP	 t          j        t          j	        | j        j
        | j        j        d                     d S # t
          $ r Y d S w xY wd S )Nrf   rO   )rg   rd   shutilrmtreerf   	ExceptionrP   imgmath_embedr   r\   r   r   )r   r|   s     r+   clean_up_filesr     s    
 s{.// 	M#+67777 	 	 	D	 {' 	M$)CK$68LfUUVVVVV 	 	 	DD	 s!   : 
AA<B 
B('B(nodec                     | j         j        j        r<d|                     |                                                                          z  S dS )Nz	 alt="%s"rM   )rd   rP   imgmath_add_tooltipsr   astextstrip)r)   r   s     r+   get_tooltipr   &  sC    |/ @T[[77==????2r,   c           	         	 t          | d|                                z   dz             \  }}n# t          $ r}t          |          }t	          j        |ddg |                                          }|                    |            t                              t          d          |                                |           t          j
        |d }~ww xY w|U| j                            d|                     |                                                                          z             n| j        j        j        r4| j        j        j                                        }t)          ||          }n?t+          j        || j        j                  }	|	                    t*          j        d          }d| d	t5          | |          z   }
||
d
| ddz  }
| j                            |
dz              t          j
        )N$WARNING   typelevelbackrefssourcezdisplay latex %r: %sz<span class="math">%s</span>/z<img class="math" src=""z style="vertical-align: dzpx"z/>)r   r   r!   r2   r   system_message	walkaboutrx   ry   r   SkipNodebodyrt   r   r   rd   rP   r   r   r   r   r   relpathr   replacesepr   )r)   r   rendered_pathrH   r|   r"   smrN   img_srcrelative_pathcs              r+   html_visit_mathr   ,  s   &*4t{{}}1Ds1JKKuu & & &#hh!#IQ+-dkkmmE E E
Tr0114;;==#FFFn#%& 	7T[[]]3399;;< 	= 	= 	= 	= <, 	;<.CIIKKL,\=IIGG L8KLLM#++DHc::G0g000;tT3J3JJ9UF99999A	T"""
.s   +. 
CBCCc           	         |d         r|                                 }n#t          |                                 d d          }	 t          | |          \  }}n# t          $ r}t	          |          }t          j        |ddg |                                           }|                    |            t          	                    t          d          |                                 |           t
          j        |d }~ww xY w| j                            |                     |dd	                     | j                            d
           |d         rjt          | |          }| j                            d|z             |                     |t#          d                     | j                            d           |U| j                            d|                     |                                                                           z             n| j        j        j        r4| j        j        j                                        }	t3          |	|          }
n?t5          j        || j        j                  }|                    t4          j        d          }
| j                            d|
 dt?          | |          z   dz              t
          j        )NnowrapFr   r   r   zinline latex %r: %sdivrO   )CLASSz<p>numberz<span class="eqno">(%s)zPermalink to this equationz</span>z'<span class="math">%s</span></p>
</div>r   z
<img src="r   z/></p>
</div>) r   r   r   r!   r2   r   r   r   rx   ry   r   r   r   rt   starttagr   add_permalink_refr   r   r   rd   rP   r   r   r   r   r   r   r   r   r   r   )r)   r   rk   r   rH   r|   r"   r   r   rN   r   r   s               r+   html_visit_displaymathr   I  s   H~ = e<<&*477uu & & &#hh!#IQ+-dkkmmE E E
Tr/00$++--EEEn#%& 	IT]]4f]==>>>IUH~ $)$55	2V;<<<tQ'C%D%DEEE	###	CT[[]]3399;;< 	= 	= 	= 	= <, 	;<.CIIKKL,\=IIGG L8KLLM#++DHc::G	0g000;tT3J3JJ)* 	+ 	+ 	+
.s   A 
C< BC77C<c                    |                      dt          d ft          d f           |                     ddd           |                     ddd           |                     ddd           |                     d	d
d           |                     ddd           |                     dg dd           |                     ddgd           |                     dg d           |                     ddd           |                     ddd           |                     ddd           |                     dddt          g           |                     dt                     t          j        ddS )Nr   r   r7   htmlr   r   r   r   rq   rk   rZ   Fr   )z-gammaz1.5z-D110z-bgTransparentr   z
--no-fontsrs   rY   rM   r   TrW      r   zbuild-finished)versionparallel_read_safe)	add_html_math_rendererr   r   add_config_valueboolconnectr   sphinx__display_version__)r   s    r+   setupr   o  s   y /6 6=? ? ? /???)8V<<<*Iv>>>'6:::.v>>>.MMM! ! ! /,HHH-r6:::12v>>>/v>>>,b&999%$@@@KK .1111NNNr,   )rM   )Qr6   r   rer   ru   rh   osr   r   r   typingr   r   r   r	   r
   docutilsr   docutils.nodesr   r   r   sphinx.applicationr   sphinx.buildersr   sphinx.configr   sphinx.errorsr   sphinx.localer   r   sphinx.utilr   r   sphinx.util.mathr   r   sphinx.util.osutilr   sphinx.util.pngr   r   sphinx.util.templater   sphinx.writers.htmlr   	getLoggerr.   rx   r\   r[   __all__r!   r5   r   compiler   r   r@   r2   rB   rG   rL   rc   rj   r}   r   r   r   r   r   r   r   r   rO   r   
math_blockr   r   r   r,   r+   <module>r      s^   0 0  				             / / / / / / / / 3 3 3 3 3 3 3 3 3 3 3 3 3 3       " " " " " "        % % % % % % # # # # # #             % % % % % %         % % % % % % % % G G G G G G G G ( ( ( ( ( ( ; ; ; ; ; ; ; ; . . . . . . . . . . . .		8	$	$;Y??

 
 
 
 
; 
 
 
) ) ) ) )+ ) ) )  2:.//bj,--RZ 9:: 
S 
Xc] 
 
 
 
/c /# /$ / / / / DFJ Js J"J,2J=@JJMJ J J J0
$G 
$ 
$ 
$ 
$ 
$W Wg W# W W W W8[$s) [3 [5c? [ [ [ [ g  RU    . g  RU    *;!
;!
;! 8C=(3-'(;! ;! ;! ;!|M Mhsm MPS M M M M Y 4    &n G     . 
 t    :# #u7G #D # # # #LOv O$sCx. O O O O O Or,   