Skip to content

Commit 7fa59a8

Browse files
committed
rewrite nbText, nbDetails and nbCode using sugar
1 parent a1c3d0b commit 7fa59a8

File tree

1 file changed

+79
-42
lines changed

1 file changed

+79
-42
lines changed

sandbox/minib/minib.nim

Lines changed: 79 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -251,25 +251,31 @@ template nbSave* =
251251

252252
# all other blocks are in a sense all custom blocks
253253
# we could add sugar for common block creation
254-
type
255-
NbText = ref object of NbBlock
256-
text: string
257-
template nbText*(ttext: string) =
258-
let blk = NbText(text: ttext, kind: "NbText")
259-
nb.add blk
260-
func nbTextToHtml*(blk: NbBlock, nb: Nb): string =
261-
let blk = blk.NbText
262-
{.cast(noSideEffect).}: # not sure why markdown is marked with side effects
263-
markdown(blk.text, config=initGfmConfig())
264-
nbToHtml.funcs["NbText"] = nbTextToHtml
265-
addNbBlockToJson(NbText)
266-
#[ the above could be shortened with sugar to:
254+
# type
255+
# NbText = ref object of NbBlock
256+
# text: string
257+
# template nbText*(ttext: string) =
258+
# let blk = NbText(text: ttext, kind: "NbText")
259+
# nb.add blk
260+
# func nbTextToHtml*(blk: NbBlock, nb: Nb): string =
261+
# let blk = blk.NbText
262+
# {.cast(noSideEffect).}: # not sure why markdown is marked with side effects
263+
# markdown(blk.text, config=initGfmConfig())
264+
# nbToHtml.funcs["NbText"] = nbTextToHtml
265+
# addNbBlockToJson(NbText)
266+
267267
newNbBlock(nbText):
268268
text: string
269269
toHtml:
270270
{.cast(noSideEffect).}: # not sure why markdown is marked with side effects
271271
markdown(blk.text, config=initGfmConfig())
272-
]#
272+
273+
proc text*(nb: var Nb, text: string) =
274+
let blk = newNbText(text=text)
275+
nb.add blk
276+
277+
template nbText(ttext: string) =
278+
nb.text(ttext)
273279

274280
# type
275281
# NbImage = ref object of NbBlock
@@ -301,40 +307,71 @@ func image*(nb: var Nb, url: string) =
301307
template nbImage*(url: string) =
302308
nb.image(url)
303309

304-
type
305-
NbDetails = ref object of NbContainer
306-
summary: string
310+
# type
311+
# NbDetails = ref object of NbContainer
312+
# summary: string
313+
# template nbDetails*(tsummary: string, body: untyped) =
314+
# let blk = NbDetails(summary: tsummary, kind: "NbDetails")
315+
# nb.withContainer(blk):
316+
# body
317+
318+
# func nbDetailsToHtml*(blk: NbBlock, nb: Nb): string =
319+
# let blk = blk.NbDetails
320+
# "<details><summary>" & blk.summary & "</summary>\n" &
321+
# nbContainerToHtml(blk, nb) &
322+
# "\n</details>"
323+
324+
# nbToHtml.funcs["NbDetails"] = nbDetailsToHtml
325+
# addNbBlockToJson(NbDetails)
326+
327+
newNbBlock(nbDetails of NbContainer):
328+
summary: string
329+
toHtml:
330+
"<details><summary>" & blk.summary & "</summary>\n" &
331+
nbContainerToHtml(blk, nb) &
332+
"\n</details>"
333+
307334
template nbDetails*(tsummary: string, body: untyped) =
308-
let blk = NbDetails(summary: tsummary, kind: "NbDetails")
335+
let blk = newNbDetails(summary=tsummary)
309336
nb.withContainer(blk):
310337
body
311338

312-
func nbDetailsToHtml*(blk: NbBlock, nb: Nb): string =
313-
let blk = blk.NbDetails
314-
"<details><summary>" & blk.summary & "</summary>\n" &
315-
nbContainerToHtml(blk, nb) &
316-
"\n</details>"
317-
318-
nbToHtml.funcs["NbDetails"] = nbDetailsToHtml
319-
addNbBlockToJson(NbDetails)
339+
# type
340+
# NbCode = ref object of NbBlock
341+
# code: string
342+
# output: string
343+
# lang: string
344+
# func nbCodeToHtml(blk: NbBlock, nb: Nb): string =
345+
# let blk = blk.NbCode
346+
# "<pre><code class=\"" & blk.lang & "\">\n" &
347+
# blk.code & '\n' &
348+
# "</code></pre>\n" &
349+
# "<pre>\n" &
350+
# blk.output & '\n' &
351+
# "</pre>"
352+
# nbToHtml.funcs["NbCode"] = nbCodeToHtml
353+
# addNbBlockToJson(NbCode)
354+
# template nbCode*(body: untyped) =
355+
# let blk = NbCode(lang: "nim", kind: "NbCode")
356+
# nb.add blk
357+
# blk.code = toStr(body)
358+
# captureStdout(blk.output):
359+
# body
360+
361+
newNbBlock(nbCode):
362+
code: string
363+
output: string
364+
lang: string
365+
toHtml:
366+
"<pre><code class=\"" & blk.lang & "\">\n" &
367+
blk.code & '\n' &
368+
"</code></pre>\n" &
369+
"<pre>\n" &
370+
blk.output & '\n' &
371+
"</pre>"
320372

321-
type
322-
NbCode = ref object of NbBlock
323-
code: string
324-
output: string
325-
lang: string
326-
func nbCodeToHtml(blk: NbBlock, nb: Nb): string =
327-
let blk = blk.NbCode
328-
"<pre><code class=\"" & blk.lang & "\">\n" &
329-
blk.code & '\n' &
330-
"</code></pre>\n" &
331-
"<pre>\n" &
332-
blk.output & '\n' &
333-
"</pre>"
334-
nbToHtml.funcs["NbCode"] = nbCodeToHtml
335-
addNbBlockToJson(NbCode)
336373
template nbCode*(body: untyped) =
337-
let blk = NbCode(lang: "nim", kind: "NbCode")
374+
let blk = newNbCode(code="", output="", lang="nim")
338375
nb.add blk
339376
blk.code = toStr(body)
340377
captureStdout(blk.output):

0 commit comments

Comments
 (0)