Extract stringifying functions

This commit is contained in:
Florian Schroedl
2022-05-04 17:00:00 +02:00
parent 074c9f0c2d
commit a33dce136e
2 changed files with 32 additions and 16 deletions

View File

@@ -8,6 +8,7 @@ import std/[
import fp/[ import fp/[
maybe, maybe,
] ]
import ../utils/printers
## Inline Block ## Inline Block
type type
@@ -102,22 +103,15 @@ type
const INDENT_SIZE = 2; const INDENT_SIZE = 2;
func stringifySpecialFields(x: OrgBlock, indent = 0): string = func stringifySpecialFields(x: OrgBlock, indent = 0): string =
( @[
case x.kind: ("kind", $x.kind, true),
of orgHeading: ("level", $x.level, true),
@[ ("todo", $x.todo, x.todo.isSome()),
("kind", $x.kind, true), ("headlineContent", $x.headlineContent, x.headlineContent.len != 0),
("level", $x.level, true), ("headlineChildrenText", $x.headlineChildrenText, x.headlineChildrenText.len != 0),
("todo", $x.todo, x.todo.isSome()), ("children", $x.children, true),
("headlineContent", $x.headlineContent, x.headlineContent.len != 0), ]
("headlineChildrenText", $x.headlineChildrenText, x.headlineChildrenText.len != 0), .stringifyFields()
("children", $x.children, true),
]
else: @[])
.filter(x => x[2])
.map(x => x[0] & ": " & x[1])
.join(",\n")
proc `$`*(x: OrgBlock, indent = 0): string = proc `$`*(x: OrgBlock, indent = 0): string =
let fieldIndent = indent + INDENT_SIZE let fieldIndent = indent + INDENT_SIZE

22
src/utils/printers.nim Normal file
View File

@@ -0,0 +1,22 @@
import std/[
sequtils,
sequtils,
strformat,
strutils,
sugar,
]
const INDENT_SIZE* = 2;
func stringifyFields*(
xs: seq[tuple[
field: string,
value: string,
print: bool
]],
indent = 0,
): string =
xs
.filter(x => x.print)
.map(x => x.field & ": " & $x.value)
.join(",\n")