|
1 # -*- coding: utf-8 -*- |
|
2 """ |
|
3 sphinx.ext.jsmath |
|
4 ~~~~~~~~~~~~~~~~~ |
|
5 |
|
6 Set up everything for use of JSMath to display math in HTML |
|
7 via JavaScript. |
|
8 |
|
9 :copyright: 2008 by Georg Brandl. |
|
10 :license: BSD. |
|
11 """ |
|
12 |
|
13 from docutils import nodes |
|
14 |
|
15 from sphinx.application import ExtensionError |
|
16 from sphinx.ext.mathbase import setup as mathbase_setup |
|
17 |
|
18 |
|
19 def html_visit_math(self, node): |
|
20 self.body.append(self.starttag(node, 'span', '', CLASS='math')) |
|
21 self.body.append(self.encode(node['latex']) + '</span>') |
|
22 raise nodes.SkipNode |
|
23 |
|
24 def html_visit_displaymath(self, node): |
|
25 if node['nowrap']: |
|
26 self.body.append(self.starttag(node, 'div', CLASS='math')) |
|
27 self.body.append(node['latex']) |
|
28 self.body.append('</div>') |
|
29 raise nodes.SkipNode |
|
30 for i, part in enumerate(node['latex'].split('\n\n')): |
|
31 part = self.encode(part) |
|
32 if i == 0: |
|
33 # necessary to e.g. set the id property correctly |
|
34 if node['number']: |
|
35 self.body.append('<span class="eqno">(%s)</span>' % node['number']) |
|
36 self.body.append(self.starttag(node, 'div', CLASS='math')) |
|
37 else: |
|
38 # but only once! |
|
39 self.body.append('<div class="math">') |
|
40 if '&' in part or '\\\\' in part: |
|
41 self.body.append('\\begin{split}' + part + '\\end{split}') |
|
42 else: |
|
43 self.body.append(part) |
|
44 self.body.append('</div>\n') |
|
45 raise nodes.SkipNode |
|
46 |
|
47 def builder_inited(app): |
|
48 if not app.config.jsmath_path: |
|
49 raise ExtensionError('jsmath_path config value must be set for the ' |
|
50 'jsmath extension to work') |
|
51 app.add_javascript(app.config.jsmath_path) |
|
52 |
|
53 |
|
54 def setup(app): |
|
55 mathbase_setup(app, (html_visit_math, None), (html_visit_displaymath, None)) |
|
56 app.add_config_value('jsmath_path', '', False) |
|
57 app.connect('builder-inited', builder_inited) |