improve escaping

This commit is contained in:
Brad Rydzewski 2019-05-29 19:35:30 -07:00
parent 2a45c1c328
commit 507291b915
2 changed files with 11 additions and 1 deletions

View File

@ -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

View File

@ -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) {