Skip to content

Commit f89f615

Browse files
authored
Go over formatting now that we have InterpretBox (#245)
* Go over formatting now that we have InterpretBox * Add Flaky TeX rendering Sometimes works, sometimes it doesn't. But better than before.
1 parent 860f447 commit f89f615

File tree

2 files changed

+41
-18
lines changed

2 files changed

+41
-18
lines changed

mathics_django/web/controllers/doc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def doc_chapter(
7878
"""
7979
check_for_new_load_modules()
8080
chapter = documentation.get_chapter(part, chapter)
81-
if not chapter:
81+
if chapter is None:
8282
raise Http404
8383
return render_doc(
8484
request,

mathics_django/web/format.py

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,31 @@
33
"""
44

55
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+
)
712
from mathics.format.box import format_element
813

914
FORM_TO_FORMAT = {
1015
"System`FullForm": "text",
1116
"System`InputForm": "text",
12-
# "System`MathMLForm": "text",
17+
"System`MathMLForm": "xml",
1318
"System`OutputForm": "text",
14-
# "System`TeXForm": "text",
19+
"System`TeXForm": "xml",
1520
"System`String": "text",
1621
}
1722

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)
2631

2732

2833
def format_output(evaluation, expr, format=None):
@@ -55,26 +60,44 @@ def format_output(evaluation, expr, format=None):
5560
format = FORM_TO_FORMAT[expr_type]
5661

5762
# This part was derived from and the same as evaluation.py format_output.
58-
5963
if format == "text":
6064
boxed = format_element(expr, evaluation, SymbolOutputForm)
6165
result = boxed.boxes_to_text()
6266

63-
return safe_html_string(result)
67+
return result
6468
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+
6689
result = (
6790
'<math display="block">'
68-
f"{boxes.boxes_to_mathml(evaluation=evaluation)}"
91+
f"{boxed.boxes_to_mathml(evaluation=evaluation)}"
6992
"</math>"
7093
)
71-
return safe_html_string(result)
94+
return result
7295
elif format == "tex":
7396
boxes = format_element(expr, evaluation, SymbolStandardForm)
7497
if isinstance(boxes, String):
7598
result = boxes.boxes_to_text()
7699
else:
77100
result = boxes.boxes_to_tex(evaluation=evaluation)
78-
return safe_html_string(result)
101+
return result
79102

80103
raise ValueError

0 commit comments

Comments
 (0)