Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mathics/builtin/box/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def __new__(cls, *elements, **kwargs):

def __init(self, *args, **kwargs):
super().__init(args, kwargs)
self.boxes = []
self.inner_box = None

def do_format(self, evaluation, format):
return self
Expand Down
51 changes: 26 additions & 25 deletions mathics/builtin/box/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,17 @@ class FormBox(BoxExpression):
def init(self, *elems, **kwargs):
self.box_options = kwargs
self.form = elems[1]
self.boxes = elems[0]
assert isinstance(self.boxes, BoxElementMixin), f"{type(self.boxes)}"

self.inner_box = elems[0]
assert isinstance(self.inner_box, BoxElementMixin), f"{type(self.inner_box)}"

@property
def elements(self):
if self._elements is None:
self._elements = elements_to_expressions(
self,
(
self.boxes,
self.inner_box,
self.form,
),
self.box_options,
Expand Down Expand Up @@ -276,12 +277,12 @@ class InterpretationBox(BoxExpression):
summary_text = "box associated to an input expression"

def __repr__(self):
result = "InterpretationBox\n " + repr(self.boxes)
result = "InterpretationBox\n " + repr(self.inner_box)
result += f"\n {self.box_options}"
return result

def init(self, *expr, **options):
self.boxes = expr[0]
self.inner_box = expr[0]
self.expr = expr[1]
self.box_options = options

Expand All @@ -291,7 +292,7 @@ def elements(self):
self._elements = elements_to_expressions(
self,
(
self.boxes,
self.inner_box,
self.expr,
),
self.box_options,
Expand Down Expand Up @@ -321,11 +322,11 @@ def eval_to_expression2(self, boxexpr, form, evaluation):

def eval_display(self, boxexpr, evaluation):
"""DisplayForm[boxexpr_InterpretationBox]"""
return boxexpr.boxes
return boxexpr.inner_box

@property
def is_multiline(self) -> bool:
return self.boxes.is_multiline
return self.inner_box.is_multiline


class PaneBox(BoxExpression):
Expand All @@ -349,12 +350,12 @@ class PaneBox(BoxExpression):
def elements(self):
if self._elements is None:
self._elements = elements_to_expressions(
self, (self.boxes,), self.box_options
self, (self.inner_box,), self.box_options
)
return self._elements

def init(self, expr, **options):
self.boxes = expr
self.inner_box = expr
self.box_options = options

def eval_panebox1(self, expr, evaluation, options):
Expand All @@ -373,7 +374,7 @@ def eval_display(boxexpr, evaluation):

@property
def is_multiline(self) -> bool:
return self.boxes.is_multiline
return self.inner_box.is_multiline


class RowBox(BoxExpression):
Expand Down Expand Up @@ -555,7 +556,7 @@ def __repr__(self):
def elements(self):
if self._elements is None:
style = self.style
boxes = self.boxes
boxes = self.inner_box
if style:
self._elements = elements_to_expressions(
self, (boxes, style), self.box_options
Expand All @@ -579,27 +580,27 @@ def eval_style(self, boxes, style, evaluation: Evaluation, options: dict):
return StyleBox(boxes, style=style, **options)

def get_string_value(self) -> str:
box = self.boxes
box = self.inner_box
if isinstance(box, String):
return box.value
return ""

def init(self, boxes, style=None, **options):
def init(self, box, style=None, **options):
# This implementation supersedes Expression.process_style_box
if isinstance(boxes, StyleBox):
options.update(boxes.box_options)
boxes = boxes.boxes
if isinstance(box, StyleBox):
options.update(box.box_options)
box = box.inner_box
self.style = style
self.box_options = options
assert options is not None
self.boxes = boxes
self.inner_box = box
assert isinstance(
self.boxes, BoxElementMixin
), f"{type(self.boxes)},{self.boxes}"
self.inner_box, BoxElementMixin
), f"{type(self.inner_box)},{self.inner_box}"

@property
def is_multiline(self) -> bool:
return self.boxes.is_multiline
return self.inner_box.is_multiline


class SubscriptBox(BoxExpression):
Expand Down Expand Up @@ -759,16 +760,16 @@ class TagBox(BoxExpression):
def init(self, *elems, **kwargs):
self.box_options = kwargs
self.form = elems[1]
self.boxes = elems[0]
assert isinstance(self.boxes, BoxElementMixin), f"{type(self.boxes)}"
self.inner_box = elems[0]
assert isinstance(self.inner_box, BoxElementMixin), f"{type(self.inner_box)}"

@property
def elements(self):
if self._elements is None:
self._elements = elements_to_expressions(
self,
(
self.boxes,
self.inner_box,
self.form,
),
self.box_options,
Expand All @@ -784,7 +785,7 @@ def eval_tagbox(self, expr, form: Symbol, evaluation: Evaluation):

@property
def is_multiline(self) -> bool:
return self.boxes.is_multiline
return self.inner_box.is_multiline


class TemplateBox(BoxExpression):
Expand Down
12 changes: 6 additions & 6 deletions mathics/core/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,19 +411,19 @@ class BoxElementMixin(ImmutableValueMixin):
def is_multiline(self) -> bool:
return True

def boxes_to_format(self, format: str, **options) -> str:
from mathics.core.formatter import boxes_to_format
def box_to_format(self, format: str, **options) -> str:
from mathics.core.formatter import box_to_format

return boxes_to_format(self, format, **options)
return box_to_format(self, format, **options)

def boxes_to_mathml(self, **options) -> str:
"""For compatibility deprecated"""
return self.boxes_to_format("mathml", **options)
return self.box_to_format("mathml", **options)

def boxes_to_tex(self, **options) -> str:
"""For compatibility deprecated"""
return self.boxes_to_format("latex", **options)
return self.box_to_format("latex", **options)

def boxes_to_text(self, **options) -> str:
"""For compatibility deprecated"""
return self.boxes_to_format("text", **options)
return self.box_to_format("text", **options)
36 changes: 12 additions & 24 deletions mathics/core/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,34 +54,22 @@ def replace(match):
return text


extra_operators = set(
(
",",
"(",
")",
"[",
"]",
"{",
"}",
"\u301a",
"\u301b",
"\u00d7",
"\u2032",
"\u2032\u2032",
" ",
"\u2062",
"\u222b",
"\u2146",
)
)
def box_to_format(box, format: str, **options) -> str: # Maybe Union[str, bytearray]
"""
Translates a box structure ``box`` to a file format ``format``.
This is used only at the root Box of a boxed expression.
"""
options["format_type"] = format
return convert_box_to_format(box, **options)


def boxes_to_format(boxes, format, **options) -> str: # Maybe Union[str, bytearray]
def convert_box_to_format(box, **options) -> str:
"""
Translates a box structure ``boxes`` to a file format ``format``.

Translates a box structure ``box`` to a file format ``format``.
This is used at either non-root-level boxes or from the
initial call from box_to_format.
"""
return lookup_method(boxes, format)(boxes, **options)
return lookup_method(box, options["format_type"])(box, **options)


def lookup_method(self, format: str) -> Callable:
Expand Down
10 changes: 6 additions & 4 deletions mathics/format/render/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,14 @@ def render(format, string_, in_text=False):


def interpretation_box(box: InterpretationBox, **options):
return lookup_conversion_method(box.boxes, "latex")(box.boxes, **options)
return lookup_conversion_method(box.inner_box, "latex")(box.inner_box, **options)


add_conversion_fn(InterpretationBox, interpretation_box)


def pane_box(box: PaneBox, **options):
content = lookup_conversion_method(box.boxes, "latex")(box.boxes, **options)
content = lookup_conversion_method(box.inner_box, "latex")(box.inner_box, **options)
options = box.box_options
size = options.get("System`ImageSize", SymbolAutomatic).to_python()

Expand Down Expand Up @@ -487,7 +487,9 @@ def rowbox(box: RowBox, **options) -> str:
def stylebox(box: StyleBox, **options) -> str:
# Note: values set in `options` take precedence over `box_options`
child_options = {**box.box_options, **options}
return lookup_conversion_method(box.boxes, "latex")(box.boxes, **child_options)
return lookup_conversion_method(box.inner_box, "latex")(
box.inner_box, **child_options
)


add_conversion_fn(StyleBox, stylebox)
Expand Down Expand Up @@ -775,7 +777,7 @@ def graphics3dbox(box: Graphics3DBox, elements=None, **options) -> str:


def tag_and_form_box(box: BoxExpression, **options):
return lookup_conversion_method(box.boxes, "latex")(box.boxes, **options)
return lookup_conversion_method(box.inner_box, "latex")(box.inner_box, **options)


add_conversion_fn(FormBox, tag_and_form_box)
Expand Down
14 changes: 9 additions & 5 deletions mathics/format/render/mathml.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,16 @@ def render(format, string):


def interpretation_box(box: InterpretationBox, **options):
return lookup_conversion_method(box.boxes, "mathml")(box.boxes, **options)
return lookup_conversion_method(box.inner_box, "mathml")(box.inner_box, **options)


add_conversion_fn(InterpretationBox, interpretation_box)


def pane_box(box: PaneBox, **options):
content = lookup_conversion_method(box.boxes, "mathml")(box.boxes, **options)
content = lookup_conversion_method(box.inner_box, "mathml")(
box.inner_box, **options
)
options = box.box_options
size = options.get("System`ImageSize", SymbolAutomatic).to_python()
if size is SymbolAutomatic:
Expand Down Expand Up @@ -328,7 +330,9 @@ def is_list_interior(content):

def stylebox(box: StyleBox, **options) -> str:
child_options = {**options, **box.box_options}
return lookup_conversion_method(box.boxes, "mathml")(box.boxes, **child_options)
return lookup_conversion_method(box.inner_box, "mathml")(
box.inner_box, **child_options
)


add_conversion_fn(StyleBox, stylebox)
Expand All @@ -337,7 +341,7 @@ def stylebox(box: StyleBox, **options) -> str:
def graphicsbox(box: GraphicsBox, elements=None, **options) -> str:
# FIXME: SVG is the only thing we can convert MathML into.
# Handle other graphics formats.
svg_body = box.boxes_to_format("svg", **options)
svg_body = box.box_to_format("svg", **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
Expand Down Expand Up @@ -371,7 +375,7 @@ def graphics3dbox(box, elements=None, **options) -> str:


def tag_and_form_box(box: BoxExpression, **options):
return lookup_conversion_method(box.boxes, "mathml")(box.boxes, **options)
return lookup_conversion_method(box.inner_box, "mathml")(box.inner_box, **options)


add_conversion_fn(FormBox, tag_and_form_box)
Expand Down
Loading
Loading