From 507291b9154c5943fc874b4c28c29b0a382da158 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Wed, 29 May 2019 19:35:30 -0700 Subject: [PATCH] improve escaping --- yaml/pretty/util.go | 2 +- yaml/pretty/writer.go | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/yaml/pretty/util.go b/yaml/pretty/util.go index 21b9ce7..202a1dc 100644 --- a/yaml/pretty/util.go +++ b/yaml/pretty/util.go @@ -65,7 +65,7 @@ func isZero(v interface{}) bool { func isQuoted(b rune) bool { switch b { - case '#', ',', '[', ']', '{', '}', '&', '*', '!', '|', '>', '\'', '"', '%', '@', '`': + case '#', ',', '[', ']', '{', '}', '&', '*', '!', '\'', '"', '%', '@', '`': return true case '\a', '\b', '\f', '\n', '\r', '\t', '\v': return true diff --git a/yaml/pretty/writer.go b/yaml/pretty/writer.go index b906e17..29b5b97 100644 --- a/yaml/pretty/writer.go +++ b/yaml/pretty/writer.go @@ -19,6 +19,7 @@ import ( "fmt" "sort" "strconv" + "strings" "github.com/drone/drone-yaml/yaml" ) @@ -195,6 +196,15 @@ func writeEncode(w writer, v string) { w.WriteByte('"') return } + trimmed := strings.TrimSpace(v) + if strings.HasPrefix(trimmed, "| ") { + fmt.Fprintf(w, "%q", v) + return + } + if strings.HasPrefix(trimmed, "> ") { + fmt.Fprintf(w, "%q", v) + return + } var prev rune for _, b := range v { if isQuoted(b) {