Skip to content

Commit

Permalink
nbDocToHtml and completed nbSave
Browse files Browse the repository at this point in the history
  • Loading branch information
pietroppeter committed Feb 28, 2024
1 parent fd2dc27 commit 48fe2a3
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
27 changes: 21 additions & 6 deletions sandbox/minib3/minib.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,6 +20,7 @@ type

# nimib.nim
import markdown
import std / strutils

template nbInit* =
var nb {. inject .}: Nb
Expand All @@ -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
"<img src= '" & blk.url & "'>"

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)
"<!DOCTYPE html>\n<html><head></head>\n<body>\n" & blocks.join("\n") & "\n</body>\n</html>"

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
Expand All @@ -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]
print nb.render nb.doc.blocks[1]

print nb.render nb.doc

35 changes: 31 additions & 4 deletions sandbox/notes.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 48fe2a3

Please sign in to comment.