Skip to content

Commit 56e3708

Browse files
committed
Handle Unicode/WL charcter code mismatches
1 parent 26de5b8 commit 56e3708

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed

mathicsscript/__main__.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77
import re
88
from pathlib import Path
99

10-
from mathicsscript.termshell import TerminalShell
10+
from mathicsscript.termshell import (
11+
TerminalShell,
12+
wl_replace_dict_esc,
13+
wl_replace_pattern,
14+
)
15+
1116
from mathicsscript.format import format_output
1217

1318
from mathics.core.parser import FileLineFeeder
@@ -25,18 +30,6 @@
2530

2631
from mathicsscript.version import __version__
2732

28-
wl_replace_dict = {
29-
"": "Ạ",
30-
"": "ạ",
31-
"": "Ḅ",
32-
"": "ḅ",
33-
# ...
34-
"": "→",
35-
"": "↔",
36-
}
37-
wl_replace_dict_esc = dict((re.escape(k), v) for k, v in wl_replace_dict.items())
38-
wl_replace_pattern = re.compile("|".join(wl_replace_dict_esc.keys()))
39-
4033

4134
def replace_wl_to_unicode(wl_input: str) -> str:
4235
"""WL uses some non-unicode character for various things.

mathicsscript/termshell.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,39 @@
7272
RL_COMPLETER_DELIMS_WITH_BRACE = " \t\n_~!@#%^&*()-=+{]}|;:'\",<>/?"
7373
RL_COMPLETER_DELIMS = " \t\n_~!@#%^&*()-=+[{]}\\|;:'\",<>/?"
7474

75+
wl_replace_dict = {
76+
"": "Ạ",
77+
"": "ạ",
78+
"": "Ḅ",
79+
"": "ḅ",
80+
# ...
81+
"": "→",
82+
"": "↔",
83+
}
84+
85+
wl_replace_dict_esc = dict((re.escape(k), v) for k, v in wl_replace_dict.items())
86+
wl_replace_pattern = re.compile("|".join(wl_replace_dict_esc.keys()))
87+
88+
unicode_replace_dict = {v: k for k, v in wl_replace_dict.items()}
89+
unicode_replace_dict_esc = dict(
90+
(re.escape(k), v) for k, v in unicode_replace_dict.items()
91+
)
92+
unicode_replace_pattern = re.compile("|".join(unicode_replace_dict_esc.keys()))
93+
94+
7595
from mathics.core.parser import LineFeeder
7696

7797

98+
def replace_unicode_to_wl(unicode_input: str) -> str:
99+
"""WL uses some non-unicode character for various things.
100+
Replace the unicode equivalent with the WL equivalent.
101+
Formal values like FormalA are like this.
102+
"""
103+
return unicode_replace_pattern.sub(
104+
lambda m: unicode_replace_dict_esc[re.escape(m.group(0))], wl_input
105+
)
106+
107+
78108
def is_pygments_style(style):
79109
if style not in ALL_PYGMENTS_STYLES:
80110
print("Pygments style name '%s' not found." % style)
@@ -221,7 +251,7 @@ def out_callback(self, out):
221251
def read_line(self, prompt):
222252
if self.using_readline:
223253
return self.rl_read_line(prompt)
224-
return input(prompt)
254+
return replace_unicode_to_wl(input(prompt))
225255

226256
def print_result(self, result, output_style=""):
227257
if result is None:

0 commit comments

Comments
 (0)