Basic headline
This commit is contained in:
@@ -40,19 +40,38 @@ let parseHeadingText = @[
|
|||||||
# token
|
# token
|
||||||
|
|
||||||
let buildStars = func(tokens: seq[ParserToken], org: OrgBlock): OrgBlock {.closure.}=
|
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
|
org
|
||||||
|
|
||||||
proc tryBuildHeading(builder: OrgBuilderResult): OrgBuilderResult =
|
proc tryBuildHeading(builder: OrgBuilderResult): OrgBuilderResult =
|
||||||
builder
|
builder
|
||||||
.applyParsersSeqToSingle(
|
.applyParsersSeqToSingle(
|
||||||
OrgBlock(kind: orgHeading),
|
OrgBlock(kind: orgHeading),
|
||||||
@[(parseHeadingStars, buildStars)]
|
@[
|
||||||
|
(parseHeadingStars, buildStars, false),
|
||||||
|
|
||||||
|
(@[optional(parseTodoKeyword)], buildTodo, true),
|
||||||
|
(@[optional(parseDoneKeyword)], buildTodo, true),
|
||||||
|
|
||||||
|
(parseHeadingText, buildHeadlineContent, false),
|
||||||
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
echo initOrgBuilder("**** TODO Some stars")
|
echo initOrgBuilder("*** Some stars")
|
||||||
.tryBuildHeading()
|
.tryBuildHeading()
|
||||||
|
.fold(
|
||||||
|
x => "Nothing",
|
||||||
|
x => $x.tree
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
# let sampleBuilder = StringBuilderResult
|
# let sampleBuilder = StringBuilderResult
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import std/options
|
||||||
import std/strformat
|
import std/strformat
|
||||||
import std/sugar
|
import std/sugar
|
||||||
import std/strutils
|
import std/strutils
|
||||||
@@ -89,11 +90,15 @@ type
|
|||||||
case kind*: orgBlockKind
|
case kind*: orgBlockKind
|
||||||
of orgHeading:
|
of orgHeading:
|
||||||
level*: int
|
level*: int
|
||||||
|
todo*: Option[string]
|
||||||
|
headlineContent*: string
|
||||||
|
|
||||||
func stringifySpecialFields(x: OrgBlock): string =
|
func stringifySpecialFields(x: OrgBlock): string =
|
||||||
let specialFields = case x.kind:
|
let specialFields = case x.kind:
|
||||||
of orgHeading:
|
of orgHeading:
|
||||||
&"""level: {x.level}"""
|
&"""level: {x.level}
|
||||||
|
todo: {x.todo}
|
||||||
|
headlineContent: {x.headlineContent}"""
|
||||||
else: ""
|
else: ""
|
||||||
|
|
||||||
specialFields
|
specialFields
|
||||||
|
|||||||
@@ -166,6 +166,7 @@ proc applyParsersToSingle*[T](
|
|||||||
builder: Builder[T],
|
builder: Builder[T],
|
||||||
parsers: seq[Parser -> ParserResult],
|
parsers: seq[Parser -> ParserResult],
|
||||||
tokenFoldFn: (seq[ParserToken], T) -> T,
|
tokenFoldFn: (seq[ParserToken], T) -> T,
|
||||||
|
optional = false,
|
||||||
initT: T,
|
initT: T,
|
||||||
): BuilderResult[T] =
|
): BuilderResult[T] =
|
||||||
# Apply the current parsing functions and convert to text tokens wrapped in ParserResult
|
# Apply the current parsing functions and convert to text tokens wrapped in ParserResult
|
||||||
@@ -178,7 +179,11 @@ proc applyParsersToSingle*[T](
|
|||||||
newParser
|
newParser
|
||||||
.foldTokens(
|
.foldTokens(
|
||||||
(err: ParserError) => BuilderResult[T].err((builder, "foo")),
|
(err: ParserError) => BuilderResult[T].err((builder, "foo")),
|
||||||
(newTokens: seq[ParserToken]) => BuilderResult[T].ok(
|
(newTokens: seq[ParserToken]) => (
|
||||||
|
if optional and newTokens.len == 0:
|
||||||
|
BuilderResult[T].ok(builder)
|
||||||
|
else:
|
||||||
|
BuilderResult[T].ok(
|
||||||
builder.initBuilder(
|
builder.initBuilder(
|
||||||
newParser.unsafeGet(),
|
newParser.unsafeGet(),
|
||||||
builder.tree
|
builder.tree
|
||||||
@@ -189,6 +194,7 @@ proc applyParsersToSingle*[T](
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
)
|
||||||
|
|
||||||
proc applyParsersSeqToSingle*[T](
|
proc applyParsersSeqToSingle*[T](
|
||||||
builderResult: BuilderResult[T],
|
builderResult: BuilderResult[T],
|
||||||
@@ -196,10 +202,17 @@ proc applyParsersSeqToSingle*[T](
|
|||||||
xs: seq[tuple[
|
xs: seq[tuple[
|
||||||
parsers: seq[Parser -> ParserResult],
|
parsers: seq[Parser -> ParserResult],
|
||||||
tokenFoldFn: (seq[ParserToken], T) -> T,
|
tokenFoldFn: (seq[ParserToken], T) -> T,
|
||||||
|
ignoreEmpty: bool,
|
||||||
]],
|
]],
|
||||||
): BuilderResult[T] =
|
): BuilderResult[T] =
|
||||||
xs.foldl(
|
xs.foldl(
|
||||||
a.flatMap((builder: Builder[T]) => applyParsersToSingle(builder, b[0], b[1], initT)),
|
a.flatMap((builder: Builder[T]) => applyParsersToSingle(
|
||||||
|
builder,
|
||||||
|
b.parsers,
|
||||||
|
b.tokenFoldFn,
|
||||||
|
b.ignoreEmpty,
|
||||||
|
initT
|
||||||
|
)),
|
||||||
builderResult
|
builderResult
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user