From 48fe2a394a9b621ce591ff7a093fabcba47e6ceb Mon Sep 17 00:00:00 2001 From: Pietro Peterlongo Date: Wed, 28 Feb 2024 11:42:03 +0100 Subject: [PATCH] nbDocToHtml and completed nbSave --- sandbox/minib3/minib.nim | 27 +++++++++++++++++++++------ sandbox/notes.md | 35 +++++++++++++++++++++++++++++++---- 2 files changed, 52 insertions(+), 10 deletions(-) diff --git a/sandbox/minib3/minib.nim b/sandbox/minib3/minib.nim index 95e1d9d..76edde2 100644 --- a/sandbox/minib3/minib.nim +++ b/sandbox/minib3/minib.nim @@ -10,7 +10,7 @@ type url: string NbDoc = ref object of NbBlock blocks: seq[NbBlock] - NbRenderFunc = proc (blk: NbBlock): string {. noSideEffect .} + NbRenderFunc = proc (blk: NbBlock, nb: Nb): string {. noSideEffect .} NbRender = object funcs: Table[string, NbRenderFunc] Nb = object @@ -20,6 +20,7 @@ type # nimib.nim import markdown +import std / strutils template nbInit* = var nb {. inject .}: Nb @@ -37,30 +38,41 @@ template nbImage*(turl: string) = nb.blk.kind = "NbImage" nb.doc.blocks.add nb.blk -func nbImageToHtml*(blk: NbBlock): string = +func nbImageToHtml*(blk: NbBlock, nb: Nb): string = let blk = blk.NbImage "" -func nbTextToHtml*(blk: NbBlock): string = +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()) +# forward declare +func render(nb: Nb, blk: NbBlock): string + +func nbDocToHtml*(blk: NbBlock, nb: Nb): string = + let blk = blk.NbDoc + var blocks: seq[string] + for b in blk.blocks: + blocks.add nb.render(b) + "\n\n\n" & blocks.join("\n") & "\n\n" + template addToBackend*(kind: string, f: NbRenderFunc) = nb.backend.funcs[kind] = f template nbInitBackend* = addToBackend("NbImage", nbImageToHtml) addToBackend("NbText", nbTextToHtml) + addToBackend("NbDoc", nbDocToHtml) func render(nb: Nb, blk: NbBlock): string = if blk.kind in nb.backend.funcs: - nb.backend.funcs[blk.kind](blk) + nb.backend.funcs[blk.kind](blk, nb) else: "" template nbSave* = - discard + echo nb.render nb.doc when isMainModule: import print @@ -76,4 +88,7 @@ when isMainModule: print nb.render nb.doc.blocks[0] # print nb.blocks[0].NbImage # correctly fails at runtime print nb.doc.blocks[1].NbImage - print nb.render nb.doc.blocks[1] \ No newline at end of file + print nb.render nb.doc.blocks[1] + + print nb.render nb.doc + \ No newline at end of file diff --git a/sandbox/notes.md b/sandbox/notes.md index b55c110..ba51e65 100644 --- a/sandbox/notes.md +++ b/sandbox/notes.md @@ -1,7 +1,20 @@ +## Naming + +- not sure I still like the name backend for the rendering engine + that changes from blocks to string (and could be a themed html, a json, markdown...) + +### notes on backends + +- I could implement specific behaviours: + - markdown backend defaults to html backend when not defined + - html backend could default to a commented json output when not defined + - json backend should always be defined + - but stuff can be marked as to be skipped, e.g. the default theme should not be serialized + ## Second day - Feb 28 - [x] review Hugo solution for oop -- define a scope and make explicit goals and assumptions +- [x] define a scope and make explicit goals and assumptions - answer why OOP question - start working on it, starting from api @@ -29,14 +42,28 @@ implementation of above scope, structure: todo: - [x] structure - [x] implement nbText, nbImage -- [ ] implement nbSave (html backend, default theme, echo to terminal) +- [x] implement nbSave (html backend, default theme, echo to terminal) - [x] refactor NbDoc as NbBlock and add Nb object - [x] NbRender and NbRenderFunc - [x] single backend (for now) - [x] nb.render blk - [x] nbImageToHtml - - [ ] nbTextToHtml - - [ ] nbDocToHtml + - [x] nbTextToHtml + - [x] nbDocToHtml +- [ ] implement json backend +- later: + - md backend + - nbCode, nbSummaryDetails + - sugar for block creation + - ... + +decisions: +- 1) what do I pass to the render function? + - docs and any container block will need the full backend + - currently passing the whole Nb object but it might be an overkill +- 2) where is the default backend defined? + - currently in Nb object (same as before) + #### key understanding