Respect string Style in Lua encoder

Lua has 'single', "double" and [[ long ]] strings.
This commit is contained in:
Kim Alvefur 2023-08-04 12:55:07 +02:00
parent 0debfc3cf9
commit 11605f6b63
2 changed files with 24 additions and 3 deletions

View File

@ -80,7 +80,28 @@ func (le *luaEncoder) PrintLeadingContent(writer io.Writer, content string) erro
}
func (le *luaEncoder) encodeString(writer io.Writer, node *yaml.Node) error {
return writeString(writer, "\""+le.escape.Replace(node.Value)+"\"")
quote := "\""
switch node.Style {
case yaml.LiteralStyle, yaml.FoldedStyle, yaml.FlowStyle:
for i := 0; i < 10; i++ {
if !strings.Contains(node.Value, "]"+strings.Repeat("=", i)+"]") {
err := writeString(writer, "["+strings.Repeat("=", i)+"[\n")
if err != nil {
return err
}
err = writeString(writer, node.Value)
if err != nil {
return err
}
return writeString(writer, "]"+strings.Repeat("=", i)+"]")
}
}
case yaml.SingleQuotedStyle:
quote = "'"
// falltrough to regular ol' string
}
return writeString(writer, quote+le.escape.Replace(node.Value)+quote)
}
func (le *luaEncoder) writeIndent(writer io.Writer) error {

View File

@ -47,8 +47,8 @@ numbers: [123,456]
{
skipDoc: true,
description: "Scalar str",
input: "str: |\n foo\n bar\n",
expected: "return {\n\t[\"str\"] = \"foo\\nbar\\n\";\n};\n",
input: "str: |\n foo\n bar\nanother: 'single'\nand: \"double\"",
expected: "return {\n\t[\"str\"] = [[\nfoo\nbar\n]];\n\t[\"another\"] = 'single';\n\t[\"and\"] = \"double\";\n};\n",
scenarioType: "encode",
},
{