-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathmathml.py
353 lines (279 loc) · 10.5 KB
/
mathml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# -*- coding: utf-8 -*-
"""
Lower-level formatter of Mathics objects as MathML strings.
MathML formatting is usually initiated in Mathics via MathMLForm[].
"""
import base64
import html
from mathics_scanner import is_symbol_name
from mathics.builtin.box.graphics import GraphicsBox
from mathics.builtin.box.graphics3d import Graphics3DBox
from mathics.builtin.box.layout import (
FractionBox,
GridBox,
RowBox,
SqrtBox,
StyleBox,
SubscriptBox,
SubsuperscriptBox,
SuperscriptBox,
)
from mathics.core.atoms import String
from mathics.core.element import BoxElementMixin
from mathics.core.exceptions import BoxConstructError
from mathics.core.formatter import (
add_conversion_fn,
lookup_method as lookup_conversion_method,
)
from mathics.core.load_builtin import display_operators_set as operators
from mathics.core.symbols import SymbolTrue
def encode_mathml(text: str) -> str:
text = text.replace("&", "&").replace("<", "<").replace(">", ">")
text = text.replace('"', """).replace(" ", " ")
text = text.replace("\n", '<mspace linebreak="newline" />')
return text
extra_operators = {
",",
"(",
")",
"[",
"]",
"{",
"}",
"\u301a",
"\u301b",
"\u00d7",
"\u2032",
"\u2032\u2032",
" ",
"\u2062",
"\u222b",
"\u2146",
}
def string(self, **options) -> str:
text = self.value
number_as_text = options.get("number_as_text", None)
show_string_characters = (
options.get("System`ShowStringCharacters", None) is SymbolTrue
)
if isinstance(self, BoxElementMixin):
if number_as_text is None:
number_as_text = options.get("number_as_text", False)
def render(format, string):
encoded_text = encode_mathml(string)
return format % encoded_text
if text.startswith('"') and text.endswith('"'):
if show_string_characters:
return render("<ms>%s</ms>", text[1:-1])
else:
outtext = ""
for line in text[1:-1].split("\n"):
outtext += render("<mtext>%s</mtext>", line)
return outtext
elif (
text and not number_as_text and ("0" <= text[0] <= "9" or text[0] in (".", "-"))
):
return render("<mn>%s</mn>", text)
else:
if text in operators or text in extra_operators:
if text == "\u2146":
return render(
'<mo form="prefix" lspace="0.2em" rspace="0">%s</mo>', text
)
if text == "\u2062":
return render(
'<mo form="prefix" lspace="0" rspace="0.2em">%s</mo>', text
)
return render("<mo>%s</mo>", text)
elif is_symbol_name(text):
return render("<mi>%s</mi>", text)
else:
outtext = ""
for line in text.split("\n"):
outtext += render("<mtext>%s</mtext>", line)
return outtext
add_conversion_fn(String, string)
def fractionbox(self, **options) -> str:
_options = self.box_options.copy()
_options.update(options)
options = _options
# This removes the auxiliar parentheses in
# numerator and denominator if they are not
# needed. See comment in `mathics.builtin.arithfns.basic.Divide`
def remove_trivial_parentheses(x):
if not isinstance(x, RowBox):
return x
elements = x.elements
if len(elements) != 3:
return x
left, center, right = elements
if not (isinstance(left, String) and isinstance(right, String)):
return x
open_p, close_p = elements[0].value, elements[-1].value
if open_p == "(" and close_p == ")":
return center
return x
num = remove_trivial_parentheses(self.num)
den = remove_trivial_parentheses(self.den)
return "<mfrac>%s %s</mfrac>" % (
lookup_conversion_method(num, "mathml")(num, **options),
lookup_conversion_method(den, "mathml")(den, **options),
)
add_conversion_fn(FractionBox, fractionbox)
def gridbox(self, elements=None, **box_options) -> str:
def boxes_to_mathml(box, **options):
return lookup_conversion_method(box, "mathml")(box, **options)
if not elements:
elements = self._elements
evaluation = box_options.get("evaluation")
items, options = self.get_array(elements, evaluation)
num_fields = max(len(item) if isinstance(item, tuple) else 1 for item in items)
attrs = {}
column_alignments = options["System`ColumnAlignments"].get_name()
try:
attrs["columnalign"] = {
"System`Center": "center",
"System`Left": "left",
"System`Right": "right",
}[column_alignments]
except KeyError:
# invalid column alignment
raise BoxConstructError
joined_attrs = " ".join(f'{name}="{value}"' for name, value in attrs.items())
result = f"<mtable {joined_attrs}>\n"
new_box_options = box_options.copy()
new_box_options["inside_list"] = True
for row in items:
result += "<mtr>"
if isinstance(row, tuple):
for item in row:
result += f"<mtd {joined_attrs}>{boxes_to_mathml(item, **new_box_options)}</mtd>"
else:
result += f"<mtd {joined_attrs} columnspan={num_fields}>{boxes_to_mathml(row, **new_box_options)}</mtd>"
result += "</mtr>\n"
result += "</mtable>"
# print(f"gridbox: {result}")
return result
add_conversion_fn(GridBox, gridbox)
def sqrtbox(self, **options):
_options = self.box_options.copy()
_options.update(options)
options = _options
if self.index:
return "<mroot> %s %s </mroot>" % (
lookup_conversion_method(self.radicand, "mathml")(self.radicand, **options),
lookup_conversion_method(self.index, "mathml")(self.index, **options),
)
return "<msqrt> %s </msqrt>" % lookup_conversion_method(self.radicand, "mathml")(
self.radicand, **options
)
add_conversion_fn(SqrtBox, sqrtbox)
def subscriptbox(self, **options):
_options = self.box_options.copy()
_options.update(options)
options = _options
return "<msub>%s %s</msub>" % (
lookup_conversion_method(self.base, "mathml")(self.base, **options),
lookup_conversion_method(self.subindex, "mathml")(self.subindex, **options),
)
add_conversion_fn(SubscriptBox, subscriptbox)
def superscriptbox(self, **options):
_options = self.box_options.copy()
_options.update(options)
options = _options
return "<msup>%s %s</msup>" % (
lookup_conversion_method(self.base, "mathml")(self.base, **options),
lookup_conversion_method(self.superindex, "mathml")(self.superindex, **options),
)
add_conversion_fn(SuperscriptBox, superscriptbox)
def subsuperscriptbox(self, **options):
_options = self.box_options.copy()
_options.update(options)
options = _options
options["inside_row"] = True
return "<msubsup>%s %s %s</msubsup>" % (
lookup_conversion_method(self.base, "mathml")(self.base, **options),
lookup_conversion_method(self.subindex, "mathml")(self.subindex, **options),
lookup_conversion_method(self.superindex, "mathml")(self.superindex, **options),
)
add_conversion_fn(SubsuperscriptBox, subsuperscriptbox)
def rowbox(self, **options) -> str:
_options = self.box_options.copy()
_options.update(options)
options = _options
result = []
inside_row = options.get("inside_row")
# inside_list = options.get('inside_list')
options = options.copy()
def is_list_interior(content):
if all(element.get_string_value() == "," for element in content[1::2]):
return True
return False
is_list_row = False
if (
len(self.items) >= 3
and self.items[0].get_string_value() == "{"
and self.items[2].get_string_value() == "}"
and self.items[1].has_form("RowBox", 1, None)
):
content = self.items[1].items
if is_list_interior(content):
is_list_row = True
if not inside_row and is_list_interior(self.items):
is_list_row = True
if is_list_row:
options["inside_list"] = True
else:
options["inside_row"] = True
for element in self.items:
result.append(lookup_conversion_method(element, "mathml")(element, **options))
# print(f"mrow: {result}")
return "<mrow>%s</mrow>" % " ".join(result)
add_conversion_fn(RowBox, rowbox)
def stylebox(self, **options) -> str:
_options = self.box_options.copy()
_options.update(options)
options = _options
return lookup_conversion_method(self.boxes, "mathml")(self.boxes, **options)
add_conversion_fn(StyleBox, stylebox)
def graphicsbox(self, elements=None, **options) -> str:
# FIXME: SVG is the only thing we can convert MathML into.
# Handle other graphics formats.
svg_body = self.boxes_to_svg(elements, **options)
# mglyph, which is what we have been using, is bad because MathML standard changed.
# metext does not work because the way in which we produce the svg images is also based on this outdated mglyph
# behaviour.
# template = '<mtext width="%dpx" height="%dpx"><img width="%dpx" height="%dpx" src="data:image/svg+xml;base64,%s"/></mtext>'
template = (
'<mglyph width="%dpx" height="%dpx" src="data:image/svg+xml;base64,%s"/>'
# '<mglyph src="data:image/svg+xml;base64,%s"/>'
)
# print(svg_body)
mathml = template % (
int(self.width),
int(self.height),
base64.b64encode(svg_body.encode("utf8")).decode("utf8"),
)
# print("boxes_to_mathml", mathml)
return mathml
add_conversion_fn(GraphicsBox, graphicsbox)
def sqrtbox(self, **options):
_options = self.box_options.copy()
_options.update(options)
options = _options
if self.index:
return "<mroot> %s %s </mroot>" % (
lookup_conversion_method(self.radicand, "mathml")(self.radicand, **options),
lookup_conversion_method(self.index, "mathml")(self.index, **options),
)
return "<msqrt> %s </msqrt>" % lookup_conversion_method(self.radicand, "mathml")(
self.radicand, **options
)
add_conversion_fn(SqrtBox, sqrtbox)
def graphics3dbox(self, elements=None, **options) -> str:
"""Turn the Graphics3DBox into a MathML string"""
json_repr = self.boxes_to_json(elements, **options)
mathml = f'<graphics3d data="{html.escape(json_repr)}" />'
mathml = f"<mtable><mtr><mtd>{mathml}</mtd></mtr></mtable>"
return mathml
add_conversion_fn(Graphics3DBox, graphics3dbox)