-
-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathtext.py
More file actions
243 lines (174 loc) · 6.71 KB
/
text.py
File metadata and controls
243 lines (174 loc) · 6.71 KB
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
# -*- coding: utf-8 -*-
"""
Mathics3 box rendering to plain text.
"""
from mathics.builtin.box.expression import BoxExpression
from mathics.builtin.box.graphics import GraphicsBox
from mathics.builtin.box.graphics3d import Graphics3DBox
from mathics.builtin.box.layout import (
FormBox,
FractionBox,
GridBox,
InterpretationBox,
PaneBox,
RowBox,
SqrtBox,
StyleBox,
SubscriptBox,
SubsuperscriptBox,
SuperscriptBox,
TagBox,
)
from mathics.core.atoms import String
from mathics.core.exceptions import BoxConstructError
from mathics.core.formatter import (
add_conversion_fn,
lookup_method as lookup_conversion_method,
)
from mathics.core.symbols import Atom, SymbolTrue
from mathics.format.box.graphics import prepare_elements as prepare_elements2d
from mathics.format.box.graphics3d import prepare_elements as prepare_elements3d
from mathics.format.form.util import _WrongFormattedExpression, text_cells_to_grid
def boxes_to_text(boxes, **options) -> str:
return lookup_conversion_method(boxes, "text")(boxes, **options)
def string(s: String, **options) -> str:
value = s.value
show_string_characters = (
options.get("System`ShowStringCharacters", None) is SymbolTrue
)
if value.startswith('"') and value.endswith('"'): # nopep8
if not show_string_characters:
value = value[1:-1]
return value
add_conversion_fn(String, string)
def interpretation_box(box: InterpretationBox, **options):
return boxes_to_text(box.boxes, **options)
add_conversion_fn(InterpretationBox, interpretation_box)
def pane_box(box: PaneBox, **options):
return boxes_to_text(box.boxes, **options)
add_conversion_fn(PaneBox, pane_box)
def fractionbox(box: FractionBox, **options) -> str:
# Note: values set in `options` take precedence over `box_options`
child_options = {**options, **box.box_options}
num_text = boxes_to_text(box.num, **child_options)
den_text = boxes_to_text(box.den, **child_options)
if isinstance(box.num, RowBox):
num_text = f"({num_text})"
if isinstance(box.den, RowBox):
den_text = f"({den_text})"
return " / ".join([num_text, den_text])
add_conversion_fn(FractionBox, fractionbox)
def gridbox(box: GridBox, elements=None, **box_options) -> str:
if not elements:
elements = box.items
evaluation = box_options.get("evaluation", None)
items, options = box.get_array(elements, evaluation)
box_options.update(box.options)
if not items:
return ""
cells = [
(
[
# TODO: check if this evaluation is necessary.
boxes_to_text(item, **box_options)
for item in row
]
if isinstance(row, tuple)
else boxes_to_text(row, **box_options)
)
for row in items
]
try:
return text_cells_to_grid(cells)
except _WrongFormattedExpression:
raise BoxConstructError
add_conversion_fn(GridBox, gridbox)
def sqrtbox(box: SqrtBox, **options) -> str:
# Note: values set in `options` take precedence over `box_options`
child_options = {**options, **box.box_options}
if box.index:
return "Sqrt[%s,%s]" % (
boxes_to_text(box.radicand, **child_options),
boxes_to_text(box.index, **child_options),
)
return "Sqrt[%s]" % (boxes_to_text(box.radicand, **child_options))
add_conversion_fn(SqrtBox, sqrtbox)
def superscriptbox(box: SuperscriptBox, **options) -> str:
# Note: values set in `options` take precedence over `box_options`
child_options = {**options, **box.box_options}
no_parenthesize = True
index = box.superindex
while not isinstance(index, Atom):
if isinstance(index, StyleBox):
index = index.boxes
else:
break
if isinstance(index, FractionBox):
no_parenthesize = False
fmt_str = "%s^%s" if no_parenthesize else "%s^(%s)"
return fmt_str % (
boxes_to_text(box.base, **child_options),
boxes_to_text(box.superindex, **child_options),
)
add_conversion_fn(SuperscriptBox, superscriptbox)
def subscriptbox(box: SubscriptBox, **options) -> str:
# Note: values set in `options` take precedence over `box_options`
child_options = {**box.box_options, **options}
return "Subscript[%s, %s]" % (
boxes_to_text(box.base, **child_options),
boxes_to_text(box.subindex, **child_options),
)
add_conversion_fn(SubscriptBox, subscriptbox)
def subsuperscriptbox(box: SubsuperscriptBox, **options) -> str:
# Note: values set in `options` take precedence over `box_options`
child_options = {**box.box_options, **options}
return "Subsuperscript[%s, %s, %s]" % (
boxes_to_text(box.base, **child_options),
boxes_to_text(box.subindex, **child_options),
boxes_to_text(box.superindex, **child_options),
)
add_conversion_fn(SubsuperscriptBox, subsuperscriptbox)
def rowbox(box: RowBox, elements=None, **options) -> str:
# Note: values set in `options` take precedence over `box_options`
child_options = {**box.box_options, **options}
parts_str = [boxes_to_text(element, **child_options) for element in box.items]
if len(parts_str) == 0:
return ""
if len(parts_str) == 1:
return parts_str[0]
# This loop integrate all the row adding spaces after a ",", followed
# by something which is not a comma. For example,
# >> ToString[RowBox[{",",",","p"}]//DisplayForm]
# = ",, p"
result = parts_str[0]
comma = result == ","
for elem in parts_str[1:]:
if elem == ",":
result += elem
comma = True
continue
if comma:
result += " "
comma = False
result += elem
return result
add_conversion_fn(RowBox, rowbox)
def stylebox(box: StyleBox, **options) -> str:
# Note: values set in `options` take precedence over `box_options`
child_options = {**box.box_options, **options}
return boxes_to_text(box.boxes, **child_options)
add_conversion_fn(StyleBox, stylebox)
def graphicsbox(box: GraphicsBox, elements=None, **options) -> str:
assert elements is None
prepare_elements2d(box, box.content, options) # to test for Box errors
return "-Graphics-"
add_conversion_fn(GraphicsBox, graphicsbox)
def graphics3dbox(box: Graphics3DBox, elements=None, **options) -> str:
assert elements is None
prepare_elements3d(box, box.content, options) # to test for Box errors
return "-Graphics3D-"
add_conversion_fn(Graphics3DBox, graphics3dbox)
def tag_and_form_box(box: BoxExpression, **options):
return boxes_to_text(box.boxes, **options)
add_conversion_fn(FormBox, tag_and_form_box)
add_conversion_fn(TagBox, tag_and_form_box)