diff --git a/mathics/format/form/outputform.py b/mathics/format/form/outputform.py index 8f67b34ce..b6697d080 100644 --- a/mathics/format/form/outputform.py +++ b/mathics/format/form/outputform.py @@ -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 @@ -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 @@ -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: @@ -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,