M src/xidoc.nim => src/xidoc.nim +6 -1
@@ 72,7 72,12 @@ proc renderXidoc*(body: string, path = "", target = teHtml, snippet = false, saf
let documentClass =
if doc.settings.documentClass == "": "article"
else: doc.settings.documentClass
- "documentclass"{documentClass} &
+ let documentClassLine =
+ if doc.settings.documentClassOptions == "":
+ "documentclass"{documentClass}
+ else:
+ "documentclass"[doc.settings.documentClassOptions]{documentClass}
+ documentClassLine &
"usepackage"["utf8"]{"inputenc"} &
"usepackage"[lang]{"babel"} &
"usepackage"{"geometry"} &
M src/xidocpkg/commands/default.nim => src/xidocpkg/commands/default.nim +27 -9
@@ 140,17 140,31 @@ commands defaultCommands:
doc.addToHead.incl arg
proc argRawCmd(name: !String): String {.command: "arg-raw".} =
- let arg = doc.lookup(args, name)
+ let arg = doc.lookupArg(name)
ifSome arg:
- arg
+ arg.val
do:
xidocError &"Parameter not found: {name}"
proc argCmd(name: !String): Markup {.command: "arg", safe.} =
- doc.renderStr(argRawCmd(name))
+ let arg = doc.lookupArg(name)
+ ifSome arg:
+ let stack = doc.stack
+ doc.stack = arg.stack
+ result = doc.renderStr(arg.val)
+ doc.stack = stack
+ do:
+ xidocError &"Parameter not found: {name}"
proc argExpandCmd(name: !String): String {.command: "arg-expand".} =
- doc.expandStr(argRawCmd(name))
+ let arg = doc.lookupArg(name)
+ ifSome arg:
+ let stack = doc.stack
+ doc.stack = arg.stack
+ result = doc.expandStr(arg.val)
+ doc.stack = stack
+ do:
+ xidocError &"Parameter not found: {name}"
proc argRawEscapeCmd(name: !String): Markup {.command: "arg-raw-escape".} =
argRawCmd(name).escapeText(doc.target)
@@ 275,15 289,15 @@ commands defaultCommands:
template def(global: static bool) {.dirty.} =
let params = ifSome(paramList, paramList.splitWhitespace, @[])
doc.stack[when global: 0 else: ^2].commands[name] = proc(arg: StringView): XidocValue =
- let argsList = if arg == "": @[] else: parseXidocArguments(arg)
+ let argsList = if arg.isEmpty: @[] else: parseXidocArguments(arg)
if argsList.len != params.len:
xidocError &"""Command {name} needs exactly {params.len} argument{(if params.len == 1: "" else: "s")}, {argsList.len} given"""
# Merging the following two lines into one causes the thing to break. WTF?
let argsTable = zip(params, argsList).toTable
- doc.stack[^1].args = argsTable
+ doc.stack[^1].args = Arguments(vals: argsTable, stack: doc.stack)
result = XidocValue(typ: Markup, str: doc.renderStr(body))
- proc defCmd(name: !String, paramList: ?String, body: Raw): Markup {.command: "def", safe.} =
+ proc defCmd(name: !String, paramList: ?String, body: Raw): Markup {.command: "def".} =
def(global = false)
proc defGlobalCmd(name: !String, paramList: ?String, body: Raw): Markup {.command: "def-global".} =
@@ 681,7 695,9 @@ commands defaultCommands:
of "dark-mode":
doc.settings.darkMode = false
of "document-class":
- doc.settings.katexStylesheetPath = ""
+ doc.settings.documentClass = ""
+ of "document-class-options":
+ doc.settings.documentClassOptions = ""
of "katex-stylesheet-path":
doc.settings.katexStylesheetPath = ""
of "math-renderer":
@@ 757,7 773,9 @@ commands defaultCommands:
of "dark-mode":
doc.settings.darkMode = val.parseBool
of "document-class":
- doc.settings.katexStylesheetPath = val
+ doc.settings.documentClass = val
+ of "document-class-options":
+ doc.settings.documentClassOptions = val
of "katex-stylesheet-path":
doc.settings.katexStylesheetPath = val
of "math-renderer":
M src/xidocpkg/types.nim => src/xidocpkg/types.nim +12 -2
@@ 54,12 54,16 @@ type
Settings* = object
darkMode*: bool
documentClass*: string
+ documentClassOptions*: string
katexStylesheetPath*: string
mathRenderer*: MathRenderer
syntaxHighlightingTheme*: SyntaxHighlightingTheme
temmlStylesheetPath*: string
- Frame* = object
- args*: Table[string, StringView]
+ Arguments* = object
+ vals*: Table[string, StringView]
+ stack*: seq[Frame]
+ Frame* = ref object
+ args*: Arguments
cmd*: StringView
cmdArg*: StringView
cmdName*: StringView
@@ 105,6 109,12 @@ template lookup*(doc: Document, field: untyped, key: typed): auto =
none(typeof(doc.stack[0].field[key]))
)()
+proc lookupArg*(doc: Document, name: string): Option[tuple[val: StringView, stack: seq[Frame]]] =
+ for i in countdown(doc.stack.len - 1, 0):
+ let frame = doc.stack[i]
+ if frame.args.vals.hasKey(name):
+ return some((val: frame.args.vals[name], stack: frame.args.stack))
+
proc `!`*(typ: XidocType): ParamType =
result.kind = ptkOne
result.base = typ
M tests/test_documents.nim => tests/test_documents.nim +4 -0
@@ 229,6 229,10 @@ suite "Custom commands":
"[() [def a; x]][a]".shouldError
"[() [def-global a; x]][a]".shouldRenderAs("[]x")
+ test "correct context":
+ "[def foo; x; foo [arg x]][def bar; x; [foo [arg x]] bar][bar quux]"
+ .shouldRenderAs("foo quux bar")
+
suite "\"Target detection\" commands":
test "[if-html]":