Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor NbBlock #235

Draft
wants to merge 28 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
d71d0c2
WIP - pair programming session w Elias
pietroppeter Feb 27, 2024
5b698ad
review hugoop
pietroppeter Feb 27, 2024
b663eb4
minib3: nbText, nbImage
pietroppeter Feb 27, 2024
d2f243f
minib3: refactor NbDoc as NbBlock and add Nb object
pietroppeter Feb 27, 2024
d47349e
minib3: wip render (single backend)
pietroppeter Feb 27, 2024
fd2dc27
minib3: nbTextToHtml
pietroppeter Feb 28, 2024
48fe2a3
nbDocToHtml and completed nbSave
pietroppeter Feb 28, 2024
0932cdb
more understanding on how this should look (less progress on code)
pietroppeter Feb 29, 2024
2ad8153
json backend now works!
pietroppeter Feb 29, 2024
a52dbe6
notes from fourth day
pietroppeter Mar 2, 2024
ffb4bd9
notes from fifth day
pietroppeter Mar 2, 2024
5e6727e
[skip ci] fix type and test skip ci message
pietroppeter Mar 2, 2024
c35114e
add NbContainer
pietroppeter Mar 25, 2024
954ef22
change minimal theme and add NbContainerToHtml
pietroppeter Mar 25, 2024
5d995ac
even a specific dumpHoook does not work!
pietroppeter Mar 27, 2024
5d761c3
new NbContainer implementation, add api `nb.add` and `nb.withContaine…
pietroppeter Mar 27, 2024
80cd2c5
hide nb.blk api in add
pietroppeter Mar 27, 2024
e338f67
update dev notes
pietroppeter Mar 27, 2024
ad3b148
implement nbCode
pietroppeter Mar 28, 2024
53c17fe
copy jsutils to minib
HugoGranstrom May 18, 2024
b8333ba
start implementing nbJsFromCode
HugoGranstrom May 18, 2024
70dd4c8
add showCode parameter to NbJsFromCode
HugoGranstrom May 18, 2024
45d2ca6
hacky nbJsFromCode implementation
HugoGranstrom May 18, 2024
73efd51
use of syntex instead of checking kind
HugoGranstrom May 19, 2024
08b5eea
implement blocksFlattened func for container blocks and use it
HugoGranstrom May 19, 2024
591f8ed
start working on sugar macro
HugoGranstrom Oct 13, 2024
a1c3d0b
fully implemented sugar
HugoGranstrom Nov 18, 2024
7fa59a8
rewrite nbText, nbDetails and nbCode using sugar
HugoGranstrom Nov 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions sandbox/hugoop/one.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import jsony

type
Nimib = ref object of RootObj
a, b, c: int
NimibChild = ref object of Nimib
d, e: float


method dump(n: Nimib): string =
n[].toJson()

when not defined(noNimibChild): # essential, see below
method dump(n: NimibChild): string =
n[].toJson()

proc dumpHook*(s: var string, v: Nimib) =
s.add v.dump()

let n1: Nimib = Nimib(a: 1, b: 2, c: 3)
let n2: Nimib = NimibChild(d: 3.14, e: 4.56)

echo n1.toJson()
echo n2.toJson()
#[ nim r one
{"a":1,"b":2,"c":3}
{"d":3.14,"e":4.56,"a":0,"b":0,"c":0}
]#
#[ nim -d:noNimibChild r one
{"a":1,"b":2,"c":3}
{"a":0,"b":0,"c":0}
]#
51 changes: 51 additions & 0 deletions sandbox/hugoop/three.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import jsony, tables

type
Nimib = ref object of RootObj
a, b, c: int
typename: string
NimibChild = ref object of Nimib
d, e: float

var parseDefs: Table[string, proc (s: string, i: var int): Nimib]

template registerBlock(typename: untyped) =
parseDefs[$typename] =
proc (s: string, i: var int): Nimib =
var v: typename
new v
parseHook(s, i, v[])
result = v

method dump(n: typename): string =
n[].toJson()

proc parseHook*(s: string, i: var int, v: var Nimib) =
# First parse the typename
var n: Nimib = Nimib()
let current_i = i
parseHook(s, i, n[])
# Reset i
i = current_i
# Parse the correct type
let typename = n.typename
v = parseDefs[typename](s, i)

registerBlock(Nimib)
registerBlock(NimibChild)

# moved this here, I do not need to add dummy dumphook
proc dumpHook*(s: var string, v: Nimib) =
s.add v.dump()


let n1: Nimib = Nimib(a: 1, b: 2, c: 3, typename: "Nimib")
let n2: Nimib = NimibChild(a: 100, d: 3.14, e: 4.56, typename: "NimibChild")

echo n1.toJson()
echo n2.toJson()

let s1 = n1.toJson().fromJson(Nimib)
echo s1.toJson()
let s2 = n2.toJson().fromJson(Nimib)
echo s2.toJson()
51 changes: 51 additions & 0 deletions sandbox/hugoop/two.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import jsony, tables

type
Nimib = ref object of RootObj
a, b, c: int
typename: string
NimibChild = ref object of Nimib
d, e: float

method dump(n: Nimib): string =
n[].toJson()

method dump(n: NimibChild): string =
n[].toJson()

proc dumpHook*(s: var string, v: Nimib) =
s.add v.dump()

let n1: Nimib = Nimib(a: 1, b: 2, c: 3, typename: "Nimib")
let n2: Nimib = NimibChild(a: 100, d: 3.14, e: 4.56, typename: "NimibChild")

echo n1.toJson()
echo n2.toJson()

proc parseNimib(s: string, i: var int): Nimib =
var v = Nimib()
parseHook(s, i, v[])
result = v

proc parseNimibChild(s: string, i: var int): Nimib =
var v = NimibChild()
parseHook(s, i, v[])
result = v

let parseDefs = {
"Nimib": parseNimib,
"NimibChild": parseNimibChild
}.toTable()

proc parseHook*(s: string, i: var int, v: var Nimib) =
var n: Nimib = Nimib()
let current_i = i
parseHook(s, i, n[])
i = current_i
let typename = n.typename
v = parseDefs[typename](s, i)

let s1 = n1.toJson().fromJson(Nimib)
echo s1.toJson()
let s2 = n2.toJson().fromJson(Nimib)
echo s2.toJson()
30 changes: 30 additions & 0 deletions sandbox/minib/capture.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
## This submodule implements only one template to temporarily redirect and capture stdout during execution of a chunk of code.
import tempfile

# see https://stackoverflow.com/questions/64026829/how-to-temporarily-capture-stdout
# Thanks, Clonk!

# low level, should be Posix only but they happen to work on (my) Windows, too!
proc dup(oldfd: FileHandle): FileHandle {.importc, header: "unistd.h".}
proc dup2(oldfd: FileHandle, newfd: FileHandle): cint {.importc, header: "unistd.h".}

template captureStdout*(ident: untyped, body: untyped) =
## redirect stdout to a temporary file and captures output of body in ident
let
# Duplicate stdout
stdoutFileno = stdout.getFileHandle()
stdoutDupfd = dup(stdoutFileno)
# Create a new temporary file
(tmpFile, tmpFilename) = mkstemp(mode=fmWrite)
tmpFileFd: FileHandle = tmpFile.getFileHandle()
discard dup2(tmpFileFd, stdoutFileno) # writing to stdoutFileno now writes to tmpFile

body
flushFile stdout

# before reading tmpFile, flush and close
tmpFile.flushFile()
tmpFile.close()
ident = readFile(tmpFileName)
# Restore stdout
discard dup2(stdoutDupfd, stdoutFileno)
Loading
Loading