Added the :mod:sphinx.ext.mathjax extension, based on

https://bitbucket.org/kevindunn/sphinx-extension-mathjax/ by Kevin Dunn.
This commit is contained in:
Georg Brandl
2011-01-06 13:21:36 +01:00
parent 0d1a8e5720
commit 1675a454ce
4 changed files with 111 additions and 9 deletions

View File

@@ -11,6 +11,7 @@ Other contributors, listed alphabetically, are:
* Etienne Desautels -- apidoc module
* Michael Droettboom -- inheritance_diagram extension
* Charles Duffy -- original graphviz extension
* Kevin Dunn -- MathJax extension
* Josip Dzolonga -- coverage builder
* Horst Gutmann -- internationalization support
* Martin Hans -- autodoc improvements

View File

@@ -24,7 +24,9 @@ Release 1.1 (in development)
* #443: Allow referencing external graphviz files.
* Add ``pyramid`` theme.
* Added the :mod:`sphinx.ext.mathjax` extension.
* Added ``pyramid`` theme.
* #98: Added a ``sphinx-apidoc`` script that autogenerates a
hierarchy of source files containing autodoc directives to

View File

@@ -171,19 +171,50 @@ There are various config values you can set to influence how the images are buil
installed. Therefore, the default for this option is ``False``.
:mod:`sphinx.ext.mathjax` -- Render math via JavaScript
-------------------------------------------------------
.. module:: sphinx.ext.mathjax
:synopsis: Render math using JavaScript via MathJax.
.. versionadded:: 1.1
This extension puts math as-is into the HTML files. The JavaScript package
MathJax_ is then loaded and transforms the LaTeX markup to readable math live in
the browser.
Because MathJax (and the necessary fonts) is very large, it is not included in
Sphinx. You must install it yourself, and give Sphinx its path in this config
value:
.. confval:: mathjax_path
The path to the JavaScript file to include in the HTML files in order to load
JSMath. There is no default.
The path can be absolute or relative; if it is relative, it is relative to
the ``_static`` directory of the built docs.
For example, if you put JSMath into the static path of the Sphinx docs, this
value would be ``MathJax/MathJax.js``. If you host more than one Sphinx
documentation set on one server, it is advisable to install MathJax in a
shared location.
You can also give a full ``http://`` URL. Kevin Dunn maintains a MathJax
installation on a public server, which he offers for use by development and
production servers::
mathjax_path = 'http://mathjax.connectmv.com/MathJax.js'
:mod:`sphinx.ext.jsmath` -- Render math via JavaScript
------------------------------------------------------
.. module:: sphinx.ext.jsmath
:synopsis: Render math via JavaScript.
:synopsis: Render math using JavaScript via JSMath.
This extension puts math as-is into the HTML files. The JavaScript package
jsMath_ is then loaded and transforms the LaTeX markup to readable math live in
the browser.
Because jsMath (and the necessary fonts) is very large, it is not included in
Sphinx. You must install it yourself, and give Sphinx its path in this config
value:
This extension works just as the MathJax extension does, but uses the older
package jsMath_. It provides this config value:
.. confval:: jsmath_path
@@ -200,6 +231,7 @@ value:
.. _dvipng: http://savannah.nongnu.org/projects/dvipng/
.. _MathJax: http://www.mathjax.org/
.. _jsMath: http://www.math.union.edu/~dpvc/jsmath/
.. _preview-latex package: http://www.gnu.org/software/auctex/preview-latex.html
.. _AmSMath LaTeX package: http://www.ams.org/tex/amslatex.html

67
sphinx/ext/mathjax.py Normal file
View File

@@ -0,0 +1,67 @@
# -*- coding: utf-8 -*-
"""
sphinx.ext.mathjax
~~~~~~~~~~~~~~~~~~
Allow `MathJax <http://mathjax.org/>`_ to be used to display math in
Sphinx's HTML writer -- requires the MathJax JavaScript library on your
webserver/computer.
:copyright: Copyright 2007-2011 by the Sphinx team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
from docutils import nodes
from sphinx.application import ExtensionError
from sphinx.ext.mathbase import setup_math as mathbase_setup
def html_visit_math(self, node):
self.body.append(self.starttag(node, 'span', '', CLASS='math'))
self.body.append(self.builder.config.mathjax_inline[0] +
self.encode(node['latex']) +
self.builder.config.mathjax_inline[1] + '</span>')
raise nodes.SkipNode
def html_visit_displaymath(self, node):
self.body.append(self.starttag(node, 'div', CLASS='math'))
if node['nowrap']:
self.body.append(self.builder.config.mathjax_display[0] +
node['latex'] +
self.builder.config.mathjax_display[1])
self.body.append('</div>')
raise nodes.SkipNode
parts = [prt for prt in node['latex'].split('\n\n') if prt.strip()]
for i, part in enumerate(parts):
part = self.encode(part)
if i == 0:
# necessary to e.g. set the id property correctly
if node['number']:
self.body.append('<span class="eqno">(%s)</span>' %
node['number'])
if '&' in part or '\\\\' in part:
self.body.append(self.builder.config.mathjax_display[0] +
'\\begin{split}' + part + '\\end{split}' +
self.builder.config.mathjax_display[1])
else:
self.body.append(self.builder.config.mathjax_display[0] + part +
self.builder.config.mathjax_display[1])
self.body.append('</div>\n')
raise nodes.SkipNode
def builder_inited(app):
if not app.config.mathjax_path:
raise ExtensionError('mathjax_path config value must be set for the '
'mathjax extension to work')
app.add_javascript(app.config.mathjax_path)
def setup(app):
mathbase_setup(app, (html_visit_math, None), (html_visit_displaymath, None))
app.add_config_value('mathjax_path', '', False)
app.add_config_value('mathjax_inline', [r'\(', r'\)'], 'html')
app.add_config_value('mathjax_display', [r'\[', r'\]'], 'html')
app.connect('builder-inited', builder_inited)