WIP: #726, #969: Add mathjax_config

This commit is contained in:
Takeshi KOMIYA 2018-06-01 17:23:08 +09:00
parent 004b68f281
commit da7a06b323
4 changed files with 45 additions and 4 deletions

View File

@ -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
------------------------------------------------------

View File

@ -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 '<script %s></script>' % ' '.join(attrs)
return '<script %s>%s</script>' % (' '.join(attrs), body)
context['js_tag'] = js_tag

View File

@ -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}

View File

@ -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 ('<script type="text/x-mathjax-config">'
'MathJax.Hub.Config({"extensions": ["tex2jax.js"]})'
'</script>' in content)