Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Use literal style when emitting multiline strings, fixes #64 #97

Merged
merged 1 commit into from

2 participants

@atambo

See #64 for details.

@atambo

This commit changes the emitted yaml from:

--- ! 'multi

  line

  string'

to:

--- ! |-
  multi
  line
  string

I'm still not sure why the exclamation mark shows up. @tenderlove, do you have any idea how to remove that exclamation mark?

@tenderlove
Owner

I think we need to invert the quote and plain flags to get rid of the !. Can you try this patch?

diff --git a/lib/psych/visitors/yaml_tree.rb b/lib/psych/visitors/yaml_tree.rb
index 9cbc05d..d420abd 100644
--- a/lib/psych/visitors/yaml_tree.rb
+++ b/lib/psych/visitors/yaml_tree.rb
@@ -231,15 +231,18 @@ module Psych
         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
@atambo

Your patch worked. I updated the pull request. Thanks @tenderlove

@tenderlove tenderlove merged commit eb029f0 into tenderlove:master

1 check passed

Details default The Travis build passed
@rkoster rkoster referenced this pull request in cloudfoundry-incubator/bosh-workspace
Closed

Unable to use multi line string in meta #9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
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
Something went wrong with that request. Please try again.