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