Skip to content

Commit f9beb14

Browse files
committed
fix: handle double-quote and inline escape in template processing
1 parent cb5bc2f commit f9beb14

5 files changed

Lines changed: 92 additions & 6 deletions

File tree

‎src/osw/core.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
AggregateGeneratedSchemasParam,
2727
AggregateGeneratedSchemasParamMode,
2828
aggregate_generated_schemas,
29+
escape_double_quotes,
2930
)
3031
from osw.utils.templates import (
3132
compile_handlebars_template,
@@ -1176,7 +1177,7 @@ def store_entity_(
11761177
meta_category_template = meta_category_templates[key]
11771178
schema_str = eval_compiled_handlebars_template(
11781179
meta_category_template,
1179-
jsondata,
1180+
escape_double_quotes(jsondata),
11801181
{
11811182
"_page_title": entity_title, # Legacy
11821183
"_current_subject_": entity_title,

‎src/osw/utils/oold.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,3 +396,35 @@ def aggregate_generated_schemas(
396396
schema["description"] = generated_schema["description"]
397397

398398
return AggregateGeneratedSchemasResult(aggregated_schema=schema)
399+
400+
401+
def escape_double_quotes(obj: JsonType) -> JsonType:
402+
"""replace double quotes `"` with escaped double quotes `\"` in strings.
403+
If the object is a string, the escaped string is returned.
404+
If the object is a list, the function is called recursively for each element.
405+
If the object is a dictionary, the function is called recursively for each value.
406+
Else the object is returned as is.
407+
408+
Parameters
409+
----------
410+
obj
411+
the object to handle
412+
413+
Returns
414+
-------
415+
returns the object with double quotes escaped if applicable
416+
"""
417+
if isinstance(obj, str):
418+
# Escape double quotes in string
419+
return obj.replace('"', '\\"')
420+
elif isinstance(obj, list):
421+
# Iterate over array elements
422+
return [escape_double_quotes(item) for item in obj]
423+
elif isinstance(obj, dict):
424+
# Iterate over object properties
425+
escaped_obj = {}
426+
for key, value in obj.items():
427+
escaped_obj[key] = escape_double_quotes(value)
428+
return escaped_obj
429+
# Return the value as is for non-string, non-object types
430+
return obj

‎src/osw/utils/templates.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import re
2+
13
from pybars import Compiler
24

35

@@ -15,6 +17,13 @@ def compile_handlebars_template(template):
1517
the compiled template
1618
"""
1719
compiler = Compiler()
20+
# pybars does not support inline escaping, so we have to wrap the raw block
21+
# e.g. \{{escaped}} => {{{{raw}}}}{{escaped}}{{{{/raw}}}}
22+
# this workaround does not support expressions withing the escaped block,
23+
# e.g. \{{escaped {{some_var}} }} will not work
24+
# see https://handlebarsjs.com/guide/expressions.html#escaping-handlebars-expressions
25+
# see https://github.com/wbond/pybars3/pull/47
26+
template = re.sub(r"\\\{\{([^}]+)\}\}", r"{{{{raw}}}}{{\1}}{{{{/raw}}}}", template)
1827
compiled_template = compiler.compile(template)
1928
return compiled_template
2029

‎tests/utils/oold.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
AggregateGeneratedSchemasParamMode,
44
aggregate_generated_schemas,
55
deep_equal,
6+
escape_double_quotes,
67
merge_deep,
78
merge_jsonld_context_object_list,
89
unique_array,
@@ -133,3 +134,8 @@ def test_aggregate_generated_schemas():
133134
},
134135
"$ref": "#/$defs/generated",
135136
}
137+
138+
139+
def test_escape_double_quotes():
140+
data = 'test "string"'
141+
assert escape_double_quotes(data) == r"test \"string\""

‎tests/utils/templates_test.py‎

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

4+
from osw.utils.oold import escape_double_quotes
45
from osw.utils.templates import eval_handlebars_template
56

67

@@ -49,7 +50,7 @@ def test_category_template():
4950
"metaclass": ["Category:OSW725a3cf5458f4daea86615fcbd0029f8"],
5051
"description": [
5152
{
52-
"text": "Represents the set of all individuals. In the DL literature this is often called the top concept.",
53+
"text": 'Represents the set of all "individuals". In the DL literature this is often called the top concept.',
5354
"lang": "en",
5455
}
5556
],
@@ -72,9 +73,9 @@ def test_category_template():
7273
"title*": {
7374
"en": "OwlThing"
7475
},
75-
"description": "Represents the set of all individuals. In the DL literature this is often called the top concept.",
76+
"description": "Represents the set of all \\"individuals\\". In the DL literature this is often called the top concept.",
7677
"description*": {
77-
"en": "Represents the set of all individuals. In the DL literature this is often called the top concept."
78+
"en": "Represents the set of all \\"individuals\\". In the DL literature this is often called the top concept."
7879
},
7980
"required": [
8081
"type"
@@ -92,7 +93,7 @@ def test_category_template():
9293
output = json.loads(
9394
eval_handlebars_template(
9495
template,
95-
data,
96+
escape_double_quotes(data),
9697
{"_page_title": "Category:OSW379d5a1589c74c82bc0de47938264d00"},
9798
)
9899
)
@@ -326,4 +327,41 @@ def test_helper_join():
326327
assert output == expected
327328

328329

329-
test_helper_join()
330+
def test_raw_block():
331+
template = """
332+
{
333+
"unit": {
334+
"format": "autocomplete",
335+
"options": {
336+
"autocomplete": {
337+
"query": "[[-HasUnit::{{{quantity}}}]][[HasSymbol::like:*\{{_user_input}}*]]OR[[-HasPrefixUnit.-HasUnit::{{{quantity}}}]][[HasSymbol::like:*\{{_user_input}}*]]|?HasSymbol=label"
338+
}
339+
}
340+
}
341+
}"""
342+
343+
data = {
344+
"quantity": "Item:OSW1bd92826da6f5c53982ed6ea45bc1b9b",
345+
}
346+
347+
expected = """
348+
{
349+
"unit": {
350+
"format": "autocomplete",
351+
"options": {
352+
"autocomplete": {
353+
"query": "[[-HasUnit::Item:OSW1bd92826da6f5c53982ed6ea45bc1b9b]][[HasSymbol::like:*{{_user_input}}*]]OR[[-HasPrefixUnit.-HasUnit::Item:OSW1bd92826da6f5c53982ed6ea45bc1b9b]][[HasSymbol::like:*{{_user_input}}*]]|?HasSymbol=label"
354+
}
355+
}
356+
}
357+
}
358+
"""
359+
res = eval_handlebars_template(
360+
template,
361+
data,
362+
{"_page_title": "Category:OSW379d5a1589c74c82bc0de47938264d00"},
363+
)
364+
365+
output = json.loads(res)
366+
367+
assert output == json.loads(expected)

0 commit comments

Comments
 (0)