Skip to content

Commit

Permalink
Merge pull request #6 from belerico/latex2asciimath
Browse files Browse the repository at this point in the history
Latex2asciimath fix minor issues
  • Loading branch information
belerico authored Apr 22, 2020
2 parents b4d96f5 + e49569e commit 195b2b5
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 23 deletions.
4 changes: 2 additions & 2 deletions py_asciimath/asciimath.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": "∂"},
Expand Down
1 change: 0 additions & 1 deletion py_asciimath/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": "|"},
Expand Down
11 changes: 6 additions & 5 deletions py_asciimath/transformer/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -401,8 +405,8 @@ 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])
return "(" + items[0] + ")^(" + items[1] + ")_(" + items[2] + ")"
items[2] = self.remove_parenthesis(items[2])
return "(" + items[0] + ")_(" + items[1] + ")^(" + items[2] + ")"

@log
def exp_par(self, items):
Expand All @@ -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] + ")"
Expand Down
9 changes: 7 additions & 2 deletions py_asciimath/translator/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ def _translate(
parsed = MathMLParser.parse(
parsed,
dtd=dtd,
dtd_validation=dtd_validation,
dtd_validation=True,
network=network,
**kwargs
)
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions tests/test_ASCIIMath2MathML.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def test_asciimath2mathml_ok_4(self):
s,
'<!DOCTYPE math SYSTEM "'
+ PROJECT_ROOT
+ '/dtd/mathml1/mathml1.dtd">\n<math><mstyle displaystyle="true"><mrow><mo>&langle;</mo><mtable><mtr><mtd><mn>1</mn></mtd><mtd><mn>2</mn></mtd></mtr><mtr><mtd><mn>2</mn></mtd><mtd><mo>&Integral;</mo><mrow><mo>[</mo><mrow><mn>3</mn><mrow><mo>(</mo><mrow><mi>x</mi><mo>+</mo><mn>1</mn></mrow><mo>)</mo></mrow></mrow><mo>]</mo></mrow><mi>dx</mi></mtd></mtr></mtable><mo/></mrow></mstyle></math>',
+ '/dtd/mathml1/mathml1.dtd">\n<math><mstyle displaystyle="true"><mrow><mo>&langle;</mo><mtable><mtr><mtd><mn>1</mn></mtd><mtd><mn>2</mn></mtd></mtr><mtr><mtd><mn>2</mn></mtd><mtd><mo>&int;</mo><mrow><mo>[</mo><mrow><mn>3</mn><mrow><mo>(</mo><mrow><mi>x</mi><mo>+</mo><mn>1</mn></mrow><mo>)</mo></mrow></mrow><mo>]</mo></mrow><mi>dx</mi></mtd></mtr></mtable><mo/></mrow></mstyle></math>',
)

def test_asciimath2mathml_ok_5(self):
Expand Down Expand Up @@ -113,7 +113,7 @@ def test_asciimath2mathml_xml_fields_ok_1(self):
)
self.assertEqual(
s,
"<?xml version='1.0' encoding='UTF-8'?>\n<math><mn>1</mn></math>",
"<?xml version='1.0' encoding='UTF-8'?>\n<!DOCTYPE math SYSTEM \"http://www.w3.org/Math/DTD/mathml1/mathml.dtd\">\n<math><mn>1</mn></math>",
)

def test_asciimath2mathml_xml_fields_ok_2(self):
Expand Down
22 changes: 11 additions & 11 deletions tests/test_Tex2ASCIIMath.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 195b2b5

Please sign in to comment.