|
3 | 3 | """ |
4 | 4 |
|
5 | 5 | from mathics.core.atoms import String |
6 | | -from mathics.core.systemsymbols import SymbolOutputForm, SymbolStandardForm |
| 6 | +from mathics.core.systemsymbols import ( |
| 7 | + SymbolMathMLForm, |
| 8 | + SymbolOutputForm, |
| 9 | + SymbolStandardForm, |
| 10 | + SymbolTeXForm, |
| 11 | +) |
7 | 12 | from mathics.format.box import format_element |
8 | 13 |
|
9 | 14 | FORM_TO_FORMAT = { |
10 | 15 | "System`FullForm": "text", |
11 | 16 | "System`InputForm": "text", |
12 | | - # "System`MathMLForm": "text", |
| 17 | + "System`MathMLForm": "xml", |
13 | 18 | "System`OutputForm": "text", |
14 | | - # "System`TeXForm": "text", |
| 19 | + "System`TeXForm": "xml", |
15 | 20 | "System`String": "text", |
16 | 21 | } |
17 | 22 |
|
18 | | - |
19 | | -def safe_html_string(value): |
20 | | - """ |
21 | | - Escape characters in the |
22 | | - """ |
23 | | - # TODO: check why the escaped characters are converted back to valid |
24 | | - # HTML code and processed. |
25 | | - return value # mark_safe(escape_html(value)) |
| 23 | +# def safe_html_string(value: str) -> str: |
| 24 | +# """ |
| 25 | +# Escape characters in the |
| 26 | +# """ |
| 27 | +# # TODO: check why the escaped characters are converted back to valid |
| 28 | +# # HTML code and processed. |
| 29 | +# import html |
| 30 | +# return html.escape(value) |
26 | 31 |
|
27 | 32 |
|
28 | 33 | def format_output(evaluation, expr, format=None): |
@@ -55,26 +60,44 @@ def format_output(evaluation, expr, format=None): |
55 | 60 | format = FORM_TO_FORMAT[expr_type] |
56 | 61 |
|
57 | 62 | # This part was derived from and the same as evaluation.py format_output. |
58 | | - |
59 | 63 | if format == "text": |
60 | 64 | boxed = format_element(expr, evaluation, SymbolOutputForm) |
61 | 65 | result = boxed.boxes_to_text() |
62 | 66 |
|
63 | | - return safe_html_string(result) |
| 67 | + return result |
64 | 68 | elif format == "xml": |
65 | | - boxes = format_element(expr, evaluation, SymbolStandardForm) |
| 69 | + is_TeXForm = False |
| 70 | + if expr.get_head_name() == "System`MathMLForm": |
| 71 | + boxed = format_element(expr, evaluation, SymbolMathMLForm) |
| 72 | + elif expr.get_head_name() == "System`TeXForm": |
| 73 | + boxed = format_element(expr, evaluation, SymbolTeXForm) |
| 74 | + is_TeXForm = True |
| 75 | + else: |
| 76 | + boxed = format_element(expr, evaluation, SymbolStandardForm) |
| 77 | + |
| 78 | + if ( |
| 79 | + hasattr(boxed, "get_head_name") |
| 80 | + and boxed.get_head_name() == "System`InterpretationBox" |
| 81 | + ): |
| 82 | + first_element = boxed.elements[0] |
| 83 | + if isinstance(first_element, String): |
| 84 | + box_str_sans_quotes = boxed.elements[0].value[1:-1] |
| 85 | + if is_TeXForm: |
| 86 | + return f"$${box_str_sans_quotes}$$" |
| 87 | + return box_str_sans_quotes |
| 88 | + |
66 | 89 | result = ( |
67 | 90 | '<math display="block">' |
68 | | - f"{boxes.boxes_to_mathml(evaluation=evaluation)}" |
| 91 | + f"{boxed.boxes_to_mathml(evaluation=evaluation)}" |
69 | 92 | "</math>" |
70 | 93 | ) |
71 | | - return safe_html_string(result) |
| 94 | + return result |
72 | 95 | elif format == "tex": |
73 | 96 | boxes = format_element(expr, evaluation, SymbolStandardForm) |
74 | 97 | if isinstance(boxes, String): |
75 | 98 | result = boxes.boxes_to_text() |
76 | 99 | else: |
77 | 100 | result = boxes.boxes_to_tex(evaluation=evaluation) |
78 | | - return safe_html_string(result) |
| 101 | + return result |
79 | 102 |
|
80 | 103 | raise ValueError |
0 commit comments