Skip to content

Refactor NbBlock #235

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

Draft
wants to merge 60 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 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
571f22e
rewrite nbJs to sugar and write functional variants
HugoGranstrom Jan 31, 2025
9394c20
start the chaotic work of rewriting nimib
HugoGranstrom Feb 19, 2025
a23d1ed
finish theme-related funcs
HugoGranstrom Feb 20, 2025
0bd3cb3
inheritance now works in newNbBlock macro
HugoGranstrom Feb 21, 2025
f6800ed
make codeAsInSource not require the command as input
HugoGranstrom Feb 21, 2025
cab5c1b
implement nbCode-family of blocks (nimibCode is no longer needed!)
HugoGranstrom Feb 21, 2025
280a6e1
implement more blocks and use funcs where possible
HugoGranstrom Feb 21, 2025
0686e3a
implement nbJs blocks
HugoGranstrom Feb 21, 2025
d0f3cb0
implement the last few blocks
HugoGranstrom Feb 21, 2025
c077f79
Merge branch 'main' into refactor-nb-block
HugoGranstrom Feb 22, 2025
2c0d5ca
fix miss in merging
HugoGranstrom Feb 22, 2025
5c47bf2
port nbVideo and nbAudio
HugoGranstrom Feb 22, 2025
5018189
start fixing docs
HugoGranstrom Feb 22, 2025
1478f86
remove allblocks from build for now
HugoGranstrom Feb 22, 2025
7385cc8
update ci nim version
HugoGranstrom Feb 22, 2025
eee9efe
add install libpcre3
HugoGranstrom Feb 22, 2025
5c69446
test adding allblocks again
HugoGranstrom Feb 22, 2025
cbed8e8
add debug log to sources
HugoGranstrom Feb 22, 2025
29b018b
more debug prints
HugoGranstrom Feb 22, 2025
f1c4eb4
more debug
HugoGranstrom Feb 22, 2025
f822034
one last try
HugoGranstrom Feb 22, 2025
becf818
try handling empty nimnodes
HugoGranstrom Feb 23, 2025
6a5b455
remove debug prints in sources.nim
HugoGranstrom Feb 23, 2025
78c20a7
cheatsheet is back!
HugoGranstrom Feb 23, 2025
bb473cf
all docs are back!
HugoGranstrom Feb 23, 2025
f44d11e
include penguins in docs again
HugoGranstrom Feb 23, 2025
32be620
installblas
HugoGranstrom Feb 23, 2025
dc7a583
implement partials and try it out on NbCode
HugoGranstrom Mar 2, 2025
b775b0a
remove showCode field from NbJs blocks as it isn't used anymore
HugoGranstrom Mar 2, 2025
bef739a
nbText now uses partial
HugoGranstrom Mar 2, 2025
83fe356
use partials for rendering NbDoc
HugoGranstrom Mar 14, 2025
5bf0a5c
implement NbDiv
HugoGranstrom May 18, 2025
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
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
- uses: actions/checkout@v2
- name: install nim
id: install_nim
uses: iffy/install-nim@v3
uses: iffy/install-nim@v5
- name: install library dependencies
run: nimble install -y
- name: install doc dependencies
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/netlify_build_docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ jobs:
tests:
runs-on: ubuntu-latest
steps:
- name: apt install
run: sudo apt install -y libpcre3 libblas-dev liblapack-dev
- uses: actions/checkout@v2
- name: install nim
id: install_nim
uses: iffy/install-nim@v3
uses: iffy/install-nim@v5
- name: install library dependencies
run: nimble install -y
- name: install doc dependencies
Expand Down
12 changes: 6 additions & 6 deletions docsrc/allblocks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import std/[strutils, strformat]

nbInit

var nbToc: NbBlock
var nbToc: NbText

template addToc =
newNbBlock("nbText", false, nb, nbToc, ""):
nbToc.output = "## Table of Contents:\n\n"
nbToc = newNbText(text="## Table of Contents:\n\n")
nb.add nbToc

