Skip to content

Commit 6ab896c

Browse files
author
Michael Kelly
committed
Preserve whitespace in lang files.
1 parent 59425f8 commit 6ab896c

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

pontoon/base/formats/lang.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
LangComment = namedtuple('LangComment', ['content'])
1717

1818

19+
BLANK_LINE = 'blank_line'
1920
TAG_REGEX = re.compile(r'\{(ok|l10n-extra)\}')
2021

2122

@@ -54,8 +55,8 @@ def save(self, path=None):
5455
self.write_entity(f, child)
5556
elif isinstance(child, LangComment):
5657
self.write_comment(f, child)
57-
58-
f.write(u'\n\n')
58+
elif child == BLANK_LINE:
59+
f.write(u'\n')
5960

6061
def write_entity(self, f, entity):
6162
for comment in entity.comments:
@@ -88,17 +89,18 @@ def write_comment(self, f, comment):
8889

8990

9091
class LangVisitor(NodeVisitor):
91-
grammar = Grammar("""
92-
lang_file = (comment / entity)*
92+
grammar = Grammar(r"""
93+
lang_file = (comment / entity / blank_line)*
9394
94-
comment = "##" line_content line_end
95+
comment = "##" line_content "\n"
9596
line_content = ~r".*"
96-
line_end = ("\\n" / ~r"\s")+
97+
98+
blank_line = ~r"((?!\n)\s)*" "\n"
9799
98100
entity = entity_comment* string translation
99-
entity_comment = "#" line_content line_end
100-
string = ";" line_content line_end
101-
translation = line_content line_end
101+
entity_comment = "#" line_content "\n"
102+
string = ";" line_content "\n"
103+
translation = line_content "\n"
102104
""")
103105

104106
def visit_lang_file(self, node, children):
@@ -107,6 +109,9 @@ def visit_lang_file(self, node, children):
107109
def visit_comment(self, node, (marker, content, end)):
108110
return LangComment(content.text.strip())
109111

112+
def visit_blank_line(self, node, (whitespace, newline)):
113+
return BLANK_LINE
114+
110115
def visit_entity(self, node, (comments, string, translation)):
111116
comments = self.normalize_zero_or_more_strings(comments)
112117

0 commit comments

Comments
 (0)