From 074c9f0c2d531c92fd540244075e9edd98f41891 Mon Sep 17 00:00:00 2001 From: Florian Schroedl Date: Wed, 4 May 2022 17:00:00 +0200 Subject: [PATCH] Add better printers --- src/org/org_types.nim | 64 +++++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 27 deletions(-) diff --git a/src/org/org_types.nim b/src/org/org_types.nim index 82546bf..56d3697 100644 --- a/src/org/org_types.nim +++ b/src/org/org_types.nim @@ -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