template nbCodeBlock(name:string) =
template nbCodeBlock(name: string) =
let anchorName = name.toLower.replace(" ", "-")
nbText "<a name = \"" & anchorName & "\"></a>\n### " & name & "\n\n---"
# see below, but any number works for a numbered list
nbToc.output.add "1. <a href=\"#" & anchorName & "\">" & name & "</a>\n"
nbToc.text.add "1. <a href=\"#" & anchorName & "\">" & name & "</a>\n"

nbText: """
> This nimib document provides a brief description and example for
Expand Down Expand Up @@ -120,7 +120,7 @@ The `typ` parameter specifies the video's MIME type, and is optional!
"""

nimibCode:
nbVideo(url="media/bad_apple!!.mp4", typ="video/mp4")
nbVideo(url="media/bad_apple!!.mp4", filetype="video/mp4")

nbCodeBlock: "nbAudio"
nbText: """
Expand Down
30 changes: 16 additions & 14 deletions docsrc/cheatsheet.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## Markdown cheatsheet with nimib
import nimib, std/strutils
import nimib, std/[strutils, strformat, json]

nbInit
nbText: """
Expand All @@ -15,34 +15,36 @@ nbText: """
"""

# customize source highlighting:
nb.context["highlight"] = """
nb.doc.context["highlight"] = %"""
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/default.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>"""


# a custom text block that shows markdown source
template nbTextWithSource*(body: untyped) =
newNbBlock("nbTextWithSource", false, nb, nb.blk, body):
nb.blk.output = body
nb.blk.context["code"] = body
newNbBlock(NbTextWithSource of NbText):
toHtml:
result = withNewlines:
nbTextToHtml(blk, nb)
&"""<pre><code class="language-markdown">{blk.text}</code></pre>"""

nb.renderPlans["nbTextWithSource"] = @["mdOutputToHtml"]
nb.partials["nbTextWithSource"] = """{{&outputToHtml}}
<pre><code class=\"language-markdown\">{{code}}</code></pre>"""
template nbTextWithSource*(body: string) =
let blk = newNbTextWithSource(text=body)
nb.add blk

# how to add a ToC
var
nbToc: NbBlock
nbToc: NbText

template addToc =
newNbBlock("nbText", false, nb, nbToc, ""):
nbToc.output = "# Table of Contents:\n\n"
nbToc = newNbText(text="# Table of Contents:\n\n")
nb.add nbToc

template nbSection(name:string) =
let anchorName = name.toLower.replace(" ", "-")
nbText "<a name = \"" & anchorName & "\"></a>\n# " & name & "\n\n---"
# see below, but any number works for a numbered list
nbToc.output.add "1. <a href=\"#" & anchorName & "\">" & name & "</a>\n"
nbToc.text.add "1. <a href=\"#" & anchorName & "\">" & name & "</a>\n"

