Browse files

Use literal style when emitting multiline strings, fixes #64

  • Loading branch information...
1 parent 851de5c commit 066e600b122803d05033b5687e3d5488b312d445 @atambo committed Oct 27, 2012
Showing with 10 additions and 2 deletions.
  1. +5 −2 lib/psych/visitors/yaml_tree.rb
  2. +5 −0 test/psych/test_yaml.rb
View
7 lib/psych/visitors/yaml_tree.rb
@@ -230,15 +230,18 @@ def visit_String o
plain = false
quote = false
style = Nodes::Scalar::ANY
+ tag = nil
+ str = o
if binary?(o)
str = [o].pack('m').chomp
tag = '!binary' # FIXME: change to below when syck is removed
#tag = 'tag:yaml.org,2002:binary'
style = Nodes::Scalar::LITERAL
+ elsif o =~ /\n/
+ quote = true
+ style = Nodes::Scalar::LITERAL
else
- str = o
- tag = nil
quote = !(String === @ss.tokenize(o))
plain = !quote
end
View
5 test/psych/test_yaml.rb
@@ -1266,4 +1266,9 @@ def test_normal_exit
Psych.load("2000-01-01 00:00:00.#{"0"*1000} +00:00\n")
# '[ruby-core:13735]'
end
+
+ def test_multiline_string_uses_literal_style
+ yaml = Psych.dump("multi\nline\nstring")
+ assert_match("|", yaml)
+ end
end

0 comments on commit 066e600

Please sign in to comment.