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

Add nbFile overload for file embedding #219

Merged
merged 2 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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
Loading