summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Thursfield <sam.thursfield@codethink.co.uk>2015-07-14 12:56:07 +0100
committerSam Thursfield <sam.thursfield@codethink.co.uk>2015-07-14 12:56:07 +0100
commit64ecb99083e3c81c13dec7d63314c877c72aef98 (patch)
tree00f0668ccc588b7060a0940f7206dec916d010c8
parent678a543368772a6780e85e3558a54cb33b289660 (diff)
downloadruamel.yaml-64ecb99083e3c81c13dec7d63314c877c72aef98.tar.gz
Preserve blank lines after block scalars
-rw-r--r--py/scanner.py20
-rw-r--r--test/test_yaml.py24
2 files changed, 39 insertions, 5 deletions
diff --git a/py/scanner.py b/py/scanner.py
index 1286e4f..b497c4b 100644
--- a/py/scanner.py
+++ b/py/scanner.py
@@ -1065,15 +1065,29 @@ class Scanner(object):
else:
break
- # Chomp the tail.
+ # Process trailing line breaks. The 'chomping' setting determines
+ # whether they are included in the value.
+ comment = []
if chomping is not False:
chunks.append(line_break)
+ else:
+ comment.append(line_break)
if chomping is True:
chunks.extend(breaks)
+ else:
+ comment.extend(breaks)
# We are done.
- return ScalarToken(u''.join(chunks), False, start_mark, end_mark,
- style)
+ token = ScalarToken(u''.join(chunks), False, start_mark, end_mark,
+ style)
+ if len(comment) > 0:
+ # Keep track of the trailing whitespace as a comment token, if
+ # isn't all included in the actual value.
+ comment_end_mark = self.get_mark()
+ comment = CommentToken(''.join(comment), end_mark,
+ comment_end_mark)
+ token.add_post_comment(comment)
+ return token
def scan_block_scalar_indicators(self, start_mark):
# See the specification for details.
diff --git a/test/test_yaml.py b/test/test_yaml.py
index c5d69b4..503e153 100644
--- a/test/test_yaml.py
+++ b/test/test_yaml.py
@@ -72,12 +72,16 @@ class TestYAML:
? c
""")
- def test_blank_lines(self):
+ def test_blank_line_after_comment(self):
round_trip("""
- # Comment with a space after it.
+ # Comment with spaces after it.
+
a: 1
+ """)
+ def test_blank_line_between_seq_items(self):
+ round_trip("""
# Seq with spaces in between items.
b:
- bar
@@ -85,3 +89,19 @@ class TestYAML:
- baz
""")
+
+ def test_blank_line_after_literal(self):
+ round_trip("""
+ c:
+ - |
+ This item
+ has a blank line
+ following it.
+
+ - |
+ To visually separate it from this item.
+
+ This item contains a blank line.
+
+
+ """)