|
From: <mi...@us...> - 2021-05-20 12:19:13
|
Revision: 8737
http://sourceforge.net/p/docutils/code/8737
Author: milde
Date: 2021-05-20 12:19:09 +0000 (Thu, 20 May 2021)
Log Message:
-----------
MathML: Replace deprecated <mfenced> element.
Modified Paths:
--------------
trunk/docutils/docutils/utils/math/latex2mathml.py
trunk/docutils/test/functional/expected/math_output_mathml.html
Modified: trunk/docutils/docutils/utils/math/latex2mathml.py
===================================================================
--- trunk/docutils/docutils/utils/math/latex2mathml.py 2021-05-20 12:18:55 UTC (rev 8736)
+++ trunk/docutils/docutils/utils/math/latex2mathml.py 2021-05-20 12:19:09 UTC (rev 8737)
@@ -2,9 +2,9 @@
# -*- coding: utf-8 -*-
# :Id: $Id$
-# :Copyright: © 2010 Günter Milde.
-# Based on rst2mathml.py from the latex_math sandbox project
-# © 2005 Jens Jørgen Mortensen
+# :Copyright: © 2005 Jens Jørgen Mortensen [1]_
+# © 2010, 2021 Günter Milde.
+#
# :License: Released under the terms of the `2-Clause BSD license`_, in short:
#
# Copying and distribution of this file, with or without modification,
@@ -15,9 +15,14 @@
# .. _2-Clause BSD license: https://opensource.org/licenses/BSD-2-Clause
-"""Convert LaTex math code into presentational MathML"""
+"""Convert LaTex maths code into presentational MathML.
+"""
-# Based on the `latex_math` sandbox project by Jens Jørgen Mortensen
+# .. [1] the original `rst2mathml.py` in `sandbox/jensj/latex_math`
+#
+# Usage:
+#
+# >>> import latex2mathml
import docutils.utils.math.tex2unichar as tex2unichar
@@ -56,6 +61,9 @@
sumintprod = ''.join([special[symbol] for symbol in
['sum', 'int', 'oint', 'prod']])
+# >>> print(latex2mathml.sumintprod)
+# ∑∫∮∏
+
functions = ['arccos', 'arcsin', 'arctan', 'arg', 'cos', 'cosh',
'cot', 'coth', 'csc', 'deg', 'det', 'dim',
'exp', 'gcd', 'hom', 'inf', 'ker', 'lg',
@@ -150,10 +158,43 @@
'z': u'\U0001D4CF',
}
+# >>> print(''.join(latex2mathml.mathscr.values()))
+# 𝒜ℬ𝒞𝒟ℰℱ𝒢ℋℐ𝒥𝒦ℒℳ𝒩𝒪𝒫𝒬ℛ𝒮𝒯𝒰𝒱𝒲𝒳𝒴𝒵𝒶𝒷𝒸𝒹ℯ𝒻ℊ𝒽𝒾𝒿𝓀𝓁𝓂𝓃ℴ𝓅𝓆𝓇𝓈𝓉𝓊𝓋𝓌𝓍𝓎𝓏
+
negatables = {'=': u'\u2260',
r'\in': u'\u2209',
r'\equiv': u'\u2262'}
+# cmds/characters allowed in left/right cmds
+fence_args = {'(': '(',
+ ')': ')',
+ '[': '[',
+ ']': ']',
+ '/': '/',
+ r'\backslash': '\\',
+ '|': '|',
+ '.': '', # emty fence
+ r'\uparrow': u'\u2191', # ↑ UPWARDS ARROW
+ r'\downarrow': u'\u2193', # ↓ DOWNWARDS ARROW
+ r'\updownarrow': u'\u2195', # ↕ UP DOWN ARROW
+ r'\Uparrow': u'\u21d1', # ⇑ UPWARDS DOUBLE ARROW
+ r'\Downarrow': u'\u21d3', # ⇓ DOWNWARDS DOUBLE ARROW
+ r'\Updownarrow': u'\u21d5', # ⇕ UP DOWN DOUBLE ARROW
+
+ }
+for (key, value) in tex2unichar.mathfence.items():
+ fence_args['\\'+key] = value
+for (key, value) in tex2unichar.mathopen.items():
+ fence_args['\\'+key] = value
+for (key, value) in tex2unichar.mathclose.items():
+ fence_args['\\'+key] = value
+# shorter with {**something} syntax, new in 3.5
+# if sys.version_info >= (3, 5):
+# for (key, value) in {**tex2unichar.mathclose,
+# **tex2unichar.mathopen,
+# **tex2unichar.mathfence}.items():
+# fence_args['\\'+key] = value
+
# LaTeX to MathML translation stuff:
class math(object):
"""Base class for MathML elements."""
@@ -233,9 +274,10 @@
return ['<%s>' % self.__class__.__name__]
xmlns = 'http://www.w3.org/1998/Math/MathML'
if self.inline:
- return ['<math xmlns="%s" displaystyle="false">' % xmlns]
+ args = 'displaystyle="false"'
else:
- return ['<math xmlns="%s" display="block">' % xmlns]
+ args = 'display="block"'
+ return ['<math xmlns="%s" %s>' % (xmlns, args)]
def xml_end(self):
return ['</%s>' % self.__class__.__name__]
@@ -268,10 +310,13 @@
return [self.data]
class mo(mx):
- translation = {'<': '<', '>': '>'}
+ translation = {ord('<'): u'<', ord('>'): u'>'}
def xml_body(self):
- return [self.translation.get(self.data, self.data)]
+ return [self.data.translate(self.translation)]
+# >>> latex2mathml.mo(u'<').xml()
+# ['<mo>', '<', '</mo>']
+
class mi(mx): pass
class mn(mx): pass
@@ -303,19 +348,6 @@
self.reversed = False
return math.xml(self)
-class mfenced(math):
- translation = {'\\{': '{', '\\langle': u'\u2329',
- '\\}': '}', '\\rangle': u'\u232A',
- '.': ''}
- def __init__(self, par):
- self.openpar = par
- math.__init__(self)
-
- def xml_start(self):
- open = self.translation.get(self.openpar, self.openpar)
- close = self.translation.get(self.closepar, self.closepar)
- return ['<mfenced open="%s" close="%s">' % (open, close)]
-
class mspace(math):
nchildren = 0
@@ -494,26 +526,24 @@
node.append(frac)
node = frac
elif name == 'left':
- for par in ['(', '[', '|', '\\{', '\\langle', '.']:
+ for par in fence_args.keys():
if string.startswith(par):
break
else:
raise SyntaxError(u'Missing left-brace!')
- fenced = mfenced(par)
- node.append(fenced)
row = mrow()
- fenced.append(row)
+ node.append(row)
node = row
+ node.append(mo(fence_args[par]))
skip += len(par)
elif name == 'right':
- for par in [')', ']', '|', '\\}', '\\rangle', '.']:
+ for par in fence_args.keys():
if string.startswith(par):
break
else:
raise SyntaxError(u'Missing right-brace!')
+ node.append(mo(fence_args[par]))
node = node.close()
- node.closepar = par
- node = node.close()
skip += len(par)
elif name == 'not':
for operator in negatables:
Modified: trunk/docutils/test/functional/expected/math_output_mathml.html
===================================================================
--- trunk/docutils/test/functional/expected/math_output_mathml.html 2021-05-20 12:18:55 UTC (rev 8736)
+++ trunk/docutils/test/functional/expected/math_output_mathml.html 2021-05-20 12:19:09 UTC (rev 8737)
@@ -25,10 +25,10 @@
<mtr>
<mtd><mi>f</mi><mo>(</mo><mi>ϵ</mi><mo>)</mo><mo>=</mo><mfrac>
<mrow><mn>1</mn></mrow>
-<mrow><mn>1</mn><mo>+</mo><mo>exp</mo><mfenced open="(" close=")">
-<mrow><mfrac>
+<mrow><mn>1</mn><mo>+</mo><mo>exp</mo>
+<mrow><mo>(</mo><mfrac>
<mrow><mi>ε</mi></mrow>
-<mrow><msub><mi>k</mi><mtext>B</mtext></msub><mi>T</mi></mrow></mfrac></mrow></mfenced></mrow></mfrac></mtd></mtr></mtable></math>
+<mrow><msub><mi>k</mi><mtext>B</mtext></msub><mi>T</mi></mrow></mfrac><mo>)</mo></mrow></mrow></mfrac></mtd></mtr></mtable></math>
</div>
<p>Content may start on the first line of the directive, e.g.</p>
<div>
@@ -47,8 +47,8 @@
<mtable>
<mtr>
<mtd><mstyle mathvariant="bold">
-<mrow><mi>M</mi></mrow></mstyle><mo>=</mo><mfenced open="(" close=")">
-<mrow>
+<mrow><mi>M</mi></mrow></mstyle><mo>=</mo>
+<mrow><mo>(</mo>
<mtable>
<mtr>
<mtd><mi>a</mi></mtd>
@@ -55,7 +55,7 @@
<mtd><mi>b</mi></mtd></mtr>
<mtr>
<mtd><mi>c</mi></mtd>
-<mtd><mi>d</mi></mtd></mtr></mtable></mrow></mfenced></mtd></mtr></mtable></math>
+<mtd><mi>d</mi></mtd></mtr></mtable><mo>)</mo></mrow></mtd></mtr></mtable></math>
</div>
<p>is <math xmlns="http://www.w3.org/1998/Math/MathML" displaystyle="false">
<mrow><mo>|</mo><mstyle mathvariant="bold">
@@ -154,13 +154,13 @@
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block">
<mtable>
<mtr>
-<mtd><mtext>MTF</mtext><mo>=</mo><mfenced open="|" close="|">
-<mrow><mfrac>
+<mtd><mtext>MTF</mtext><mo>=</mo>
+<mrow><mo>|</mo><mfrac>
<mrow><mi>ℱ</mi><mo>{</mo><mi>s</mi><mo>(</mo><mi>x</mi><mo>)</mo><mo>}</mo></mrow>
<mrow><mi>ℱ</mi><mo>{</mo><mi>s</mi><mo>(</mo><mi>x</mi><mo>)</mo><mo>}</mo><msub><mo>|</mo>
<mrow><msub><mi>ω</mi>
-<mrow><mi>x</mi></mrow></msub><mo>=</mo><mn>0</mn></mrow></msub></mrow></mfrac></mrow></mfenced><mo>=</mo><mtext>abs</mtext><mfenced open="(" close=")">
-<mrow><mfrac>
+<mrow><mi>x</mi></mrow></msub><mo>=</mo><mn>0</mn></mrow></msub></mrow></mfrac><mo>|</mo></mrow><mo>=</mo><mtext>abs</mtext>
+<mrow><mo>(</mo><mfrac>
<mrow><munderover><mo>∫</mo>
<mrow><mo>-</mo><mo>∞</mo></mrow>
<mrow><mo>∞</mo></mrow></munderover><mi>s</mi><mo>(</mo><mi>x</mi><mo>)</mo><msup><mtext>e</mtext>
@@ -170,7 +170,7 @@
<mrow><munderover><mo>∫</mo>
<mrow><mo>-</mo><mo>∞</mo></mrow>
<mrow><mo>∞</mo></mrow></munderover><mi>s</mi><mo>(</mo><mi>x</mi><mo>)</mo><mtext>d</mtext>
-<mrow><mi>x</mi></mrow></mrow></mfrac></mrow></mfenced><mo>.</mo></mtd></mtr></mtable></math>
+<mrow><mi>x</mi></mrow></mrow></mfrac><mo>)</mo></mrow><mo>.</mo></mtd></mtr></mtable></math>
</div>
<p>Math split over two lines: If a double backslash is detected outside a
<span class="docutils literal"><span class="pre">\begin{...}</span> <span class="pre">\end{...}</span></span> pair, the math code is wrapped in an <a class="reference external" href="ftp://ftp.ams.org/ams/doc/amsmath/short-math-guide.pdf">AMSmath</a>
@@ -193,8 +193,8 @@
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block">
<mtable>
<mtr>
-<mtd><mtext>sgn</mtext><mo>(</mo><mi>x</mi><mo>)</mo><mo>=</mo><mfenced open="{" close="">
-<mrow>
+<mtd><mtext>sgn</mtext><mo>(</mo><mi>x</mi><mo>)</mo><mo>=</mo>
+<mrow><mo>{</mo>
<mtable>
<mtr>
<mtd><mo>-</mo><mn>1</mn></mtd>
@@ -201,7 +201,7 @@
<mtd><mi>x</mi><mo><</mo><mn>0</mn></mtd></mtr>
<mtr>
<mtd><mn>1</mn></mtd>
-<mtd><mi>x</mi><mo>></mo><mn>0</mn></mtd></mtr></mtable></mrow></mfenced></mtd></mtr></mtable></math>
+<mtd><mi>x</mi><mo>></mo><mn>0</mn></mtd></mtr></mtable><mo></mo></mrow></mtd></mtr></mtable></math>
</div>
<p>Cases with the <a class="reference external" href="ftp://ftp.ams.org/ams/doc/amsmath/short-math-guide.pdf">AMSmath</a> <span class="docutils literal">cases</span> environment (not (yet) supported by
HTML writers with <span class="docutils literal"><span class="pre">--math-output=MathML</span></span>):</p>
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|