Skip to content

Commit

Permalink
CHAM-572 Remove the exclamation mark from yaml context
Browse files Browse the repository at this point in the history
  • Loading branch information
karampitsos committed Sep 5, 2023
1 parent 590e19c commit 01753fe
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 9 deletions.
5 changes: 4 additions & 1 deletion openformats/formats/yaml/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ def construct_mapping(self, node, deep=True):
and self._is_custom_tag(value_node.tag)
):
tag = six.text_type(value_node.tag)
# remove the exclamation mark from the tag
tag = tag[1:] if tag.startswith('!') else tag

value = Node(value, start, end, style, tag)
pairs.append((key, value))
Expand Down Expand Up @@ -347,8 +349,9 @@ def generate_yaml_dict(self, stringset):
yaml_dict, keys, flags, se.string.get(rule), tag=None,
)
else:
tag = '!' + se.context if se.context else se.context
self._insert_translation_in_dict(
yaml_dict, keys, flags, se.string, tag=se.context,
yaml_dict, keys, flags, se.string, tag=tag,
)
return yaml_dict

Expand Down
3 changes: 2 additions & 1 deletion openformats/formats/yaml/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,8 @@ def _compile_from_template(self, template, stringset, **kwargs):
# it and apply a space afterwards so it doesn't get merged with the
# string
if string.context:
transcriber.add(string.context)
# add an exclamation mark to the context to make it a tag
transcriber.add('!' + string.context)
transcriber.add(' ')
transcriber.add(translation)
transcriber.skip(len(string.template_replacement))
Expand Down
7 changes: 7 additions & 0 deletions openformats/tests/formats/yaml/files/1_el.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ hello: "el:World" # Translatable
number: !!int 123 # Should ignore
bin: !!binary aGVsbG8= # Should ignore

# Custom tags with numbers and symbols
context_string: !he:fd94;fd/la "el:context string"
verbim_context_string: !contex!t545qa "el:verbim context string"
context_on_nested_map:
first: !first_context:54KJFLA95KJ4 "el:context in nested map"
second: !second_context:FDKJ40DK "el:context in nested map"

# Test with non-ASCII keys
#σχόλιο
σχόλιο: "el:κείμενο"
Expand Down
7 changes: 7 additions & 0 deletions openformats/tests/formats/yaml/files/1_en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ hello: !!str World # Translatable
number: !!int 123 # Should ignore
bin: !!binary aGVsbG8= # Should ignore

# Custom tags with numbers and symbols
context_string: !he:fd94;fd/la "context string"
verbim_context_string: !<!contex!t545qa> "verbim context string"
context_on_nested_map:
first: !first_context:54KJFLA95KJ4 "context in nested map"
second: !second_context:FDKJ40DK "context in nested map"

# Test with non-ASCII keys
#σχόλιο
σχόλιο: κείμενο
Expand Down
7 changes: 7 additions & 0 deletions openformats/tests/formats/yaml/files/1_en_exported.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ hello: World # Translatable
number: !!int 123 # Should ignore
bin: !!binary aGVsbG8= # Should ignore

# Custom tags with numbers and symbols
context_string: !he:fd94;fd/la "context string"
verbim_context_string: !contex!t545qa "verbim context string"
context_on_nested_map:
first: !first_context:54KJFLA95KJ4 "context in nested map"
second: !second_context:FDKJ40DK "context in nested map"

# Test with non-ASCII keys
#σχόλιο
σχόλιο: κείμενο
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ alias_key:
foo: !test 'bar'
bar: !xml "foo <xml>bar</xml>"
hello: World
context_string: !he:fd94;fd/la "context string"
verbim_context_string: !contex%21t545qa "verbim context string"
context_on_nested_map:
first: "context in nested map"
second: "context in nested map"
σχόλιο: κείμενο
emojis: \uD83D\uDC40 🏹🍍🍍 \U0001F418
anchor_with_label:
Expand Down
11 changes: 9 additions & 2 deletions openformats/tests/formats/yaml/files/1_tpl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,19 @@ alias_key:
- ddc3cfcedcf1686d9e3ba6b99a0d091b_tr

# Custom tags
foo: 3e4000f6f4cd8bb27db6fb82e1b50bb4_tr # Should treat as string and ignore leading spaces
bar: 75ce597a505a4faf6369b66b885926c8_tr # Also a string
foo: 023503fb466f78932b77209b6581156e_tr # Should treat as string and ignore leading spaces
bar: fac8140a2ec031af14bc758e73f59017_tr # Also a string
hello: b0ed9cf22c0a5186d1c5b483a910dd33_tr # Translatable
number: !!int 123 # Should ignore
bin: !!binary aGVsbG8= # Should ignore

# Custom tags with numbers and symbols
context_string: 629aded197db84c4d323cd3ed4cf0485_tr
verbim_context_string: d37275706209cb19ee621ff3835522fe_tr
context_on_nested_map:
first: 95175e30d6fbfe0f658e75919cd4982c_tr
second: dce391c231a7ad7fef9e6cc0ddcbf549_tr

# Test with non-ASCII keys
#σχόλιο
σχόλιο: 8687f34a9bf89770b456b48ad2395788_tr
Expand Down
2 changes: 1 addition & 1 deletion openformats/tests/formats/yaml/test_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def test_openstring_attributes(self):
self.assertEqual(test_string.flags, '')

content_string = strings[1]
self.assertEqual(content_string.context, '!tag')
self.assertEqual(content_string.context, 'tag')
self.assertEqual(content_string.flags, "'")

def test_parse_duplicate_keys(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ en:
798cf4a4e275a90e80b0aac837f06793_pl

# context
context_string: 1e31a39d57f088cfc6c2eb9585c0b467_tr
verbim_context_string: e147f6d42ae7e10950e4157fcce2e534_tr
context_string: 629aded197db84c4d323cd3ed4cf0485_tr
verbim_context_string: d37275706209cb19ee621ff3835522fe_tr
context_on_nested_map:
first: bbb714442aca9ca3d3dbf18aa54811d6_tr
second: 8a7ebe3f36daee5d0aed8c11384db83d_tr
first: 95175e30d6fbfe0f658e75919cd4982c_tr
second: dce391c231a7ad7fef9e6cc0ddcbf549_tr

0 comments on commit 01753fe

Please sign in to comment.