Skip to content

Commit

Permalink
Add Tex2ASCIIMath transaltor
Browse files Browse the repository at this point in the history
  • Loading branch information
belerico committed Apr 21, 2020
1 parent e5a78a4 commit d74d0ac
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 14 deletions.
6 changes: 3 additions & 3 deletions py_asciimath/transformer/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def q_str(self, items):
raise NotImplementedError


class LatexTransformer(ASCIIMathTransformer):
class ASCIIMath2TexTransformer(ASCIIMathTransformer):
"""Trasformer class, read `lark.Transformer`."""

def __init__(self, log=True, visit_tokens=False):
Expand Down Expand Up @@ -212,7 +212,7 @@ def q_str(self, items):
return "\\text{" + items[0].strip('"') + "}"


class MathMLTransformer(ASCIIMathTransformer):
class ASCIIMath2MathMLTransformer(ASCIIMathTransformer):
"""Trasformer class, read `lark.Transformer`."""

def __init__(self, log=True, visit_tokens=False):
Expand Down Expand Up @@ -352,7 +352,7 @@ def q_str(self, items):
return "<mtext>" + items[0].strip('"') + "</mtext>"


class TexTransformer(Transformer): # pragma: no cover
class Tex2ASCIIMathTransformer(Transformer): # pragma: no cover
def __init__(
self, log=True, start_end_par_pattern="{}{}", visit_tokens=False
):
Expand Down
74 changes: 63 additions & 11 deletions py_asciimath/translator/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
from lark import Lark
from .. import PROJECT_ROOT
from ..grammar.asciimath_grammar import asciimath_grammar
from ..grammar.latex_grammar import latex_grammar
from ..parser.parser import MathMLParser
from ..transformer.transformer import LatexTransformer, MathMLTransformer
from ..transformer.transformer import (
ASCIIMath2TexTransformer,
ASCIIMath2MathMLTransformer,
Tex2ASCIIMathTransformer,
)
from ..utils.utils import check_connection

logging.basicConfig(format="%(levelname)s:%(message)s", level=logging.DEBUG)
Expand Down Expand Up @@ -64,14 +69,15 @@ def translate(self, exp, from_file=False, to_file=None, **kwargs):
return exp


class ASCIIMathTranslator(Translator):
"""Class that handle the translation from ASCIIMath.
class LarkTranslator(Translator):
"""Class that handle the translation from a Lark parsed
language to the one specified by a Transformer.
An ASCIIMathTranslator translates an ASCIIMath string into another
A LarkTranslator translates a string, parsed with Lark, into another
language, specified by the `transformer` parameter
Args:
grammar (str): ASCIIMath grammar
grammar (str): BNF grammar to parse the input
transformer (lark.Transformer): A transformer instance to transform
parsed input. See :class:`~lark.Transformer`
inplace (bool, optional): If True, parse the input inplace.
Expand All @@ -92,7 +98,7 @@ def __init__(
parser="lalr",
**kwargs
):
super(ASCIIMathTranslator, self).__init__()
super(LarkTranslator, self).__init__()
self.inplace = inplace
self.grammar = grammar
self.transformer = transformer
Expand Down Expand Up @@ -128,12 +134,12 @@ def translate(
Returns:
str: Translated expression
"""
return super(ASCIIMathTranslator, self).translate(
return super(LarkTranslator, self).translate(
exp, from_file=from_file, to_file=to_file, pprint=pprint, **kwargs
)


class ASCIIMath2Tex(ASCIIMathTranslator):
class ASCIIMath2Tex(LarkTranslator):
"""Class that handle the translation from ASCIIMath to LaTeX
Args:
Expand All @@ -150,7 +156,7 @@ class ASCIIMath2Tex(ASCIIMathTranslator):

def __init__(self, log=False, **kwargs):
super(ASCIIMath2Tex, self).__init__(
asciimath_grammar, LatexTransformer(log=log), **kwargs
asciimath_grammar, ASCIIMath2TexTransformer(log=log), **kwargs
)

def _translate(self, exp, displaystyle=False, pprint=False):
Expand Down Expand Up @@ -201,7 +207,7 @@ def translate(
)


class ASCIIMath2MathML(ASCIIMathTranslator):
class ASCIIMath2MathML(LarkTranslator):
"""Class that handle the translation from ASCIIMath to MathML
Args:
Expand All @@ -218,7 +224,7 @@ class ASCIIMath2MathML(ASCIIMathTranslator):

def __init__(self, log=False, **kwargs):
super(ASCIIMath2MathML, self).__init__(
asciimath_grammar, MathMLTransformer(log=log), **kwargs
asciimath_grammar, ASCIIMath2MathMLTransformer(log=log), **kwargs
)
self.__output = ["string", "etree"]

Expand Down Expand Up @@ -348,6 +354,52 @@ def translate(
)


class Tex2ASCIIMath(LarkTranslator):
"""Class that handle the translation from LaTeX to ASCIIMath
Args:
inplace (bool, optional): If True, parse the input inplace.
See :class:`~lark.Lark`. Defaults to True.
lexer (str, optional): Lexer used during parsing. See :class:`~lark.Lark`.
Defaults to "contextual".
log (bool, optional): If True log the parsing process.
Defaults to False.
parser (str, optional): Parser algorithm. See :class:`~lark.Lark`.
Defaults to "lalr".
**kwargs: Additional keyword arguments to the :class:`~lark.Lark` class.
"""

def __init__(self, log=False, **kwargs):
super(Tex2ASCIIMath, self).__init__(
latex_grammar, Tex2ASCIIMathTransformer(log=log), **kwargs
)

def _translate(self, exp, pprint=False):
return super(Tex2ASCIIMath, self)._translate(exp, pprint=pprint)

def translate(
self, exp, from_file=False, pprint=False, to_file=None,
):
"""Translates an ASCIIMath string to LaTeX
Args:
exp (str): String to translate. If from_file is True, then s
must represent the file's path
from_file (bool, optional): If True, load the string to translate
from the file specified by s. Defaults to False.
pprint (bool, optional): Abstract Syntax Tree pretty print.
Defaults to False.
to_file (str, optional): If specified, save the translation to
`to_file`. Defaults to None.
Returns:
str: LaTeX translated expression
"""
return super(Tex2ASCIIMath, self).translate(
exp, from_file=from_file, pprint=pprint, to_file=to_file,
)


class MathML2Tex(Translator): # pragma: no cover
"""Class that handle the translation from MathML to LaTeX
Expand Down

0 comments on commit d74d0ac

Please sign in to comment.