Skip to content

Commit

Permalink
rewrite nbText, nbDetails and nbCode using sugar
Browse files Browse the repository at this point in the history
  • Loading branch information
HugoGranstrom committed Nov 21, 2024
1 parent a1c3d0b commit 7fa59a8
Showing 1 changed file with 79 additions and 42 deletions.
121 changes: 79 additions & 42 deletions sandbox/minib/minib.nim
Original file line number Diff line number Diff line change
Expand Up @@ -251,25 +251,31 @@ template nbSave* =

# all other blocks are in a sense all custom blocks
# we could add sugar for common block creation
type
NbText = ref object of NbBlock
text: string
template nbText*(ttext: string) =
let blk = NbText(text: ttext, kind: "NbText")
nb.add blk
func nbTextToHtml*(blk: NbBlock, nb: Nb): string =
let blk = blk.NbText
{.cast(noSideEffect).}: # not sure why markdown is marked with side effects
markdown(blk.text, config=initGfmConfig())
nbToHtml.funcs["NbText"] = nbTextToHtml
addNbBlockToJson(NbText)
#[ the above could be shortened with sugar to:
# type
# NbText = ref object of NbBlock
# text: string
# template nbText*(ttext: string) =
# let blk = NbText(text: ttext, kind: "NbText")
# nb.add blk
# func nbTextToHtml*(blk: NbBlock, nb: Nb): string =
# let blk = blk.NbText
# {.cast(noSideEffect).}: # not sure why markdown is marked with side effects
# markdown(blk.text, config=initGfmConfig())
# nbToHtml.funcs["NbText"] = nbTextToHtml
# addNbBlockToJson(NbText)

newNbBlock(nbText):
text: string
toHtml:
{.cast(noSideEffect).}: # not sure why markdown is marked with side effects
markdown(blk.text, config=initGfmConfig())
]#

proc text*(nb: var Nb, text: string) =
let blk = newNbText(text=text)
nb.add blk

template nbText(ttext: string) =
nb.text(ttext)

# type
# NbImage = ref object of NbBlock
Expand Down Expand Up @@ -301,40 +307,71 @@ func image*(nb: var Nb, url: string) =
template nbImage*(url: string) =
nb.image(url)

type
NbDetails = ref object of NbContainer
summary: string
# type
# NbDetails = ref object of NbContainer
# summary: string
# template nbDetails*(tsummary: string, body: untyped) =
# let blk = NbDetails(summary: tsummary, kind: "NbDetails")
# nb.withContainer(blk):
# body

# func nbDetailsToHtml*(blk: NbBlock, nb: Nb): string =
# let blk = blk.NbDetails
# "<details><summary>" & blk.summary & "</summary>\n" &
# nbContainerToHtml(blk, nb) &
# "\n</details>"

# nbToHtml.funcs["NbDetails"] = nbDetailsToHtml
# addNbBlockToJson(NbDetails)

newNbBlock(nbDetails of NbContainer):
summary: string
toHtml:
"<details><summary>" & blk.summary & "</summary>\n" &
nbContainerToHtml(blk, nb) &
"\n</details>"

template nbDetails*(tsummary: string, body: untyped) =
let blk = NbDetails(summary: tsummary, kind: "NbDetails")
let blk = newNbDetails(summary=tsummary)
nb.withContainer(blk):
body

func nbDetailsToHtml*(blk: NbBlock, nb: Nb): string =
let blk = blk.NbDetails
"<details><summary>" & blk.summary & "</summary>\n" &
nbContainerToHtml(blk, nb) &
"\n</details>"

nbToHtml.funcs["NbDetails"] = nbDetailsToHtml
addNbBlockToJson(NbDetails)
# type
# NbCode = ref object of NbBlock
# code: string
# output: string
# lang: string
# func nbCodeToHtml(blk: NbBlock, nb: Nb): string =
# let blk = blk.NbCode
# "<pre><code class=\"" & blk.lang & "\">\n" &
# blk.code & '\n' &
# "</code></pre>\n" &
# "<pre>\n" &
# blk.output & '\n' &
# "</pre>"
# nbToHtml.funcs["NbCode"] = nbCodeToHtml
# addNbBlockToJson(NbCode)
# template nbCode*(body: untyped) =
# let blk = NbCode(lang: "nim", kind: "NbCode")
# nb.add blk
# blk.code = toStr(body)
# captureStdout(blk.output):
# body

newNbBlock(nbCode):
code: string
output: string
lang: string
toHtml:
"<pre><code class=\"" & blk.lang & "\">\n" &
blk.code & '\n' &
"</code></pre>\n" &
"<pre>\n" &
blk.output & '\n' &
"</pre>"

type
NbCode = ref object of NbBlock
code: string
output: string
lang: string
func nbCodeToHtml(blk: NbBlock, nb: Nb): string =
let blk = blk.NbCode
"<pre><code class=\"" & blk.lang & "\">\n" &
blk.code & '\n' &
"</code></pre>\n" &
"<pre>\n" &
blk.output & '\n' &
"</pre>"
nbToHtml.funcs["NbCode"] = nbCodeToHtml
addNbBlockToJson(NbCode)
template nbCode*(body: untyped) =
let blk = NbCode(lang: "nim", kind: "NbCode")
let blk = newNbCode(code="", output="", lang="nim")
nb.add blk
blk.code = toStr(body)
captureStdout(blk.output):
Expand Down

0 comments on commit 7fa59a8

Please sign in to comment.