Skip to content

Commit 89c9119

Browse files
committed
Some nph formatting and now escaping strings correctly when printing kdl
1 parent 44fac79 commit 89c9119

File tree

4 files changed

+224
-143
lines changed

4 files changed

+224
-143
lines changed

src/kdl.nim

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@
1414
## Arguments are a sequence of values, while properties are an unordered table of string and values.
1515
## Arguments and properties' values are represented by the object variant `KdlVal`. `KdlVal` can be of any kind `KString`, `KFloat`, `KBool`, `KNull` or `KInt`.
1616
runnableExamples:
17-
let doc = parseKdl("node (i8)1 null key=\"val\" {child \"abc\" true}") # You can also read files using parseKdlFile("file.kdl")
18-
assert doc == @[
19-
initKNode("node",
20-
args = @[initKVal(1, "i8".some), initKNull()],
21-
props = {"key": initKVal("val")}.toTable,
22-
children = @[initKNode("child", args = @[initKVal("abc"), initKVal(true)])])
17+
let doc = parseKdl("node (i8)1 null key=\"val\" {child \"abc\" true}")
18+
# You can also read files using parseKdlFile("file.kdl")
19+
assert doc ==
20+
@[
21+
initKNode(
22+
"node",
23+
args = @[initKVal(1, "i8".some), initKNull()],
24+
props = {"key": initKVal("val")}.toTable,
25+
children = @[initKNode("child", args = @[initKVal("abc"), initKVal(true)])],
26+
)
2327
]
2428

2529
## ### Reading nodes
@@ -75,22 +79,25 @@ runnableExamples:
7579
## To create KDL documents, nodes or values without parsing or object constructors you can use the `toKdlDoc`, `toKdlNode` and`toKdlVal` macros which have a similar syntax to KDL:
7680
runnableExamples:
7781
let doc = toKdlDoc:
78-
node[tag](1, true, nil, key="val"):
82+
node[tag](1, true, nil, key = "val"):
7983
child(3.14[pi])
8084
81-
person(name="pat")
85+
person(name = "pat")
8286
83-
assert doc == parseKdl("(tag)node 1 true null key=\"val\" {child (pi)3.14}; person name=\"pat\"")
87+
assert doc ==
88+
parseKdl("(tag)node 1 true null key=\"val\" {child (pi)3.14}; person name=\"pat\"")
8489
85-
let node = toKdlNode: numbers(1, 2.13, 3.1e-10)
90+
let node = toKdlNode:
91+
numbers(1, 2.13, 3.1e-10)
8692
assert node == parseKdl("numbers 1 2.13 3.1e-10")[0]
8793

8894
assert toKdlVal("abc"[tag]) == parseKdl("node (tag)\"abc\"")[0].args[0]
8995

9096
## Furthermore there are the `toKdlArgs` and `toKdlProps` macros, they provide shortcuts for creating a sequence and a table of `KdlVal`:
9197
runnableExamples:
9298
assert toKdlArgs(1, 2[tag], "a") == [1.initKVal, 2.initKVal("tag".some), "a".initKVal]
93-
assert toKdlProps({"a": 1[tag], "b": 2}) == {"a": 1.initKVal("tag".some), "b": 2.initKVal}.toTable
99+
assert toKdlProps({"a": 1[tag], "b": 2}) ==
100+
{"a": 1.initKVal("tag".some), "b": 2.initKVal}.toTable
94101

95102
## ## Compile flags
96103
## `-d:kdlDecoderAllowHoleyEnums`: to allow converting integers into holey enums.
@@ -116,7 +123,7 @@ func indent(s: string, count: Natural, padding = " ", newLine = "\n"): string =
116123
if e > 0:
117124
result.add newLine
118125

119-
for j in 1..count:
126+
for j in 1 .. count:
120127
result.add padding
121128

122129
result.add line
@@ -157,15 +164,15 @@ proc pretty*(node: KdlNode): string =
157164
if node.args.len > 0:
158165
result.add " "
159166
for e, val in node.args:
160-
if e in 1..node.args.high:
167+
if e in 1 .. node.args.high:
161168
result.add " "
162169

163170
result.add val.pretty()
164171

165172
if node.props.len > 0:
166173
result.add " "
167174
for e, (key, val) in node.props.pairs.toSeq.sortedByIt(it[0]):
168-
if e in 1..<node.props.len:
175+
if e in 1 ..< node.props.len:
169176
result.add " "
170177

171178
result.add &"{key.prettyIdent}={val.pretty}"
@@ -184,12 +191,12 @@ proc pretty*(doc: KdlDoc, newLine = true): string =
184191
if e < doc.high:
185192
result.add "\p"
186193

187-
if newLine: result.add "\p"
194+
if newLine:
195+
result.add "\p"
188196

189197
proc writeFile*(path: string, doc: KdlDoc, pretty = false) =
190198
## Writes `doc` to path. Set `pretty` to true to use `pretty` instead of `$`.
191199
if pretty:
192200
writeFile(path, doc.pretty())
193201
else:
194202
writeFile(path, $doc & '\n')
195-

0 commit comments

Comments
 (0)