Skip to content

Commit

Permalink
add nbCodeDisplay and nbCodeAnd (#158)
Browse files Browse the repository at this point in the history
* add nbCodeDisplay

* add nbCodeAnd

* add tests for nbCodeDisplay and nbCodeAnd

* document nbCodeDisplay and nbCodeAnd

* deprecate nbJsShowSource

* bump version and update changelog
  • Loading branch information
pietroppeter authored Nov 29, 2022
1 parent 8ad307d commit ad0e44b
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 3 deletions.
9 changes: 9 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ When contributing a fix, feature or example please add a new line to briefly exp
## 0.3.x
* _add next change here_

## 0.3.4

* added `nbCodeDisplay` and `nbCodeAnd` (#158).
They provide an easy way to:
- display code in `nbJsFromCode` and friends
- run code both in js and c backend
They are also generic templates that can be used with other templates
and do not depend on any specificities of `nbJsFromCode` templates.

## 0.3.3

* Refactored nbJs (#148)
Expand Down
30 changes: 30 additions & 0 deletions docsrc/interactivity.nim
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,36 @@ karaxExample()

nbText: "Another example on how to use `nbKaraxCode` can be found in the [caesar document](./caesar.html) by clicking the `Show Source` button at the bottom."

nbText: hlMd"""
## nbCodeDisplay and nbCodeAnd
We introduce in this section two generic templates that can be useful when used with the
templates of `nbJsFromCode` family.
### Display code in nbJsFromCode with nbCodeDisplay
If you wish to display the code used in one of `nbJsFromCode`, `nbJsFromCodeInBlock`, `nbJsFromCodeGlobal`
you can use `nbCodeDisplay` (which can be used in general with any template that does not show code by itself):
"""
nimibCode:
nbCodeDisplay(nbJsFromCodeInBlock):
echo "hi nbCodeDisplay"
nbText: hlMd"""
Note that in this same document we gave examples of two other methods
to show code:
- `nimibCode`: to show the code as you would use it in a nimib file
- `nbCode` + template: create a template (e.g. `karaxExample`) inside a `nbCode` and call the template later.
### Running the same code with both c and js backends using nbCodeAnd
If you want to run some code both in C and js backends, you can use `nbCodeAnd`:
"""
nimibCode:
nbCodeAnd(nbJsFromCodeInBlock):
echo "hi nbCodeAnd"

nbText: hlMd"""
## Internal workings
### nbJsFromCode
Expand Down
2 changes: 1 addition & 1 deletion nimib.nimble
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Package

version = "0.3.3"
version = "0.3.4"
author = "Pietro Peterlongo"
description = "nimib 🐳 - nim 👑 driven ⛵ publishing ✍"
license = "MIT"
Expand Down
17 changes: 15 additions & 2 deletions src/nimib.nim
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,27 @@ when moduleAvailable(karax/kbase):
nbRawHtml: "<div id=\"" & rootId & "\"></div>"
nbKaraxCodeBackend(rootId, args)

template nbJsShowSource*(message: string = "") =
template nbJsShowSource*(message: string = "") {.deprecated: "Use nbCodeDisplay instead".} =
nb.blk.context["js_show_nim_source"] = true
if message.len > 0:
nb.blk.context["js_show_nim_source_message"] = message

template nbCodeToJsShowSource*(message: string = "") {.deprecated: "Use nbJsShowSource instead".} =
template nbCodeToJsShowSource*(message: string = "") {.deprecated: "Use nbCodeDisplay instead".} =
nbJsShowSource(message)

template nbCodeDisplay*(tmplCall: untyped, body: untyped) =
## display codes used in a template (e.g. nbJsFromCode) after the template call
tmplCall:
body
newNbCodeBlock("nbCode", body):
discard

template nbCodeAnd*(tmplCall: untyped, body: untyped) =
## can be used to run code both in c and js backends (e.g. nbCodeAnd(nbJsFromCode))
nbCode: # this should work because template name starts with nbCode
body
tmplCall:
body

template nbClearOutput*() =
if not nb.blk.isNil:
Expand Down
27 changes: 27 additions & 0 deletions tests/tnimib.nim
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,30 @@ when moduleAvailable(karax/kbase):
text message
check nb.blk.code.len > 0
check nb.blk.context["transformedCode"].vString.len > 0

test "nbCodeDisplay":
nbCodeDisplay(nbJsFromCode):
import p5
echo "hi p5"
draw:
ellipse(mouseX, mouseY, 20)
check nb.blocks[^1].command == "nbCode"
check nb.blocks[^2].command == "nbJsFromCode"
check nb.blocks[^2].context["transformedCode"].vString.len > 0
check "ellipse(mouseX, mouseY, 20)" in nb.blocks[^2].context["transformedCode"].vString
when defined(nimibCodeFromAst):
check nb.blocks[^1].code.startsWith("import\n p5")
else:
check nb.blocks[^1].code.startsWith("import p5")
check nb.blocks[^1].output == ""

test "nbCodeAnd":
nbCodeAnd(nbJsFromCode):
let you = "me"
echo "hi ", you
check nb.blocks[^2].command == "nbCode"
check nb.blocks[^1].command == "nbJsFromCode"
check nb.blocks[^1].context["transformedCode"].vString.len > 0
check "you = \"me\"" in nb.blocks[^1].context["transformedCode"].vString
check nb.blocks[^2].code.startsWith("let you =")
check nb.blocks[^2].output == "hi me\n"

0 comments on commit ad0e44b

Please sign in to comment.