~xigoi/xidoc

bf9e0def694f16f3cdc26690d82401903b6c4b5e — Adam Blažek 2 months ago d1a7560
Start of SavaScript commands
2 files changed, 66 insertions(+), 5 deletions(-)

M src/xidocpkg/commands/default.nim
A src/xidocpkg/commands/javascript.nim
M src/xidocpkg/commands/default.nim => src/xidocpkg/commands/default.nim +10 -5
@@ 10,6 10,7 @@ import ../types
import ./checkbox
import ./css
import ./draw
import ./javascript
import ./math
import ./utils
import aspartame


@@ 427,14 428,18 @@ commands defaultCommands:
      values.add (args[2 * i], args[2 * i + 1])
    jsEval(code, values)

  proc jsModuleCmd(arg: !String): Markup {.command: "js-module".} =
    if doc.target == tHtml:
      doc.addToHead.incl htg.script(`type` = "module", arg)
  proc jsModuleCmd(js: Raw): Markup {.command: "js-module".} =
    case doc.target
    of tHtml:
      doc.stack[^1].commands = jsCommands(doc)
      doc.addToHead.incl htg.script(`type` = "module", doc.expandStr(js))
    else:
      discard
    ""

  proc jsModuleRawCmd(arg: Raw): Markup {.command: "js-module-raw".} =
  proc jsModuleRawCmd(js: Raw): Markup {.command: "js-module-raw".} =
    if doc.target == tHtml:
      doc.addToHead.incl htg.script(`type` = "module", arg)
      doc.addToHead.incl htg.script(`type` = "module", js)
    ""

  proc langCmd(langStr: !String, body: Raw): Markup {.command: "lang", safe.} =

A src/xidocpkg/commands/javascript.nim => src/xidocpkg/commands/javascript.nim +56 -0
@@ 0,0 1,56 @@
import ../error
import ../types
import ./utils
import aspartame
import std/options
import std/strformat
import std/strutils

commands jsCommands:

  proc forCmd(name: !String, container: !String, body: !String): String {.command: "for".} =
    &"for(const {name} of {container}){{{body}}}"

  proc ifCmd(condition: !String, body: !String, elses: *String): String {.command: "if".} =
    result = &"if({condition}){{{body}}}"
    for i in 0..<(elses.len div 2):
      result &= &"else if({elses[2*i]}){{{elses[2*i+1]}}}"
    if elses.len mod 2 == 1:
      result &= &"else{{{elses[^1]}}}"

  proc ifExprCmd(condition: !String, expr: !String, elses: *String): String {.command: "if=".} =
    result = &"{condition}?{expr}"
    if elses.len mod 2 != 1:
      xidocError "[if=] requires an else clause"
    for i in 0..<(elses.len div 2):
      result &= &":{elses[2*i]}?{elses[2*i+1]}"
    result &= &":{elses[^1]}"

  proc iifCmd(body: !String): String {.command: "iif".} =
    &"(()=>{{{body}}})()"

  proc letCmd(name: !String, value: !String): String {.command: "let".} =
    &"const {name}={value};"

  proc rangeCmd(name: !String, n: !String, m: ?String, r: ?String, body: !String): String {.command: "range".} =
    ifSome m:
      ifSome r:
        &"for(let {name}={n};{name}<{m};{name}+={r}){{{body}}}"
      do:
        &"for(let {name}={n};{name}<{m};++{name}){{{body}}}"
    do:
      &"for(let {name}=0;{name}<{n};++{name}){{{body}}}"

  proc switchCmd(name: !String, cases: *String): String {.command: "switch".} =
    if cases.len mod 2 != 0:
      xidocError "Extra arguments to [switch] must come in pairs"
    result = &"switch({name}){{"
    for i in 0..<(cases.len div 2):
      let label =
        if cases[2*i] == "default": "default"
        else: "case " & cases[2*i]
      result &= &"{label}:{{{cases[2*i+1]}}}break;"
    result &= "}"#}

  proc varCmd(name: !String, value: ?String): String {.command: "var".} =
    ifSome(value, &"let {name}={value};", &"let {name};")