diff --git a/docsrc/allblocks.nim b/docsrc/allblocks.nim index 3d9f3ed7..6ac62112 100644 --- a/docsrc/allblocks.nim +++ b/docsrc/allblocks.nim @@ -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. diff --git a/src/nimib.nim b/src/nimib.nim index 0745658b..b2be22e5 100644 --- a/src/nimib.nim +++ b/src/nimib.nim @@ -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