Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions sphinxcontrib/jsmath/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@

from docutils import nodes
from sphinx.builders.html import StandaloneHTMLBuilder
from sphinx.domains.math import MathDomain
from sphinx.errors import ExtensionError
from sphinx.locale import get_translation
from sphinx.util.math import get_node_equation_number

if TYPE_CHECKING:
from sphinx.application import Sphinx
from sphinx.environment import BuildEnvironment
from sphinx.writers.html import HTMLTranslator

__version__ = '1.0.2'
Expand Down Expand Up @@ -58,16 +56,22 @@ def html_visit_displaymath(self: HTMLTranslator, node: nodes.math_block) -> None
raise nodes.SkipNode


def install_jsmath(app: Sphinx, env: BuildEnvironment) -> None:
def install_jsmath(
app: Sphinx,
pagename: str,
templatename: str,
context: dict[str, Any],
event_arg: Any,
) -> None:
if app.builder.format != 'html' or app.builder.math_renderer_name != 'jsmath': # type: ignore[attr-defined]
return
if not app.config.jsmath_path:
msg = 'jsmath_path config value must be set for the jsmath extension to work'
raise ExtensionError(msg)

builder = cast(StandaloneHTMLBuilder, app.builder)
domain = cast(MathDomain, env.get_domain('math'))
if domain.has_equations():
page_has_equations = context.get('has_maths_elements', True)
if app.registry.html_assets_policy == 'always' or page_has_equations:
# Enable jsmath only if equations exists
builder.add_js_file(app.config.jsmath_path)

Expand All @@ -80,7 +84,7 @@ def setup(app: Sphinx) -> dict[str, Any]:
(html_visit_displaymath, None))

app.add_config_value('jsmath_path', '', False)
app.connect('env-updated', install_jsmath)
app.connect('html-page-context', install_jsmath)
return {
'version': __version__,
'parallel_read_safe': True,
Expand Down
7 changes: 7 additions & 0 deletions tests/test_jsmath.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import TYPE_CHECKING

import pytest
import sphinx

if TYPE_CHECKING:
from sphinx.application import Sphinx
Expand All @@ -15,6 +16,7 @@ def test_basic(app: Sphinx) -> None:
app.builder.build_all()
content = (app.outdir / 'math.html').read_text(encoding='utf-8')
print(content)
assert 'jsmath.js' in content
assert '<div class="math notranslate nohighlight">\nE = mc^2</div>' in content
assert ('<span class="eqno">(1)<a class="headerlink" href="#equation-pythagorean" '
'title="Permalink to this equation">¶</a></span>'
Expand All @@ -34,6 +36,7 @@ def test_numfig_enabled(app: Sphinx) -> None:
app.builder.build_all()

content = (app.outdir / 'math.html').read_text(encoding='utf-8')
assert 'jsmath.js' in content
assert '<div class="math notranslate nohighlight">\nE = mc^2</div>' in content
assert ('<span class="eqno">(1.1)<a class="headerlink" href="#equation-pythagorean" '
'title="Permalink to this equation">¶</a></span>'
Expand All @@ -47,6 +50,10 @@ def test_numfig_enabled(app: Sphinx) -> None:
assert '<a class="reference internal" href="#equation-pythagorean">(1.1)</a>' in content


@pytest.mark.skipif(
sphinx.version_info < (8, 2),
reason='Sphinx < 8.2 does not have `has_maths_elements` in context',
)
@pytest.mark.sphinx('html', testroot='nomath')
def test_disabled_when_equations_not_found(app: Sphinx) -> None:
app.builder.build_all()
Expand Down