diff --git a/mathics/format/box/outputforms.py b/mathics/format/box/outputforms.py index 60d187f4d..a248d127f 100644 --- a/mathics/format/box/outputforms.py +++ b/mathics/format/box/outputforms.py @@ -30,7 +30,7 @@ def eval_mathmlform(expr: BaseElement, evaluation: Evaluation) -> BoxElementMixi boxes = format_element(expr, evaluation, SymbolTraditionalForm) try: - mathml = boxes.boxes_to_mathml(evaluation=evaluation) + mathml = boxes.boxes_to_mathml(evaluation=evaluation, _indent_level=1) except BoxError: evaluation.message( "General", @@ -44,9 +44,15 @@ def eval_mathmlform(expr: BaseElement, evaluation: Evaluation) -> BoxElementMixi # #convert_box(boxes) query = evaluation.parse("Settings`$UseSansSerif") usesansserif = query.evaluate(evaluation).to_python() - if not is_a_picture: - if isinstance(usesansserif, bool) and usesansserif: - mathml = '%s' % mathml + if is_a_picture: + usesansserif = False + elif not isinstance(usesansserif, bool): + usesansserif = False + + if usesansserif: + mathml = '\n%s\n' % mathml + else: + mathml = "\n%s\n" % mathml mathml = '%s' % mathml # convert_box(boxes) return InterpretationBox( diff --git a/mathics/format/render/mathml.py b/mathics/format/render/mathml.py index 803e50951..b15d91681 100644 --- a/mathics/format/render/mathml.py +++ b/mathics/format/render/mathml.py @@ -6,26 +6,25 @@ For readability, and following WMA MathML generated code, tags \ containing sub-tags are split on several lines, one by -sub element. For example, the Box expression +sub element, and indented according to the level of the part. \ +For example, the Box expression >> FractionBox[RowBox[{"a", "+", SuperscriptBox["b", "c"]}], "d"] produces ``` - -a -+ - -b -c - - -d + + a + + + + b + c + + + d ``` -In WMA, each line would be also indented adding one space on each \ -level of indentation. """ @@ -101,9 +100,12 @@ def string(s: String, **options) -> str: if number_as_text is None: number_as_text = SymbolFalse + indent_level = options.get("_indent_level", 0) + indent_spaces = " " * indent_level + def render(format, string): encoded_text = encode_mathml(string) - return format % encoded_text + return indent_spaces + format % encoded_text if text.startswith('"') and text.endswith('"'): text = text[1:-1] @@ -172,9 +174,13 @@ def interpretation_box(box: InterpretationBox, **options): def pane_box(box: PaneBox, **options): - content = lookup_conversion_method(box.boxes, "mathml")(box.boxes, **options) - options = box.box_options - size = options.get("System`ImageSize", SymbolAutomatic).to_python() + """render a PaneBox into mathml code""" + indent_level = options.get("_indent_level", 0) + indent_spaces = " " * indent_level + child_options = {**options, **box.box_options} + child_options["_indent_level"] = indent_level + 1 + content = lookup_conversion_method(box.boxes, "mathml")(box.boxes, **child_options) + size = child_options.get("System`ImageSize", SymbolAutomatic).to_python() if size is SymbolAutomatic: width = "" height = "" @@ -202,8 +208,8 @@ def pane_box(box: PaneBox, **options): dims += "overflow:hidden;" dims = f' style="{dims}" ' if dims: - return f"\n{content}\n" - return content + return f"{indent_spaces}\n{content}\n{indent_spaces}" + return f"{indent_spaces}{content}" add_conversion_fn(PaneBox, pane_box) @@ -211,8 +217,11 @@ def pane_box(box: PaneBox, **options): def fractionbox(box: FractionBox, **options) -> str: # Note: values set in `options` take precedence over `box_options` + indent_level = options.get("_indent_level", 0) + indent_spaces = " " * indent_level child_options = {**options, **box.box_options} - return "\n%s\n%s\n" % ( + child_options["_indent_level"] = indent_level + 1 + return f"{indent_spaces}\n%s\n%s\n{indent_spaces}" % ( lookup_conversion_method(box.num, "mathml")(box.num, **child_options), lookup_conversion_method(box.den, "mathml")(box.den, **child_options), ) @@ -221,18 +230,18 @@ def fractionbox(box: FractionBox, **options) -> str: add_conversion_fn(FractionBox, fractionbox) -def gridbox(box: GridBox, elements=None, **box_options) -> str: +def gridbox(box: GridBox, elements=None, **options) -> str: def boxes_to_mathml(box, **options): return lookup_conversion_method(box, "mathml")(box, **options) if not elements: elements = box._elements - evaluation = box_options.get("evaluation") - items, options = box.get_array(elements, evaluation) + evaluation = options.get("evaluation") + items, box_options = box.get_array(elements, evaluation) num_fields = max(len(item) if isinstance(item, tuple) else 1 for item in items) attrs = {} - column_alignments = options["System`ColumnAlignments"].get_name() + column_alignments = box_options["System`ColumnAlignments"].get_name() try: attrs["columnalign"] = { "System`Center": "center", @@ -243,20 +252,23 @@ def boxes_to_mathml(box, **options): # invalid column alignment raise BoxConstructError joined_attrs = " ".join(f'{name}="{value}"' for name, value in attrs.items()) - result = f"\n" + indent_level = options.get("_indent_level", 0) + indent_spaces = " " * indent_level + child_options = {**options, **box_options} + child_options["_indent_level"] = indent_level + 3 + result = f"{indent_spaces}\n" + for row in items: - result += "" + result += f"{indent_spaces} " if isinstance(row, tuple): for item in row: item.inside_list = True - result += ( - f"{boxes_to_mathml(item, **options)}" - ) + result += f"\n{indent_spaces} \n{boxes_to_mathml(item, **child_options)}\n{indent_spaces} " else: row.inside_list = True - result += f"{boxes_to_mathml(row, **options)}" - result += "\n" - result += "" + result += f"\n{indent_spaces} \n{boxes_to_mathml(row, **child_options)}\n{indent_spaces} " + result += f"\n{indent_spaces} \n" + result += f"{indent_spaces}" # print(f"gridbox: {result}") return result @@ -266,17 +278,23 @@ def boxes_to_mathml(box, **options): def sqrtbox(box: SqrtBox, **options): # Note: values set in `options` take precedence over `box_options` + indent_level = options.get("_indent_level", 0) + indent_spaces = " " * indent_level child_options = {**options, **box.box_options} + child_options["_indent_level"] = indent_level + 1 if box.index: - return " %s %s " % ( + return f"{indent_spaces}\n%s\n%s\n{indent_spaces}" % ( lookup_conversion_method(box.radicand, "mathml")( box.radicand, **child_options ), lookup_conversion_method(box.index, "mathml")(box.index, **child_options), ) - return "\n%s\n" % lookup_conversion_method(box.radicand, "mathml")( - box.radicand, **child_options + return ( + f"{indent_spaces}\n%s\n{indent_spaces}" + % lookup_conversion_method(box.radicand, "mathml")( + box.radicand, **child_options + ) ) @@ -285,8 +303,11 @@ def sqrtbox(box: SqrtBox, **options): def subscriptbox(box: SubscriptBox, **options): # Note: values set in `options` take precedence over `box_options` + indent_level = options.get("_indent_level", 0) + indent_spaces = " " * indent_level child_options = {**options, **box.box_options} - return "\n%s\n%s\n" % ( + child_options["_indent_level"] = indent_level + 1 + return f"{indent_spaces}\n%s\n%s\n{indent_spaces}" % ( lookup_conversion_method(box.base, "mathml")(box.base, **child_options), lookup_conversion_method(box.subindex, "mathml")(box.subindex, **child_options), ) @@ -297,8 +318,11 @@ def subscriptbox(box: SubscriptBox, **options): def superscriptbox(box: SuperscriptBox, **options): # Note: values set in `options` take precedence over `box_options` + indent_level = options.get("_indent_level", 0) + indent_spaces = " " * indent_level child_options = {**options, **box.box_options} - return "\n%s\n%s\n" % ( + child_options["_indent_level"] = indent_level + 1 + return f"{indent_spaces}\n%s\n%s\n{indent_spaces}" % ( lookup_conversion_method(box.base, "mathml")(box.base, **child_options), lookup_conversion_method(box.superindex, "mathml")( box.superindex, **child_options @@ -311,9 +335,13 @@ def superscriptbox(box: SuperscriptBox, **options): def subsuperscriptbox(box: SubsuperscriptBox, **options): # Note: values set in `options` take precedence over `box_options` + + indent_level = options.get("_indent_level", 0) + indent_spaces = " " * indent_level child_options = {**box.box_options, **options} + child_options["_indent_level"] = indent_level + 1 box.base.inside_row = box.subindex.inside_row = box.superindex.inside_row = True - return "\n%s\n%s\n%s\n" % ( + return f"{indent_spaces}\n%s\n%s\n%s\n{indent_spaces}" % ( lookup_conversion_method(box.base, "mathml")(box.base, **child_options), lookup_conversion_method(box.subindex, "mathml")(box.subindex, **child_options), lookup_conversion_method(box.superindex, "mathml")( @@ -327,7 +355,11 @@ def subsuperscriptbox(box: SubsuperscriptBox, **options): def rowbox(box: RowBox, **options) -> str: # Note: values set in `options` take precedence over `box_options` + indent_level = options.get("_indent_level", 0) + indent_spaces = " " * indent_level + child_options = {**box.box_options, **options} + child_options["_indent_level"] = indent_level + 1 result = [] inside_row = box.inside_row @@ -360,8 +392,7 @@ def is_list_interior(content): ) # print(f"mrow: {result}") - - return "\n%s\n" % "\n".join(result) + return f"{indent_spaces}\n%s\n{indent_spaces}" % ("\n".join(result),) add_conversion_fn(RowBox, rowbox) @@ -394,6 +425,10 @@ def graphicsbox(box: GraphicsBox, elements=None, **options) -> str: int(box.boxheight), base64.b64encode(svg_body.encode("utf8")).decode("utf8"), ) + indent_level = options.get("_indent_level", 0) + if indent_level: + mathml = " " * indent_level + mathml + # print("boxes_to_mathml", mathml) return mathml @@ -403,8 +438,17 @@ def graphicsbox(box: GraphicsBox, elements=None, **options) -> str: def graphics3dbox(box, elements=None, **options) -> str: """Turn the Graphics3DBox into a MathML string""" + indent_level = options.get("_indent_level", 0) + indent_spaces = " " * indent_level result = box.boxes_to_js(**options) - result = f"\n\n\n{result}\n\n\n" + result = ( + f"{indent_spaces}\n" + f"\n" + f"{indent_spaces} \n" + f"{indent_spaces} {result}\n" + f"{indent_spaces} \n\n" + f"{indent_spaces}" + ) return result diff --git a/test/format/format_tests.yaml b/test/format/format_tests.yaml index b15252cd9..569bb769b 100644 --- a/test/format/format_tests.yaml +++ b/test/format/format_tests.yaml @@ -139,8 +139,8 @@ mathml: System`InputForm: -4 System`OutputForm: -4 - System`StandardForm: "\n-\n4\n" - System`TraditionalForm: "\n-\n4\n" + System`StandardForm: "\n -\n 4\n" + System`TraditionalForm: "\n -\n 4\n" text: System`InputForm: '-4' System`OutputForm: '-4' @@ -158,8 +158,8 @@ mathml: System`InputForm: -4.32 System`OutputForm: -4.32 - System`StandardForm: "\n-\n4.32\n" - System`TraditionalForm: "\n-\n4.32\n" + System`StandardForm: "\n -\n 4.32\n" + System`TraditionalForm: "\n -\n 4.32\n" text: System`InputForm: '-4.32' System`OutputForm: '-4.32' @@ -177,8 +177,8 @@ mathml: System`InputForm: -4.33 System`OutputForm: -4.3 - System`StandardForm: "\n-\n4.33\n" - System`TraditionalForm: "\n-\n4.33\n" + System`StandardForm: "\n -\n 4.33\n" + System`TraditionalForm: "\n -\n 4.33\n" text: System`InputForm: -4.33`2. System`OutputForm: '-4.3' @@ -196,8 +196,8 @@ mathml: System`InputForm: -4.32 System`OutputForm: -4.320 - System`StandardForm: "\n-\n4.32\n" - System`TraditionalForm: "\n-\n4.32\n" + System`StandardForm: "\n -\n 4.32\n" + System`TraditionalForm: "\n -\n 4.32\n" text: System`InputForm: -4.32`4. System`OutputForm: '-4.320' @@ -271,10 +271,12 @@ System`OutputForm: - '1 / (1 + 1 / (1 + 1 / a))' - Fragile! - System`StandardForm: &id001 - - "\n1\n\n1\n+\n\n1\n\n1\n+\n\n1\na\n\n\n\n\n" + System`StandardForm: + - "\n 1\n \n 1\n +\n \n 1\n \n 1\n +\n \n 1\n a\n \n \n \n \n" + - Fragile! + System`TraditionalForm: + - "\n 1\n \n 1\n +\n \n 1\n \n 1\n +\n \n 1\n a\n \n \n \n \n" - Fragile! - System`TraditionalForm: *id001 text: System`InputForm: 1/(1 + 1/(1 + 1/a)) System`OutputForm: 1 / (1 + 1 / (1 + 1 / a)) @@ -292,8 +294,8 @@ mathml: System`InputForm: <|a -> x, b -> y, c -> <|d -> t|>|> System`OutputForm: '<|a -> x, b -> y, c -> <|d -> t|>|>' - System`StandardForm: "\n<|\n\n\na\n->\nx\n\n,\n\nb\n->\ny\n\n,\n\nc\n->\n\n<|\n\nd\n->\nt\n\n|>\n\n\n\n|>\n" - System`TraditionalForm: "\n<|\n\n\na\n->\nx\n\n,\n\nb\n->\ny\n\n,\n\nc\n->\n\n<|\n\nd\n->\nt\n\n|>\n\n\n\n|>\n" + System`StandardForm: "\n <|\n \n \n a\n ->\n x\n \n ,\n \n b\n ->\n y\n \n ,\n \n c\n ->\n \n <|\n \n d\n ->\n t\n \n |>\n \n \n \n |>\n" + System`TraditionalForm: "\n <|\n \n \n a\n ->\n x\n \n ,\n \n b\n ->\n y\n \n ,\n \n c\n ->\n \n <|\n \n d\n ->\n t\n \n |>\n \n \n \n |>\n" text: System`InputForm: <|a -> x, b -> y, c -> <|d -> t|>|> System`OutputForm: <|a -> x, b -> y, c -> <|d -> t|>|> @@ -311,8 +313,8 @@ Association[a -> x, b -> y, c -> Association[d -> t, Association[e -> u]]]: mathml: System`InputForm: "<|a -> x, b -> y, c -> <|d -> t, e -> u|>|>" System`OutputForm: '<|a -> x, b -> y, c -> <|d -> t, e -> u|>|>' - System`StandardForm: "\n<|\n\n\na\n->\nx\n\n,\n\nb\n->\ny\n\n,\n\nc\n->\n\n<|\n\n\nd\n->\nt\n\n,\n\ne\n->\nu\n\n\n|>\n\n\n\n|>\n" - System`TraditionalForm: "\n<|\n\n\na\n->\nx\n\n,\n\nb\n->\ny\n\n,\n\nc\n->\n\n<|\n\n\nd\n->\nt\n\n,\n\ne\n->\nu\n\n\n|>\n\n\n\n|>\n" + System`StandardForm: "\n <|\n \n \n a\n ->\n x\n \n ,\n \n b\n ->\n y\n \n ,\n \n c\n ->\n \n <|\n \n \n d\n ->\n t\n \n ,\n \n e\n ->\n u\n \n \n |>\n \n \n \n |>\n" + System`TraditionalForm: "\n <|\n \n \n a\n ->\n x\n \n ,\n \n b\n ->\n y\n \n ,\n \n c\n ->\n \n <|\n \n \n d\n ->\n t\n \n ,\n \n e\n ->\n u\n \n \n |>\n \n \n \n |>\n" text: System`InputForm: <|a -> x, b -> y, c -> <|d -> t, e -> u|>|> System`OutputForm: <|a -> x, b -> y, c -> <|d -> t, e -> u|>|> @@ -330,8 +332,8 @@ Complex[1.09*^12, 3.]: mathml: System`InputForm: 1.09*^12 + 3.*I System`OutputForm: '1.09×10^12 + 3. I' - System`StandardForm: "\n\n1.09\n*^\n12\n\n+\n\n3.\n \nI\n\n" - System`TraditionalForm: "\n\n1.09\n×\n\n10\n12\n\n\n+\n\n3.\n\nI\n\n" + System`StandardForm: "\n \n 1.09\n *^\n 12\n \n +\n \n 3.\n  \n I\n \n" + System`TraditionalForm: "\n \n 1.09\n ×\n \n 10\n 12\n \n \n +\n \n 3.\n \n I\n \n" text: System`InputForm: 1.09*^12 + 3.*I System`OutputForm: "1.09\xD710^12 + 3. I" @@ -458,18 +460,8 @@ Graphics[{}]: mathml: System`InputForm: "Grid[{{"Spanish", "Hola!"}, {"Portuguese", "Olà!"}, {"English", "Hi!"}}]" System`OutputForm: 'Spanish      Hola!Portuguese   Olà!English      Hi!' - System`StandardForm: "\nSpanishHola!\n\ - PortugueseOl\xE0!\nEnglishHi!\n\ - " - System`TraditionalForm: "\nSpanishHola!\n\ - PortugueseOl\xE0!\nEnglishHi!\n\ - " + System`StandardForm: "\n \n \n Spanish\n \n \n Hola!\n \n \n \n \n Portuguese\n \n \n Olà!\n \n \n \n \n English\n \n \n Hi!\n \n \n" + System`TraditionalForm: "\n \n \n Spanish\n \n \n Hola!\n \n \n \n \n Portuguese\n \n \n Olà!\n \n \n \n \n English\n \n \n Hi!\n \n \n" text: System`InputForm: "Grid[{{\"Spanish\", \"Hola!\"}, {\"Portuguese\", \"Ol\xE0!\"\ }, {\"English\", \"Hi!\"}}]" @@ -497,20 +489,8 @@ Grid[{{a,b},{c,d}}]: mathml: System`InputForm: Grid[{{a, b}, {c, d}}] System`OutputForm: 'a   bc   d' - System`StandardForm: ' - - ab - - cd - - ' - System`TraditionalForm: ' - - ab - - cd - - ' + System`StandardForm: "\n \n \n a\n \n \n b\n \n \n \n \n c\n \n \n d\n \n \n" + System`TraditionalForm: "\n \n \n a\n \n \n b\n \n \n \n \n c\n \n \n d\n \n \n" text: System`InputForm: Grid[{{a, b}, {c, d}}] System`OutputForm: 'a b @@ -543,8 +523,8 @@ Integrate[F[x], {x, a, g[b]}]: mathml: System`InputForm: 'Integrate[F[x], {x, a, g[b]}]' System`OutputForm: 'Integrate[F[x], {x, a, g[b]}]' - System`StandardForm: "\n\n\na\n\ng\n[\nb\n]\n\n\n\n\nF\n[\nx\n]\n\n\n\n𝑑\nx\n\n" - System`TraditionalForm: "\n\n\na\n\ng\n(\nb\n)\n\n\n\n\nF\n(\nx\n)\n\n\n\n𝑑\nx\n\n" + System`StandardForm: "\n \n \n a\n \n g\n [\n b\n ]\n \n \n \u2062\n \n F\n [\n x\n ]\n \n \u2062\n \n 𝑑\n x\n \n" + System`TraditionalForm: "\n \n \n a\n \n g\n (\n b\n )\n \n \n \u2062\n \n F\n (\n x\n )\n \n \u2062\n \n 𝑑\n x\n \n" text: System`InputForm: Integrate[F[x], {x, a, g[b]}] System`OutputForm: Integrate[F[x], {x, a, g[b]}] @@ -568,8 +548,8 @@ MatrixForm[{{a,b},{c,d}}]: mathml: System`InputForm: MatrixForm[{{a, b}, {c, d}}] System`OutputForm: 'a   bc   d' - System`StandardForm: "\n(\n\nab\ncd\n\n)\n" - System`TraditionalForm: "\n(\n\nab\ncd\n\n)\n" + System`StandardForm: "\n (\n \n \n \n a\n \n \n b\n \n \n \n \n c\n \n \n d\n \n \n \n )\n" + System`TraditionalForm: "\n (\n \n \n \n a\n \n \n b\n \n \n \n \n c\n \n \n d\n \n \n \n )\n" text: System`InputForm: MatrixForm[{{a, b}, {c, d}}] System`OutputForm: 'a b @@ -607,10 +587,10 @@ Sqrt[1/(1+1/(1+1/a))]: - 'Sqrt[1 / (1 + 1 / (1 + 1 / a))]' - Fragile! System`StandardForm: - - "\n\n1\n\n1\n+\n\n1\n\n1\n+\n\n1\na\n\n\n\n\n\n" + - "\n \n 1\n \n 1\n +\n \n 1\n \n 1\n +\n \n 1\n a\n \n \n \n \n \n" - Fragile! System`TraditionalForm: - - "\n\n1\n\n1\n+\n\n1\n\n1\n+\n\n1\na\n\n\n\n\n\n" + - "\n \n 1\n \n 1\n +\n \n 1\n \n 1\n +\n \n 1\n a\n \n \n \n \n \n" - Fragile! text: System`InputForm: Sqrt[1/(1 + 1/(1 + 1/a))] @@ -633,8 +613,8 @@ Subscript[a, 4]: System`OutputForm: - 'Subscript[a, 4]' - Fragile! - System`StandardForm: "\na\n4\n" - System`TraditionalForm: "\na\n4\n" + System`StandardForm: "\n a\n 4\n" + System`TraditionalForm: "\n a\n 4\n" text: System`InputForm: Subscript[a, 4] System`OutputForm: Subscript[a, 4] @@ -654,8 +634,8 @@ Subsuperscript[a, p, q]: mathml: System`InputForm: Subsuperscript[a, p, q] System`OutputForm: 'Subsuperscript[a, p, q]' - System`StandardForm: "\na\np\nq\n" - System`TraditionalForm: "\na\np\nq\n" + System`StandardForm: "\n a\n p\n q\n" + System`TraditionalForm: "\n a\n p\n q\n" text: System`InputForm: Subsuperscript[a, p, q] System`OutputForm: Subsuperscript[a, p, q] @@ -716,8 +696,8 @@ TableForm[{{a,b},{c,d}}]: mathml: System`InputForm: TableForm[{{a, b}, {c, d}}] System`OutputForm: 'a   bc   d' - System`StandardForm: "\nab\ncd\n" - System`TraditionalForm: "\nab\ncd\n" + System`StandardForm: "\n \n \n a\n \n \n b\n \n \n \n \n c\n \n \n d\n \n \n" + System`TraditionalForm: "\n \n \n a\n \n \n b\n \n \n \n \n c\n \n \n d\n \n \n" text: System`InputForm: TableForm[{{a, b}, {c, d}}] System`OutputForm: 'a b @@ -807,7 +787,7 @@ a^(g[b]/c): mathml: System`InputForm: a^(g[b]/c) System`OutputForm: 'a ^ (g[b] / c)' - System`TraditionalForm: "\na\n\n\ng\n(\nb\n)\n\nc\n\n" + System`TraditionalForm: "\n a\n \n \n g\n (\n b\n )\n \n c\n \n" text: System`InputForm: a^(g[b]/c) System`OutputForm: a ^ (g[b] / c) @@ -825,8 +805,8 @@ a^4: mathml: System`InputForm: 'a^4' System`OutputForm: 'a ^ 4' - System`StandardForm: "\na\n4\n" - System`TraditionalForm: "\na\n4\n" + System`StandardForm: "\n a\n 4\n" + System`TraditionalForm: "\n a\n 4\n" text: System`InputForm: a^4 System`OutputForm: a ^ 4 @@ -870,7 +850,7 @@ a+PrecedenceForm[b+c,10]: System`StandardForm: 'a+(b+c)' mathml: System`OutputForm: 'a + (b + c)' - System`StandardForm: "\na\n+\n\n(\n\nb\n+\nc\n\n)\n\n" + System`StandardForm: "\n a\n +\n \n (\n \n b\n +\n c\n \n )\n \n" text: System`InputForm: 'a + (PrecedenceForm[b + c, 10])' System`OutputForm: 'a + (b + c)' diff --git a/test/test_session.py b/test/test_session.py index 4f04fb0df..08d779a1f 100644 --- a/test/test_session.py +++ b/test/test_session.py @@ -55,7 +55,7 @@ def test_session_format_evaluation(): assert session.format_result(form="text") == "a / b" assert session.format_result(form="latex") == "\\frac{a}{b}" assert session.format_result(form="xml") == ( - '\na\nb\n' + '\n \n a\n b\n \n' )