From: <mi...@us...> - 2012-10-25 11:48:40
|
Revision: 7534 http://docutils.svn.sourceforge.net/docutils/?rev=7534&view=rev Author: milde Date: 2012-10-25 11:48:32 +0000 (Thu, 25 Oct 2012) Log Message: ----------- Code cleanup and test html math output options. Based on patch by Dmitry Shachnev. Modified Paths: -------------- trunk/docutils/docutils/writers/html4css1/__init__.py trunk/docutils/test/DocutilsTestSupport.py trunk/docutils/test/test_writers/test_html4css1_misc.py Modified: trunk/docutils/docutils/writers/html4css1/__init__.py =================================================================== --- trunk/docutils/docutils/writers/html4css1/__init__.py 2012-10-21 17:24:53 UTC (rev 7533) +++ trunk/docutils/docutils/writers/html4css1/__init__.py 2012-10-25 11:48:32 UTC (rev 7534) @@ -298,11 +298,8 @@ 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 @@ 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': Modified: trunk/docutils/test/DocutilsTestSupport.py =================================================================== --- trunk/docutils/test/DocutilsTestSupport.py 2012-10-21 17:24:53 UTC (rev 7533) +++ trunk/docutils/test/DocutilsTestSupport.py 2012-10-25 11:48:32 UTC (rev 7534) @@ -119,17 +119,29 @@ 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 failUnlessEqual = assertEquals = assertEqual Modified: trunk/docutils/test/test_writers/test_html4css1_misc.py =================================================================== --- trunk/docutils/test/test_writers/test_html4css1_misc.py 2012-10-21 17:24:53 UTC (rev 7533) +++ trunk/docutils/test/test_writers/test_html4css1_misc.py 2012-10-25 11:48:32 UTC (rev 7534) @@ -1,7 +1,8 @@ #! /usr/bin/env python +# coding: utf-8 # $Id$ -# Author: Lea Wiemann +# Authors: Lea Wiemann, Dmitry Shachnev, Günter Milde # Maintainer: doc...@li... # Copyright: This module has been placed in the public domain. @@ -13,7 +14,6 @@ from docutils import core from docutils._compat import b - class EncodingTestCase(DocutilsTestSupport.StandardTestCase): def test_xmlcharrefreplace(self): @@ -28,9 +28,62 @@ 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 = €')), -1) + self.assertIn(b('EUR = €'), 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__': import unittest unittest.main() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |