Skip to content

Commit

Permalink
Merge pull request #17 from pietroppeter/move-bookcfg
Browse files Browse the repository at this point in the history
[rfc] refactor bookcfg
  • Loading branch information
pietroppeter authored May 29, 2021
2 parents b56e935 + f118db0 commit 25363e3
Show file tree
Hide file tree
Showing 16 changed files with 34 additions and 37 deletions.
4 changes: 1 addition & 3 deletions book/basics/data_manipulation.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import nimib, bookcfg
nbInit
nbDoc.applyCfg
include nimibook / prelude
nbText: """## Data Manipulation
I like less data manipulation
Expand Down
4 changes: 1 addition & 3 deletions book/basics/index.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import nimib, bookcfg
nbInit
nbDoc.applyCfg
include nimibook / prelude
nbText: """# Basics
Better Basic than Acid.
Expand Down
4 changes: 1 addition & 3 deletions book/basics/models.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import nimib, bookcfg
nbInit
nbDoc.applyCfg
include nimibook / prelude
nbText: """## Models
I can fake understanding models
Expand Down
4 changes: 1 addition & 3 deletions book/basics/plotting.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import nimib, bookcfg
nbInit
nbDoc.applyCfg
include nimibook / prelude
nbText: """## Plotting
I like data visualization
Expand Down
4 changes: 1 addition & 3 deletions book/index.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import nimib, bookcfg
nbInit
nbDoc.applyCfg
include nimibook / prelude
nbText: """# Introduction
I should say something very wise about this book.
Expand Down
4 changes: 1 addition & 3 deletions book/misc/but/very/far/contributors.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import nimib, bookcfg
nbInit
nbDoc.applyCfg
include nimibook / prelude
nbText: """# Contributors
Clonkk with 2 k, pietroppeter with 3 p, HugoGranstrom with 2 r.
Expand Down
6 changes: 6 additions & 0 deletions nimibook.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 2 additions & 2 deletions src/nimibook.nim
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import nimibook / [types, render, tocs, pubs]
export types, render, tocs, pubs
import nimibook / [types, render, tocs, publishers, defaults]
export types, render, tocs, publishers, defaults
9 changes: 5 additions & 4 deletions book/bookcfg.nim → src/nimibook/defaults.nim
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import nimib, strutils, os, nimib / paths
import std / [strutils, os]
import nimib, 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
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
Expand All @@ -34,4 +35,4 @@ proc applyCfg*(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
nbDoc.partials["toc"] = render toc
2 changes: 1 addition & 1 deletion src/nimibook/entries.nim
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
3 changes: 3 additions & 0 deletions src/nimibook/prelude.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import nimib, nimibook / defaults
nbInit
nbDoc.useNimibook
9 changes: 4 additions & 5 deletions src/nimibook/pubs.nim → src/nimibook/publishers.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

import std / [os, strutils]
import nimibook / [types, tocs]
import nimib, ../../book/bookcfg
import nimibook / [types, tocs, defaults]
import nimib

proc nimPublish*(entry: Entry) =
let
Expand All @@ -14,11 +13,11 @@ 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
setCurrentDir nbInitDir # reset current directory
setCurrentDir nbInitDir # reset current directory

proc publish*(entry: Entry) =
let splitted = entry.path.splitFile()
Expand Down
2 changes: 1 addition & 1 deletion src/nimibook/render.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import std/strformat
import std / strformat
import nimibook / [types, entries]

const path_to_root = "{{path_to_root}}"
Expand Down
2 changes: 1 addition & 1 deletion src/nimibook/sort.nim
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
8 changes: 4 additions & 4 deletions src/nimibook/tocs.nim
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"
Expand All @@ -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 <=="
Expand Down
2 changes: 1 addition & 1 deletion src/nimibook/types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ type
Toc* = object
title*: string
path*: string
entries*: seq[Entry]
entries*: seq[Entry]

0 comments on commit 25363e3

Please sign in to comment.