# here is the start of the document
nbText """
Expand Down Expand Up @@ -100,7 +102,7 @@ nbTextWithSource """1. First ordered list item
* Unordered list can use asterisks
- Or minuses
+ Or pluses"""
nb.blk.output = nb.blk.output.replace(dot, " ")
nb.blk.NbTextWithSource.text = nb.blk.NbTextWithSource.text.replace(dot, " ")

nbText """
> in ordered to have the ordered sublist work correctly
Expand Down
4 changes: 2 additions & 2 deletions docsrc/far/from/home.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import nimib, strutils
nbInit
nb.context["path_to_root"] = nb.context["path_to_root"].castStr.replace("/", r"\") # for CI since I run locally on Windows and CI runs on Linux: behaviour is the same
nb.context["path_to_here"] = nb.context["path_to_here"].castStr.replace("/", r"\") # for CI since I run locally on Windows and CI runs on Linux: behaviour is the same
nb.doc.context["path_to_root"] = %nb.doc.context{"path_to_root"}.getStr.replace("/", r"\") # for CI since I run locally on Windows and CI runs on Linux: behaviour is the same
nb.doc.context["path_to_here"] = %nb.doc.context{"path_to_here"}.getStr.replace("/", r"\") # for CI since I run locally on Windows and CI runs on Linux: behaviour is the same
nb.partials["document"] = """
<html>
<head></head>
Expand Down
12 changes: 6 additions & 6 deletions docsrc/index.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ nb.title = "Nimib Docs"
let
repo = "https://github.com/pietroppeter/nimib"
docs = if defined(mdOutput): "https://pietroppeter.github.io/nimib" else: "."
hello = read(nb.srcDir / "hello.nim".RelativeFile)
hello = read(nb.doc.srcDir / "hello.nim".RelativeFile)

nbText: hlMdF"""
# nimib :whale: - nim :crown: driven :sailboat: publishing :writingHand:
Expand Down Expand Up @@ -65,13 +65,13 @@ provides syntax highlighting of embedded languages in nimib documents (eg. markd

First have a look at the following html document: [hello.html]({docs}/hello.html)

This was produced with `nim r {nb.cfg.srcDir}/hello`, where [{nb.cfg.srcDir}/hello.nim]({repo}/blob/main/{nb.cfg.srcDir}/hello.nim) is:
This was produced with `nim r {nb.doc.cfg.srcDir}/hello`, where [{nb.doc.cfg.srcDir}/hello.nim]({repo}/blob/main/{nb.doc.cfg.srcDir}/hello.nim) is:
""".emojize


when not defined(mdOutput):
nbCode: discard
nb.blk.code = hello # "\n" should not be needed here (fix required in rendering)
nb.blk.NbCode.code = hello # "\n" should not be needed here (fix required in rendering)
else:
nbText &"""
```nim
Expand Down Expand Up @@ -124,7 +124,7 @@ The following are the main elements of a default nimib document:
* (optional) latex rendering through [katex](https://katex.org/) (see below)
* a header with navigation to a home page, a minimal title and an automatic detection of github repo (with link)
* a footer with a "made with nimib" line and a `Show source` button that shows the full source to create the document.
* (optional) possibility to create a markdown version of the same document (see this document for an example: [{nb.cfg.srcDir}/index.nim]({repo}/blob/main/{nb.cfg.srcDir}/index.nim))
* (optional) possibility to create a markdown version of the same document (see this document for an example: [{nb.doc.cfg.srcDir}/index.nim]({repo}/blob/main/{nb.doc.cfg.srcDir}/index.nim))

Customization over the default is mostly achieved through nim-mustache or changing
`NbDoc` and `NbBlock` elements (see below api).
Expand Down Expand Up @@ -210,7 +210,7 @@ All the options available can be seen by running any nimib file with option `nbH

nbCode:
import osproc
withDir nb.srcDir:
withDir nb.doc.srcDir:
echo execProcess("nim r --verbosity:0 --hints:off --warnings:off hello --nbHelp")

let renderProcType = "type NbRenderProc = proc (doc: var NbDoc, blk: var NbBlock) {. nimcall .}"
Expand Down Expand Up @@ -338,7 +338,7 @@ and they tell me [imitation is the sincerest form of flattery](https://www.goodr
""".emojize

when defined(mdOutput):
nb.filename = "../README.md"
nb.doc.filename = "../README.md"
nbSave
else:
nbSave
12 changes: 6 additions & 6 deletions docsrc/nolan.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import nimib
import strutils, sequtils, strformat
import strutils, sequtils, strformat, json

nbInit
nbText: """
Expand All @@ -11,7 +11,7 @@ nbText: """
>
> It reorders a **Limerick** according to Nolan's [Memento film structure](https://en.wikipedia.org/wiki/Memento_(film)#Film_structure).
"""
let filename_variant = nb.filename.replace(".html", "_no_source.html")
let filename_variant = nb.doc.filename.replace(".html", "_no_source.html")
let iChange = nb.blocks.len
nbText:fmt"""
> Click on `Show Source` at the bottom to see the nim file that generates this document.
Expand Down Expand Up @@ -48,11 +48,11 @@ nbSave
# save another document without source to test the opt-out of Show Source feature
nbText: "---\n> This is how we can remove the `Show Source` functionality"
nbCode:
nb.context["no_source"] = true
nb.doc.context["no_source"] = %true
# we will generate this last block to change a previous block and then remove it
nbText:fmt"""
> To see the variant with Show Source [click here]({(nb.filename.AbsoluteFile).relPath})
> To see the variant with Show Source [click here]({(nb.doc.filename.AbsoluteFile).relPath})
"""
nb.blocks[iChange] = nbDoc.blocks.pop
nb.filename = filename_variant
nb.blocks[iChange] = nb.blocks.pop
nb.doc.filename = filename_variant
nbSave
22 changes: 11 additions & 11 deletions docsrc/numerical.nim
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#remember to run also with -d:numericalDefaultStyle
import nimib, strformat, strutils
import nimib, strformat, strutils, json
nbInit
nb.useLatex
let
filename_default_style = nb.filename.replace(".html", "_default_style.html")
default_style = nb.context["stylesheet"]
filename_default_style = nb.doc.filename.replace(".html", "_default_style.html")
default_style = nb.doc.context["stylesheet"]

nb.context["stylesheet"] = """<link rel="stylesheet" href="https://latex.now.sh/style.css">"""
nb.doc.context["stylesheet"] = %"""<link rel="stylesheet" href="https://latex.now.sh/style.css">"""

proc introText(defaultStyle = false): string =
let otherStyle = if defaultStyle:
fmt"; _you are looking at default style, for custom style [click here]({(nb.filename.AbsoluteFile).relPath})_"
fmt"; _you are looking at default style, for custom style [click here]({(nb.doc.filename.AbsoluteFile).relPath})_"
else:
fmt"; _for default style [click here]({(filename_default_style.AbsoluteFile).relPath})_"

Expand All @@ -26,7 +26,7 @@ proc introText(defaultStyle = false): string =

nbText: introText()
# save to change later
var blockIntroText = nb.blk
var blockIntroText = nb.blk.NbText

nbText: fmt"""# Using NumericalNim

Expand Down Expand Up @@ -128,17 +128,17 @@ $h=0.01$ | {pe y3hn[^1]} | {pe y3rk[^1]}

and here is the code for this Markdown block:
""" # right alignment of numbers does not seem to work. is this an issue of latex.css?
let mdCode = nb.blk.code
let mdCode = nb.blk.NbTextWithCode.code
nbCode:
discard
nb.blk.code = mdCode
nb.blk.NbCode.code = mdCode
# add a show Markdown source. It would be nice when hovering a code block to show (on the side? how to do it on mobile?) the call code (nbText: or other)

# first save with latex style
nbSave

# then save with default style
blockIntroText.output = introText(default_style=true)
nb.filename = filename_default_style
nb.context["stylesheet"] = default_style
blockIntroText.text = introText(default_style=true)
nb.doc.filename = filename_default_style
nb.doc.context["stylesheet"] = default_style
nbSave
5 changes: 3 additions & 2 deletions docsrc/pythno.nim
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ nbCode:

echo repeat("ThisIsNotPython", 6)

nb.blk.context["output"] = camel2snake(nb.blk.output)
nb.blk.code = nb.blk.code.multiReplace([
let codeBlock = nb.blk.NbCode
codeBlock.output = camel2snake(codeBlock.output)
codeBlock.code = codeBlock.code.multiReplace([
("proc", "def"),
("string", "str"),
(";", ","), # tricky one
Expand Down
5 changes: 2 additions & 3 deletions nimib.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ task readme, "update readme":

task docs, "Build documentation":
for file in ["allblocks", "hello", "mostaccio", "numerical", "nolan",
"pythno", "cheatsheet", "files", "index",
"interactivity", "counters", "caesar"]:
"pythno", "index", "cheatsheet", "files", "interactivity", "counters", "caesar"]:
exec "nim r --hints:off docsrc/" & file & ".nim"
when not defined(nimibDocsSkipPenguins):
exec "nim r --hints:off docsrc/penguins.nim"
exec "nimble readme"
#exec "nimble readme"
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()
Loading
Loading