Rename OrgElement -> OrgInlineBlock
This commit is contained in:
@@ -3,28 +3,28 @@ import results
|
|||||||
import ./org_types
|
import ./org_types
|
||||||
import ../parser/parser_types
|
import ../parser/parser_types
|
||||||
|
|
||||||
type OrgBuilderT* = OrgElement
|
type OrgInlineBuilderT* = OrgInlineBlock
|
||||||
type OrgBuilder* = Builder[OrgBuilderT]
|
type OrgInlineBuilder* = Builder[OrgInlineBuilderT]
|
||||||
type OrgBuilderResult* = BuilderResult[OrgBuilderT]
|
type OrgInlineBuilderResult* = BuilderResult[OrgInlineBuilderT]
|
||||||
|
|
||||||
func initOrgBuilder*(content: string): OrgBuilderResult =
|
func initOrgInlineBuilder*(content: string): OrgInlineBuilderResult =
|
||||||
return OrgBuilderResult.ok(OrgBuilder((
|
return OrgInlineBuilderResult.ok(OrgInlineBuilder((
|
||||||
parser: initParser(content),
|
parser: initParser(content),
|
||||||
tree: newSeq[OrgBuilderT](),
|
tree: newSeq[OrgInlineBuilderT](),
|
||||||
)))
|
)))
|
||||||
|
|
||||||
func textTokenizer*(kind: orgElementKind): seq[ParserToken] -> seq[OrgBuilderT] =
|
func textTokenizer*(kind: orgInlineBlockKind): seq[ParserToken] -> seq[OrgInlineBuilderT] =
|
||||||
return func(tokens: seq[ParserToken]): seq[OrgBuilderT] =
|
return func(tokens: seq[ParserToken]): seq[OrgInlineBuilderT] =
|
||||||
return @[
|
return @[
|
||||||
OrgBuilderT(
|
OrgInlineBuilderT(
|
||||||
kind: kind,
|
kind: kind,
|
||||||
content: tokens.tokensToString(),
|
content: tokens.tokensToString(),
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
|
|
||||||
func rawTextTokenizer*(kind: orgElementKind): string -> OrgBuilderT =
|
func rawTextTokenizer*(kind: orgInlineBlockKind): string -> OrgInlineBuilderT =
|
||||||
return func(content: string): OrgBuilderT =
|
return func(content: string): OrgInlineBuilderT =
|
||||||
return OrgBuilderT(
|
return OrgInlineBuilderT(
|
||||||
kind: kind,
|
kind: kind,
|
||||||
content: content,
|
content: content,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ let orgStyledTextBuilders = @[
|
|||||||
(linkParser, linkTokenizerSeq),
|
(linkParser, linkTokenizerSeq),
|
||||||
]
|
]
|
||||||
|
|
||||||
proc makeRawTokenOrEmpty(tokens: seq[ParserToken]): seq[OrgBuilderT] =
|
proc makeRawTokenOrEmpty(tokens: seq[ParserToken]): seq[OrgInlineBuilderT] =
|
||||||
## Merge all parser `tokens` into a string to tokenize for the builder.
|
## Merge all parser `tokens` into a string to tokenize for the builder.
|
||||||
## Unless the string is empty, in this case return an empty list.
|
## Unless the string is empty, in this case return an empty list.
|
||||||
let str = tokens.foldl(a & b.tokenStringValue(), "")
|
let str = tokens.foldl(a & b.tokenStringValue(), "")
|
||||||
@@ -47,10 +47,10 @@ proc makeRawTokenOrEmpty(tokens: seq[ParserToken]): seq[OrgBuilderT] =
|
|||||||
else: @[rawTokenizer(str)]
|
else: @[rawTokenizer(str)]
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
let test = initOrgBuilder(
|
let test = initOrgInlineBuilder(
|
||||||
"Regular *bold* [[https://some.url]] /italic/ _underline_ =verbatim= ~code~ +strikethrough+ [[https://some.url][title]]"
|
"Regular *bold* [[https://some.url]] /italic/ _underline_ =verbatim= ~code~ +strikethrough+ [[https://some.url][title]]"
|
||||||
)
|
)
|
||||||
.flatMap((builder: OrgBuilder) => tryParseBuild(
|
.flatMap((builder: OrgInlineBuilder) => tryParseBuild(
|
||||||
builder = builder,
|
builder = builder,
|
||||||
builderFns = orgStyledTextBuilders,
|
builderFns = orgStyledTextBuilders,
|
||||||
defaultBuilderFn = makeRawTokenOrEmpty,
|
defaultBuilderFn = makeRawTokenOrEmpty,
|
||||||
|
|||||||
@@ -43,17 +43,17 @@ proc linkStringifier*(linkUrl: string, linkDescription: Maybe[string]): string =
|
|||||||
of (@linkUrl, None()):
|
of (@linkUrl, None()):
|
||||||
return &"[[{linkUrl}]]"
|
return &"[[{linkUrl}]]"
|
||||||
|
|
||||||
func linkTokenizer*(parserTokens: seq[ParserToken]): OrgBuilderT =
|
func linkTokenizer*(parserTokens: seq[ParserToken]): OrgInlineBuilderT =
|
||||||
[@linkUrl, @linkDescription] := parserTokens.map(tokenStringValue)
|
[@linkUrl, @linkDescription] := parserTokens.map(tokenStringValue)
|
||||||
let maybeLinkDescription = linkDescription.just().notEmpty()
|
let maybeLinkDescription = linkDescription.just().notEmpty()
|
||||||
return OrgBuilderT(
|
return OrgInlineBuilderT(
|
||||||
kind: orgLink,
|
kind: orgLink,
|
||||||
content: linkStringifier(linkUrl, maybeLinkDescription),
|
content: linkStringifier(linkUrl, maybeLinkDescription),
|
||||||
linkUrl: linkUrl,
|
linkUrl: linkUrl,
|
||||||
linkDescription: maybeLinkDescription,
|
linkDescription: maybeLinkDescription,
|
||||||
)
|
)
|
||||||
|
|
||||||
let linkTokenizerSeq* = proc(parserTokens: seq[ParserToken]): seq[OrgBuilderT] {.closure.} =
|
let linkTokenizerSeq* = proc(parserTokens: seq[ParserToken]): seq[OrgInlineBuilderT] {.closure.} =
|
||||||
return @[linkTokenizer(parserTokens)]
|
return @[linkTokenizer(parserTokens)]
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
@@ -61,6 +61,6 @@ when isMainModule:
|
|||||||
echo initParser("[[https://florianschroedl.com][My blog]]")
|
echo initParser("[[https://florianschroedl.com][My blog]]")
|
||||||
.linkParser()
|
.linkParser()
|
||||||
.foldTokens(
|
.foldTokens(
|
||||||
onError = (x) => newSeq[OrgBuilderT](),
|
onError = (x) => newSeq[OrgInlineBuilderT](),
|
||||||
onSuccess = proc(xs: seq[ParserToken]): seq[OrgBuilderT] = linkTokenizerSeq(xs),
|
onSuccess = proc(xs: seq[ParserToken]): seq[OrgInlineBuilderT] = linkTokenizerSeq(xs),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import std/strutils
|
|||||||
import fp/maybe
|
import fp/maybe
|
||||||
|
|
||||||
type
|
type
|
||||||
orgElementKind* = enum
|
orgInlineBlockKind* = enum
|
||||||
orgRawText,
|
orgRawText,
|
||||||
orgText,
|
orgText,
|
||||||
|
|
||||||
@@ -18,11 +18,11 @@ type
|
|||||||
|
|
||||||
# Links
|
# Links
|
||||||
orgLink,
|
orgLink,
|
||||||
OrgElement* = ref object
|
OrgInlineBlock* = ref object
|
||||||
children*: seq[OrgElement]
|
children*: seq[OrgInlineBlock]
|
||||||
content*: string
|
content*: string
|
||||||
|
|
||||||
case kind*: orgElementKind
|
case kind*: orgInlineBlockKind
|
||||||
of orgRawText: discard
|
of orgRawText: discard
|
||||||
of orgText: discard
|
of orgText: discard
|
||||||
|
|
||||||
@@ -39,7 +39,7 @@ type
|
|||||||
linkUrl*: string
|
linkUrl*: string
|
||||||
linkDescription*: Maybe[string]
|
linkDescription*: Maybe[string]
|
||||||
|
|
||||||
proc `$`*(x: orgElementKind): string =
|
proc `$`*(x: orgInlineBlockKind): string =
|
||||||
case x:
|
case x:
|
||||||
of orgRawText: "Text (Raw)"
|
of orgRawText: "Text (Raw)"
|
||||||
of orgText: "Text"
|
of orgText: "Text"
|
||||||
@@ -55,7 +55,7 @@ proc `$`*(x: orgElementKind): string =
|
|||||||
# Links
|
# Links
|
||||||
of orgLink: "Link"
|
of orgLink: "Link"
|
||||||
|
|
||||||
func stringifySpecialFields(x: OrgElement): string =
|
func stringifySpecialFields(x: OrgInlineBlock): string =
|
||||||
let specialFields = case x.kind:
|
let specialFields = case x.kind:
|
||||||
of orgLink:
|
of orgLink:
|
||||||
&"""linkUrl: "{x.linkUrl}"""" &
|
&"""linkUrl: "{x.linkUrl}"""" &
|
||||||
@@ -69,8 +69,8 @@ func stringifySpecialFields(x: OrgElement): string =
|
|||||||
.map(x => "\n" & x & "\n")
|
.map(x => "\n" & x & "\n")
|
||||||
.getOrElse("")
|
.getOrElse("")
|
||||||
|
|
||||||
proc `$`*(x: OrgElement): string =
|
proc `$`*(x: OrgInlineBlock): string =
|
||||||
&"""OrgElement(
|
&"""OrgInlineBlock(
|
||||||
content: "{x.content}"
|
content: "{x.content}"
|
||||||
kind: {x.kind}""" &
|
kind: {x.kind}""" &
|
||||||
stringifySpecialFields(x) &
|
stringifySpecialFields(x) &
|
||||||
|
|||||||
@@ -1,23 +1,23 @@
|
|||||||
import print
|
import print
|
||||||
|
|
||||||
type
|
type
|
||||||
orgElementContent = string
|
orgInlineBlockContent = string
|
||||||
orgElementChildren = seq[OrgElement]
|
orgInlineBlockChildren = seq[OrgInlineBlock]
|
||||||
|
|
||||||
OrgElementKind = enum
|
OrgInlineBlockKind = enum
|
||||||
orgDocument,
|
orgDocument,
|
||||||
orgHeadline,
|
orgHeadline,
|
||||||
orgText,
|
orgText,
|
||||||
|
|
||||||
OrgElement* = ref object
|
OrgInlineBlock* = ref object
|
||||||
children*: orgElementChildren
|
children*: orgInlineBlockChildren
|
||||||
id*: string
|
id*: string
|
||||||
|
|
||||||
case kind*: OrgElementKind
|
case kind*: OrgInlineBlockKind
|
||||||
of orgHeadline:
|
of orgHeadline:
|
||||||
level*: int
|
level*: int
|
||||||
of orgText:
|
of orgText:
|
||||||
content: orgElementContent
|
content: orgInlineBlockContent
|
||||||
of orgDocument: discard
|
of orgDocument: discard
|
||||||
|
|
||||||
let emptyChildrenSeq: orgElementChildren = newSeq[OrgElement]()
|
let emptyChildrenSeq: orgInlineBlockChildren = newSeq[OrgInlineBlock]()
|
||||||
|
|||||||
Reference in New Issue
Block a user