44"""
55
66
7+ from mathics .builtin .box .expression import BoxExpression
78from mathics .builtin .box .graphics import GraphicsBox
89from mathics .builtin .box .graphics3d import Graphics3DBox
910from mathics .builtin .box .layout import (
2223)
2324from mathics .core .atoms import String
2425from mathics .core .exceptions import BoxConstructError
25- from mathics .core .formatter import add_conversion_fn , lookup_method
26+ from mathics .core .formatter import (
27+ add_conversion_fn ,
28+ lookup_method as lookup_conversion_method ,
29+ )
2630from mathics .core .symbols import Atom , SymbolTrue
2731from mathics .format .box .graphics import prepare_elements as prepare_elements2d
2832from mathics .format .box .graphics3d import prepare_elements as prepare_elements3d
2933from mathics .format .form .util import _WrongFormattedExpression , text_cells_to_grid
3034
3135
3236def boxes_to_text (boxes , ** options ) -> str :
33- return lookup_method (boxes , "text" )(boxes , ** options )
37+ return lookup_conversion_method (boxes , "text" )(boxes , ** options )
3438
3539
3640def string (s : String , ** options ) -> str :
@@ -47,30 +51,28 @@ def string(s: String, **options) -> str:
4751add_conversion_fn (String , string )
4852
4953
50- def interpretation_box (self , ** options ):
51- return boxes_to_text (self .boxes , ** options )
54+ def interpretation_box (box : InterpretationBox , ** options ):
55+ return boxes_to_text (box .boxes , ** options )
5256
5357
5458add_conversion_fn (InterpretationBox , interpretation_box )
5559
5660
57- def pane_box (self , ** options ):
58- result = boxes_to_text (self .boxes , ** options )
59- return result
61+ def pane_box (box : PaneBox , ** options ):
62+ return boxes_to_text (box .boxes , ** options )
6063
6164
6265add_conversion_fn (PaneBox , pane_box )
6366
6467
65- def fractionbox (self , ** options ) -> str :
66- _options = self .box_options .copy ()
67- _options .update (options )
68- options = _options
69- num_text = boxes_to_text (self .num , ** options )
70- den_text = boxes_to_text (self .den , ** options )
71- if isinstance (self .num , RowBox ):
68+ def fractionbox (box : FractionBox , ** options ) -> str :
69+ # Note: values set in `options` take precedence over `box_options`
70+ child_options = {** options , ** box .box_options }
71+ num_text = boxes_to_text (box .num , ** child_options )
72+ den_text = boxes_to_text (box .den , ** child_options )
73+ if isinstance (box .num , RowBox ):
7274 num_text = f"({ num_text } )"
73- if isinstance (self .den , RowBox ):
75+ if isinstance (box .den , RowBox ):
7476 den_text = f"({ den_text } )"
7577
7678 return " / " .join ([num_text , den_text ])
@@ -79,13 +81,13 @@ def fractionbox(self, **options) -> str:
7981add_conversion_fn (FractionBox , fractionbox )
8082
8183
82- def gridbox (self , elements = None , ** box_options ) -> str :
84+ def gridbox (box : GridBox , elements = None , ** box_options ) -> str :
8385 if not elements :
84- elements = self .items
86+ elements = box .items
8587 evaluation = box_options .get ("evaluation" , None )
86- items , options = self .get_array (elements , evaluation )
88+ items , options = box .get_array (elements , evaluation )
8789
88- box_options .update (self .options )
90+ box_options .update (box .options )
8991
9092 if not items :
9193 return ""
@@ -112,27 +114,25 @@ def gridbox(self, elements=None, **box_options) -> str:
112114add_conversion_fn (GridBox , gridbox )
113115
114116
115- def sqrtbox (self , ** options ) -> str :
116- _options = self .box_options .copy ()
117- _options .update (options )
118- options = _options
119- if self .index :
117+ def sqrtbox (box : SqrtBox , ** options ) -> str :
118+ # Note: values set in `options` take precedence over `box_options`
119+ child_options = {** options , ** box .box_options }
120+ if box .index :
120121 return "Sqrt[%s,%s]" % (
121- boxes_to_text (self .radicand , ** options ),
122- boxes_to_text (self .index , ** options ),
122+ boxes_to_text (box .radicand , ** child_options ),
123+ boxes_to_text (box .index , ** child_options ),
123124 )
124- return "Sqrt[%s]" % (boxes_to_text (self .radicand , ** options ))
125+ return "Sqrt[%s]" % (boxes_to_text (box .radicand , ** child_options ))
125126
126127
127128add_conversion_fn (SqrtBox , sqrtbox )
128129
129130
130- def superscriptbox (self , ** options ) -> str :
131- _options = self .box_options .copy ()
132- _options .update (options )
133- options = _options
131+ def superscriptbox (box : SuperscriptBox , ** options ) -> str :
132+ # Note: values set in `options` take precedence over `box_options`
133+ child_options = {** options , ** box .box_options }
134134 no_parenthesize = True
135- index = self .superindex
135+ index = box .superindex
136136 while not isinstance (index , Atom ):
137137 if isinstance (index , StyleBox ):
138138 index = index .boxes
@@ -143,46 +143,43 @@ def superscriptbox(self, **options) -> str:
143143
144144 fmt_str = "%s^%s" if no_parenthesize else "%s^(%s)"
145145 return fmt_str % (
146- boxes_to_text (self .base , ** options ),
147- boxes_to_text (self .superindex , ** options ),
146+ boxes_to_text (box .base , ** child_options ),
147+ boxes_to_text (box .superindex , ** child_options ),
148148 )
149149
150150
151151add_conversion_fn (SuperscriptBox , superscriptbox )
152152
153153
154- def subscriptbox (self , ** options ) -> str :
155- _options = self .box_options .copy ()
156- _options .update (options )
157- options = _options
154+ def subscriptbox (box : SubscriptBox , ** options ) -> str :
155+ # Note: values set in `options` take precedence over `box_options`
156+ child_options = {** box .box_options , ** options }
158157 return "Subscript[%s, %s]" % (
159- boxes_to_text (self .base , ** options ),
160- boxes_to_text (self .subindex , ** options ),
158+ boxes_to_text (box .base , ** child_options ),
159+ boxes_to_text (box .subindex , ** child_options ),
161160 )
162161
163162
164163add_conversion_fn (SubscriptBox , subscriptbox )
165164
166165
167- def subsuperscriptbox (self , ** options ) -> str :
168- _options = self .box_options .copy ()
169- _options .update (options )
170- options = _options
166+ def subsuperscriptbox (box : SubsuperscriptBox , ** options ) -> str :
167+ # Note: values set in `options` take precedence over `box_options`
168+ child_options = {** box .box_options , ** options }
171169 return "Subsuperscript[%s, %s, %s]" % (
172- boxes_to_text (self .base , ** options ),
173- boxes_to_text (self .subindex , ** options ),
174- boxes_to_text (self .superindex , ** options ),
170+ boxes_to_text (box .base , ** child_options ),
171+ boxes_to_text (box .subindex , ** child_options ),
172+ boxes_to_text (box .superindex , ** child_options ),
175173 )
176174
177175
178176add_conversion_fn (SubsuperscriptBox , subsuperscriptbox )
179177
180178
181- def rowbox (self , elements = None , ** options ) -> str :
182- _options = self .box_options .copy ()
183- _options .update (options )
184- options = _options
185- parts_str = [boxes_to_text (element , ** options ) for element in self .items ]
179+ def rowbox (box : RowBox , elements = None , ** options ) -> str :
180+ # Note: values set in `options` take precedence over `box_options`
181+ child_options = {** box .box_options , ** options }
182+ parts_str = [boxes_to_text (element , ** child_options ) for element in box .items ]
186183 if len (parts_str ) == 0 :
187184 return ""
188185 if len (parts_str ) == 1 :
@@ -209,39 +206,37 @@ def rowbox(self, elements=None, **options) -> str:
209206add_conversion_fn (RowBox , rowbox )
210207
211208
212- def stylebox (self , ** options ) -> str :
213- options .pop ("evaluation" , None )
214- _options = self .box_options .copy ()
215- _options .update (options )
216- options = _options
217- return boxes_to_text (self .boxes , ** options )
209+ def stylebox (box : StyleBox , ** options ) -> str :
210+ # Note: values set in `options` take precedence over `box_options`
211+ child_options = {** box .box_options , ** options }
212+ return boxes_to_text (box .boxes , ** child_options )
218213
219214
220215add_conversion_fn (StyleBox , stylebox )
221216
222217
223- def graphicsbox (self , elements = None , ** options ) -> str :
218+ def graphicsbox (box : GraphicsBox , elements = None , ** options ) -> str :
224219 assert elements is None
225220
226- prepare_elements2d (self , self .content , options ) # to test for Box errors
221+ prepare_elements2d (box , box .content , options ) # to test for Box errors
227222 return "-Graphics-"
228223
229224
230225add_conversion_fn (GraphicsBox , graphicsbox )
231226
232227
233- def graphics3dbox (self , elements = None , ** options ) -> str :
228+ def graphics3dbox (box : Graphics3DBox , elements = None , ** options ) -> str :
234229 assert elements is None
235230
236- prepare_elements3d (self , self .content , options ) # to test for Box errors
231+ prepare_elements3d (box , box .content , options ) # to test for Box errors
237232 return "-Graphics3D-"
238233
239234
240235add_conversion_fn (Graphics3DBox , graphics3dbox )
241236
242237
243- def tag_and_form_box (self , ** options ):
244- return boxes_to_text (self .boxes , ** options )
238+ def tag_and_form_box (box : BoxExpression , ** options ):
239+ return boxes_to_text (box .boxes , ** options )
245240
246241
247242add_conversion_fn (FormBox , tag_and_form_box )
0 commit comments