Parsing content as string
This commit is contained in:
@@ -18,6 +18,7 @@ import ./org_types
|
|||||||
{.experimental: "caseStmtMacros".}
|
{.experimental: "caseStmtMacros".}
|
||||||
|
|
||||||
let parseHeadingStars = @[
|
let parseHeadingStars = @[
|
||||||
|
ignore(optional(newline)),
|
||||||
manyUntil(ch('*'), ch(' ')),
|
manyUntil(ch('*'), ch(' ')),
|
||||||
ignore(ch(' '))
|
ignore(ch(' '))
|
||||||
]
|
]
|
||||||
@@ -100,19 +101,31 @@ let headingParser = choice(@[
|
|||||||
endOfStream,
|
endOfStream,
|
||||||
])
|
])
|
||||||
|
|
||||||
proc parseHeadlineChildren(builder: OrgBuilder): OrgBuilder =
|
proc parseHeadlineChildren(builder: OrgBuilder): OrgBuilderResult =
|
||||||
let headingBlock = builder.tree[0]
|
var headingBlock = builder.tree[0]
|
||||||
|
|
||||||
var parserAcc: ParserResult = ParserResult.ok(builder.parser)
|
var parserAcc: ParserResult = ParserResult.ok(builder.parser)
|
||||||
|
|
||||||
echo parserAcc
|
# echo parserAcc
|
||||||
|
|
||||||
while parserAcc.isOk() and parserAcc.flatMap(headingParser).isErr():
|
while parserAcc.isOk() and parserAcc.flatMap(headingParser).isErr():
|
||||||
parserAcc = parserAcc.flatMap(anyCh)
|
parserAcc = parserAcc.flatMap(anyCh)
|
||||||
|
|
||||||
# echo parserAcc.flatMap(flattenParserTokens)
|
let content = parserAcc.foldTokens(
|
||||||
|
(err) => "",
|
||||||
|
(xs: seq[ParserToken]) => xs.tokensToString(),
|
||||||
|
)
|
||||||
|
headingBlock.content = content
|
||||||
|
|
||||||
builder
|
let res = parserAcc.fold(
|
||||||
|
(err) => OrgBuilderResult.err((builder, "Could not parse content")),
|
||||||
|
(parser: Parser) => OrgBuilderResult.ok((
|
||||||
|
parser: parser.emptyTokens(),
|
||||||
|
tree: @[headingBlock],
|
||||||
|
))
|
||||||
|
)
|
||||||
|
|
||||||
|
res
|
||||||
|
|
||||||
proc makeOrg*(x: string): OrgBuilderResult =
|
proc makeOrg*(x: string): OrgBuilderResult =
|
||||||
var acc = initOrgBuilder(x)
|
var acc = initOrgBuilder(x)
|
||||||
@@ -122,7 +135,7 @@ proc makeOrg*(x: string): OrgBuilderResult =
|
|||||||
|
|
||||||
let item = acc
|
let item = acc
|
||||||
.tryBuildHeading()
|
.tryBuildHeading()
|
||||||
.map(
|
.flatMap(
|
||||||
(x: OrgBuilder) => parseHeadlineChildren(x)
|
(x: OrgBuilder) => parseHeadlineChildren(x)
|
||||||
)
|
)
|
||||||
.flatMap(
|
.flatMap(
|
||||||
@@ -139,7 +152,7 @@ proc makeOrg*(x: string): OrgBuilderResult =
|
|||||||
proc foldOrg*(x: OrgBuilderResult): string =
|
proc foldOrg*(x: OrgBuilderResult): string =
|
||||||
x
|
x
|
||||||
.fold(
|
.fold(
|
||||||
(err) => "Error",
|
(err) => "Error" & $err,
|
||||||
(builder: OrgBuilder) => $builder.tree,
|
(builder: OrgBuilder) => $builder.tree,
|
||||||
)
|
)
|
||||||
# echo acc.unsafeGet().tree[^1]
|
# echo acc.unsafeGet().tree[^1]
|
||||||
@@ -149,7 +162,10 @@ when isMainModule:
|
|||||||
|
|
||||||
Some text inbetween
|
Some text inbetween
|
||||||
|
|
||||||
** DONE Level 2"""
|
** DONE Level 2
|
||||||
|
|
||||||
|
Some more content
|
||||||
|
"""
|
||||||
|
|
||||||
let acc = makeOrg(test1).foldOrg()
|
let acc = makeOrg(test1).foldOrg()
|
||||||
# echo acc
|
echo acc
|
||||||
|
|||||||
@@ -109,6 +109,7 @@ type
|
|||||||
level*: int
|
level*: int
|
||||||
todo*: Option[string]
|
todo*: Option[string]
|
||||||
headlineContent*: seq[OrgInlineBlock]
|
headlineContent*: seq[OrgInlineBlock]
|
||||||
|
content*: string
|
||||||
|
|
||||||
## OrgBlock.PrettyPrinters
|
## OrgBlock.PrettyPrinters
|
||||||
|
|
||||||
@@ -119,6 +120,7 @@ func pprint*(x: OrgBlock, indent = 0): string =
|
|||||||
("todo", $x.todo, x.todo.isSome()),
|
("todo", $x.todo, x.todo.isSome()),
|
||||||
("headlineContent", $x.headlineContent, x.headlineContent.len != 0),
|
("headlineContent", $x.headlineContent, x.headlineContent.len != 0),
|
||||||
("children", $x.children, x.children.len != 0),
|
("children", $x.children, x.children.len != 0),
|
||||||
|
("content", $x.content, x.content != ""),
|
||||||
]
|
]
|
||||||
.stringifyFields()
|
.stringifyFields()
|
||||||
|
|
||||||
@@ -175,4 +177,4 @@ when isMainModule:
|
|||||||
OrgBlock(kind: orgHeading),
|
OrgBlock(kind: orgHeading),
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
echo doc
|
||||||
|
|||||||
@@ -14,6 +14,20 @@ import ../utils/str
|
|||||||
|
|
||||||
# -- Parsing Functions
|
# -- Parsing Functions
|
||||||
|
|
||||||
|
func lookBack*(count: int): (Parser -> ParserResult) =
|
||||||
|
return func(parser: Parser): ParserResult =
|
||||||
|
let state = parser.state
|
||||||
|
let newIndex = state.position - 1
|
||||||
|
|
||||||
|
Parser(
|
||||||
|
state: ParserState(
|
||||||
|
stream: state.stream,
|
||||||
|
position: newIndex,
|
||||||
|
lastPosition: parser.state.position,
|
||||||
|
),
|
||||||
|
tokens: parser.tokens,
|
||||||
|
).ok()
|
||||||
|
|
||||||
func ch*(expectedChars: set[char]): (Parser -> ParserResult) {.inline.} =
|
func ch*(expectedChars: set[char]): (Parser -> ParserResult) {.inline.} =
|
||||||
return func(parser: Parser): ParserResult =
|
return func(parser: Parser): ParserResult =
|
||||||
let state = parser.state
|
let state = parser.state
|
||||||
|
|||||||
@@ -288,7 +288,7 @@ func highlightStreamPosition(stream: string, position: int): string =
|
|||||||
|
|
||||||
proc `$`*(x: ParserState): string =
|
proc `$`*(x: ParserState): string =
|
||||||
&"""ParserState(
|
&"""ParserState(
|
||||||
stream: "{x.stream}",
|
stream: "{x.stream.highlightStreamPosition(x.position)}",
|
||||||
position: {x.position},
|
position: {x.position},
|
||||||
lastPosition: {x.lastPosition},
|
lastPosition: {x.lastPosition},
|
||||||
)"""
|
)"""
|
||||||
|
|||||||
Reference in New Issue
Block a user