Skip to content

Commit c5a87df

Browse files
committed
formatting OutputForm
1 parent f9ee908 commit c5a87df

File tree

7 files changed

+871
-2
lines changed

7 files changed

+871
-2
lines changed

mathics/builtin/box/layout.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ class GridBox(BoxExpression):
146146
# >> MathMLForm[TableForm[{{a,b},{c,d}}]]
147147
# = ...
148148
"""
149+
149150
options = {"ColumnAlignments": "Center"}
150151
summary_text = "low-level representation of an arbitrary 2D layout"
151152

@@ -211,6 +212,33 @@ def eval_display(boxexpr, evaluation):
211212
return boxexpr.elements[0]
212213

213214

215+
class PaneBox(BoxExpression):
216+
"""
217+
<url>
218+
:WMA link:
219+
https://reference.wolfram.com/language/ref/InterpretationBox.html</url>
220+
221+
<dl>
222+
<dt>'PaneBox[expr]'
223+
<dd> is a low-level box construct, used in OutputForm.
224+
</dl>
225+
226+
"""
227+
228+
attributes = A_HOLD_ALL_COMPLETE | A_PROTECTED | A_READ_PROTECTED
229+
summary_text = "box associated to panel"
230+
231+
def apply_display(boxexpr, evaluation, expression):
232+
"""ToExpression[boxexpr_PaneBox, form_]"""
233+
return Expression(expression.head, boxexpr.elements[0], form).evaluate(
234+
evaluation
235+
)
236+
237+
def apply_display(boxexpr, evaluation):
238+
"""DisplayForm[boxexpr_PaneBox]"""
239+
return boxexpr.elements[0]
240+
241+
214242
class RowBox(BoxExpression):
215243
"""
216244
<url>

mathics/builtin/forms/output.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@
1616
from math import ceil
1717
from typing import Optional
1818

19-
from mathics.builtin.box.layout import GridBox, RowBox, to_boxes
19+
from mathics.builtin.box.layout import (
20+
GridBox,
21+
InterpretationBox,
22+
PaneBox,
23+
RowBox,
24+
to_boxes,
25+
)
2026
from mathics.builtin.forms.base import FormBaseClass
2127
from mathics.builtin.makeboxes import MakeBoxes, NumberForm_to_String
2228
from mathics.builtin.tensors import get_dimensions
@@ -54,11 +60,13 @@
5460
SymbolOutputForm,
5561
SymbolRowBox,
5662
SymbolRuleDelayed,
63+
SymbolStandardForm,
5764
SymbolSubscriptBox,
5865
SymbolSuperscriptBox,
5966
)
6067
from mathics.eval.makeboxes import StringLParen, StringRParen, format_element
6168
from mathics.eval.testing_expressions import expr_min
69+
from mathics.format.outputform import expression_to_outputform_text
6270

6371
MULTI_NEWLINE_RE = re.compile(r"\n{2,}")
6472

@@ -561,8 +569,18 @@ class OutputForm(FormBaseClass):
561569
= -Graphics-
562570
"""
563571

572+
formats = {"OutputForm[s_String]": "s"}
564573
summary_text = "plain-text output format"
565574

575+
def eval_makeboxes(self, expr, form, evaluation):
576+
"""MakeBoxes[OutputForm[expr_], form_]"""
577+
print(" eval Makeboxes outputform")
578+
text_outputform = str(expression_to_outputform_text(expr, evaluation, form))
579+
elem1 = PaneBox(String(text_outputform))
580+
elem2 = Expression(SymbolOutputForm, expr)
581+
result = InterpretationBox(elem1, elem2)
582+
return result
583+
566584

567585
class PythonForm(FormBaseClass):
568586
"""
@@ -707,7 +725,7 @@ class TeXForm(FormBaseClass):
707725

708726
def eval_tex(self, expr, evaluation) -> Expression:
709727
"MakeBoxes[expr_, TeXForm]"
710-
boxes = MakeBoxes(expr).evaluate(evaluation)
728+
boxes = format_element(expr, evaluation, SymbolStandardForm)
711729
try:
712730
# Here we set ``show_string_characters`` to False, to reproduce
713731
# the standard behaviour in WMA. Remove this parameter to recover the

mathics/eval/makeboxes.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@
3030
SymbolRepeated,
3131
SymbolRepeatedNull,
3232
SymbolTimes,
33+
SymbolTrue,
3334
)
3435
from mathics.core.systemsymbols import (
3536
SymbolComplex,
3637
SymbolMinus,
38+
SymbolOutputForm,
3739
SymbolRational,
3840
SymbolRowBox,
3941
SymbolStandardForm,
42+
SymbolTraditionalForm,
4043
)
4144

4245
# An operator precedence value that will ensure that whatever operator
@@ -203,12 +206,31 @@ def eval_makeboxes(
203206
return Expression(SymbolMakeBoxes, expr, form).evaluate(evaluation)
204207

205208

209+
def make_output_form(expr, evaluation, form):
210+
"""
211+
Build a 2D text representation of the expression.
212+
"""
213+
from mathics.builtin.box.layout import InterpretationBox, PaneBox
214+
from mathics.format.outputform import expression_to_outputform_text
215+
216+
text_outputform = str(expression_to_outputform_text(expr, evaluation, form))
217+
elem1 = PaneBox(String(text_outputform))
218+
elem2 = Expression(SymbolOutputForm, expr)
219+
return InterpretationBox(elem1, elem2)
220+
221+
206222
def format_element(
207223
element: BaseElement, evaluation: Evaluation, form: Symbol, **kwargs
208224
) -> Optional[BaseElement]:
209225
"""
210226
Applies formats associated to the expression, and then calls Makeboxes
211227
"""
228+
while element.get_head() is form:
229+
element = element.elements[0]
230+
231+
if element.has_form("OutputForm", 1):
232+
return make_output_form(element.elements[0], evaluation, form)
233+
212234
expr = do_format(element, evaluation, form)
213235
if expr is None:
214236
return None

mathics/format/latex.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
from mathics.builtin.box.layout import (
2020
FractionBox,
2121
GridBox,
22+
InterpretationBox,
23+
PaneBox,
2224
RowBox,
2325
SqrtBox,
2426
StyleBox,
@@ -142,6 +144,16 @@ def render(format, string, in_text=False):
142144
add_conversion_fn(String, string)
143145

144146

147+
def interpretation_panebox(self, **options):
148+
return lookup_conversion_method(self.elements[0], "latex")(
149+
self.elements[0], **options
150+
)
151+
152+
153+
add_conversion_fn(InterpretationBox, interpretation_panebox)
154+
add_conversion_fn(PaneBox, interpretation_panebox)
155+
156+
145157
def fractionbox(self, **options) -> str:
146158
_options = self.box_options.copy()
147159
_options.update(options)

mathics/format/mathml.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
from mathics.builtin.box.layout import (
1616
FractionBox,
1717
GridBox,
18+
InterpretationBox,
19+
PaneBox,
1820
RowBox,
1921
SqrtBox,
2022
StyleBox,
@@ -110,6 +112,16 @@ def render(format, string):
110112
add_conversion_fn(String, string)
111113

112114

115+
def interpretation_panebox(self, **options):
116+
return lookup_conversion_method(self.elements[0], "latex")(
117+
self.elements[0], **options
118+
)
119+
120+
121+
add_conversion_fn(InterpretationBox, interpretation_panebox)
122+
add_conversion_fn(PaneBox, interpretation_panebox)
123+
124+
113125
def fractionbox(self, **options) -> str:
114126
_options = self.box_options.copy()
115127
_options.update(options)

0 commit comments

Comments
 (0)