source: sasview/docs/sphinx-docs/source/_extensions/mathjax.py @ f4771596

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalcmagnetic_scattrelease-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since f4771596 was f4771596, checked in by Paul Kienzle <pkienzle@…>, 7 years ago

prerender mathjax for comparison

  • Property mode set to 100644
File size: 4.3 KB
Line 
1# -*- coding: utf-8 -*-
2"""
3    sphinx.ext.mathjax
4    ~~~~~~~~~~~~~~~~~~
5
6    Allow `MathJax <http://mathjax.org/>`_ to be used to display math in
7    Sphinx's HTML writer -- requires the MathJax JavaScript library on your
8    webserver/computer.
9
10    :copyright: Copyright 2007-2016 by the Sphinx team, see AUTHORS.
11    :license: BSD, see LICENSE for details.
12
13    sample lines in conf.py to use this::
14
15        mathjax_path = ['https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.6.0/katex.min.js',
16                        'http://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.6.0/contrib/auto-render.min.js',
17                        'rendermath.js']
18        mathjax_css = 'https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.6.0/katex.min.css'
19
20    with rendermath.js containing::
21
22        document.addEventListener("DOMContentLoaded", function(event) {
23            renderMathInElement(document.body);
24        });
25
26    2016-08-09 Jason Sachs
27    * allow list of URLS for mathjax_path, which permits use of katex.js
28"""
29
30from docutils import nodes
31
32import sphinx
33from sphinx.locale import _
34from sphinx.errors import ExtensionError
35from sphinx.ext.mathbase import setup_math as mathbase_setup
36
37
38def html_visit_math(self, node):
39    self.body.append(self.starttag(node, 'span', '', CLASS='math'))
40    self.body.append(self.builder.config.mathjax_inline[0] +
41                     self.encode(node['latex']) +
42                     self.builder.config.mathjax_inline[1] + '</span>')
43    raise nodes.SkipNode
44
45
46def html_visit_displaymath(self, node):
47    self.body.append(self.starttag(node, 'div', CLASS='math'))
48    if node['nowrap']:
49        self.body.append(self.encode(node['latex']))
50        self.body.append('</div>')
51        raise nodes.SkipNode
52
53    # necessary to e.g. set the id property correctly
54    if node['number']:
55        self.body.append('<span class="eqno">(%s)' % node['number'])
56        self.add_permalink_ref(node, _('Permalink to this equation'))
57        self.body.append('</span>')
58    self.body.append(self.builder.config.mathjax_display[0])
59    parts = [prt for prt in node['latex'].split('\n\n') if prt.strip()]
60    if len(parts) > 1:  # Add alignment if there are more than 1 equation
61        self.body.append(r' \begin{align}\begin{aligned}')
62    for i, part in enumerate(parts):
63        part = self.encode(part)
64        if r'\\' in part:
65            self.body.append(r'\begin{split}' + part + r'\end{split}')
66        else:
67            self.body.append(part)
68        if i < len(parts) - 1:  # append new line if not the last equation
69            self.body.append(r'\\')
70    if len(parts) > 1:  # Add alignment if there are more than 1 equation
71        self.body.append(r'\end{aligned}\end{align} ')
72    self.body.append(self.builder.config.mathjax_display[1])
73    self.body.append('</div>\n')
74    raise nodes.SkipNode
75
76try:
77    basestring
78except:
79    basestring = str
80
81def builder_inited(app):
82    jaxpath = app.config.mathjax_path
83    if not jaxpath:
84        raise ExtensionError('mathjax_path config value must be set for the '
85                             'mathjax extension to work')
86
87    # app.config.mathjax_path can be a string or a list of strings
88    if isinstance(jaxpath, basestring):
89        app.add_javascript(jaxpath)
90    else:
91        for p in jaxpath:
92            app.add_javascript(p)
93
94    if app.config.mathjax_css:
95        app.add_stylesheet(app.config.mathjax_css)
96
97
98def setup(app):
99    try:
100        mathbase_setup(app, (html_visit_math, None), (html_visit_displaymath, None))
101    except ExtensionError:
102        raise ExtensionError('sphinx.ext.mathjax: other math package is already loaded')
103
104    # more information for mathjax secure url is here:
105    # http://docs.mathjax.org/en/latest/start.html#secure-access-to-the-cdn
106    app.add_config_value('mathjax_path',
107                         'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?'
108                         'config=TeX-MML-AM_CHTML',
109                         #'config=TeX-AMS-MML_HTMLorMML',
110                         False)
111    app.add_config_value('mathjax_css', None, 'html')
112    app.add_config_value('mathjax_use_katex', False, 'html')
113    app.add_config_value('mathjax_inline', [r'\(', r'\)'], 'html')
114    app.add_config_value('mathjax_display', [r'\[', r'\]'], 'html')
115    app.connect('builder-inited', builder_inited)
116
117    return {'version': sphinx.__display_version__, 'parallel_read_safe': True}
Note: See TracBrowser for help on using the repository browser.