|
25 | 25 | from mathics.core.expression import BoxError, Expression |
26 | 26 | from mathics.core.list import ListExpression |
27 | 27 | from mathics.core.number import dps |
28 | | -from mathics.core.symbols import Atom, Symbol, SymbolFullForm, SymbolList, SymbolTimes |
| 28 | +from mathics.core.symbols import ( |
| 29 | + Atom, |
| 30 | + Symbol, |
| 31 | + SymbolFullForm, |
| 32 | + SymbolList, |
| 33 | + SymbolTimes, |
| 34 | + SymbolTrue, |
| 35 | +) |
29 | 36 | from mathics.core.systemsymbols import ( |
30 | 37 | SymbolDerivative, |
31 | 38 | SymbolInfinity, |
@@ -88,8 +95,8 @@ def _default_render_output_form( |
88 | 95 | if isinstance(expr, Atom): |
89 | 96 | result = expr.atom_to_boxes(SymbolOutputForm, evaluation) |
90 | 97 | if isinstance(result, String): |
91 | | - return result.value |
92 | | - return result.to_text() |
| 98 | + return result.to_text(**kwargs) |
| 99 | + return result.to_text(**kwargs) |
93 | 100 |
|
94 | 101 | expr_head = expr.head |
95 | 102 | head = render_output_form(expr_head, evaluation, **kwargs) |
@@ -835,12 +842,24 @@ def _slotsequence_outputform_text(expr: Expression, evaluation: Evaluation, **kw |
835 | 842 |
|
836 | 843 | @register_outputform("System`String") |
837 | 844 | def string_render_output_form(expr: String, evaluation: Evaluation, **kwargs) -> str: |
838 | | - # lines = expr.value.split("\n") |
839 | | - # max_len = max([len(line) for line in lines]) |
840 | | - # lines = [line + (max_len - len(line)) * " " for line in lines] |
841 | | - # return "\n".join(lines) |
842 | | - value = expr.value |
843 | | - return value |
| 845 | + from mathics.format.render.text import string as render_string |
| 846 | + |
| 847 | + # To render a string in OutputForm, we use the |
| 848 | + # function that render strings from Boxed expressions. |
| 849 | + # When a String object is converted into Boxes, |
| 850 | + # MakeBoxes enclose the original string with quotes. |
| 851 | + # Then, depending on the value of the option |
| 852 | + # `System`ShowStringCharacters`, these quotes are render or not. |
| 853 | + # If this option is set to `False`, the added quotes are removed. |
| 854 | + # Here we are not going through that route: if |
| 855 | + # `System`ShowStringCharacters` is set to True, add the quotes: |
| 856 | + if kwargs.get("System`ShowStringCharacters", None) is SymbolTrue: |
| 857 | + expr = String(f'"{expr.value}"') |
| 858 | + else: |
| 859 | + # if not, set "System`ShowStringCharacters" to True, |
| 860 | + # to avoid remove quotes that was there before formatting: |
| 861 | + kwargs["System`ShowStringCharacters"] = SymbolTrue |
| 862 | + return render_string(expr, **kwargs) |
844 | 863 |
|
845 | 864 |
|
846 | 865 | @register_outputform("System`StringForm") |
@@ -940,7 +959,23 @@ def style_to_outputform_text(expr: Expression, evaluation: Evaluation, **kwargs) |
940 | 959 | elements = expr.elements |
941 | 960 | if not elements: |
942 | 961 | raise _WrongFormattedExpression |
943 | | - return render_output_form(elements[0], evaluation, **kwargs) |
| 962 | + elem, *style_and_options = elements |
| 963 | + options = {} |
| 964 | + if style_and_options: |
| 965 | + style, *style_and_options = style_and_options |
| 966 | + option = style.get_option_values(evaluation) |
| 967 | + if option is not None: |
| 968 | + options.update(option) |
| 969 | + |
| 970 | + for opt_arg in style_and_options: |
| 971 | + option = opt_arg.get_option_values(evaluation) |
| 972 | + if option is None: |
| 973 | + raise _WrongFormattedExpression |
| 974 | + for opt, val in option.items(): |
| 975 | + options[opt] = val |
| 976 | + |
| 977 | + kwargs.update(options) |
| 978 | + return render_output_form(elem, evaluation, **kwargs) |
944 | 979 |
|
945 | 980 |
|
946 | 981 | @register_outputform("System`Symbol") |
|
0 commit comments