Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly write YAML with special characters #73

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/lib/yaml/writer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function writer.write(filename, data, options)
return false, serialized
end

local indicators = "[-?:,%[%]{}#&*!|>'\"%%@`]" --A pattern that matches all yaml characters listed as indicators in the most recent standard, which could cause problems if simply blindly written in strings
function writer.serialize(data, options, depth, firstDepth)
options = options or {}
depth = depth or 0
Expand All @@ -39,7 +40,12 @@ function writer.serialize(data, options, depth, firstDepth)
local dataType = type(data)

if dataType == "string" then
-- TODO - Use quotes when needed
--use quotes as needed
if string.match(data, indicators) then
local out = string.gsub(data, "\\", "\\\\") --escape backslashes
out = string.gsub(out, "\"", "\\\"") --escape quotes. Not sure if the reader can correctly deal with escape sequences
return "\"" .. out .. "\""
end
return data

elseif dataType == "boolean" then
Expand Down