Skip to content

Commit

Permalink
✨ Allow xml conversion only (#431)
Browse files Browse the repository at this point in the history
* Added vscode config folder to .gitignore

* Added py.typed to allow use of package types (PEP 561)

* Split out generation of Element from conversion to string

* Added a test for convert_to_element

* Added space after standard library import

* Reformat

---------

Co-authored-by: Rob Blackbourn <rob.blackbourn@gmail.com>
Co-authored-by: Ronie Martinez <ronmarti18@gmail.com>
  • Loading branch information
3 people authored Dec 6, 2023
1 parent b3a1ca0 commit fa2c722
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,6 @@ dmypy.json

# Cython debug symbols
cython_debug/

# vscode config
.vscode/
23 changes: 20 additions & 3 deletions latex2mathml/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,28 @@ class Mode(enum.Enum):
MATH = enum.auto()


def convert(latex: str, xmlns: str = "http://www.w3.org/1998/Math/MathML", display: str = "inline") -> str:
math = Element("math", xmlns=xmlns, display=display)
def convert(
latex: str,
xmlns: str = "http://www.w3.org/1998/Math/MathML",
display: str = "inline",
parent: Optional[Element] = None,
) -> str:
math = convert_to_element(latex, xmlns, display, parent)
return _convert(math)


def convert_to_element(
latex: str,
xmlns: str = "http://www.w3.org/1998/Math/MathML",
display: str = "inline",
parent: Optional[Element] = None,
) -> Element:
tag = "math"
attrib = {"xmlns": xmlns, "display": display}
math = Element(tag, attrib) if parent is None else SubElement(parent, tag, attrib)
row = SubElement(math, "mrow")
_convert_group(iter(walk(latex)), row)
return _convert(math)
return math


def _convert(tree: Element) -> str:
Expand Down
1 change: 1 addition & 0 deletions latex2mathml/py.typed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# PEP 581
29 changes: 28 additions & 1 deletion tests/test_converter.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from xml.etree.cElementTree import Element

import pytest
from multidict import MultiDict
from xmljson import BadgerFish

# noinspection PyProtectedMember
from latex2mathml.converter import _convert, convert
from latex2mathml.converter import _convert, convert, convert_to_element


@pytest.mark.parametrize(
Expand Down Expand Up @@ -4213,3 +4215,28 @@ def test_attributes() -> None:
convert("1", display="block")
== '<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow><mn>1</mn></mrow></math>'
)


def test_convert_to_element() -> None:
bf = BadgerFish()

element = convert_to_element("1")
assert bf.data(element) == {
"math": {
"@display": "inline",
"@xmlns": "http://www.w3.org/1998/Math/MathML",
"mrow": {"mn": {"$": 1}},
},
}, "should convert to element"

parent = Element("div")
convert_to_element("1", parent=parent)
assert bf.data(parent) == {
"div": {
"math": {
"@display": "inline",
"@xmlns": "http://www.w3.org/1998/Math/MathML",
"mrow": {"mn": {"$": 1}},
},
}
}, "should convert to element as child of parent"

0 comments on commit fa2c722

Please sign in to comment.