Finish pretty printer for blocks

This commit is contained in:
Florian Schroedl
2022-05-04 17:00:00 +02:00
parent e75482d98a
commit 4c6ca48b8b

View File

@@ -85,7 +85,9 @@ proc `$`*(x: OrgInlineBlock): string =
"""
)"""
## Block
## OrgBlock
## --------
type
orgBlockKind* = enum
orgHeading
@@ -100,7 +102,7 @@ type
headlineContent*: seq[OrgInlineBlock]
headlineChildrenText*: string
proc `$`*(x: OrgBlock, indent = 0): string =
func pprint*(x: OrgBlock, indent = 0): string =
let fieldIndent = indent + INDENT_SIZE
let fields = @[
("kind", $x.kind, true),
@@ -119,14 +121,51 @@ proc `$`*(x: OrgBlock, indent = 0): string =
]
.join("\n")
func `$`*(x: OrgBlock): string = pprint(x)
func pprint*(xs: seq[OrgBlock], indent = 0): string =
let fieldIndent = indent + INDENT_SIZE
@[
"@[",
xs
.mapIt(it.pprint())
.join(",\n")
.indent(fieldIndent),
"]",
]
.join("\n")
func `$`*(xs: seq[OrgBlock]): string = pprint(xs)
## OrgDocuemnt
## -----------
type
OrgDocument* = ref object
children*: seq[OrgBlock]
proc `$`*(x: OrgDocument): string =
&"""OrgDocument(
children: {x.children}
)"""
func pprint*(x: OrgDocument, indent = 0): string =
let fieldIndent = indent + INDENT_SIZE
let fields = @[
("children", $x.children, true),
]
.stringifyFields()
@[
"OrgDocument(",
fields.indent(fieldIndent),
")",
]
.join("\n")
func `$`*(xs: OrgDocument): string = pprint(xs)
when isMainModule:
echo OrgBlock(kind: orgHeading)
echo OrgDocument(
children: @[
OrgBlock(kind: orgHeading),
OrgBlock(kind: orgHeading),
OrgBlock(kind: orgHeading),
]
)