diff --git a/docsrc/allblocks.nim b/docsrc/allblocks.nim index 6ac62112..cd34fb65 100644 --- a/docsrc/allblocks.nim +++ b/docsrc/allblocks.nim @@ -17,7 +17,7 @@ template nbCodeBlock(name:string) = nbToc.output.add "1. " & name & "\n" nbText: """ -> This nimib document provides a brief description and example for +> This nimib document provides a brief description and example for > all blocks in nimib. """.emojize @@ -32,8 +32,8 @@ The markdown syntax is explained in the [Markdown Cheatsheet](./cheatsheet.html) nimibCode: nbText: """ #### Markdown Example - My **text** is *formatted* with the - [Markdown](./cheatsheet.html) syntax. + My **text** is *formatted* with the + [Markdown](https://pietroppeter.github.io/nimib/cheatsheet.html) syntax. """ nbCodeBlock: "nbCode" @@ -46,6 +46,15 @@ nimibCode: nbCode: echo "Hello World!" +nbText: """ +You can also display the line numbers on the left thanks to the global document switch: +""" + +nimibCode: + enableLineNumbersDoc() + nbCode: + echo "Hello World!" + nbCodeBlock: "nbCodeSkip" nbText: """ Similar to `nbCode`, `nbCodeSkip` is a block that displays @@ -95,7 +104,7 @@ nimibCode: nbCodeBlock: "nbTextWithCode" nbText: """ -`nbText` only stores the string it is given, but it doesn't store the code passed to `nbText`. For example, `nbText: fmt"{1+1}"` only stores the string `"2"` but not the code `fmt"{1+1}"` that produced that string. `nbTextWithCode` works like `nbText` but it also stores the code in the created block. It can be accessed with `nb.blk.code` right after the `nbTextWithCode` call. See the end of +`nbText` only stores the string it is given, but it doesn't store the code passed to `nbText`. For example, `nbText: fmt"{1+1}"` only stores the string `"2"` but not the code `fmt"{1+1}"` that produced that string. `nbTextWithCode` works like `nbText` but it also stores the code in the created block. It can be accessed with `nb.blk.code` right after the `nbTextWithCode` call. See the end of [numerical](./numerical.html) for an example. """ @@ -110,7 +119,7 @@ nimibCode: nbCodeBlock: "nbFile" nbText: """ -`nbFile` can save the contents of block into a file or display the contents of a 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. diff --git a/nimib.nimble b/nimib.nimble index cbdb98d1..9ed831e0 100644 --- a/nimib.nimble +++ b/nimib.nimble @@ -19,13 +19,13 @@ task docsdeps, "install dependendencies required for doc building": exec "nimble -y install ggplotnim@0.5.9 numericalnim@0.8.8 nimoji nimpy karax@1.2.2 happyx@2.0.0" task test, "General tests": - for file in ["tsources.nim", "tblocks.nim", "tnimib.nim", "trenders.nim"]: + for file in ["tsources.nim", "tblocks.nim", "tnimib.nim", "trenders.nim", "tcodeLines.nim"]: exec "nim r --hints:off tests/" & file for file in ["tblocks.nim", "tnimib.nim", "trenders.nim"]: exec "nim r --hints:off -d:nimibCodeFromAst tests/" & file task readme, "update readme": - exec "nim -d:mdOutput r docsrc/index.nim" + exec "nim -d:mdOutput r docsrc/index.nim" task docs, "Build documentation": for file in ["allblocks", "hello", "mostaccio", "numerical", "nolan", @@ -34,4 +34,4 @@ task docs, "Build documentation": exec "nim r --hints:off docsrc/" & file & ".nim" when not defined(nimibDocsSkipPenguins): exec "nim r --hints:off docsrc/penguins.nim" - exec "nimble readme" + exec "nimble readme" diff --git a/src/nimib.nim b/src/nimib.nim index b2be22e5..e0aa4901 100644 --- a/src/nimib.nim +++ b/src/nimib.nim @@ -58,7 +58,7 @@ template nbInit*(theme = themes.useDefault, backend = renders.useHtmlBackend, th # apply theme theme nb -template nbInitMd*(thisFileRel = "") = +template nbInitMd*(thisFileRel = "") = var tfr = if thisFileRel == "": instantiationInfo(-1).filename else: @@ -69,6 +69,12 @@ template nbInitMd*(thisFileRel = "") = if nb.options.filename == "": nb.filename = nb.filename.splitFile.name & ".md" +template enableLineNumbersDoc* = + nb.context["enableLineNumbers"] = true + +template enableLineNumbersBlock* = + nb.blk.context["enableLineNumbers"] = true + # block generation templates template newNbCodeBlock*(cmd: string, body, blockImpl: untyped) = newNbBlock(cmd, true, nb, nb.blk, body, blockImpl) @@ -87,6 +93,11 @@ template nbCodeSkip*(body: untyped) = newNbCodeBlock("nbCodeSkip", body): discard +template nbCodeWithNumbers*(body: untyped) = + newNbCodeBlock("nbCode", body): + captureStdout(nb.blk.output): + body + template nbCapture*(body: untyped) = newNbCodeBlock("nbCapture", body): captureStdout(nb.blk.output): @@ -117,13 +128,13 @@ template nbImage*(url: string, caption = "", alt = "") = url else: nb.context["path_to_root"].vString / url - - nb.blk.context["alt_text"] = + + nb.blk.context["alt_text"] = if alt == "": caption else: alt - + nb.blk.context["caption"] = caption template nbFile*(name: string, content: string) = @@ -162,7 +173,7 @@ when moduleAvailable(nimpy): template nbShow*(obj: untyped) = nbRawHtml(obj.toHtml()) -template nbRawOutput*(content: string) {.deprecated: "Use nbRawHtml instead".} = +template nbRawOutput*(content: string) {.deprecated: "Use nbRawHtml instead".} = nbRawHtml(content) template nbRawHtml*(content: string) = diff --git a/src/nimib/renders.nim b/src/nimib/renders.nim index cbcd9b79..06f17c8c 100644 --- a/src/nimib/renders.nim +++ b/src/nimib/renders.nim @@ -1,4 +1,4 @@ -import std / [strutils, tables, sugar, os, strformat, sequtils] +import std / [strutils, tables, sugar, sequtils] import ./types, ./jsutils, markdown, mustache import highlight @@ -10,6 +10,17 @@ proc mdOutputToHtml(doc: var NbDoc, blk: var NbBlock) = proc highlightCode(doc: var NbDoc, blk: var NbBlock) = blk.context["codeHighlighted"] = highlightNim(blk.code) +proc addLineNumbersToHighlightedCode(code: string): string = + let nlines = code.splitLines().len + result.add("""""" & $1 & "\n") + for i in 2..") + result.add(code) + +proc addLineNumbers(doc: var NbDoc, blk: var NbBlock) = + if blk.context["enableLineNumbers"].castBool or doc.context["enableLineNumbers"].castBool: + blk.context["codeHighlighted"] = addLineNumbersToHighlightedCode(blk.context["codeHighlighted"].castStr) proc useHtmlBackend*(doc: var NbDoc) = doc.partials["nbText"] = "{{&outputToHtml}}" @@ -45,14 +56,15 @@ proc useHtmlBackend*(doc: var NbDoc) = # I prefer to initialize here instead of in nimib (each backend should re-initialize) doc.renderPlans = initTable[string, seq[string]]() doc.renderPlans["nbText"] = @["mdOutputToHtml"] - doc.renderPlans["nbCode"] = @["highlightCode"] # default partial automatically escapes output (code is escaped when highlighting) - doc.renderPlans["nbCodeSkip"] = @["highlightCode"] + doc.renderPlans["nbCode"] = @["highlightCode", "addLineNumbers"] # default partial automatically escapes output (code is escaped when highlighting) + doc.renderPlans["nbCodeSkip"] = @["highlightCode", "addLineNumbers"] doc.renderPlans["nbJsFromCodeOwnFile"] = @["compileNimToJs", "highlightCode"] doc.renderPlans["nbJsFromCode"] = @["highlightCode"] doc.renderPlans["nimibCode"] = doc.renderPlans["nbCode"] doc.renderProcs = initTable[string, NbRenderProc]() doc.renderProcs["mdOutputToHtml"] = mdOutputToHtml + doc.renderProcs["addLineNumbers"] = addLineNumbers doc.renderProcs["highlightCode"] = highlightCode doc.renderProcs["compileNimToJs"] = compileNimToJs diff --git a/tests/tcodeLines.nim b/tests/tcodeLines.nim new file mode 100644 index 00000000..6dad9c23 --- /dev/null +++ b/tests/tcodeLines.nim @@ -0,0 +1,11 @@ +import nimib +import unittest + +suite "test sources": + nbInit() + enableLineNumbersDoc() + nbCode: + # a comment + let + x = 1 + nbSave()