3

T'íct3ã@sªdZddlZddlZddlZddlZddlZddlmZddlmZm	Z	ddl
mZmZm
Z
mZddlmZddlmZddlZddlmZdd	lmZdd
lmZddlmZdd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!j/e0ƒZ1ej2eddƒZ3Gdd„deƒZ4Gdd„deƒZ5dGZ6ej7dƒZ8ej7dƒZ9ej7dƒZ:e;e<dœd d!„Z=e;e<dd"œd#d$„Z>dHe;e;ee;e;d&œd'd(„Z?ee;d)œd*d+„Z@e;ee;d,œd-d.„ZAe
e;e;ee;e;fd/œd0d1„ZBe;eee;e<fd2œd3d4„ZCe;eee;e<fd2œd5d6„ZDe.e;ee;e<fd7œd8d9„ZEeeFdd:œd;d<„ZGe.ee;d=œd>d?„ZHe.ejIdd=œd@dA„ZJe.ejKdd=œdBdC„ZLeee;efdDœdEdF„ZMdS)Iz*Render math in HTML via dvipng or dvisvgm.éN)Úpath)ÚPIPEÚCalledProcessError)ÚAnyÚDictÚListÚ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)ÚHTMLTranslatorZ	templatesÚimgmathcs.eZdZdZdeeeddœ‡fdd„
Z‡ZS)ÚMathExtErrorzMath extension errorN)ÚmsgÚstderrÚstdoutÚreturncs0|r|d|7}|r |d|7}tƒj|ƒdS)Nz

[stderr]
z

[stdout]
)ÚsuperÚ__init__)Úselfrrr)Ú	__class__©ú4/tmp/pip-build-gk9425m9/sphinx/sphinx/ext/imgmath.pyr"%s
zMathExtError.__init__)NN)Ú__name__Ú
__module__Ú__qualname__ÚcategoryÚstrr"Ú
__classcell__r%r%)r$r&r"src@seZdZdZdS)ÚInvokeErrorzerrors on invoking converters.N)r'r(r)Ú__doc__r%r%r%r&r--sr-ÚpngÚsvgz\[\d+ depth=(-?\d+)\]z.*, depth=(.*)ptz<!-- DEPTH=(-?\d+) -->)Úfilenamer c
CsBt|ƒ0}x|D]}qWtj|ƒ}|r4t|jdƒƒSdSQRXdS)z9Read the depth from comment at last line of SVG file
    éN)ÚopenÚdepthsvgcomment_reÚmatchÚintÚgroup)r1ÚfÚlineÚmatchedr%r%r&Úread_svg_depth8s


r;)r1Údepthr c
Cs(t|dƒ}|jd|ƒWdQRXdS)z<Write the depth to SVG file as a comment at end of file
    Úaz
<!-- DEPTH=%s -->N)r3Úwrite)r1r<r8r%r%r&Úwrite_svg_depthEsr?Ú)Úimage_formatÚmathÚconfigÚconfdirr cCs„|jtt|jdƒƒ|j|dkr$dnd|dœ}|jr:d}nd}x4|jD]*}tj|||ƒ}tj|ƒrFt	ƒj
||ƒSqFWt	tƒj
||ƒS)zGenerate LaTeX macro.g333333ó?r/r@z
,tightpage)ZfontsizeZbaselineskipÚpreambleZ	tightpagerBz
preview.tex_tztemplate.tex_t)Úimgmath_font_sizer6ÚroundÚimgmath_latex_preambleÚimgmath_use_previewÚtemplates_pathrÚjoinÚexistsrÚrender)rArBrCrDÚ	variablesZ
template_nameZtemplate_dirÚtemplater%r%r&Úgenerate_latex_macroLs
rP)Úbuilderr cCst|dƒstjƒ|_|jS)zîCreate 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ÚmkdtemprR)rQr%r%r&Úensure_tempdirds

