Skip to content

Commit a336191

Browse files
committed
feat: add join helper
1 parent 42d156e commit a336191

2 files changed

Lines changed: 100 additions & 0 deletions

File tree

‎src/osw/utils/templates.py‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,43 @@ def compile_handlebars_template(template):
1919
return compiled_template
2020

2121

22+
# Python implementations of handlebars helpers
23+
# https://github.com/OpenSemanticLab/mediawiki-extensions-MwJson/blob/main/modules/ext.MwJson.editor/MwJson_editor.js#L1342
24+
25+
26+
def helper_join(this, options, context, separator=None, intro=None, outro=None):
27+
"""
28+
removes all empty interation results and delimits them with
29+
the given separator (default: ", ")
30+
{{#join literal_array }}{{.}}{{/join}}
31+
{{#join object_array ", " "[" "]"}}{{#if print}}{{value}}{{/if}}{{/join}}
32+
"""
33+
34+
# handle optional params
35+
if intro is None:
36+
intro = ""
37+
if outro is None:
38+
outro = ""
39+
if separator is None:
40+
separator = ", "
41+
42+
if context is None:
43+
context = []
44+
items = []
45+
46+
for e in context:
47+
inner = "".join(options["fn"](e))
48+
items.append(inner)
49+
50+
# Remove empty or whitespace-only elements
51+
items = [item for item in items if item.strip() != ""]
52+
if len(items) == 0:
53+
intro = outro = ""
54+
55+
# Join with separator, wrap with intro + outro
56+
return intro + separator.join(items) + outro
57+
58+
2259
def eval_compiled_handlebars_template(
2360
compiled_template, data, helpers=None, partials=None, add_self_as_partial=True
2461
):
@@ -41,12 +78,19 @@ def eval_compiled_handlebars_template(
4178
-------
4279
the evaluated template as a string
4380
"""
81+
82+
default_helpers = {
83+
"join": helper_join,
84+
}
4485
if helpers is None:
4586
helpers = {}
87+
helpers = {**default_helpers, **helpers}
88+
4689
if partials is None:
4790
partials = {}
4891
if add_self_as_partial:
4992
partials["self"] = compiled_template
93+
5094
return compiled_template(data, helpers=helpers, partials=partials)
5195

5296

‎tests/utils/templates_test.py‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,59 @@ def test_metamodel_template():
271271
output = json.loads(eval_handlebars_template(template, data))
272272

273273
assert output == json.loads(expected)
274+
275+
276+
def test_helper_join():
277+
template = """{
278+
"@context": [{
279+
{{#join properties}}
280+
{{#if rdf_property}}"{{name}}": "{{rdf_property}}"{{/if}}
281+
{{/join}}
282+
}]
283+
}"""
284+
285+
data = {
286+
"properties": [
287+
{
288+
"name": "test_property",
289+
"rdf_property": "Property:TestPropertyWithSchema",
290+
},
291+
{
292+
"name": "test_property2",
293+
"rdf_property": "Property:TestPropertyWithSchema",
294+
},
295+
]
296+
}
297+
298+
expected = {
299+
"@context": [
300+
{
301+
"test_property": "Property:TestPropertyWithSchema",
302+
"test_property2": "Property:TestPropertyWithSchema",
303+
}
304+
]
305+
}
306+
307+
output = json.loads(eval_handlebars_template(template, data))
308+
309+
assert output == expected
310+
311+
template = """
312+
{{#join object_array ", " "[" "]"}}{{#if print}}{{value}}{{/if}}{{/join}}
313+
"""
314+
315+
data = {
316+
"object_array": [
317+
{"value": 1, "print": True},
318+
{"value": 2},
319+
{"value": 3, "print": True},
320+
]
321+
}
322+
323+
expected = [1, 3]
324+
325+
output = json.loads(eval_handlebars_template(template, data))
326+
assert output == expected
327+
328+
329+
test_helper_join()

0 commit comments

Comments
 (0)