o Wa 4 @sdZddlZddlZddlZddlZddlZddlmZddlmZm Z ddl m Z m Z m Z mZddlmZddlmZddlZddlmZdd lmZdd lmZdd lmZdd lmZdd lmZmZddl m!Z!m"Z"ddl#m$Z$m%Z%ddl&m'Z'ddl(m)Z)m*Z*ddl+m,Z,ddl-m.Z.e!/e0Z1e2eddZ3GdddeZ4GdddeZ5dZ6e7dZ8e7dZ9e7dZ:de;de %dKd&e;d'e;d(ed)e;de;f d*d+Z?d,ede;fd-d.Z@d/e;d,ede;fd0d1ZAd2e e;d3e;dee;e;ffd4d5ZBd6e;d,edee;eed?eFddfd@dAZGd;e.dBede;fdCdDZHd;e.dBejIddfdEdFZJd;e.dBejKddfdGdHZLd>ede e;e ffdIdJZMdS)Lz sphinx.ext.imgmath ~~~~~~~~~~~~~~~~~~ Render math in HTML via dvipng or dvisvgm. :copyright: Copyright 2007-2021 by the Sphinx team, see AUTHORS. :license: BSD, see LICENSE for details. N)path)PIPECalledProcessError)AnyDictListTuple)nodes)Element) package_dir)Sphinx)Builder)Config) SphinxError)___)loggingsha1)get_node_equation_numberwrap_displaymath) ensuredir)read_png_depthwrite_png_depth) LaTeXRenderer)HTMLTranslator templatesimgmathc s4eZdZdZd dedededdffdd ZZS) MathExtErrorzMath extension errorNmsgstderrstdoutreturncs0|r|d|7}|r|d|7}t|dS)Nz [stderr] z [stdout] )super__init__)selfrrr  __class__4/usr/lib/python3/dist-packages/sphinx/ext/imgmath.pyr#-s   zMathExtError.__init__NN)__name__ __module__ __qualname__categorystrr# __classcell__r'r'r%r(r*s(rc@seZdZdZdS) InvokeErrorzerrors on invoking converters.N)r*r+r,__doc__r'r'r'r(r05sr0)pngsvgz\[\d+ depth=(-?\d+)\]z.*, depth=(.*)ptzfilenamer!cCsdt|$}|D]}qt|}|rt|dWdS WddS1s+wYdS)z9Read the depth from comment at last line of SVG file N)opendepthsvgcomment_rematchintgroup)r4flinematchedr'r'r(read_svg_depth@s   $r>depthcCs>t|d}|d|WddS1swYdS)zN)r6write)r4r?r;r'r'r(write_svg_depthMs "rB image_formatmathconfigconfdircCs|jtt|jd|j|dkrdnd|d}|jrd}nd}|jD]}t|||}t|r9t ||Sq"t t ||S)zGenerate LaTeX macro.g333333?r2rCz ,tightpage)fontsize baselineskippreamble tightpagerEz preview.tex_tztemplate.tex_t) imgmath_font_sizer9roundimgmath_latex_preambleimgmath_use_previewtemplates_pathrjoinexistsrrender)rDrErFrG variables template_name template_dirtemplater'r'r(generate_latex_macroTs  rXbuildercCst|ds t|_|jS)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)hasattrtempfilemkdtemprZ)rYr'r'r(ensure_tempdirls  r^latexc Cst|}t|d}t|ddd }||Wdn1s!wY|jjdg}||jj| dzt j |t t |ddd t|d WSt yc}z ttd |jjt|d}~wtyw}z td |j|j|d}~ww) z%Compile LaTeX macros for math to DVI.zmath.texwzutf-8)encodingNz--interaction=nonstopmodeTascii)r rcwdcheckrazmath.dvizYLaTeX command %r cannot be run (needed for math display), check the imgmath_latex settingzlatex exited with error)r^rrQr6rArF imgmath_latexextendimgmath_latex_argsappend subprocessrunrOSErrorloggerwarningrr0rrrr )r_rYtempdirr4r;commandexcr'r'r( compile_mathys.     rqronamec Csztj|ttddd}|j|jfWSty,}zttd||d|t |d}~wt yB}z t d||j|j|d}~ww)z*Convert DVI file to specific image format.Trb)r rrdrazS%s command %r cannot be run (needed for math display), check the imgmath_%s settingrNz%s exited with error) rirjrr rrkrlrmrr0rr)rorrretrpr'r'r(convert_dvi_to_images  rtdvipathc Cst|}t|d}d}|jjd|dddg}||jj|jjr&|d||t ||\}}d}|jjrW| D]} t | } | rVt | d }t||||fSq<||fS) zConvert DVI file to PNG image.zmath.pngdvipng-oz-Ttightz-z9z--depthNr5)r^rrQrFimgmath_dvipngrfimgmath_dvipng_argsrOrhrt splitlinesdepth_rer8r9r:r rurYrnr4rrror rr?r<r=r'r'r(convert_dvi_to_pngs(      r~c Cst|}t|d}d}|jjd|g}||jj||t||\}}d}|jj rQ| D] } t | } | rPt t| ddd}t||||fSq0||fS)zConvert DVI file to SVG image.zmath.svgdvisvgmrwNr5dgzGR@)r^rrQrFimgmath_dvisvgmrfimgmath_dvisvgm_argsrhrtrOr{ depthsvg_rer8rMfloatr:rBr}r'r'r(convert_dvi_to_svgs$     rr$c Csp|jjj}|tvrtdt|||jj|jj}dt|  |f}t |jj d|}t |jj|jjd|}t|rX|dkrLt|}||fS|dkrTt|}||fSt|jdsdt|jdrfdSzt||j}Wnty|d |j_YdSwz|dkrt||j\} }n |dkrt||j\} }Wntyd |j_YdSwtt|t| |||fS) aRender 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. 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. z2imgmath_image_format must be either "png" or "svg"z%s.%srEr2r3_imgmath_warned_latex _imgmath_warned_image_translatorr)T)rYrFimgmath_image_formatlowerSUPPORT_FORMATrrXrGrencode hexdigest posixpathrQimgpathroutdirimagedirisfilerr>r[rqr0rr~rrrdirnameshutilmove) r$rErDr_r4relfnoutfnr?rurr'r'r( render_mathsR      rapprpcCsB|rdSt|jds dSz t|jjWdSty YdSw)NrZ)r[rYrrmtree_mathpng_tempdir Exception)rrpr'r'r(cleanup_tempdirs  rnodecCs$|jjjrd||SdS)Nz alt="%s"rC)rYrFimgmath_add_tooltipsrastextstrip)r$rr'r'r( get_tooltips rc Cszt|d|d\}}Wn0ty?}z$t|}tj|ddg|d}||tt d||tj |d}~ww|durV|j d| |tj d|t||}|durj|d| 7}|j |d tj ) N$WARNINGtypelevelbackrefssourcezdisplay latex %r: %sz%sz)rrrr.r system_message walkaboutrlrmrSkipNodebodyrhrrr)r$rfnamer?rprsmcr'r'r(html_visit_math$s,  rc CsH|dr |}nt|dd}z t||\}}Wn0tyJ}z$t|}tj|ddg|d}||t t d||tj |d}~ww|j |j|ddd |j d |d r|t||}|j d |||td |j d|dur|j d||tj |j d|t||dtj )NnowrapFrrrzinline latex %r: %sdivrE)CLASSz

numberz(%s)zPermalink to this equationzz'%s

z

)rrrrr.r rrrlrmrrrrhstarttagradd_permalink_refrrrr) r$rr_rr?rprrrr'r'r(html_visit_displaymath:s>      rcCs|dtdftdf|ddd|ddd|ddd|d d d|d d d|d gdd|ddgd|dgd|ddd|ddd|ddd|dttjddS)Nrrr2htmlryrvrrrer_rOFrz)z-gammaz1.5z-D110z-bg Transparentrz --no-fontsrgrNrCrTrL zbuild-finished)versionparallel_read_safe)add_html_math_rendererrradd_config_valueconnectrsphinx__display_version__)rr'r'r(setupYs(  r)rC)Nr1rrerrir\osrrrtypingrrrrdocutilsr docutils.nodesr rr sphinx.applicationr sphinx.buildersr sphinx.configr sphinx.errorsr sphinx.localerr sphinx.utilrrsphinx.util.mathrrsphinx.util.osutilrsphinx.util.pngrrsphinx.util.templatersphinx.writers.htmlr getLoggerr*rlrQrPrr0rcompiler|rr7r.r9r>rBrXr^rqrtr~rrrrrrEr math_blockrrr'r'r'r(sn                    ">