Skip to content
Merged
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
19 changes: 17 additions & 2 deletions mathics/format/form/outputform.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@

OutputForm is two-dimensional keyboard-character-only output, suitable for CLI
and text terminals.

The entry point of this module is the function `render_output_form`. This function takes an expression and an evaluation object as parameters, and returns a string representing the expression in its OutputForm.

To do this, `render_output_form` looks for the head of the expression, and uses its name to lookup one of the registered functions
to process specific kind of expressions. Callback functions are registered using the decorator `@register_outputform([lookupname])`.

If the callback function cannot process its argument, it raises a `_WrongFormattedExpression`, to make it know `render_output_form`
it must use the default callback function. This default function is also called when an specific callback function is not available.

"""

import re
Expand Down Expand Up @@ -247,6 +256,7 @@ def render_output_form(expr: BaseElement, evaluation: Evaluation, **kwargs):
"""
Build a pretty-print text from an `Expression`
"""
lookup_name: str
format_expr: Expression = do_format(expr, evaluation, SymbolOutputForm) # type: ignore

while format_expr.has_form("HoldForm", 1): # type: ignore
Expand All @@ -256,7 +266,7 @@ def render_output_form(expr: BaseElement, evaluation: Evaluation, **kwargs):
return ""

head = format_expr.get_head()
lookup_name: str = head.get_name() or head.get_lookup_name()
lookup_name = head.get_name() or head.get_lookup_name()
callback = EXPR_TO_OUTPUTFORM_TEXT_MAP.get(lookup_name, None)
if callback is None:
if head in evaluation.definitions.outputforms:
Expand Down Expand Up @@ -841,9 +851,14 @@ def _slotsequence_outputform_text(expr: Expression, evaluation: Evaluation, **kw


@register_outputform("System`String")
def string_render_output_form(expr: String, evaluation: Evaluation, **kwargs) -> str:
def string_render_output_form(
expr: BaseElement, evaluation: Evaluation, **kwargs
) -> str:
from mathics.format.render.text import string as render_string

if not isinstance(expr, String):
raise _WrongFormattedExpression

# To render a string in OutputForm, we use the
# function that render strings from Boxed expressions.
# When a String object is converted into Boxes,
Expand Down
Loading