Skip to content

Commit

Permalink
Merge pull request #76 from transifex/wrap_in_quotes
Browse files Browse the repository at this point in the history
Wrap problematic translations in quotes
  • Loading branch information
SofiaMargariti committed Sep 14, 2017
2 parents 6ce81a8 + 540477d commit f1404f6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
15 changes: 14 additions & 1 deletion openformats/formats/github_markdown_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ class GithubMarkdownHandlerV2(OrderedCompilerMixin, Handler):
BACKSLASH = u'\\'
DOUBLE_QUOTES = u'"'
NEWLINE = u'\n'
COLON = u':'
ASTERISK = u'*'
AMPERSAND = u'&'
DASH = u'-'

def _should_wrap_in_quotes(self, tr_string):
return any([
self.NEWLINE in tr_string[:-1],
self.COLON in tr_string,
tr_string.lstrip().startswith(self.ASTERISK),
tr_string.lstrip().startswith(self.AMPERSAND),
tr_string.lstrip().startswith(self.DASH),
])

def compile(self, template, stringset, **kwargs):
# assume stringset is ordered within the template
Expand All @@ -31,7 +44,7 @@ def compile(self, template, stringset, **kwargs):
# if string's key is int this is a markdown string
int(string.key)
except ValueError:
if self.NEWLINE in tr_string[:-1]:
if self._should_wrap_in_quotes(tr_string):
# escape double quotes inside strings
tr_string = string.string.replace(
self.DOUBLE_QUOTES,
Expand Down
27 changes: 14 additions & 13 deletions openformats/tests/formats/github_markdown_v2/files/1_el.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
---
title: el:About writing and formatting on GitHub
intro: el:into text
title: "el:About writing and formatting on GitHub"
intro: "el:into text"
numeric_var: 12.5

# one comment

key1:
- list_key:
- el:li within li
- "el:li within li"
# second comment
- el:li within li 2
- el:li 2
- "el:li within li 2"
- "el:li 2"
key2:
- object_within_list: el:value
- el:li 4
- el:li 5
- object_within_list: "el:value"
- "el:li 4"
- "el:li 5"

description: el:folded style text
description: "el:folded style text
"
custom_vars:
var1: el:some value
var1: "el:text: some value"
var2: "el:literal
style with \"quotes\"
text
"
nested_key_outer:
nested_key_inner:
el:nested_value
"el:nested_value"

key.with.dots: el:dot value
key.with.dots: "el:dot value"

1: el:integer key
1: "el:integer key"

---

Expand Down
2 changes: 1 addition & 1 deletion openformats/tests/formats/github_markdown_v2/files/1_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ description: >
style
text
custom_vars:
var1: some value
var1: "text: some value"
var2: |
literal
style with "quotes"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ key2:

description: folded style text
custom_vars:
var1: some value
var1: "text: some value"
var2: "literal
style with \"quotes\"
text
Expand Down

0 comments on commit f1404f6

Please sign in to comment.