~xigoi/xidoc

beab88291d162e8edcca2c8c86bcb79fdf3aa879 — Adam Blažek 2 years ago 34dd238 named-args
Test drive of named args
A examples/named.html => examples/named.html +2 -0
@@ 0,0 1,2 @@
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="generator" content="xidoc"><meta name="viewport" content="width=device-width,initial-scale=1"></head><body> a | b | c | p = (typ: String, str: &quot;q&quot;, args: {:}) | x = (typ: String, str: &quot;y&quot;, args: {:})
</body></html>

A examples/named.xd => examples/named.xd +1 -0
@@ 0,0 1,1 @@
[debug [=s x; y] [=s p; q] a; b; c]

M src/xidocpkg/commands/default.nim => src/xidocpkg/commands/default.nim +10 -0
@@ 107,6 107,10 @@ commands defaultCommands:
  command "\\", literal, String:
    arg.strip(chars = {'\n'}).dedent

  command "=s", (key: String, val: String), Markup:
    resultArgs[key] = XidocValue(typ: String, str: val)
    ""

  command "LaTeX", void, Markup:
    case doc.target
    of tHtml:


@@ 205,6 209,12 @@ commands defaultCommands:
    of tGemtext:
      text

  command "debug", (args: *String), String:
    var res = args.join(" | ")
    for key, val in named:
      res.add " | $1 = $2" % [key, $val]
    res

  template def(global: static bool): string {.dirty.} =
    let params = paramList.map(it => it.splitWhitespace).get(@[])
    doc.stack[when global: 0 else: ^2].commands[name] = proc(arg: string): XidocValue =

M src/xidocpkg/commands/utils.nim => src/xidocpkg/commands/utils.nim +12 -3
@@ 37,7 37,9 @@ macro command*(name: string, sig: untyped, retTyp: XidocType, body: untyped): un
    elif sig.kind == nnkIdent:
      let get = getter(sig)
      quote:
        let arg {.inject.} = doc.expand(`arg`.strip, `sig`).`get`
        let val = doc.expand(`arg`.strip, `sig`)
        let arg {.inject.} = val.`get`
        let named {.inject.} = val.args
        `body`
    else:
      var starPos = none int


@@ 71,7 73,11 @@ macro command*(name: string, sig: untyped, retTyp: XidocType, body: untyped): un
          return str
        else:
          let get = getter(typ)
          return quote: expand(doc, `str`, `typ`).`get`
          return quote:
            let val = doc.expand(`str`, `typ`)
            for key, value in val.args:
              named[key] = value
            val.`get`
      if starPos.isSome:
        block beforeStar:
          for index, pair in sig[0..<starPos.get(sigLen)]:


@@ 133,12 139,15 @@ macro command*(name: string, sig: untyped, retTyp: XidocType, body: untyped): un
      quote:
        let `args` = parseXidocArguments(`arg`)
        `lenCheck`
        var named {.inject.}: Table[string, XidocValue]
        `unpacks`
        block:
          `body`
  let retGet = getter(retTyp)
  quote:
    commands[`name`] = proc(`arg`: string): XidocValue = XidocValue(typ: `retTyp`, `retGet`: `logic`)
    commands[`name`] = proc(`arg`: string): XidocValue =
      var resultArgs {.inject, used.}: Table[string, XidocValue]
      XidocValue(typ: `retTyp`, `retGet`: `logic`, args: resultArgs)

template commands*(name, defs: untyped) =
  proc name*(doc {.inject.}: Document): Table[string, Command] =

M src/xidocpkg/expand.nim => src/xidocpkg/expand.nim +3 -0
@@ 3,6 3,7 @@ import ./parser
import ./types
import std/strformat
import std/strutils
import std/tables

proc escapeText*(text: string, target: Target): string =
  case target


@@ 78,6 79,8 @@ proc expand*(doc: Document, str: string, typ: XidocType): XidocValue =
            result.list.add val
          of List:
            result.list &= val.list
        for key, val in val.args:
          result.args[key] = val

proc expandStr*(doc: Document, str: string): string =
  doc.expand(str, String).str

M src/xidocpkg/types.nim => src/xidocpkg/types.nim +8 -0
@@ 21,6 21,7 @@ type
      str*: string
    of List:
      list*: seq[XidocValue]
    args*: Table[string, XidocValue]
  SyntaxHighlightingTheme* = enum
    shtDefault = "default"
    shtDark = "dark"


@@ 71,3 72,10 @@ template lookup*(doc: Document, field: untyped, key: typed): auto =
      if frame.field.hasKey(key):
        return frame.field[key]
  )()

template `$`(val: XidocValue): string =
  case val.typ
  of String, Markup:
    val.str
  of List:
    $val.list