-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathtext.py
225 lines (169 loc) · 5.94 KB
/
text.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
# -*- coding: utf-8 -*-
"""
Lower-level formatter Mathics objects as plain text.
"""
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.exceptions import BoxConstructError
from mathics.core.formatter import add_conversion_fn, lookup_method
from mathics.core.symbols import Atom, SymbolTrue
def boxes_to_text(boxes, **options) -> str:
return lookup_method(boxes, "text")(boxes, **options)
def string(self, **options) -> str:
value = self.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 fractionbox(self, **options) -> str:
_options = self.box_options.copy()
_options.update(options)
options = _options
num_text = boxes_to_text(self.num, **options)
den_text = boxes_to_text(self.den, **options)
if isinstance(self.num, RowBox):
num_text = f"{num_text}"
if isinstance(self.den, RowBox):
den_text = f"{den_text}"
return " / ".join([num_text, den_text])
add_conversion_fn(FractionBox, fractionbox)
def gridbox(self, elements=None, **box_options) -> str:
if not elements:
elements = self._elements
evaluation = box_options.get("evaluation", None)
items, options = self.get_array(elements, evaluation)
result = ""
if not items:
return ""
try:
widths = [0] * max(1, max(len(row) for row in items if isinstance(row, tuple)))
except ValueError:
widths = [0]
cells = [
[
# TODO: check if this evaluation is necesary.
boxes_to_text(item, **box_options).splitlines()
for item in row
]
if isinstance(row, tuple)
else [boxes_to_text(row, **box_options).splitlines()]
for row in items
]
# compute widths
full_width = 0
for i, row in enumerate(cells):
for index, cell in enumerate(row):
if index >= len(widths):
raise BoxConstructError
if not isinstance(items[i], tuple):
for line in cell:
full_width = max(full_width, len(line))
else:
for line in cell:
widths[index] = max(widths[index], len(line))
full_width = max(sum(widths), full_width)
for row_index, row in enumerate(cells):
if row_index > 0:
result += "\n"
k = 0
while True:
line_exists = False
line = ""
for cell_index, cell in enumerate(row):
if len(cell) > k:
line_exists = True
text = cell[k]
else:
text = ""
line += text
if isinstance(items[row_index], tuple):
if cell_index < len(row) - 1:
line += " " * (widths[cell_index] - len(text))
# if cell_index < len(row) - 1:
line += " "
if line_exists:
result += line + "\n"
else:
break
k += 1
return result
add_conversion_fn(GridBox, gridbox)
def sqrtbox(self, **options) -> str:
_options = self.box_options.copy()
_options.update(options)
options = _options
if self.index:
return "Sqrt[%s,%s]" % (
boxes_to_text(self.radicand, **options),
boxes_to_text(self.index, **options),
)
return "Sqrt[%s]" % (boxes_to_text(self.radicand, **options))
add_conversion_fn(SqrtBox, sqrtbox)
def superscriptbox(self, **options) -> str:
_options = self.box_options.copy()
_options.update(options)
options = _options
fmt_str = "%s^%s" if isinstance(self.superindex, Atom) else "%s^(%s)"
return fmt_str % (
boxes_to_text(self.base, **options),
boxes_to_text(self.superindex, **options),
)
add_conversion_fn(SuperscriptBox, superscriptbox)
def subscriptbox(self, **options) -> str:
_options = self.box_options.copy()
_options.update(options)
options = _options
return "Subscript[%s, %s]" % (
boxes_to_text(self.base, **options),
boxes_to_text(self.subindex, **options),
)
add_conversion_fn(SubscriptBox, subscriptbox)
def subsuperscriptbox(self, **options) -> str:
_options = self.box_options.copy()
_options.update(options)
options = _options
return "Subsuperscript[%s, %s, %s]" % (
boxes_to_text(self.base, **options),
boxes_to_text(self.subindex, **options),
boxes_to_text(self.superindex, **options),
)
add_conversion_fn(SubsuperscriptBox, subsuperscriptbox)
def rowbox(self, elements=None, **options) -> str:
_options = self.box_options.copy()
_options.update(options)
options = _options
return "".join([boxes_to_text(element, **options) for element in self.items])
add_conversion_fn(RowBox, rowbox)
def stylebox(self, **options) -> str:
options.pop("evaluation", None)
_options = self.box_options.copy()
_options.update(options)
options = _options
return boxes_to_text(self.boxes, **options)
add_conversion_fn(StyleBox, stylebox)
def graphicsbox(self, elements=None, **options) -> str:
if not elements:
elements = self._elements
self._prepare_elements(elements, options) # to test for Box errors
return "-Graphics-"
add_conversion_fn(GraphicsBox, graphicsbox)
def graphics3dbox(self, elements=None, **options) -> str:
if not elements:
elements = self._elements
return "-Graphics3D-"
add_conversion_fn(Graphics3DBox, graphics3dbox)