Basic headline

This commit is contained in:
Florian Schroedl
2022-05-04 17:00:00 +02:00
parent 722e66cf85
commit dae7e7e0a3
3 changed files with 51 additions and 14 deletions

View File

@@ -40,19 +40,38 @@ let parseHeadingText = @[
# token
let buildStars = func(tokens: seq[ParserToken], org: OrgBlock): OrgBlock {.closure.}=
org.level = tokens[0].tokenStringValue().len
org.level = tokens.len
org
let buildTodo = func(tokens: seq[ParserToken], org: OrgBlock): OrgBlock {.closure.}=
org.todo = tokens.tokensToString().some()
org
let buildHeadlineContent = func(tokens: seq[ParserToken], org: OrgBlock): OrgBlock {.closure.}=
org.headlineContent = tokens.tokensToString()
org
proc tryBuildHeading(builder: OrgBuilderResult): OrgBuilderResult =
builder
.applyParsersSeqToSingle(
OrgBlock(kind: orgHeading),
@[(parseHeadingStars, buildStars)]
@[
(parseHeadingStars, buildStars, false),
(@[optional(parseTodoKeyword)], buildTodo, true),
(@[optional(parseDoneKeyword)], buildTodo, true),
(parseHeadingText, buildHeadlineContent, false),
]
)
when isMainModule:
echo initOrgBuilder("**** TODO Some stars")
echo initOrgBuilder("*** Some stars")
.tryBuildHeading()
.fold(
x => "Nothing",
x => $x.tree
)
# let sampleBuilder = StringBuilderResult

View File

@@ -1,3 +1,4 @@
import std/options
import std/strformat
import std/sugar
import std/strutils
@@ -89,11 +90,15 @@ type
case kind*: orgBlockKind
of orgHeading:
level*: int
todo*: Option[string]
headlineContent*: string
func stringifySpecialFields(x: OrgBlock): string =
let specialFields = case x.kind:
of orgHeading:
&"""level: {x.level}"""
&"""level: {x.level}
todo: {x.todo}
headlineContent: {x.headlineContent}"""
else: ""
specialFields