Skip to content

Commit

Permalink
Add nbFile overload for file embedding (#219)
Browse files Browse the repository at this point in the history
* Add nbFile overload for file embedding

This also adds an example

* Update src/nimib.nim

Co-authored-by: Pietro Peterlongo <[email protected]>

---------

Co-authored-by: Pietro Peterlongo <[email protected]>
  • Loading branch information
PhilippMDoerner and pietroppeter authored Oct 29, 2023
1 parent dc89060 commit e256687
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
11 changes: 10 additions & 1 deletion docsrc/allblocks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,22 @@ nimibCode:

nbCodeBlock: "nbFile"
nbText: """
`nbFile` saves the content of the block into a file. It takes two arguments: the name of the file and the content of the file.
`nbFile` can save the contents of block into a file or display the contents of a file.
To save to a file it takes two arguments: the name of the file and the content of the file.
The content can be a string or a code block.
"""
nimibCode:
nbFile("exampleCode.nim"):
echo "This code will be saved in the exampleCode.nim file."

nbText: """
To display a file, it takes one argument: the file's path.
"""
nimibCode:
nbFile("../LICENSE")

nbCodeBlock: "nbRawHtml"
nbText: """
Certain things are not doable with pure Markdown. You can use raw HTML directly with the `nbRawHtml` block.
Expand Down
7 changes: 7 additions & 0 deletions src/nimib.nim
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ template nbFile*(name: string, body: untyped) =
nb.blk.context["ext"] = name.getExt
nb.blk.context["content"] = nb.blk.code

template nbFile*(name: string) =
## Read content from a file instead of writing to it
newNbSlimBlock("nbFile"):
nb.blk.context["filename"] = name
nb.blk.context["ext"] = name.getExt
nb.blk.context["content"] = readFile(name)

when moduleAvailable(nimpy):
template nbInitPython*() =
import nimpy
Expand Down

0 comments on commit e256687

Please sign in to comment.