-
-
Notifications
You must be signed in to change notification settings - Fork 64
Improve use of CharacterEncoding #1735
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,6 @@ | |
| """ | ||
| Mathics3 box rendering to plain text. | ||
| """ | ||
|
|
||
| from mathics.builtin.box.graphics import GraphicsBox | ||
| from mathics.builtin.box.graphics3d import Graphics3DBox | ||
| from mathics.builtin.box.layout import ( | ||
|
|
@@ -34,6 +33,43 @@ | |
| add_render_function(FormBox, convert_inner_box_field) | ||
|
|
||
|
|
||
| # Map WMA encoding names to Python encoding names | ||
| ENCODING_WMA_TO_PYTHON = { | ||
| "WindowsEastEurope": "cp1250", | ||
| "WindowsCyrillic": "cp1251", | ||
| "WindowsANSI": "cp1252", | ||
| "WindowsGreek": "cp1252", | ||
| "WindowsTurkish": "cp1254", | ||
| } | ||
|
|
||
|
|
||
| def encode_string_value(value: str, encoding: str): | ||
| """Convert an Unicode string `value` to the required `encoding`""" | ||
| if encoding == "ASCII": | ||
| # TODO: replace from a table from MathicsScanner | ||
| ascii_map = { | ||
| "⇒": "=>", | ||
| "↔": "<->", | ||
| "→": "->", | ||
| "⇾": "->", | ||
| "⇾": "->", | ||
| "⇴": "->", | ||
| "∫": r"\[Integral]", | ||
| "𝑑": r"\[DifferentialD]", | ||
| "⧦": r"\[Equivalent]", | ||
| "×": r" x ", | ||
| } | ||
| result = "" | ||
| for ch in value: | ||
| ch = ascii_map.get(ch, ch) | ||
| result += ch | ||
| return result | ||
|
|
||
| encoding = ENCODING_WMA_TO_PYTHON.get(encoding, encoding) | ||
| result = value.encode("utf-8").decode(encoding) | ||
| return result | ||
|
|
||
|
|
||
| def fractionbox(box: FractionBox, **options) -> str: | ||
| # Note: values set in `options` take precedence over `box_options` | ||
| child_options = {**options, **box.box_options} | ||
|
|
@@ -159,6 +195,9 @@ def string(s: String, **options) -> str: | |
| if value.startswith('"') and value.endswith('"'): # nopep8 | ||
| if not show_string_characters: | ||
| value = value[1:-1] | ||
|
|
||
| if "encoding" in options and options["encoding"] != "Unicode": | ||
| value = encode_string_value(value, options["encoding"]) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at this more closely, there may be a deeper problem here. If the Mathics3 string was encoded with Unicode under the user's control, that should remain. If Mathics3 added the Unicode because an operator appeared, that is probably wrong, and the code that added the Unicode should be fixed. So, what is a specific scenario or situation where line 200 is triggered?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line 200 is triggered when the required encoding is not the standard Unicode. It happens when the SystemCharacterEncoding is not Unicode (for example by setting
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This paraphrases the if condition. I meant, what is it that is causing an operator to get converted before |
||
| return value | ||
|
|
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function is a just a proof of concept. The final version should look into the MathicsScanner tables