diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b067edd --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/Manifest.toml diff --git a/Project.toml b/Project.toml index cf9be6b..7317e1d 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "YAML" uuid = "ddb6d928-2868-570f-bddf-ab3f9cf99eb6" -version = "0.4.3" +version = "0.4.4" [deps] Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" @@ -11,9 +11,9 @@ Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7" julia = "1" [extras] -Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" -OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" +OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] test = ["Test", "OrderedCollections", "DataStructures"] diff --git a/src/writer.jl b/src/writer.jl index f323be4..9284900 100644 --- a/src/writer.jl +++ b/src/writer.jl @@ -79,7 +79,22 @@ end # _print a single string _print(io::IO, str::AbstractString, level::Int=0, ignore_level::Bool=false) = - println(io, repr(MIME("text/plain"), str)) # quote and escape + if occursin('\n', strip(str)) || occursin('"', str) + if endswith(str, "\n\n") # multiple trailing newlines: keep + println(io, "|+") + str = str[1:end-1] # otherwise, we have one too many + elseif endswith(str, "\n") # one trailing newline: clip + println(io, "|") + else # no trailing newlines: strip + println(io, "|-") + end + indent = repeat(" ", max(level, 1)) + for line in split(str, "\n") + println(io, indent, line) + end + else + println(io, repr(MIME("text/plain"), str)) # quote and escape + end # handle NaNs and Infs _print(io::IO, val::Float64, level::Int=0, ignore_level::Bool=false) = diff --git a/test/runtests.jl b/test/runtests.jl index 64a6a18..ebc2471 100755 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -224,4 +224,11 @@ order_two = OrderedDict(dict_content[[2,1]]...) # reverse order @test YAML.load(YAML.yaml(Dict("a" => """a "quoted" string""")))["a"] == """a "quoted" string""" @test YAML.load(YAML.yaml(Dict("a" => """a \\"quoted\\" string""")))["a"] == """a \\"quoted\\" string""" +@test YAML.load(YAML.yaml(Dict("a" => "")))["a"] == "" +@test YAML.load(YAML.yaml(Dict("a" => "nl at end\n")))["a"] == "nl at end\n" +@test YAML.load(YAML.yaml(Dict("a" => "one\nnl\n")))["a"] == "one\nnl\n" +@test YAML.load(YAML.yaml(Dict("a" => "many\nnls\n\n\n")))["a"] == "many\nnls\n\n\n" +@test YAML.load(YAML.yaml(Dict("a" => "no\ntrailing\nnls")))["a"] == "no\ntrailing\nnls" +@test YAML.load(YAML.yaml(Dict("a" => "foo\n\"bar\\'")))["a"] == "foo\n\"bar\\'" + end # module