From 497df185cb68d2fe4f4b500573496c049135cfa1 Mon Sep 17 00:00:00 2001 From: belerico Date: Tue, 21 Apr 2020 23:42:01 +0200 Subject: [PATCH 1/3] Delete double symbol --- py_asciimath/latex.py | 1 - 1 file changed, 1 deletion(-) diff --git a/py_asciimath/latex.py b/py_asciimath/latex.py index 796fc5b..0eb9318 100644 --- a/py_asciimath/latex.py +++ b/py_asciimath/latex.py @@ -308,7 +308,6 @@ def get_symbols_for(symbol_group, lang_to): # pragma: no cover ".": {"asciimath": ".", "mathml": "."}, "_": {"asciimath": "_", "mathml": "_"}, "'": {"asciimath": "'", "mathml": "'"}, - "/": {"asciimath": "/", "mathml": "/"}, "|": {"asciimath": "|", "mathml": "|"}, "\\vert": {"asciimath": "|", "mathml": "|"}, "\\mid": {"asciimath": "|", "mathml": "|"}, From ef581274b7db2538df2d457ddfa6c7c62a305657 Mon Sep 17 00:00:00 2001 From: belerico Date: Wed, 22 Apr 2020 15:08:09 +0200 Subject: [PATCH 2/3] Fix minors --- py_asciimath/asciimath.py | 4 ++-- py_asciimath/transformer/transformer.py | 9 +++++---- py_asciimath/translator/translator.py | 9 +++++++-- tests/test_ASCIIMath2MathML.py | 4 ++-- tests/test_Tex2ASCIIMath.py | 22 +++++++++++----------- 5 files changed, 27 insertions(+), 21 deletions(-) diff --git a/py_asciimath/asciimath.py b/py_asciimath/asciimath.py index 74c9b7a..343511c 100644 --- a/py_asciimath/asciimath.py +++ b/py_asciimath/asciimath.py @@ -387,8 +387,8 @@ def get_symbols_for(symbol_group, lang_to): # pragma: no cover "/": {"latex": "/", "mathml": "/"}, "|": {"latex": ["|", "\\vert", "\\mid"], "mathml": "|"}, ":": {"latex": ":", "mathml": ":"}, - "int": {"latex": "\\int", "mathml": "∫"}, - "integral": {"latex": "\\int", "mathml": "∫"}, + "int": {"latex": "\\int", "mathml": "∫"}, + "integral": {"latex": "\\int", "mathml": "∫"}, "oint": {"latex": "\\oint", "mathml": "∮"}, "del": {"latex": "\\partial", "mathml": "∂"}, "partial": {"latex": "\\partial", "mathml": "∂"}, diff --git a/py_asciimath/transformer/transformer.py b/py_asciimath/transformer/transformer.py index 75edaf6..e61ca22 100644 --- a/py_asciimath/transformer/transformer.py +++ b/py_asciimath/transformer/transformer.py @@ -312,6 +312,10 @@ def exp_par(self, items): def exp_unary(self, items): unary = mathml_una[items[0]] items[1] = self.remove_parenthesis(items[1]) + if items[0] == "text": + return encapsulate_mrow( + unary.format(re.sub(r"<.*?>", "", items[1])) + ) return encapsulate_mrow(unary.format(encapsulate_mrow(items[1]))) @MathTransformer.log @@ -402,7 +406,7 @@ def exp_under_super(self, items): items[0] = self.remove_parenthesis(items[0]) items[1] = self.remove_parenthesis(items[1]) items[1] = self.remove_parenthesis(items[2]) - return "(" + items[0] + ")^(" + items[1] + ")_(" + items[2] + ")" + return "(" + items[0] + ")_(" + items[1] + ")^(" + items[2] + ")" @log def exp_par(self, items): @@ -424,13 +428,10 @@ def exp_par(self, items): @log def exp_unary(self, items): - items[1] = self.remove_parenthesis(items[1]) return l2mml_una[items[0]] + "(" + items[1] + ")" @log def exp_binary(self, items): - items[1] = self.remove_parenthesis(items[1]) - items[2] = self.remove_parenthesis(items[2]) if items[0].startswith("\\sqrt"): return "root(" + "".join(items[1:-1]) + ")(" + items[-1] + ")" return l2mml_bin[items[0]] + "(" + items[1] + ")(" + items[2] + ")" diff --git a/py_asciimath/translator/translator.py b/py_asciimath/translator/translator.py index c49a02a..7556cfe 100644 --- a/py_asciimath/translator/translator.py +++ b/py_asciimath/translator/translator.py @@ -272,7 +272,7 @@ def _translate( parsed = MathMLParser.parse( parsed, dtd=dtd, - dtd_validation=dtd_validation, + dtd_validation=True, network=network, **kwargs ) @@ -313,7 +313,12 @@ def translate( against. It can be: `mathml1`, `mathml2` or `mathml3`. Defaults to None. dtd_validation (bool, optional): If True validate output against - the DTD version specified by `dtd`. + the DTD version specified by `dtd`. By default, if one of + `dtd`, `dtd_validation`, `xml_declaration` or `xml_pprint` is + True or is not None, then `dtd_validation` will be set to True. + This is because if either one of them set to True, then + py_asciimath must parse the input XML, but in order to do that + it needs to know how to intepret the entities from the DTD. Defaults to False. from_file (bool, optional): If True, load the string to translate from the file specified by s. Defaults to False. diff --git a/tests/test_ASCIIMath2MathML.py b/tests/test_ASCIIMath2MathML.py index 180b57f..5facdf4 100644 --- a/tests/test_ASCIIMath2MathML.py +++ b/tests/test_ASCIIMath2MathML.py @@ -80,7 +80,7 @@ def test_asciimath2mathml_ok_4(self): s, '\n122[3(x+1)]dx', + + '/dtd/mathml1/mathml1.dtd">\n122[3(x+1)]dx', ) def test_asciimath2mathml_ok_5(self): @@ -113,7 +113,7 @@ def test_asciimath2mathml_xml_fields_ok_1(self): ) self.assertEqual( s, - "\n1", + "\n\n1", ) def test_asciimath2mathml_xml_fields_ok_2(self): diff --git a/tests/test_Tex2ASCIIMath.py b/tests/test_Tex2ASCIIMath.py index fdd08a8..1179b49 100644 --- a/tests/test_Tex2ASCIIMath.py +++ b/tests/test_Tex2ASCIIMath.py @@ -17,17 +17,17 @@ def test_tex2asciimath_ok_1(self): ) self.assertEqual(s, "[{:[int x text(d x)],[log (x + 1)]:}]") - # def test_tex2asciimath_ok_2(self): - # s = Tex2ASCIIMath( - # inplace=True, parser="lalr", lexer="contextual", - # ).translate( - # "((1,2))int sin{x^2}/4pidxroot(5)(x_1^2+x_2^2)", displaystyle=True - # ) - # self.assertEqual( - # s, - # r"\[\left(\left(1 , 2\right)\right) \int \sin " - # r"\frac{{x}^{2}}{4} \pi \mathrm{dx} \sqrt[5]{{x}_{1}^{2} + {x}_{2}^{2}}\]", - # ) + def test_tex2asciimath_ok_2(self): + s = Tex2ASCIIMath( + inplace=True, parser="lalr", lexer="contextual", + ).translate( + r"\[\left(\left(1 , 2\right)\right) \int \sin " + r"\frac{{x}^{2}}{4} \pi \mathrm{dx} \sqrt[5]{{x}_{1}^{2} + {x}_{2}^{2}}\]", + ) + self.assertEqual( + s, + "((1 , 2)) int sin frac((x)^(2))(4) pi text(d x) root(5)((x)_(1)^(2) + (x)_(2)^(2))", + ) # def test_tex2asciimath_ok_3(self): # s = Tex2ASCIIMath( From e49569e6a67dd70b9d209f9eb7039827dad25b11 Mon Sep 17 00:00:00 2001 From: belerico Date: Wed, 22 Apr 2020 15:20:53 +0200 Subject: [PATCH 3/3] Fix minor --- py_asciimath/transformer/transformer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py_asciimath/transformer/transformer.py b/py_asciimath/transformer/transformer.py index e61ca22..48d0c99 100644 --- a/py_asciimath/transformer/transformer.py +++ b/py_asciimath/transformer/transformer.py @@ -405,7 +405,7 @@ def exp_super(self, items): def exp_under_super(self, items): items[0] = self.remove_parenthesis(items[0]) items[1] = self.remove_parenthesis(items[1]) - items[1] = self.remove_parenthesis(items[2]) + items[2] = self.remove_parenthesis(items[2]) return "(" + items[0] + ")_(" + items[1] + ")^(" + items[2] + ")" @log