1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
|
"""Test math extensions."""
from __future__ import annotations
import re
import shutil
import subprocess
import warnings
from typing import TYPE_CHECKING
import pytest
from docutils import nodes
from sphinx.ext.mathjax import MATHJAX_URL
from sphinx.testing.util import assert_node
if TYPE_CHECKING:
from sphinx.testing.util import SphinxTestApp
def has_binary(binary: str) -> bool:
try:
subprocess.check_output([binary])
except FileNotFoundError:
return False
except OSError:
pass
return True
@pytest.mark.skipif(
not has_binary('dvipng'),
reason='Requires dvipng" binary',
)
@pytest.mark.sphinx(
'html',
testroot='ext-math-simple',
confoverrides={'extensions': ['sphinx.ext.imgmath']},
)
def test_imgmath_png(app: SphinxTestApp) -> None:
app.build(force_all=True)
if "LaTeX command 'latex' cannot be run" in app.warning.getvalue():
msg = 'LaTeX command "latex" is not available'
raise pytest.skip.Exception(msg)
if "dvipng command 'dvipng' cannot be run" in app.warning.getvalue():
msg = 'dvipng command "dvipng" is not available'
raise pytest.skip.Exception(msg)
content = (app.outdir / 'index.html').read_text(encoding='utf8')
shutil.rmtree(app.outdir)
html = (
r'<div class="math">\s*<p>\s*<img src="_images/math/\w+.png"'
r'\s*alt="a\^2\+b\^2=c\^2"/>\s*</p>\s*</div>'
)
assert re.search(html, content, re.DOTALL)
@pytest.mark.skipif(
not has_binary('dvisvgm'),
reason='Requires dvisvgm" binary',
)
@pytest.mark.sphinx(
'html',
testroot='ext-math-simple',
confoverrides={'extensions': ['sphinx.ext.imgmath'], 'imgmath_image_format': 'svg'},
)
def test_imgmath_svg(app: SphinxTestApp) -> None:
app.build(force_all=True)
if "LaTeX command 'latex' cannot be run" in app.warning.getvalue():
msg = 'LaTeX command "latex" is not available'
raise pytest.skip.Exception(msg)
if "dvisvgm command 'dvisvgm' cannot be run" in app.warning.getvalue():
msg = 'dvisvgm command "dvisvgm" is not available'
raise pytest.skip.Exception(msg)
content = (app.outdir / 'index.html').read_text(encoding='utf8')
shutil.rmtree(app.outdir)
html = (
r'<div class="math">\s*<p>\s*<img src="_images/math/\w+.svg"'
r'\s*alt="a\^2\+b\^2=c\^2"/>\s*</p>\s*</div>'
)
assert re.search(html, content, re.DOTALL)
@pytest.mark.skipif(
not has_binary('dvisvgm'),
reason='Requires dvisvgm" binary',
)
@pytest.mark.sphinx(
'html',
testroot='ext-math-simple',
confoverrides={
'extensions': ['sphinx.ext.imgmath'],
'imgmath_image_format': 'svg',
'imgmath_embed': True,
},
)
def test_imgmath_svg_embed(app: SphinxTestApp) -> None:
app.build(force_all=True)
if "LaTeX command 'latex' cannot be run" in app.warning.getvalue():
msg = 'LaTeX command "latex" is not available'
raise pytest.skip.Exception(msg)
if "dvisvgm command 'dvisvgm' cannot be run" in app.warning.getvalue():
msg = 'dvisvgm command "dvisvgm" is not available'
raise pytest.skip.Exception(msg)
content = (app.outdir / 'index.html').read_text(encoding='utf8')
shutil.rmtree(app.outdir)
html = r'<img src="data:image/svg\+xml;base64,[\w\+/=]+"'
assert re.search(html, content, re.DOTALL)
@pytest.mark.sphinx(
'html',
testroot='ext-math',
confoverrides={
'extensions': ['sphinx.ext.mathjax'],
'mathjax_options': {'integrity': 'sha384-0123456789'},
},
)
def test_mathjax_options(app: SphinxTestApp) -> None:
app.build(force_all=True)
content = (app.outdir / 'index.html').read_text(encoding='utf8')
shutil.rmtree(app.outdir)
assert (
'<script async="async" integrity="sha384-0123456789" '
'src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js">'
'</script>'
) in content
@pytest.mark.sphinx(
'html',
testroot='ext-math',
confoverrides={'extensions': ['sphinx.ext.mathjax']},
)
def test_mathjax_align(app: SphinxTestApp) -> None:
app.build(force_all=True)
content = (app.outdir / 'index.html').read_text(encoding='utf8')
shutil.rmtree(app.outdir)
html = (
r'<div class="math notranslate nohighlight">\s*'
r'\\\[ \\begin\{align\}\\begin\{aligned\}S \&= \\pi r\^2\\\\'
r'V \&= \\frac\{4\}\{3\} \\pi r\^3\\end\{aligned\}\\end\{align\} \\\]</div>'
)
assert re.search(html, content, re.DOTALL)
@pytest.mark.sphinx(
'html',
testroot='ext-math',
confoverrides={'math_number_all': True, 'extensions': ['sphinx.ext.mathjax']},
)
def test_math_number_all_mathjax(app: SphinxTestApp) -> None:
app.build()
content = (app.outdir / 'index.html').read_text(encoding='utf8')
html = (
r'<div class="math notranslate nohighlight" id="equation-index-0">\s*'
r'<span class="eqno">\(1\)<a .*>\xb6</a></span>\\\[a\^2\+b\^2=c\^2\\\]</div>'
)
assert re.search(html, content, re.DOTALL)
@pytest.mark.sphinx(
'latex',
testroot='ext-math',
confoverrides={'extensions': ['sphinx.ext.mathjax']},
)
def test_math_number_all_latex(app: SphinxTestApp) -> None:
app.build()
content = (app.outdir / 'projectnamenotset.tex').read_text(encoding='utf8')
macro = (
r'\\begin{equation\*}\s*'
r'\\begin{split}a\^2\+b\^2=c\^2\\end{split}\s*'
r'\\end{equation\*}'
)
assert re.search(macro, content, re.DOTALL)
macro = r'Inline \\\(E=mc\^2\\\)'
assert re.search(macro, content, re.DOTALL)
macro = (
r'\\begin{equation\*}\s*'
r'\\begin{split}e\^{i\\pi}\+1=0\\end{split}\s+'
r'\\end{equation\*}'
)
assert re.search(macro, content, re.DOTALL)
macro = (
r'\\begin{align\*}\\!\\begin{aligned}\s*'
r'S &= \\pi r\^2\\\\\s*'
r'V &= \\frac\{4}\{3} \\pi r\^3\\\\\s*'
r'\\end{aligned}\\end{align\*}'
)
assert re.search(macro, content, re.DOTALL)
macro = r'Referencing equation \\eqref{equation:math:foo}.'
assert re.search(macro, content, re.DOTALL)
@pytest.mark.sphinx(
'html',
testroot='ext-math',
confoverrides={
'extensions': ['sphinx.ext.mathjax'],
'math_eqref_format': 'Eq.{number}',
},
)
def test_math_eqref_format_html(app: SphinxTestApp) -> None:
app.build(force_all=True)
content = (app.outdir / 'math.html').read_text(encoding='utf8')
html = (
'<p>Referencing equation <a class="reference internal" '
'href="#equation-foo">Eq.1</a> and <a class="reference internal" '
'href="#equation-foo">Eq.1</a>.</p>'
)
assert html in content
@pytest.mark.sphinx(
'latex',
testroot='ext-math',
confoverrides={
'extensions': ['sphinx.ext.mathjax'],
'math_eqref_format': 'Eq.{number}',
},
)
def test_math_eqref_format_latex(app: SphinxTestApp) -> None:
app.build(force_all=True)
content = (app.outdir / 'projectnamenotset.tex').read_text(encoding='utf8')
macro = (
r'Referencing equation Eq.\\ref{equation:math:foo} and '
r'Eq.\\ref{equation:math:foo}.'
)
assert re.search(macro, content, re.DOTALL)
@pytest.mark.sphinx(
'html',
testroot='ext-math',
confoverrides={
'extensions': ['sphinx.ext.mathjax'],
'numfig': True,
'math_numfig': True,
},
)
def test_mathjax_numfig_html(app: SphinxTestApp) -> None:
app.build(force_all=True)
content = (app.outdir / 'math.html').read_text(encoding='utf8')
html = (
'<div class="math notranslate nohighlight" id="equation-math-0">\n'
'<span class="eqno">(1.2)'
)
assert html in content
html = (
'<p>Referencing equation <a class="reference internal" '
'href="#equation-foo">(1.1)</a> and '
'<a class="reference internal" href="#equation-foo">(1.1)</a>.</p>'
)
assert html in content
@pytest.mark.sphinx(
'html',
testroot='ext-math',
confoverrides={
'extensions': ['sphinx.ext.mathjax'],
'numfig': True,
'math_numfig': True,
'math_numsep': '-',
},
)
def test_mathjax_numsep_html(app: SphinxTestApp) -> None:
app.build(force_all=True)
content = (app.outdir / 'math.html').read_text(encoding='utf8')
html = (
'<div class="math notranslate nohighlight" id="equation-math-0">\n'
'<span class="eqno">(1-2)'
)
assert html in content
html = (
'<p>Referencing equation <a class="reference internal" '
'href="#equation-foo">(1-1)</a> and '
'<a class="reference internal" href="#equation-foo">(1-1)</a>.</p>'
)
assert html in content
@pytest.mark.usefixtures('_http_teapot')
@pytest.mark.sphinx(
'html',
testroot='ext-math',
confoverrides={
'extensions': ['sphinx.ext.imgmath'],
'numfig': True,
'numfig_secnum_depth': 0,
'math_numfig': True,
},
)
def test_imgmath_numfig_html(app: SphinxTestApp) -> None:
app.build(force_all=True)
content = (app.outdir / 'page.html').read_text(encoding='utf8')
html = '<span class="eqno">(3)<a class="headerlink" href="#equation-bar"'
assert html in content
html = (
'<p>Referencing equations <a class="reference internal" '
'href="math.html#equation-foo">(1)</a> and '
'<a class="reference internal" href="#equation-bar">(3)</a>.</p>'
)
assert html in content
@pytest.mark.sphinx('dummy', testroot='ext-math-compat')
def test_math_compat(app):
with warnings.catch_warnings(record=True):
app.build(force_all=True)
doctree = app.env.get_and_resolve_doctree('index', app.builder)
assert_node(
doctree,
[
nodes.document,
nodes.section,
(
nodes.title,
[nodes.section, (nodes.title, nodes.paragraph)],
nodes.section,
),
],
)
assert_node(
doctree[0][1][1],
(
'Inline: ',
[nodes.math, 'E=mc^2'],
'\nInline my math: ',
[nodes.math, 'E = mc^2'],
),
)
assert_node(
doctree[0][2],
(
[nodes.title, 'block'],
[nodes.math_block, 'a^2+b^2=c^2\n\n'],
[nodes.paragraph, 'Second math'],
[nodes.math_block, 'e^{i\\pi}+1=0\n\n'],
[nodes.paragraph, 'Multi math equations'],
[nodes.math_block, 'E = mc^2'],
),
)
@pytest.mark.sphinx(
'html',
testroot='ext-math',
confoverrides={
'extensions': ['sphinx.ext.mathjax'],
'mathjax3_config': {'extensions': ['tex2jax.js']},
},
)
def test_mathjax3_config(app: SphinxTestApp) -> None:
app.build(force_all=True)
content = (app.outdir / 'index.html').read_text(encoding='utf8')
assert MATHJAX_URL in content
assert '<script defer="defer" src="%s">' % MATHJAX_URL in content
assert '<script>window.MathJax = {"extensions": ["tex2jax.js"]}</script>' in content
@pytest.mark.sphinx(
'html',
testroot='ext-math',
confoverrides={
'extensions': ['sphinx.ext.mathjax'],
'mathjax2_config': {'extensions': ['tex2jax.js']},
},
)
def test_mathjax2_config(app: SphinxTestApp) -> None:
app.build(force_all=True)
content = (app.outdir / 'index.html').read_text(encoding='utf8')
assert '<script async="async" src="%s">' % MATHJAX_URL in content
assert (
'<script type="text/x-mathjax-config">'
'MathJax.Hub.Config({"extensions": ["tex2jax.js"]})'
'</script>'
) in content
@pytest.mark.sphinx(
'html',
testroot='ext-math',
confoverrides={
'extensions': ['sphinx.ext.mathjax'],
'mathjax_options': {'async': 'async'},
'mathjax3_config': {'extensions': ['tex2jax.js']},
},
)
def test_mathjax_options_async_for_mathjax3(app: SphinxTestApp) -> None:
app.build(force_all=True)
content = (app.outdir / 'index.html').read_text(encoding='utf8')
assert MATHJAX_URL in content
assert '<script async="async" src="%s">' % MATHJAX_URL in content
@pytest.mark.sphinx(
'html',
testroot='ext-math',
confoverrides={
'extensions': ['sphinx.ext.mathjax'],
'mathjax_options': {'defer': 'defer'},
'mathjax2_config': {'extensions': ['tex2jax.js']},
},
)
def test_mathjax_options_defer_for_mathjax2(app: SphinxTestApp) -> None:
app.build(force_all=True)
content = (app.outdir / 'index.html').read_text(encoding='utf8')
assert '<script defer="defer" src="%s">' % MATHJAX_URL in content
@pytest.mark.sphinx(
'html',
testroot='ext-math',
confoverrides={
'extensions': ['sphinx.ext.mathjax'],
'mathjax_path': 'MathJax.js',
},
)
def test_mathjax_path(app: SphinxTestApp) -> None:
app.build(force_all=True)
content = (app.outdir / 'index.html').read_text(encoding='utf8')
assert '<script async="async" src="_static/MathJax.js"></script>' in content
@pytest.mark.sphinx(
'html',
testroot='ext-math',
confoverrides={
'extensions': ['sphinx.ext.mathjax'],
'mathjax_path': 'MathJax.js?config=scipy-mathjax',
},
)
def test_mathjax_path_config(app: SphinxTestApp) -> None:
app.build(force_all=True)
content = (app.outdir / 'index.html').read_text(encoding='utf8')
assert (
'<script async="async" src="_static/MathJax.js?config=scipy-mathjax"></script>'
) in content
@pytest.mark.sphinx(
'html',
testroot='ext-math',
confoverrides={'extensions': ['sphinx.ext.mathjax']},
)
def test_mathjax_is_installed_only_if_document_having_math(app: SphinxTestApp) -> None:
app.build(force_all=True)
content = (app.outdir / 'index.html').read_text(encoding='utf8')
assert MATHJAX_URL in content
content = (app.outdir / 'nomath.html').read_text(encoding='utf8')
assert MATHJAX_URL not in content
@pytest.mark.sphinx(
'html',
testroot='basic',
confoverrides={'extensions': ['sphinx.ext.mathjax']},
)
def test_mathjax_is_not_installed_if_no_equations(app: SphinxTestApp) -> None:
app.build(force_all=True)
content = (app.outdir / 'index.html').read_text(encoding='utf8')
assert 'MathJax.js' not in content
@pytest.mark.sphinx(
'html',
testroot='ext-math',
confoverrides={'extensions': ['sphinx.ext.mathjax']},
)
def test_mathjax_is_installed_if_no_equations_when_forced(app: SphinxTestApp) -> None:
app.set_html_assets_policy('always')
app.build(force_all=True)
content = (app.outdir / 'index.html').read_text(encoding='utf8')
assert MATHJAX_URL in content
content = (app.outdir / 'nomath.html').read_text(encoding='utf8')
assert MATHJAX_URL in content
@pytest.mark.sphinx(
'html',
testroot='ext-math-include',
confoverrides={'extensions': ['sphinx.ext.mathjax']},
)
def test_mathjax_is_installed_if_included_file_has_equations(
app: SphinxTestApp,
) -> None:
app.build(force_all=True)
# no real equations at the rst level, but includes "included"
content = (app.outdir / 'index.html').read_text(encoding='utf8')
assert MATHJAX_URL in content
# no real equations at the rst level, but includes "math.rst"
content = (app.outdir / 'included.html').read_text(encoding='utf8')
assert MATHJAX_URL in content
content = (app.outdir / 'math.html').read_text(encoding='utf8')
assert MATHJAX_URL in content
@pytest.mark.sphinx(
'singlehtml',
testroot='ext-math',
confoverrides={'extensions': ['sphinx.ext.mathjax']},
)
def test_mathjax_is_installed_only_if_document_having_math_singlehtml(
app: SphinxTestApp,
) -> None:
app.build(force_all=True)
content = (app.outdir / 'index.html').read_text(encoding='utf8')
assert MATHJAX_URL in content
@pytest.mark.sphinx(
'singlehtml',
testroot='basic',
confoverrides={'extensions': ['sphinx.ext.mathjax']},
)
def test_mathjax_is_not_installed_if_no_equations_singlehtml(
app: SphinxTestApp,
) -> None:
app.build(force_all=True)
content = (app.outdir / 'index.html').read_text(encoding='utf8')
assert 'MathJax.js' not in content
@pytest.mark.sphinx(
'singlehtml',
testroot='ext-math-include',
confoverrides={'extensions': ['sphinx.ext.mathjax']},
)
def test_mathjax_is_installed_if_included_file_has_equations_singlehtml(
app: SphinxTestApp,
) -> None:
app.build(force_all=True)
content = (app.outdir / 'index.html').read_text(encoding='utf8')
assert MATHJAX_URL in content
|