Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change toml lib + fix macro bug on devel #173

Merged
merged 4 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion nimib.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ requires "nim >= 1.4.0"
requires "tempfile >= 0.1.6"
requires "markdown >= 0.8.1"
requires "mustache >= 0.2.1"
requires "toml_serialization >= 0.2.0"
requires "parsetoml >= 0.7.0"
requires "jsony >= 1.1.5"

task docsdeps, "install dependendencies required for doc building":
exec "nimble -y install ggplotnim@0.5.3 numericalnim@0.6.1 nimoji nimpy karax@1.2.2"
Expand Down
62 changes: 60 additions & 2 deletions src/nimib/config.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import types, os, toml_serialization
import types, parsetoml, jsony, std / [json, os, math, sequtils]

proc hasCfg*(doc: var NbDoc): bool = doc.cfgDir.string != ""

Expand All @@ -13,6 +13,64 @@ proc optOverride*(doc: var NbDoc) =
if doc.options.homeDir != "":
doc.cfg.homeDir = doc.options.homeDir


proc customToJson*(table: parsetoml.TomlTableRef): JsonNode

proc customToJson*(value: parsetoml.TomlValueRef): JsonNode =
case value.kind:
of TomlValueKind.Int:
%* value.intVal
of TomlValueKind.Float:
if classify(value.floatVal) == fcNan:
if value.forcedSign != Pos:
%* value.floatVal
else:
%* value.floatVal
else:
%* value.floatVal
of TomlValueKind.Bool:
%* $value.boolVal
of TomlValueKind.Datetime:
if value.dateTimeVal.shift == false:
%* value.dateTimeVal
else:
%* value.dateTimeVal
of TomlValueKind.Date:
%* value.dateVal
of TomlValueKind.Time:
%* value.timeVal
of TomlValueKind.String:
%* value.stringVal
of TomlValueKind.Array:
if value.arrayVal.len == 0:
when defined(newtestsuite):
%[]
else:
%* []
elif value.arrayVal[0].kind == TomlValueKind.Table:
%value.arrayVal.map(customToJson)
else:
when defined(newtestsuite):
%*value.arrayVal.map(customToJson)
else:
%* value.arrayVal.map(customToJson)
of TomlValueKind.Table:
value.tableVal.customToJson()
of TomlValueKind.None:
%*{"type": "ERROR"}

proc customToJson*(table: parsetoml.TomlTableRef): JsonNode =
result = newJObject()
for key, value in pairs(table):
result[key] = value.customToJson


proc loadTomlSection*[T](content, section: string, _: typedesc[T]): T =
let toml = parsetoml.parseString(content)
result = T()
if section in toml:
result = ($toml[section].customToJson()).fromJson(T)

proc loadNimibCfg*(cfgName: string): tuple[found: bool, dir: AbsoluteDir, raw: string, nb: NbConfig] =
for dir in parentDirs(getCurrentDir()):
if fileExists(dir / cfgName):
Expand All @@ -22,7 +80,7 @@ proc loadNimibCfg*(cfgName: string): tuple[found: bool, dir: AbsoluteDir, raw: s
break
if result.found:
result.raw = readFile(result.dir.string / cfgName)
result.nb = Toml.decode(result.raw, NbConfig, "nimib")
result.nb = loadTomlSection(result.raw, "nimib", NbConfig)

proc loadCfg*(doc: var NbDoc) =
if not doc.options.skipCfg:
Expand Down
5 changes: 4 additions & 1 deletion src/nimib/sources.nim
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ macro getCodeAsInSource*(source: string, command: static string, body: untyped):
let endPos = finishPos(body)
let endFilename = endPos.filename.newLit

let endPosLit = endPos.newLit
let startPosLit = startPos.newLit

result = quote do:
if `filename` notin nb.sourceFiles:
nb.sourceFiles[`filename`] = readFile(`filename`)
Expand All @@ -160,4 +163,4 @@ macro getCodeAsInSource*(source: string, command: static string, body: untyped):
If you want to mix code from different files in nbCode, use -d:nimibCodeFromAst instead.
If you are not mixing code from different files, please open an issue on nimib's Github with a minimal reproducible example."""

getCodeBlock(nb.sourceFiles[`filename`], `command`, `startPos`, `endPos`)
getCodeBlock(nb.sourceFiles[`filename`], `command`, `startPosLit`, `endPosLit`)