From e256687a007f4e45e7f0291d5be96121aea9a470 Mon Sep 17 00:00:00 2001 From: PhilippMDoerner Date: Mon, 30 Oct 2023 00:34:02 +0100 Subject: [PATCH] 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 --------- Co-authored-by: Pietro Peterlongo --- docsrc/allblocks.nim | 11 ++++++++++- src/nimib.nim | 7 +++++++ 2 files changed, 17 insertions(+), 1 deletion(-) 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