Skip to content

Commit 034ec76

Browse files
committed
fix: escape also non-standard escape sequences
1 parent f9beb14 commit 034ec76

4 files changed

Lines changed: 36 additions & 13 deletions

File tree

‎src/osw/core.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
AggregateGeneratedSchemasParam,
2727
AggregateGeneratedSchemasParamMode,
2828
aggregate_generated_schemas,
29-
escape_double_quotes,
29+
escape_json_strings,
3030
)
3131
from osw.utils.templates import (
3232
compile_handlebars_template,
@@ -1177,7 +1177,7 @@ def store_entity_(
11771177
meta_category_template = meta_category_templates[key]
11781178
schema_str = eval_compiled_handlebars_template(
11791179
meta_category_template,
1180-
escape_double_quotes(jsondata),
1180+
escape_json_strings(jsondata),
11811181
{
11821182
"_page_title": entity_title, # Legacy
11831183
"_current_subject_": entity_title,

‎src/osw/utils/oold.py‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
https://github.com/OpenSemanticLab/mediawiki-extensions-MwJson/blob/main/modules/ext.MwJson.util/MwJson_util.js
44
"""
55

6+
import json
67
from copy import deepcopy
78
from enum import Enum
89
from typing import Dict, Optional, TypeVar
@@ -398,8 +399,9 @@ def aggregate_generated_schemas(
398399
return AggregateGeneratedSchemasResult(aggregated_schema=schema)
399400

400401

401-
def escape_double_quotes(obj: JsonType) -> JsonType:
402-
"""replace double quotes `"` with escaped double quotes `\"` in strings.
402+
def escape_json_strings(obj: JsonType) -> JsonType:
403+
"""replace double quotes `"` with escaped double quotes `\"` in
404+
and non-standard escape-squences in strings.
403405
If the object is a string, the escaped string is returned.
404406
If the object is a list, the function is called recursively for each element.
405407
If the object is a dictionary, the function is called recursively for each value.
@@ -416,15 +418,16 @@ def escape_double_quotes(obj: JsonType) -> JsonType:
416418
"""
417419
if isinstance(obj, str):
418420
# Escape double quotes in string
419-
return obj.replace('"', '\\"')
421+
# Replace invalid backslashes outside of math environments
422+
return json.dumps(obj)[1:-1]
420423
elif isinstance(obj, list):
421424
# Iterate over array elements
422-
return [escape_double_quotes(item) for item in obj]
425+
return [escape_json_strings(item) for item in obj]
423426
elif isinstance(obj, dict):
424427
# Iterate over object properties
425428
escaped_obj = {}
426429
for key, value in obj.items():
427-
escaped_obj[key] = escape_double_quotes(value)
430+
escaped_obj[key] = escape_json_strings(value)
428431
return escaped_obj
429432
# Return the value as is for non-string, non-object types
430433
return obj

‎tests/utils/oold.py‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import json
2+
13
from osw.utils.oold import (
24
AggregateGeneratedSchemasParam,
35
AggregateGeneratedSchemasParamMode,
46
aggregate_generated_schemas,
57
deep_equal,
6-
escape_double_quotes,
8+
escape_json_strings,
79
merge_deep,
810
merge_jsonld_context_object_list,
911
unique_array,
@@ -136,6 +138,24 @@ def test_aggregate_generated_schemas():
136138
}
137139

138140

139-
def test_escape_double_quotes():
140-
data = 'test "string"'
141-
assert escape_double_quotes(data) == r"test \"string\""
141+
def test_escape_json_strings():
142+
org_data = {
143+
"test": "normal string",
144+
"test2": 'string with "double quotes"',
145+
"test3": r"latex command $\textit{impedance}$",
146+
"test4": r"math $$M \cdot L^2 \cdot T^{-2} \cdot N^{-1}$$",
147+
}
148+
data = escape_json_strings(org_data)
149+
150+
data_string = "{"
151+
# serialize manually to json as a handlebars template would do
152+
for key, value in data.items():
153+
data_string += f'\n "{key}": "{value}"'
154+
if key != list(data.keys())[-1]:
155+
data_string += ", "
156+
data_string += "\n}"
157+
data = data_string
158+
159+
result = json.loads(data)
160+
161+
assert result == org_data

‎tests/utils/templates_test.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# flake8: noqa: E501
22
import json
33

4-
from osw.utils.oold import escape_double_quotes
4+
from osw.utils.oold import escape_json_strings
55
from osw.utils.templates import eval_handlebars_template
66

77

@@ -93,7 +93,7 @@ def test_category_template():
9393
output = json.loads(
9494
eval_handlebars_template(
9595
template,
96-
escape_double_quotes(data),
96+
escape_json_strings(data),
9797
{"_page_title": "Category:OSW379d5a1589c74c82bc0de47938264d00"},
9898
)
9999
)

0 commit comments

Comments
 (0)