summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormilde <milde@929543f6-e4f2-0310-98a6-ba3bd3dd1d04>2012-10-25 11:48:32 +0000
committermilde <milde@929543f6-e4f2-0310-98a6-ba3bd3dd1d04>2012-10-25 11:48:32 +0000
commit1867aa887538dde49fbdcbad87533aec3f4dcb99 (patch)
tree3618467667e990e2a6ab51bb7439ed2ddcfa2bf2
parentfaac66c9b0d7a4130f5272fc5e0c91a26842d48a (diff)
downloaddocutils-1867aa887538dde49fbdcbad87533aec3f4dcb99.tar.gz
Code cleanup and test html math output options.
Based on patch by Dmitry Shachnev. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@7534 929543f6-e4f2-0310-98a6-ba3bd3dd1d04
-rw-r--r--docutils/writers/html4css1/__init__.py12
-rw-r--r--test/DocutilsTestSupport.py20
-rwxr-xr-xtest/test_writers/test_html4css1_misc.py59
3 files changed, 77 insertions, 14 deletions
diff --git a/docutils/writers/html4css1/__init__.py b/docutils/writers/html4css1/__init__.py
index acc11aa1d..1ea34dd76 100644
--- a/docutils/writers/html4css1/__init__.py
+++ b/docutils/writers/html4css1/__init__.py
@@ -298,11 +298,8 @@ class HTMLTranslator(nodes.NodeVisitor):
self.section_level = 0
self.initial_header_level = int(settings.initial_header_level)
- self.math_output = settings.math_output.split(None, 1)
- if len(self.math_output) == 2:
- self.math_output_option = self.math_output[1]
- else:
- self.math_output_option = None
+ self.math_output = settings.math_output.split()
+ self.math_output_options = self.math_output[1:]
self.math_output = self.math_output[0].lower()
# A heterogenous stack used in conjunction with the tree traversal.
@@ -1206,8 +1203,9 @@ class HTMLTranslator(nodes.NodeVisitor):
if self.math_output in ('latex', 'mathjax'):
math_code = self.encode(math_code)
if self.math_output == 'mathjax':
- self.math_header = self.mathjax_script % (
- self.math_output_option or self.mathjax_url)
+ if self.math_output_options:
+ self.mathjax_url = self.math_output_options[0]
+ self.math_header = self.mathjax_script % self.mathjax_url
elif self.math_output == 'html':
math_code = math2html(math_code)
elif self.math_output == 'mathml':
diff --git a/test/DocutilsTestSupport.py b/test/DocutilsTestSupport.py
index 2988768b6..e410070d4 100644
--- a/test/DocutilsTestSupport.py
+++ b/test/DocutilsTestSupport.py
@@ -119,16 +119,28 @@ class StandardTestCase(unittest.TestCase):
operator.
"""
if not first == second:
- raise self.failureException, \
- (msg or '%s != %s' % _format_str(first, second))
+ raise self.failureException, (
+ msg or '%s != %s' % _format_str(first, second))
def assertNotEqual(self, first, second, msg=None):
"""Fail if the two objects are equal as determined by the '=='
operator.
"""
if first == second:
- raise self.failureException, \
- (msg or '%s == %s' % _format_str(first, second))
+ raise self.failureException, (
+ msg or '%s == %s' % _format_str(first, second))
+
+ # assertIn and assertNotIn: new in Python 2.7:
+
+ def assertIn(self, a, b, msg=None):
+ if a not in b:
+ raise self.failureException, (
+ msg or '%s not in %s' % _format_str(a, b))
+
+ def assertNotIn(self, a, b, msg=None):
+ if a in b:
+ raise self.failureException, (
+ msg or '%s in %s' % _format_str(a, b))
# aliases for assertion methods, deprecated since Python 2.7
diff --git a/test/test_writers/test_html4css1_misc.py b/test/test_writers/test_html4css1_misc.py
index e171e52ac..b0f32a351 100755
--- a/test/test_writers/test_html4css1_misc.py
+++ b/test/test_writers/test_html4css1_misc.py
@@ -1,7 +1,8 @@
#! /usr/bin/env python
+# coding: utf-8
# $Id$
-# Author: Lea Wiemann
+# Authors: Lea Wiemann, Dmitry Shachnev, Günter Milde
# Maintainer: docutils-develop@lists.sourceforge.net
# Copyright: This module has been placed in the public domain.
@@ -13,7 +14,6 @@ from __init__ import DocutilsTestSupport
from docutils import core
from docutils._compat import b
-
class EncodingTestCase(DocutilsTestSupport.StandardTestCase):
def test_xmlcharrefreplace(self):
@@ -28,7 +28,60 @@ class EncodingTestCase(DocutilsTestSupport.StandardTestCase):
settings_overrides=settings_overrides)
# Encoding a euro sign with latin1 doesn't work, so the
# xmlcharrefreplace handler is used.
- self.assertNotEqual(result.find(b('EUR = &#8364;')), -1)
+ self.assertIn(b('EUR = &#8364;'), result)
+
+class MathTestCase(DocutilsTestSupport.StandardTestCase):
+
+ """Attention: This class tests the current implementation of maths support
+ which is open to change in future Docutils releases. """
+
+ settings_overrides={'_disable_config': True,}
+ mathjax_script = '<script type="text/javascript" src="%s">'
+ default_mathjax_url = ('http://cdn.mathjax.org/mathjax/latest/MathJax.js'
+ '?config=TeX-AMS-MML_HTMLorMML')
+ custom_mathjax_url = ('file:///usr/share/javascript/mathjax/MathJax.js'
+ '?config=TeX-AMS-MML_HTMLorMML')
+ data = ':math:`42`'
+
+ def test_math_output_default(self):
+ # Currently MathJax with default URL. Likely to change to HTML!
+ mysettings = self.settings_overrides
+ head = core.publish_parts(self.data, writer_name='html4css1',
+ settings_overrides=mysettings)['head']
+ self.assertIn(self.mathjax_script % self.default_mathjax_url, head)
+
+ def test_math_output_mathjax(self):
+ # Explicitly specifying math_output=MathJax, case insensitively
+ # use default MathJax URL
+ mysettings = self.settings_overrides.copy()
+ mysettings.update({'math_output': 'MathJax'})
+ head = core.publish_parts(self.data, writer_name='html4css1',
+ settings_overrides=mysettings)['head']
+ self.assertIn(self.mathjax_script % self.default_mathjax_url, head)
+
+ def test_math_output_mathjax_custom(self):
+ # Customizing MathJax URL
+ mysettings = self.settings_overrides.copy()
+ mysettings.update({'math_output':
+ 'mathjax %s' % self.custom_mathjax_url})
+ head = core.publish_parts(self.data, writer_name='html4css1',
+ settings_overrides=mysettings)['head']
+ self.assertIn(self.mathjax_script % self.custom_mathjax_url, head)
+
+ def test_math_output_html(self):
+ # There should be no MathJax script when math_output is not MathJax
+ mysettings = self.settings_overrides.copy()
+ mysettings.update({'math_output': 'HTML'})
+ head = core.publish_parts(self.data, writer_name='html4css1',
+ settings_overrides=mysettings)['head']
+ self.assertNotIn('MathJax.js', head)
+
+ def test_math_output_mathjax_no_math(self):
+ mysettings = self.settings_overrides.copy()
+ mysettings.update({'math_output': 'MathJax'})
+ # There should be no math script when text does not contain math
+ head = core.publish_parts('No math.', writer_name='html4css1')['head']
+ self.assertNotIn('MathJax', head)
if __name__ == '__main__':