rV)ÚlatexrQr cCsòt|ƒ}tj|dƒ}t|ddd}|j|ƒWdQRX|jjdg}|j|jjƒ|j	dƒy"t
j|tt|ddd	tj|d
ƒSt
k
rº}z tjtdƒ|jjƒt|‚WYdd}~Xn4tk
rì}ztd|j|jƒ|‚WYdd}~XnXdS)
z%Compile LaTeX macros for math to DVI.zmath.texÚwzutf-8)ÚencodingNz--interaction=nonstopmodeTÚascii)rrÚcwdÚcheckrYzmath.dvizYLaTeX command %r cannot be run (needed for math display), check the imgmath_latex settingzlatex exited with error)rVrrKr3r>rCÚ
imgmath_latexÚextendÚimgmath_latex_argsÚappendÚ
subprocessÚrunrÚOSErrorÚloggerÚwarningrr-rrrr)rWrQÚtempdirr1r8ÚcommandÚexcr%r%r&Úcompile_mathqs"


ri)rgÚnamer cCsœy tj|ttddd}|j|jfStk
r`}z$tjtdƒ||d|ƒt	|‚WYdd}~Xn8t
k
r–}ztd||j|jƒ|‚WYdd}~XnXdS)z*Convert DVI file to specific image format.TrZ)rrr\rYzS%s command %r cannot be run (needed for math display), check the imgmath_%s settingrNz%s exited with error)rarbrrrrcrdrerr-rr)rgrjÚretrhr%r%r&Úconvert_dvi_to_images
rl)ÚdvipathrQr cCs²t|ƒ}tj|dƒ}d}|jjd|dddg}|j|jjƒ|jjrL|jdƒ|j|ƒt	||ƒ\}}d}|jjrªx8|j
ƒD],}	tj|	ƒ}
|
rzt
|
jd	ƒƒ}t||ƒPqzW||fS)
zConvert DVI file to PNG image.zmath.pngÚdvipngz-oz-TZtightz-z9z--depthNr2)rVrrKrCÚimgmath_dvipngr^Úimgmath_dvipng_argsrIr`rlÚ
splitlinesÚdepth_rer5r6r7r)rmrQrfr1rjrgrrr<r9r:r%r%r&Úconvert_dvi_to_png›s$



rscCs¦t|ƒ}tj|dƒ}d}|jjd|g}|j|jjƒ|j|ƒt||ƒ\}}d}|jj	ržxD|j
ƒD]8}	tj|	ƒ}
|
rbt
t|
jdƒƒddƒ}t||ƒPqbW||fS)zConvert DVI file to SVG image.zmath.svgÚdvisvgmz-oNr2édgáz®GR@)rVrrKrCÚimgmath_dvisvgmr^Úimgmath_dvisvgm_argsr`rlrIrqÚdepthsvg_rer5rGÚfloatr7r?)rmrQrfr1rjrgrrr<r9r:r%r%r&Úconvert_dvi_to_svgµs 


rz)r#rBr c
Csp|jjjjƒ}|tkrtdƒ‚t|||jj|jjƒ}dt|j	ƒƒj
ƒ|f}tj|jj
d|ƒ}tj|jj|jjd|ƒ}tj|ƒrª|dkr’t|ƒ}n|dkr¢t|ƒ}||fSt|jdƒsÂt|jdƒrÆd
Syt||jƒ}Wntk
ròd	|j_dSXy:|dkrt||jƒ\}	}n|dkr,t||jƒ\}	}Wntk
rLd	|j_dSXttj|ƒƒtj|	|ƒ||fS)
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.

    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.%srBr/r0Ú_imgmath_warned_latexÚ _imgmath_warned_image_translatorNT)NN)NN)NN)rQrCÚimgmath_image_formatÚlowerÚSUPPORT_FORMATrrPrDrÚencodeÚ	hexdigestÚ	posixpathrKÚimgpathrZoutdirZimagedirÚisfilerr;rSrir-r{rsrzr|rÚdirnameÚshutilÚmove)
r#rBrArWr1ZrelfnZoutfnr<rmrƒr%r%r&Úrender_mathÍsF




rˆ)Úapprhr cCsD|rdSt|jdƒsdSytj|jjƒWntk
r>YnXdS)NrR)rSrQr†ÚrmtreeZ_mathpng_tempdirÚ	Exception)r‰rhr%r%r&Úcleanup_tempdirsrŒ)r#Únoder cCs$|jjjr d|j|jƒƒjƒSdS)Nz	 alt="%s"r@)rQrCÚimgmath_add_tooltipsr€ÚastextÚstrip)r#rr%r%r&Úget_tooltips
r‘cCsôyt|d|jƒdƒ\}}Wnjtk
rˆ}zNt|ƒ}tj|ddg|jƒd}|j|ƒtjt	dƒ|jƒ|ƒtj
|‚WYdd}~XnX|dkr²|jjd|j
|jƒƒjƒƒn8d|t||ƒ}|dk	rÚ|d|7}|jj|d	ƒtj
‚dS)
Nú$ÚWARNINGé)ÚtypeÚlevelÚbackrefsÚsourcezdisplay latex %r: %sz<span class="math">%s</span>z<img class="math" src="%s"z style="vertical-align: %dpx"z/>)rˆrrr+r	Úsystem_messageÚ	walkaboutrdrerÚSkipNodeÚbodyr`r€rr‘)r#rÚfnamer<rhrÚsmÚcr%r%r&Úhtml_visit_maths"

