Commits

Erik Hetzner committed af18423

add htmlmath math extension

Comments (0)

Files changed (1)

sphinx/ext/htmlmath.py

+# -*- coding: utf-8 -*-
+"""
+    sphinx.ext.htmlmath
+    ~~~~~~~~~~~~~~~~~
+
+    Simple HTML math.
+
+    :copyright: Copyright 2007-2011 by the Sphinx team, see AUTHORS.
+    :license: BSD, see LICENSE for details.
+"""
+
+from docutils import nodes
+from docutils.math.math2html import math2html
+from docutils.math import unichar2tex
+
+from sphinx.application import ExtensionError
+from sphinx.ext.mathbase import setup_math as mathbase_setup
+
+def html_visit_math(self, node):
+    self.body.append(math2html(node['latex'].translate(unichar2tex.uni2tex_table)))
+    raise nodes.SkipNode
+
+def html_visit_displaymath(self, node):
+    if node.get('label'):
+        self.add_anchor(node['label'], node)
+    if node['nowrap']:
+        self.body.append(self.starttag(node, 'div', CLASS='math'))
+        self.body.append(math2html(node['latex'].translate(unichar2tex.uni2tex_table)))
+        self.body.append('</div>')
+        raise nodes.SkipNode
+    for i, part in enumerate(node['latex'].split('\n\n')):
+        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'])
+            self.body.append(self.starttag(node, 'div', CLASS='math'))
+        else:
+            # but only once!
+            self.body.append('<div class="math">')
+        self.body.append(math2html(part.translate(unichar2tex.uni2tex_table)))
+        self.body.append('</div>\n')
+    raise nodes.SkipNode
+
+def setup(app):
+    mathbase_setup(app, (html_visit_math, None), (html_visit_displaymath, None))