@@ -2,43 +2,46 @@ module BlockScalars
2
2
3
3
export @blk_str
4
4
5
- const DEFAULT_BLOCK_SCALAR = " fs"
6
5
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)
6
+ const DEFAULT_STYLE = ' f '
7
+ const DEFAULT_CHOMP = ' s '
8
+
9
+ function block (str :: AbstractString , block_scalar :: AbstractString = " " )
10
+ block_scalar_len = length (block_scalar)
11
+ block_scalar_len > 2 && throw ( ArgumentError ( " Too many indicators provided " ))
12
+
13
+ style, chomp = if block_scalar_len == 2
14
+ block_scalar
15
+ elseif block_scalar_len == 1
16
+ ind = block_scalar[ 1 ]
17
+ if ind in " fl "
18
+ ind, DEFAULT_CHOMP
19
+ else
20
+ DEFAULT_STYLE, ind
21
+ end
23
22
else
24
- throw ( ArgumentError ( " Only one block chomp indicators can be provided " ))
23
+ DEFAULT_STYLE, DEFAULT_CHOMP
25
24
end
26
25
27
- if style_indicator == ' f'
26
+ if style == ' f'
28
27
str = replace (str, r" (?<=\S )\n (?=\S )" => " " )
29
28
str = replace (str, r" (?<=\S )\n (\n +)(?!$)" => s "\1 " )
29
+ elseif style != ' l'
30
+ throw (ArgumentError (" Unknown block style indicator: $(repr (style)) " ))
30
31
end
31
32
32
- if chomp_indicator == ' c'
33
+ if chomp == ' c'
33
34
str = replace (str, r" \n +$" => " \n " )
34
- elseif chomp_indicator == ' s'
35
+ elseif chomp == ' s'
35
36
str = replace (str, r" \n +$" => " " )
37
+ elseif chomp != ' k'
38
+ throw (ArgumentError (" Unknown block chomping indicator: $(repr (chomp)) " ))
36
39
end
37
40
38
41
return str
39
42
end
40
43
41
- macro blk_str (str:: AbstractString , suffix:: AbstractString = DEFAULT_BLOCK_SCALAR )
44
+ macro blk_str (str:: AbstractString , suffix:: AbstractString = " " )
42
45
block (str, suffix)
43
46
end
44
47
0 commit comments