Add better printers

This commit is contained in:
Florian Schroedl
2022-05-04 17:00:00 +02:00
parent c8b4253f5e
commit 074c9f0c2d

View File

@@ -1,8 +1,13 @@
import std/options
import std/strformat
import std/sugar
import std/strutils
import fp/maybe
import std/[
options,
sequtils,
strformat,
strutils,
sugar,
]
import fp/[
maybe,
]
## Inline Block
type
@@ -94,30 +99,35 @@ type
headlineContent*: seq[OrgInlineBlock]
headlineChildrenText*: string
func stringifySpecialFields(x: OrgBlock): string =
let specialFields = case x.kind:
of orgHeading:
&"""level: {x.level}
todo: {x.todo}
headlineContent: {x.headlineContent}
headlineChildrenText: {x.headlineChildrenText}"""
else: ""
const INDENT_SIZE = 2;
specialFields
.just()
.notEmpty()
.map(x => x.indent(2))
.map(x => "\n" & x & "\n")
.getOrElse("")
func stringifySpecialFields(x: OrgBlock, indent = 0): string =
(
case x.kind:
of orgHeading:
@[
("kind", $x.kind, true),
("level", $x.level, true),
("todo", $x.todo, x.todo.isSome()),
("headlineContent", $x.headlineContent, x.headlineContent.len != 0),
("headlineChildrenText", $x.headlineChildrenText, x.headlineChildrenText.len != 0),
("children", $x.children, true),
]
else: @[])
.filter(x => x[2])
.map(x => x[0] & ": " & x[1])
.join(",\n")
proc `$`*(x: OrgBlock): string =
&"""OrgBlock(
kind: {x.kind}""" &
stringifySpecialFields(x) &
&"""
children: {x.children}
)
"""
proc `$`*(x: OrgBlock, indent = 0): string =
let fieldIndent = indent + INDENT_SIZE
@[
"OrgBlock(",
stringifySpecialFields(x).indent(fieldIndent),
")",
]
.join("\n")
type
OrgDocument* = ref object