diff --git a/doc/usage/extensions/math.rst b/doc/usage/extensions/math.rst index 35f283846..9daa03186 100644 --- a/doc/usage/extensions/math.rst +++ b/doc/usage/extensions/math.rst @@ -168,6 +168,23 @@ Sphinx. The default is empty (``{}``). +.. confval:: mathjax_config + + The inline configuration options for mathjax. The value is used as a + parameter of ``MathJax.Hub.Config()``. For more information, please + read `Using in-line configuration options`_. + + For example:: + + mathjax_config = { + 'extensions': ['tex2jax.js'], + 'jax': ['input/TeX', 'output/HTML-CSS'], + } + + The default is empty (not configured). + +.. _Using in-line configuration options: http://docs.mathjax.org/en/latest/configuration.html#using-in-line-configuration-options + :mod:`sphinx.ext.jsmath` -- Render math via JavaScript ------------------------------------------------------ diff --git a/sphinx/builders/html.py b/sphinx/builders/html.py index f08cb909f..0deaec707 100644 --- a/sphinx/builders/html.py +++ b/sphinx/builders/html.py @@ -420,7 +420,7 @@ class StandaloneHTMLBuilder(Builder): def add_js_file(self, filename, **kwargs): # type: (unicode, **unicode) -> None - if '://' not in filename: + if filename and '://' not in filename: filename = posixpath.join('_static', filename) self.script_files.append(JavaScript(filename, **kwargs)) # type: ignore @@ -1630,17 +1630,22 @@ def setup_js_tag_helper(app, pagename, templatexname, context, doctree): def js_tag(js): # type: (JavaScript) -> unicode attrs = [] + body = '' # type: unicode if isinstance(js, JavaScript): for key in sorted(js.attributes): value = js.attributes[key] if value is not None: - attrs.append('%s="%s"' % (key, htmlescape(value, True))) - attrs.append('src="%s"' % pathto(js.filename, resource=True)) + if key == 'body': + body = value + else: + attrs.append('%s="%s"' % (key, htmlescape(value, True))) + if js.filename: + attrs.append('src="%s"' % pathto(js.filename, resource=True)) else: # str value (old styled) attrs.append('type="text/javascript"') attrs.append('src="%s"' % pathto(js, resource=True)) - return '' % ' '.join(attrs) + return '' % (' '.join(attrs), body) context['js_tag'] = js_tag diff --git a/sphinx/ext/mathjax.py b/sphinx/ext/mathjax.py index 20d5866c5..341eb12c8 100644 --- a/sphinx/ext/mathjax.py +++ b/sphinx/ext/mathjax.py @@ -11,6 +11,8 @@ :license: BSD, see LICENSE for details. """ +import json + from docutils import nodes import sphinx @@ -79,6 +81,10 @@ def builder_inited(app): options.update(app.config.mathjax_options) app.builder.add_js_file(app.config.mathjax_path, **options) # type: ignore + if app.config.mathjax_config: + body = "MathJax.Hub.Config(%s)" % json.dumps(app.config.mathjax_config) + app.builder.add_js_file(None, type="text/x-mathjax-config", body=body) # type: ignore # NOQA + def setup(app): # type: (Sphinx) -> Dict[unicode, Any] @@ -94,6 +100,7 @@ def setup(app): app.add_config_value('mathjax_options', {}, 'html') app.add_config_value('mathjax_inline', [r'\(', r'\)'], 'html') app.add_config_value('mathjax_display', [r'\[', r'\]'], 'html') + app.add_config_value('mathjax_config', None, 'html') app.connect('builder-inited', builder_inited) return {'version': sphinx.__display_version__, 'parallel_read_safe': True} diff --git a/tests/test_ext_math.py b/tests/test_ext_math.py index 99940977b..8f24349a4 100644 --- a/tests/test_ext_math.py +++ b/tests/test_ext_math.py @@ -249,3 +249,15 @@ def test_math_compat(app, status, warning): [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='basic', + confoverrides={'extensions': ['sphinx.ext.mathjax'], + 'mathjax_config': {'extensions': ['tex2jax.js']}}) +def test_mathjax_config(app, status, warning): + app.builder.build_all() + + content = (app.outdir / 'index.html').text() + assert ('' in content)