Add link tokenizer & stringifier
This commit is contained in:
@@ -1,5 +1,11 @@
|
|||||||
|
import std/strformat
|
||||||
|
import std/sugar
|
||||||
|
import std/collections/sequtils
|
||||||
import results
|
import results
|
||||||
import fusion/matching
|
import fusion/matching
|
||||||
|
import fp/maybe
|
||||||
|
import ./org_types
|
||||||
|
import ./org_builder
|
||||||
import ../utils/fp
|
import ../utils/fp
|
||||||
import ../parser/parser_internals
|
import ../parser/parser_internals
|
||||||
import ../parser/parser_types
|
import ../parser/parser_types
|
||||||
@@ -30,6 +36,29 @@ let linkParser* = proc(parser: Parser): ParserResult {.closure.} =
|
|||||||
else:
|
else:
|
||||||
linkValue
|
linkValue
|
||||||
|
|
||||||
|
func linkStringifier*(linkUrl: string, linkDescription: Maybe[string]): string =
|
||||||
|
case (linkUrl, linkDescription):
|
||||||
|
of (@linkUrl, Some(@linkDescription)):
|
||||||
|
return &"[[{linkUrl}][{linkDescription}]]"
|
||||||
|
of (@linkUrl, None()):
|
||||||
|
return &"[[{linkUrl}]]"
|
||||||
|
|
||||||
|
func linkTokenizer*(parserTokens: seq[ParserToken]): OrgBuilderT =
|
||||||
|
[@linkUrl, @linkDescription] := parserTokens.map(tokenStringValue)
|
||||||
|
let maybeLinkDescription = linkDescription.just().notEmpty()
|
||||||
|
OrgBuilderT(
|
||||||
|
kind: orgLink,
|
||||||
|
content: linkStringifier(linkUrl, maybeLinkDescription),
|
||||||
|
linkUrl: linkUrl,
|
||||||
|
linkDescription: maybeLinkDescription,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
echo initParser("[[]]").linkParser()
|
# echo initParser("[[https://florianschroedl.com]]").linkParser()
|
||||||
echo initParser("[[Foo][Bar]]").linkParser()
|
echo initParser("[[https://florianschroedl.com][My blog]]")
|
||||||
|
.linkParser()
|
||||||
|
.foldTokens(
|
||||||
|
onError = (x) => nothing(OrgBuilderT),
|
||||||
|
onSuccess = xs => just(linkTokenizer(xs)),
|
||||||
|
)
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
import std/strformat
|
import std/strformat
|
||||||
|
import std/sugar
|
||||||
|
import std/strutils
|
||||||
|
import fp/maybe
|
||||||
|
|
||||||
type
|
type
|
||||||
orgElementKind* = enum
|
orgElementKind* = enum
|
||||||
@@ -12,6 +15,9 @@ type
|
|||||||
orgVerbatimText,
|
orgVerbatimText,
|
||||||
orgCodeText,
|
orgCodeText,
|
||||||
orgStrikeThroughText,
|
orgStrikeThroughText,
|
||||||
|
|
||||||
|
# Links
|
||||||
|
orgLink,
|
||||||
OrgElement* = ref object
|
OrgElement* = ref object
|
||||||
children*: seq[OrgElement]
|
children*: seq[OrgElement]
|
||||||
content*: string
|
content*: string
|
||||||
@@ -28,6 +34,11 @@ type
|
|||||||
of orgCodeText: discard
|
of orgCodeText: discard
|
||||||
of orgStrikeThroughText: discard
|
of orgStrikeThroughText: discard
|
||||||
|
|
||||||
|
# Links
|
||||||
|
of orgLink:
|
||||||
|
linkUrl*: string
|
||||||
|
linkDescription*: Maybe[string]
|
||||||
|
|
||||||
proc `$`*(x: orgElementKind): string =
|
proc `$`*(x: orgElementKind): string =
|
||||||
case x:
|
case x:
|
||||||
of orgRawText: "Text (Raw)"
|
of orgRawText: "Text (Raw)"
|
||||||
@@ -41,8 +52,27 @@ proc `$`*(x: orgElementKind): string =
|
|||||||
of orgCodeText: "Text (Code)"
|
of orgCodeText: "Text (Code)"
|
||||||
of orgStrikeThroughText: "Text (StrikeThrough)"
|
of orgStrikeThroughText: "Text (StrikeThrough)"
|
||||||
|
|
||||||
|
# Links
|
||||||
|
of orgLink: "Link"
|
||||||
|
|
||||||
|
func stringifySpecialFields(x: OrgElement): string =
|
||||||
|
let specialFields = case x.kind:
|
||||||
|
of orgLink:
|
||||||
|
&"""linkUrl: "{x.linkUrl}"""" &
|
||||||
|
x.linkDescription.map(y => &"\nlinkValue: \"{y}\"").getOrElse("")
|
||||||
|
else: ""
|
||||||
|
|
||||||
|
specialFields
|
||||||
|
.just()
|
||||||
|
.notEmpty()
|
||||||
|
.map(x => x.indent(2))
|
||||||
|
.map(x => "\n" & x & "\n")
|
||||||
|
.getOrElse("")
|
||||||
|
|
||||||
proc `$`*(x: OrgElement): string =
|
proc `$`*(x: OrgElement): string =
|
||||||
&"""OrgElement(
|
&"""OrgElement(
|
||||||
content: {x.content}
|
content: "{x.content}"
|
||||||
kind: {x.kind}
|
kind: {x.kind}""" &
|
||||||
|
stringifySpecialFields(x) &
|
||||||
|
"""
|
||||||
)"""
|
)"""
|
||||||
|
|||||||
Reference in New Issue
Block a user