Skip to content

Commit 2ad8153

Browse files
committed
json backend now works!
1 parent 0932cdb commit 2ad8153

File tree

3 files changed

+70
-28
lines changed

3 files changed

+70
-28
lines changed

sandbox/minib3/minib.nim

Lines changed: 68 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,49 @@ type
1414
doc: NbDoc # could be a NbBlock but we could give more guarantees with a NbDoc
1515
backend: NbRender
1616

17-
# this needs to be know for all container blocks
17+
# this needs to be know for all container blocks not sure whether to put it in types
1818
func render(nb: Nb, blk: NbBlock): string =
1919
if blk.kind in nb.backend.funcs:
2020
nb.backend.funcs[blk.kind](blk, nb)
2121
else:
2222
""
2323
24+
# globals.nim
25+
var nbToJson: Table[string, proc (s: string, i: var int): NbBlock]
26+
var nbToHtml: NbRender # since we need it for json, let's make it also for html
27+
28+
# jsons.nim
29+
import jsony
30+
31+
template addNbBlockToJson*(kind: untyped) =
32+
nbToJson[$kind] =
33+
proc (s: string, i: var int): NbBlock =
34+
var v: kind
35+
new v
36+
parseHook(s, i, v[])
37+
result = v
38+
39+
method dump(n: kind): string =
40+
n[].toJson()
41+
42+
proc parseHook*(s: string, i: var int, v: var NbBlock) =
43+
# First parse the typename
44+
var n: NbBlock = NbBlock()
45+
let current_i = i
46+
parseHook(s, i, n[])
47+
# Reset i
48+
i = current_i
49+
# Parse the correct type
50+
let kind = n.kind
51+
v = nbToJson[kind](s, i)
52+
53+
method dump(n: NbBlock): string =
54+
n[].toJson()
55+
56+
proc dumpHook*(s: var string, v: NbBlock) =
57+
s.add v.dump()
58+
59+
2460
# themes.nim
2561
import std / strutils
2662
@@ -30,16 +66,16 @@ func nbDocToHtml*(blk: NbBlock, nb: Nb): string =
3066
for b in blk.blocks:
3167
blocks.add nb.render(b)
3268
"<!DOCTYPE html>\n<html><head></head>\n<body>\n" & blocks.join("\n") & "\n</body>\n</html>"
33-
69+
addNbBlockToJson(NbDoc)
3470
3571
# nimib.nim
3672
import markdown
3773
3874
template nbInit* =
3975
var nb {. inject .}: Nb
40-
nb.doc = NbDoc()
41-
nb.doc.kind = "NbDoc"
42-
nbInitBackend
76+
nb.doc = NbDoc(kind: "NbDoc")
77+
nbToHtml.funcs["NbDoc"] = nbDocToHtml
78+
nb.backend = nbToHtml
4379

4480
template nbSave* =
4581
echo nb.render nb.doc
@@ -56,6 +92,8 @@ func nbTextToHtml*(blk: NbBlock, nb: Nb): string =
5692
let blk = blk.NbText
5793
{.cast(noSideEffect).}: # not sure why markdown is marked with side effects
5894
markdown(blk.text, config=initGfmConfig())
95+
nbToHtml.funcs["NbText"] = nbTextToHtml
96+
addNbBlockToJson(NbText)
5997
#[ the above could be shortened with sugar to:
6098
newNbBlock(nbText):
6199
text: string
@@ -65,7 +103,6 @@ newNbBlock(nbText):
65103
]#
66104

67105

68-
69106
type
70107
NbImage = ref object of NbBlock
71108
url: string
@@ -75,37 +112,41 @@ template nbImage*(turl: string) =
75112
func nbImageToHtml*(blk: NbBlock, nb: Nb): string =
76113
let blk = blk.NbImage
77114
"<img src= '" & blk.url & "'>"
115+
nbToHtml.funcs["NbImage"] = nbImageToHtml
116+
addNbBlockToJson(NbImage)
78117
#[ the above could be shortened with sugar to:
79118
newNbBlock(nbImage):
80119
url: string
81120
toHtml:
82121
"<img src= '" & blk.url & "'>"
83122
]#
84123

85-
86-
87-
template addToBackend*(kind: string, f: NbRenderFunc) =
88-
nb.backend.funcs[kind] = f
89-
90-
template nbInitBackend* =
91-
addToBackend("NbImage", nbImageToHtml)
92-
addToBackend("NbText", nbTextToHtml)
93-
addToBackend("NbDoc", nbDocToHtml)
94-
95124
when isMainModule:
96125
import print
97-
# hello.nim
98126
nbInit
99127
nbText: "*hi*"
100128
nbImage("img.png")
101129
nbSave
102-
103-
print nb
104-
print nb.doc.blocks[0]
105-
print nb.doc.blocks[0].NbText
106-
print nb.render nb.doc.blocks[0]
107-
# print nb.blocks[0].NbImage # correctly fails at runtime
108-
print nb.doc.blocks[1].NbImage
109-
print nb.render nb.doc.blocks[1]
110-
111-
print nb.render nb.doc
130+
#[
131+
<!DOCTYPE html>
132+
<html><head></head>
133+
<body>
134+
<p><em>hi</em></p>
135+
136+
<img src= 'img.png'>
137+
</body>
138+
</html>
139+
]#
140+
141+
let docToJson = nb.doc.toJson()
142+
let docFromJson = docToJson.fromJson(NbDoc)
143+
print docToJson
144+
print docFromJson
145+
print docFromJson.blocks[0].NbText
146+
print docFromJson.blocks[1].NbImage
147+
#[
148+
docToJson="{"blocks":[{"text":"*hi*","kind":"NbText"},{"url":"img.png","kind":"NbImage"}],"kind":"NbDoc"}"
149+
docFromJson=NbDoc:ObjectType(blocks: @[NbBlock:ObjectType(kind: "NbText"), NbBlock:ObjectType(kind: "NbImage")], kind: "NbDoc")
150+
docFromJson.blocks[0].NbText=NbText:ObjectType(text: "*hi*", kind: "NbText")
151+
docFromJson.blocks[1].NbImage=NbImage:ObjectType(url: "img.png", kind: "NbImage")
152+
]#

sandbox/minib3/minilib.nim

Whitespace-only changes.

sandbox/notes.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@
2222
- in particular this means that block that need an id from global nb object need to generate it at block creation time!
2323
- reminder for later: to fully support custom container blocks I will need to add in
2424
Nb object something that tells me where to add next block
25+
- removed minilib, not useful since I would need import and that's a different problem
2526

2627
todo:
2728
- [x] restructured minib and added sugar for blocks
28-
- [ ] json backend
29+
- [x] json backend (hugo's work is great!)
2930

3031
## Second day - Feb 28
3132

0 commit comments

Comments
 (0)