Skip to content

Commit

Permalink
Merge pull request #272 from transifex/TX-13920-yaml-front-matter-for…
Browse files Browse the repository at this point in the history
…matting-doesnt-get-preserved

Add pipe check to GITHUBMARKDOWNV2
  • Loading branch information
petrosklg authored May 19, 2022
2 parents c1e44d8 + a60e2a1 commit 18f29a9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
10 changes: 7 additions & 3 deletions openformats/formats/github_markdown_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class GithubMarkdownHandlerV2(OrderedCompilerMixin, Handler):
AT_SIGN = '@' # reserved character in the YAML spec
BRACKET_LEFT = '['
BRACKET_RIGHT = ']'
PIPE = '|'

# A string that looks like '\u0008'
ESCAPED_UNICODE = re.compile(r'\\u[a-fA-F0-9]{4}')
Expand Down Expand Up @@ -238,9 +239,8 @@ def _transform_yaml_string(self, openstring):
# this is to ensure that if the style is literal or folded
# http://www.yaml.org/spec/1.2/spec.html#id2795688
# a new line always follows the string
if (openstring.flags and openstring.flags[-1] in '|>'
and string[-1] != self.NEWLINE):
string = string + self.NEWLINE
if (openstring.flags and openstring.flags[-1] in '|>'):
string = openstring.flags[-1] + self.NEWLINE + string

return string

Expand Down Expand Up @@ -280,6 +280,10 @@ def _should_wrap_in_quotes(self, openstring):
the character that should be used for wrapping
:rtype: tuple (bool, str)
"""
# If pipe flag appears do not wrap it.
if self.PIPE in openstring.flags:
return False, openstring.flags

# If single or double quote flags appear, wrap it.
if openstring.flags in [self.DOUBLE_QUOTES, self.SINGLE_QUOTE]:
return True, openstring.flags
Expand Down
12 changes: 6 additions & 6 deletions openformats/tests/formats/github_markdown_v2/files/1_el.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ key2:
- "el:text with a #hashtag in it"
- "el:li 5"

description: "el:folded style text
"
custom_vars:
description: >
"el:folded style text
"custom_vars:
var1: "el:text: some value"
var2: "el:literal
style with \"quotes\"
var2: |
el:literal
style with "quotes"
text
"
nested_key_outer:
nested_key_inner:
"el:nested_value"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ key2:
- "text with a #hashtag in it"
- li 5

description: folded style text
description: >
folded style text
custom_vars:
var1: "text: some value"
var2: "literal
style with \"quotes\"
var2: |
literal
style with "quotes"
text
"
nested_key_outer:
nested_key_inner:
nested_value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ def test_should_wrap_in_quotes_if_starts_but_not_ends_with_bracket(self):
self.assertTrue(should_wrap)
self.assertEqual(wrap_char, u'"')

def test_should_not_wrap_in_quotes_if_has_pipe_char(self):
openstring = OpenString('k', u'{} Something else'.format(GithubMarkdownHandlerV2.PIPE))
should_wrap, wrap_char = self.handler._should_wrap_in_quotes(
openstring
)
self.assertFalse(should_wrap)

def test_wrap_in_quotes(self):
"""Make sure that the string is wrapped and that any existing quote chars
are escaped."""
Expand Down

0 comments on commit 18f29a9

Please sign in to comment.