Skip to content

Commit e256687

Browse files
Add nbFile overload for file embedding (#219)
* 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]>
1 parent dc89060 commit e256687

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

docsrc/allblocks.nim

+10-1
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,22 @@ nimibCode:
110110

111111
nbCodeBlock: "nbFile"
112112
nbText: """
113-
`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.
113+
`nbFile` can save the contents of block into a file or display the contents of a file.
114+
115+
To save to a file it takes two arguments: the name of the file and the content of the file.
114116
The content can be a string or a code block.
115117
"""
116118
nimibCode:
117119
nbFile("exampleCode.nim"):
118120
echo "This code will be saved in the exampleCode.nim file."
119121

122+
nbText: """
123+
124+
To display a file, it takes one argument: the file's path.
125+
"""
126+
nimibCode:
127+
nbFile("../LICENSE")
128+
120129
nbCodeBlock: "nbRawHtml"
121130
nbText: """
122131
Certain things are not doable with pure Markdown. You can use raw HTML directly with the `nbRawHtml` block.

src/nimib.nim

+7
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,13 @@ template nbFile*(name: string, body: untyped) =
141141
nb.blk.context["ext"] = name.getExt
142142
nb.blk.context["content"] = nb.blk.code
143143

144+
template nbFile*(name: string) =
145+
## Read content from a file instead of writing to it
146+
newNbSlimBlock("nbFile"):
147+
nb.blk.context["filename"] = name
148+
nb.blk.context["ext"] = name.getExt
149+
nb.blk.context["content"] = readFile(name)
150+
144151
when moduleAvailable(nimpy):
145152
template nbInitPython*() =
146153
import nimpy

0 commit comments

Comments
 (0)