From: <je...@us...> - 2006-12-12 15:53:24
|
Author: jensj Date: 2006-12-12 16:53:18 +0100 (Tue, 12 Dec 2006) New Revision: 4834 Modified: trunk/sandbox/jensj/latex_math/test/test.txt trunk/sandbox/jensj/latex_math/tools/rst2latexmath.py trunk/sandbox/jensj/latex_math/tools/rst2mathml.py Log: Registration of new latex-math directive has been changed to use the new class-based style. Modified: trunk/sandbox/jensj/latex_math/test/test.txt =================================================================== --- trunk/sandbox/jensj/latex_math/test/test.txt 2006-12-12 15:26:47 UTC (rev 4833) +++ trunk/sandbox/jensj/latex_math/test/test.txt 2006-12-12 15:53:18 UTC (rev 4834) @@ -1,7 +1,7 @@ Testing MathML -------------- -Inline math: :latex-math:`\sin(x_n^2)`. And displayed math: +Inline math: :latex-math:`\siin(x_n^2)`. And displayed math: .. latex-math:: Modified: trunk/sandbox/jensj/latex_math/tools/rst2latexmath.py =================================================================== --- trunk/sandbox/jensj/latex_math/tools/rst2latexmath.py 2006-12-12 15:26:47 UTC (rev 4833) +++ trunk/sandbox/jensj/latex_math/tools/rst2latexmath.py 2006-12-12 15:53:18 UTC (rev 4834) @@ -13,7 +13,6 @@ from docutils.parsers.rst.roles import register_canonical_role from docutils import nodes from docutils.writers.latex2e import LaTeXTranslator -from docutils.parsers.rst.directives import _directives from docutils.core import publish_cmdline, default_description @@ -35,17 +34,29 @@ # Register directive: -def latex_math_directive(name, arguments, options, content, lineno, - content_offset, block_text, state, state_machine): - latex = ''.join(content) - node = latex_math(block_text, latex) - return [node] -latex_math_directive.arguments = None -latex_math_directive.options = {} -latex_math_directive.content = 1 -_directives['latex-math'] = latex_math_directive # XXX +try: + from docutils.parsers.rst import Directive +except ImportError: + from docutils.parsers.rst.directives import _directives + def latex_math_directive(name, arguments, options, content, lineno, + content_offset, block_text, state, state_machine): + latex = ''.join(content) + node = latex_math(block_text, latex) + return [node] + latex_math_directive.arguments = None + latex_math_directive.options = {} + latex_math_directive.content = 1 + _directives['latex-math'] = latex_math_directive +else: + from docutils.parsers.rst import directives + class latex_math_directive(Directive): + has_content = True + def run(self): + latex = ''.join(self.content) + node = latex_math(self.block_text, latex) + return [node] + directives.register_directive('latex-math', latex_math_directive) - # Add visit/depart methods to HTML-Translator: def visit_latex_math(self, node): inline = isinstance(node.parent, nodes.TextElement) Modified: trunk/sandbox/jensj/latex_math/tools/rst2mathml.py =================================================================== --- trunk/sandbox/jensj/latex_math/tools/rst2mathml.py 2006-12-12 15:26:47 UTC (rev 4833) +++ trunk/sandbox/jensj/latex_math/tools/rst2mathml.py 2006-12-12 15:53:18 UTC (rev 4834) @@ -13,7 +13,6 @@ from docutils.parsers.rst.roles import register_canonical_role from docutils import nodes from docutils.writers.html4css1 import HTMLTranslator -from docutils.parsers.rst.directives import _directives from docutils.core import publish_cmdline, default_description @@ -41,23 +40,44 @@ # Register directive: -def latex_math_directive(name, arguments, options, content, lineno, - content_offset, block_text, state, state_machine): - latex = ''.join(content) - try: - mathml_tree = parse_latex_math(latex, inline=False) - except SyntaxError, msg: - error = state_machine.reporter.error( - msg, nodes.literal_block(block_text, block_text), line=lineno) - return [error] - node = latex_math(block_text, mathml_tree) - return [node] -latex_math_directive.arguments = None -latex_math_directive.options = {} -latex_math_directive.content = 1 -_directives['latex-math'] = latex_math_directive +try: + from docutils.parsers.rst import Directive +except ImportError: + # Register directive the old way: + from docutils.parsers.rst.directives import _directives + def latex_math_directive(name, arguments, options, content, lineno, + content_offset, block_text, state, state_machine): + latex = ''.join(content) + try: + mathml_tree = parse_latex_math(latex, inline=False) + except SyntaxError, msg: + error = state_machine.reporter.error( + msg, nodes.literal_block(block_text, block_text), line=lineno) + return [error] + node = latex_math(block_text, mathml_tree) + return [node] + latex_math_directive.arguments = None + latex_math_directive.options = {} + latex_math_directive.content = 1 + _directives['latex-math'] = latex_math_directive +else: + # Register directive the new way: + from docutils.parsers.rst import directives + class latex_math_directive(Directive): + has_content = True + def run(self): + latex = ''.join(self.content) + try: + mathml_tree = parse_latex_math(latex, inline=False) + except SyntaxError, msg: + error = self.state_machine.reporter.error( + msg, nodes.literal_block(self.block_text, self.block_text), + line=self.lineno) + return [error] + node = latex_math(self.block_text, mathml_tree) + return [node] + directives.register_directive('latex-math', latex_math_directive) - # Add visit/depart methods to HTML-Translator: def visit_latex_math(self, node): mathml = ''.join(node.mathml_tree.xml()) |