Skip to content

Commit d425bdc

Browse files
committed
Initial implementation
1 parent e98cbfe commit d425bdc

File tree

3 files changed

+98
-4
lines changed

3 files changed

+98
-4
lines changed

Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ julia = "1"
88

99
[extras]
1010
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
11+
YAML = "ddb6d928-2868-570f-bddf-ab3f9cf99eb6"
1112

1213
[targets]
13-
test = ["Test"]
14+
test = ["Test", "YAML"]

src/BlockScalars.jl

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,45 @@
11
module BlockScalars
22

3-
# Write your package code here.
3+
export @blk_str
4+
5+
const DEFAULT_BLOCK_SCALAR = "fs"
6+
7+
function block(str::AbstractString, block_scalar::AbstractString=DEFAULT_BLOCK_SCALAR)
8+
style_indicators = intersect(block_scalar, "fl")
9+
chomp_indicators = intersect(block_scalar, "csk")
10+
11+
if length(style_indicators) == 0
12+
style_indicator = 'f'
13+
elseif length(style_indicators) == 1
14+
style_indicator = first(style_indicators)
15+
else
16+
throw(ArgumentError("Only one block style indicators can be provided"))
17+
end
18+
19+
if length(chomp_indicators) == 0
20+
chomp_indicator = 's'
21+
elseif length(chomp_indicators) == 1
22+
chomp_indicator = first(chomp_indicators)
23+
else
24+
throw(ArgumentError("Only one block chomp indicators can be provided"))
25+
end
26+
27+
if style_indicator == 'f'
28+
str = replace(str, r"(?<=\S)\n(?=\S)" => " ")
29+
str = replace(str, r"(?<=\S)\n(\n+)(?!$)" => s"\1")
30+
end
31+
32+
if chomp_indicator == 'c'
33+
str = replace(str, r"\n+$" => "\n")
34+
elseif chomp_indicator == 's'
35+
str = replace(str, r"\n+$" => "")
36+
end
37+
38+
return str
39+
end
40+
41+
macro blk_str(str::AbstractString, suffix::AbstractString=DEFAULT_BLOCK_SCALAR)
42+
block(str, suffix)
43+
end
444

545
end

test/runtests.jl

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,59 @@
1-
using BlockScalars
1+
using BlockScalars: @blk_str, block
22
using Test
3+
using YAML: YAML
4+
5+
indent(str, n) = join(map(line -> (" " ^ n) * line, split(str, '\n')), '\n')
6+
7+
function yaml_block(str, block_scalar)
8+
yaml = "example: $block_scalar\n$(indent(str, 2))"
9+
YAML.load(yaml)["example"]
10+
end
11+
12+
# https://yaml-multiline.info/
13+
const TEXT = """
14+
Several lines of text,
15+
with some "quotes" of various 'types'
16+
and also two blank lines:
17+
18+
19+
plus another line at the end.
20+
21+
22+
"""
23+
24+
const WHITESPACE = "a\nb\n c \nd\n"
25+
26+
# Validate `yaml_block` function
27+
@assert yaml_block(TEXT, "|+") == TEXT
28+
@assert yaml_block(TEXT, "|+") == TEXT
329

430
@testset "BlockScalars.jl" begin
5-
# Write your tests here.
31+
@testset "block: $name" for (name, str) in ("TEXT" => TEXT, "WHITESPACE" => WHITESPACE)
32+
@testset "literal" begin
33+
@test block(str, "lk") == yaml_block(str, "|+")
34+
@test block(str, "lc") == yaml_block(str, "|")
35+
@test block(str, "ls") == yaml_block(str, "|-")
36+
end
37+
38+
@testset "folding" begin
39+
@test block(str, "fk") == yaml_block(str, ">+")
40+
@test block(str, "fc") == yaml_block(str, ">")
41+
@test block(str, "fs") == yaml_block(str, ">-")
42+
end
43+
44+
@testset "default style" begin
45+
@test block(str, "k") == yaml_block(str, ">+")
46+
@test block(str, "c") == yaml_block(str, ">")
47+
@test block(str, "s") == yaml_block(str, ">-")
48+
end
49+
50+
@testset "default chomp" begin
51+
@test block(str, "l") == yaml_block(str, "|-")
52+
@test block(str, "f") == yaml_block(str, ">-")
53+
end
54+
55+
@testset "default" begin
56+
@test block(str) == yaml_block(str, ">-")
57+
end
58+
end
659
end

0 commit comments

Comments
 (0)