r c	CsV|dr|jƒ}nt|jƒddƒ}yt||ƒ\}}Wnjtk
rž}zNt|ƒ}tj|ddg|jƒd}|j|ƒtj	t
dƒ|jƒ|ƒtj|‚WYdd}~XnX|jj
|j|ddd	ƒ|jj
d
ƒ|drt||ƒ}|jj
d|ƒ|j|td
ƒƒ|jj
dƒ|dkr.|jj
d|j|jƒƒjƒƒn|jj
d|t||ƒdƒtj‚dS)NZnowrapFr“r”)r•r–r—r˜zinline latex %r: %sÚdivrB)ZCLASSz<p>Únumberz<span class="eqno">(%s)zPermalink to this equationz</span>z'<span class="math">%s</span></p>
</div>z
<img src="%s"z
/></p>
</div>)rrrˆrr+r	r™ršrdrerr›rœr`ZstarttagrZadd_permalink_refrr€rr‘)	r#rrWrr<rhrržr¢r%r%r&Úhtml_visit_displaymath2s2





r£)r‰r cCsÖ|jdtdftdfƒ|jdddƒ|jdddƒ|jdddƒ|jd	d
dƒ|jdddƒ|jd
ddddddgdƒ|jddgdƒ|jdgdƒ|jdddƒ|jdddƒ|jdddƒ|jdtƒtjddœS)Nrr}r/Úhtmlrornrvrtr]rWrIFrpz-gammaz1.5z-DZ110z-bgZTransparentrwz
--no-fontsr_rHr@rŽTrFézbuild-finished)ÚversionZparallel_read_safe)Zadd_html_math_rendererr r£Zadd_config_valueÚconnectrŒÚsphinxZ__display_version__)r‰r%r%r&ÚsetupQs$
r©)r/r0)r@)Nr.r‚Úrer†rarTÚosrrrÚtypingrrrrZdocutilsr	Zdocutils.nodesr
r¨rZsphinx.applicationrZsphinx.buildersr
Z
sphinx.configrZ
sphinx.errorsrZ
sphinx.localerrZsphinx.utilrrZsphinx.util.mathrrZsphinx.util.osutilrZsphinx.util.pngrrZsphinx.util.templaterZsphinx.writers.htmlrÚ	getLoggerr'rdrKrJrr-rÚcompilerrrxr4r+r6r;r?rPrVrirlrsrzrˆr‹rŒr‘rBr Z
math_blockr£r©r%r%r%r&Ú<module>sZ





>