Skip to content

Commit

Permalink
Merge pull request #95 from christopher-dG/cdg/strings
Browse files Browse the repository at this point in the history
Print multiline strings
  • Loading branch information
kescobo authored Dec 11, 2020
2 parents 6813b9f + ad4052b commit 9748e1b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/Manifest.toml
6 changes: 3 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"]
17 changes: 16 additions & 1 deletion src/writer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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) =
Expand Down
7 changes: 7 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 comments on commit 9748e1b

@kescobo
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/26247

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.4.4 -m "<description of version>" 9748e1b9db6256b6c03618ca5a3fae8ca1be1de2
git push origin v0.4.4

Please sign in to comment.