From 1a0602cf2f25bc2d359134150ec45ffa2eee2d10 Mon Sep 17 00:00:00 2001 From: Pietro Peterlongo Date: Fri, 28 May 2021 06:53:39 +0200 Subject: [PATCH 1/6] move book/bookcfg.nim -> src/nimibook/cfg.nim --- book/basics/data_manipulation.nim | 2 +- book/basics/index.nim | 2 +- book/basics/models.nim | 2 +- book/basics/plotting.nim | 2 +- book/index.nim | 2 +- book/misc/but/very/far/contributors.nim | 2 +- book/bookcfg.nim => src/nimibook/cfg.nim | 0 src/nimibook/pubs.nim | 4 ++-- 8 files changed, 8 insertions(+), 8 deletions(-) rename book/bookcfg.nim => src/nimibook/cfg.nim (100%) diff --git a/book/basics/data_manipulation.nim b/book/basics/data_manipulation.nim index 8d65915..d9bf2ae 100644 --- a/book/basics/data_manipulation.nim +++ b/book/basics/data_manipulation.nim @@ -1,4 +1,4 @@ -import nimib, bookcfg +import nimib, nimibook / cfg nbInit nbDoc.applyCfg nbText: """## Data Manipulation diff --git a/book/basics/index.nim b/book/basics/index.nim index e596286..30e04a0 100644 --- a/book/basics/index.nim +++ b/book/basics/index.nim @@ -1,4 +1,4 @@ -import nimib, bookcfg +import nimib, nimibook / cfg nbInit nbDoc.applyCfg nbText: """# Basics diff --git a/book/basics/models.nim b/book/basics/models.nim index 96a5772..23d31da 100644 --- a/book/basics/models.nim +++ b/book/basics/models.nim @@ -1,4 +1,4 @@ -import nimib, bookcfg +import nimib, nimibook / cfg nbInit nbDoc.applyCfg nbText: """## Models diff --git a/book/basics/plotting.nim b/book/basics/plotting.nim index b0710ad..cd646ad 100644 --- a/book/basics/plotting.nim +++ b/book/basics/plotting.nim @@ -1,4 +1,4 @@ -import nimib, bookcfg +import nimib, nimibook / cfg nbInit nbDoc.applyCfg nbText: """## Plotting diff --git a/book/index.nim b/book/index.nim index 03ca942..00fe19a 100644 --- a/book/index.nim +++ b/book/index.nim @@ -1,4 +1,4 @@ -import nimib, bookcfg +import nimib, nimibook / cfg nbInit nbDoc.applyCfg nbText: """# Introduction diff --git a/book/misc/but/very/far/contributors.nim b/book/misc/but/very/far/contributors.nim index 0846f44..0a860ba 100644 --- a/book/misc/but/very/far/contributors.nim +++ b/book/misc/but/very/far/contributors.nim @@ -1,4 +1,4 @@ -import nimib, bookcfg +import nimib, nimibook / cfg nbInit nbDoc.applyCfg nbText: """# Contributors diff --git a/book/bookcfg.nim b/src/nimibook/cfg.nim similarity index 100% rename from book/bookcfg.nim rename to src/nimibook/cfg.nim diff --git a/src/nimibook/pubs.nim b/src/nimibook/pubs.nim index 134291c..384d2a0 100644 --- a/src/nimibook/pubs.nim +++ b/src/nimibook/pubs.nim @@ -1,7 +1,7 @@ import std / [os, strutils] -import nimibook / [types, tocs] -import nimib, ../../book/bookcfg +import nimibook / [types, tocs, cfg] +import nimib proc nimPublish*(entry: Entry) = let From 6eb11f8e9031ceaf98bb0e91a672daed37aadab1 Mon Sep 17 00:00:00 2001 From: Pietro Peterlongo Date: Fri, 28 May 2021 07:11:15 +0200 Subject: [PATCH 2/6] rename nimibook/cfg to nimibook/defaults + add prelude --- book/basics/data_manipulation.nim | 4 +--- book/basics/index.nim | 4 +--- book/basics/models.nim | 4 +--- book/basics/plotting.nim | 4 +--- book/index.nim | 4 +--- book/misc/but/very/far/contributors.nim | 4 +--- src/nimibook.nim | 4 ++-- src/nimibook/{cfg.nim => defaults.nim} | 2 +- src/nimibook/prelude.nim | 3 +++ src/nimibook/pubs.nim | 4 ++-- 10 files changed, 14 insertions(+), 23 deletions(-) rename src/nimibook/{cfg.nim => defaults.nim} (98%) create mode 100644 src/nimibook/prelude.nim diff --git a/book/basics/data_manipulation.nim b/book/basics/data_manipulation.nim index d9bf2ae..70feae0 100644 --- a/book/basics/data_manipulation.nim +++ b/book/basics/data_manipulation.nim @@ -1,6 +1,4 @@ -import nimib, nimibook / cfg -nbInit -nbDoc.applyCfg +include nimibook / prelude nbText: """## Data Manipulation I like less data manipulation diff --git a/book/basics/index.nim b/book/basics/index.nim index 30e04a0..248260d 100644 --- a/book/basics/index.nim +++ b/book/basics/index.nim @@ -1,6 +1,4 @@ -import nimib, nimibook / cfg -nbInit -nbDoc.applyCfg +include nimibook / prelude nbText: """# Basics Better Basic than Acid. diff --git a/book/basics/models.nim b/book/basics/models.nim index 23d31da..e0d251b 100644 --- a/book/basics/models.nim +++ b/book/basics/models.nim @@ -1,6 +1,4 @@ -import nimib, nimibook / cfg -nbInit -nbDoc.applyCfg +include nimibook / prelude nbText: """## Models I can fake understanding models diff --git a/book/basics/plotting.nim b/book/basics/plotting.nim index cd646ad..daca248 100644 --- a/book/basics/plotting.nim +++ b/book/basics/plotting.nim @@ -1,6 +1,4 @@ -import nimib, nimibook / cfg -nbInit -nbDoc.applyCfg +include nimibook / prelude nbText: """## Plotting I like data visualization diff --git a/book/index.nim b/book/index.nim index 00fe19a..86481aa 100644 --- a/book/index.nim +++ b/book/index.nim @@ -1,6 +1,4 @@ -import nimib, nimibook / cfg -nbInit -nbDoc.applyCfg +include nimibook / prelude nbText: """# Introduction I should say something very wise about this book. diff --git a/book/misc/but/very/far/contributors.nim b/book/misc/but/very/far/contributors.nim index 0a860ba..8cd007b 100644 --- a/book/misc/but/very/far/contributors.nim +++ b/book/misc/but/very/far/contributors.nim @@ -1,6 +1,4 @@ -import nimib, nimibook / cfg -nbInit -nbDoc.applyCfg +include nimibook / prelude nbText: """# Contributors Clonkk with 2 k, pietroppeter with 3 p, HugoGranstrom with 2 r. diff --git a/src/nimibook.nim b/src/nimibook.nim index df3eafe..8b5ea17 100644 --- a/src/nimibook.nim +++ b/src/nimibook.nim @@ -1,2 +1,2 @@ -import nimibook / [types, render, tocs, pubs] -export types, render, tocs, pubs +import nimibook / [types, render, tocs, pubs, defaults] +export types, render, tocs, pubs, defaults diff --git a/src/nimibook/cfg.nim b/src/nimibook/defaults.nim similarity index 98% rename from src/nimibook/cfg.nim rename to src/nimibook/defaults.nim index 1ae1b82..cf867fa 100644 --- a/src/nimibook/cfg.nim +++ b/src/nimibook/defaults.nim @@ -1,7 +1,7 @@ import nimib, strutils, os, nimib / paths import nimibook / [types, tocs, render, entries] -proc applyCfg*(nbDoc: var NbDoc) = +proc useNimibook*(nbDoc: var NbDoc) = let nbThisFile = changeFileExt(nbDoc.filename.AbsoluteFile, ".nim") thisTuple = nbThisFile.splitFile diff --git a/src/nimibook/prelude.nim b/src/nimibook/prelude.nim new file mode 100644 index 0000000..83793e9 --- /dev/null +++ b/src/nimibook/prelude.nim @@ -0,0 +1,3 @@ +import nimib, nimibook / defaults +nbInit +nbDoc.useNimibook \ No newline at end of file diff --git a/src/nimibook/pubs.nim b/src/nimibook/pubs.nim index 384d2a0..30a6664 100644 --- a/src/nimibook/pubs.nim +++ b/src/nimibook/pubs.nim @@ -1,6 +1,6 @@ import std / [os, strutils] -import nimibook / [types, tocs, cfg] +import nimibook / [types, tocs, defaults] import nimib proc nimPublish*(entry: Entry) = @@ -14,7 +14,7 @@ proc nimPublish*(entry: Entry) = proc mdPublish*(entry: Entry) = nbInit nbDoc.filename = (nbThisDir / ("../../" & entry.path).RelativeDir).string - nbDoc.applyCfg + nbDoc.useNimibook withDir(nbHomeDir / "..".RelativeDir): nbText entry.path.readFile nbSave From 17a0159a529d4f9064762898468168b23c101d38 Mon Sep 17 00:00:00 2001 From: Pietro Peterlongo Date: Sat, 29 May 2021 08:10:37 +0200 Subject: [PATCH 3/6] rename pubs -> publishers --- src/nimibook.nim | 4 ++-- src/nimibook/publishers.nim | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 src/nimibook/publishers.nim diff --git a/src/nimibook.nim b/src/nimibook.nim index 8b5ea17..eff18c3 100644 --- a/src/nimibook.nim +++ b/src/nimibook.nim @@ -1,2 +1,2 @@ -import nimibook / [types, render, tocs, pubs, defaults] -export types, render, tocs, pubs, defaults +import nimibook / [types, render, tocs, publishers, defaults] +export types, render, tocs, publishers, defaults diff --git a/src/nimibook/publishers.nim b/src/nimibook/publishers.nim new file mode 100644 index 0000000..d80990d --- /dev/null +++ b/src/nimibook/publishers.nim @@ -0,0 +1,36 @@ +import std / [os, strutils] +import nimibook / [types, tocs, defaults] +import nimib + +proc nimPublish*(entry: Entry) = + let + cmd = "nim" + args = ["r", "-d:release", entry.path] + debugEcho "[Executing] ", cmd, " ", args.join(" ") + if execShellCmd(cmd & " " & args.join(" ")) != 0: + quit(1) + +proc mdPublish*(entry: Entry) = + nbInit + nbDoc.filename = (nbThisDir / ("../../" & entry.path).RelativeDir).string + nbDoc.useNimibook + withDir(nbHomeDir / "..".RelativeDir): + nbText entry.path.readFile + nbSave + setCurrentDir nbInitDir # reset current directory + +proc publish*(entry: Entry) = + let splitted = entry.path.splitFile() + if splitted.ext == ".nim": + nimPublish(entry) + elif splitted.ext == ".md": + mdPublish(entry) + else: + raise newException(IOError, "Error invalid file extension.") + +proc publish*(toc: Toc) = + dump toc + for entry in toc.entries: + entry.publish() + clean toc + check(toc) From d51e8356531757276d182389768510ee7a4df807 Mon Sep 17 00:00:00 2001 From: Pietro Peterlongo Date: Sat, 29 May 2021 08:10:50 +0200 Subject: [PATCH 4/6] rename pubs -> publishers --- src/nimibook/pubs.nim | 37 ------------------------------------- 1 file changed, 37 deletions(-) delete mode 100644 src/nimibook/pubs.nim diff --git a/src/nimibook/pubs.nim b/src/nimibook/pubs.nim deleted file mode 100644 index 30a6664..0000000 --- a/src/nimibook/pubs.nim +++ /dev/null @@ -1,37 +0,0 @@ - -import std / [os, strutils] -import nimibook / [types, tocs, defaults] -import nimib - -proc nimPublish*(entry: Entry) = - let - cmd = "nim" - args = ["r", "-d:release", entry.path] - debugEcho "[Executing] ", cmd, " ", args.join(" ") - if execShellCmd(cmd & " " & args.join(" ")) != 0: - quit(1) - -proc mdPublish*(entry: Entry) = - nbInit - nbDoc.filename = (nbThisDir / ("../../" & entry.path).RelativeDir).string - nbDoc.useNimibook - withDir(nbHomeDir / "..".RelativeDir): - nbText entry.path.readFile - nbSave - setCurrentDir nbInitDir # reset current directory - -proc publish*(entry: Entry) = - let splitted = entry.path.splitFile() - if splitted.ext == ".nim": - nimPublish(entry) - elif splitted.ext == ".md": - mdPublish(entry) - else: - raise newException(IOError, "Error invalid file extension.") - -proc publish*(toc: Toc) = - dump toc - for entry in toc.entries: - entry.publish() - clean toc - check(toc) From f19bd3e343cfb11a0d84075ae18039431758a3b5 Mon Sep 17 00:00:00 2001 From: Pietro Peterlongo Date: Sat, 29 May 2021 08:11:06 +0200 Subject: [PATCH 5/6] standardize imports --- src/nimibook/defaults.nim | 3 ++- src/nimibook/entries.nim | 2 +- src/nimibook/render.nim | 2 +- src/nimibook/sort.nim | 2 +- src/nimibook/tocs.nim | 2 +- 5 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/nimibook/defaults.nim b/src/nimibook/defaults.nim index cf867fa..69dc821 100644 --- a/src/nimibook/defaults.nim +++ b/src/nimibook/defaults.nim @@ -1,4 +1,5 @@ -import nimib, strutils, os, nimib / paths +import std / [strutils, os] +import nimib, nimib / paths import nimibook / [types, tocs, render, entries] proc useNimibook*(nbDoc: var NbDoc) = diff --git a/src/nimibook/entries.nim b/src/nimibook/entries.nim index 7fbc00b..254a162 100644 --- a/src/nimibook/entries.nim +++ b/src/nimibook/entries.nim @@ -1,5 +1,5 @@ -import nimibook / types import std / [os, strutils, strformat] +import nimibook / types proc url*(e: Entry): string = var path = changeFileExt(e.path, "html").tailDir() diff --git a/src/nimibook/render.nim b/src/nimibook/render.nim index 04c2709..09fe8e5 100644 --- a/src/nimibook/render.nim +++ b/src/nimibook/render.nim @@ -1,4 +1,4 @@ -import std/strformat +import std / strformat import nimibook / [types, entries] const path_to_root = "{{path_to_root}}" diff --git a/src/nimibook/sort.nim b/src/nimibook/sort.nim index 9778eb1..2734345 100644 --- a/src/nimibook/sort.nim +++ b/src/nimibook/sort.nim @@ -1,6 +1,6 @@ # in case you need to sort entries (it can be useful for automatically generated entries) +import std / algorithm import nimibook / types -import std/algorithm proc intcmp(x, y: int): int = if x < y: diff --git a/src/nimibook/tocs.nim b/src/nimibook/tocs.nim index b608314..ccac75a 100644 --- a/src/nimibook/tocs.nim +++ b/src/nimibook/tocs.nim @@ -1,6 +1,6 @@ -import nimibook / [types, entries] import std / [os, strutils, strformat] import jsony +import nimibook / [types, entries] proc inc(levels: var seq[int]) = levels[levels.high] = levels[levels.high] + 1 From f118db01cbd65364389ecd01b6a23a6f353b7595 Mon Sep 17 00:00:00 2001 From: Pietro Peterlongo Date: Sat, 29 May 2021 08:16:19 +0200 Subject: [PATCH 6/6] add task srcpretty and apply it --- nimibook.nimble | 6 ++++++ src/nimibook/defaults.nim | 4 ++-- src/nimibook/prelude.nim | 2 +- src/nimibook/publishers.nim | 2 +- src/nimibook/tocs.nim | 6 +++--- src/nimibook/types.nim | 2 +- 6 files changed, 14 insertions(+), 8 deletions(-) diff --git a/nimibook.nimble b/nimibook.nimble index f0724f0..6bc8636 100644 --- a/nimibook.nimble +++ b/nimibook.nimble @@ -34,3 +34,9 @@ task cleanbook, "cleanbook": echo "removed ", file +task srcpretty, "run nimpretty on nim files in src folder": + for file in walkDirRec("src"): + if file.endsWith(".nim"): + let cmd = "nimpretty --maxLineLen:160 " & file + echo "[executing] ", cmd + exec(cmd) diff --git a/src/nimibook/defaults.nim b/src/nimibook/defaults.nim index 69dc821..1f00163 100644 --- a/src/nimibook/defaults.nim +++ b/src/nimibook/defaults.nim @@ -9,7 +9,7 @@ proc useNimibook*(nbDoc: var NbDoc) = nbThisDir: AbsoluteDir = thisTuple.dir nbHomeDir: AbsoluteDir = findNimbleDir(nbThisDir) / "docs".RelativeDir nbSrcDir = nbHomeDir / RelativeDir(".." / "book") - + nbDoc.filename = relativeTo(changeFileExt(nbThisFile, ".html"), nbSrcDir).string nbDoc.context["here_path"] = (nbThisFile.relativeTo nbSrcDir).string nbDoc.context["home_path"] = (nbSrcDir.relativeTo nbThisDir).string @@ -35,4 +35,4 @@ proc useNimibook*(nbDoc: var NbDoc) = for entry in toc.entries.mitems: if entry.url == nbDoc.filename.replace('\\', '/'): # replace needed for windows entry.isActive = true - nbDoc.partials["toc"] = render toc \ No newline at end of file + nbDoc.partials["toc"] = render toc diff --git a/src/nimibook/prelude.nim b/src/nimibook/prelude.nim index 83793e9..1424a00 100644 --- a/src/nimibook/prelude.nim +++ b/src/nimibook/prelude.nim @@ -1,3 +1,3 @@ import nimib, nimibook / defaults nbInit -nbDoc.useNimibook \ No newline at end of file +nbDoc.useNimibook diff --git a/src/nimibook/publishers.nim b/src/nimibook/publishers.nim index d80990d..ebf33e0 100644 --- a/src/nimibook/publishers.nim +++ b/src/nimibook/publishers.nim @@ -17,7 +17,7 @@ proc mdPublish*(entry: Entry) = withDir(nbHomeDir / "..".RelativeDir): nbText entry.path.readFile nbSave - setCurrentDir nbInitDir # reset current directory + setCurrentDir nbInitDir # reset current directory proc publish*(entry: Entry) = let splitted = entry.path.splitFile() diff --git a/src/nimibook/tocs.nim b/src/nimibook/tocs.nim index ccac75a..1e1e3ae 100644 --- a/src/nimibook/tocs.nim +++ b/src/nimibook/tocs.nim @@ -11,12 +11,12 @@ proc add(toc: var Toc, entry: Entry) = raise newException(IOError, fmt"Error entry {fullpath} doesn't exist.") toc.entries.add entry -proc joinPath(parts: seq[string], tail: string) : string = +proc joinPath(parts: seq[string], tail: string): string = var parts = parts parts.add tail normalizedPath(joinPath(parts)) -proc formatFileName(inputs: tuple[dir: string, name: string, ext: string]) : string = +proc formatFileName(inputs: tuple[dir: string, name: string, ext: string]): string = result = inputs.name if inputs.ext.isEmptyOrWhitespace(): result &= ".nim" @@ -26,7 +26,7 @@ proc formatFileName(inputs: tuple[dir: string, name: string, ext: string]) : str template newToc*(booklabel: string, rootfolder: string, body: untyped): Toc = var toc = Toc(title: booklabel, path: rootfolder) var levels: seq[int] = @[1] - var folders : seq[string] = @[rootfolder] + var folders: seq[string] = @[rootfolder] template entry(label, rfile: string) = # debugEcho "==> entry <==" diff --git a/src/nimibook/types.nim b/src/nimibook/types.nim index d3eb3aa..0ff2eef 100644 --- a/src/nimibook/types.nim +++ b/src/nimibook/types.nim @@ -8,4 +8,4 @@ type Toc* = object title*: string path*: string - entries*: seq[Entry] \ No newline at end of file + entries*: seq[Entry]