From 95f838f44c69fb8867401ec56a3a16bf92a4d414 Mon Sep 17 00:00:00 2001 From: George Lemon Date: Tue, 12 Mar 2024 10:44:07 +0200 Subject: [PATCH] update Signed-off-by: George Lemon --- src/tim/engine/compilers/html.nim | 2 +- src/tim/engine/compilers/js.nim | 97 ------------------------------- 2 files changed, 1 insertion(+), 98 deletions(-) delete mode 100644 src/tim/engine/compilers/js.nim diff --git a/src/tim/engine/compilers/html.nim b/src/tim/engine/compilers/html.nim index 644a88a..6889f01 100755 --- a/src/tim/engine/compilers/html.nim +++ b/src/tim/engine/compilers/html.nim @@ -8,7 +8,7 @@ import std/[tables, strutils, json, jsonutils, options, terminal] import pkg/jsony -import ./tim, ./js +import ./tim from std/xmltree import escape from ../meta import TimEngine, TimTemplate, TimTemplateType, diff --git a/src/tim/engine/compilers/js.nim b/src/tim/engine/compilers/js.nim deleted file mode 100644 index bdf0dd9..0000000 --- a/src/tim/engine/compilers/js.nim +++ /dev/null @@ -1,97 +0,0 @@ -# A super fast template engine for cool kids -# -# (c) 2023 George Lemon | LGPL License -# Made by Humans from OpenPeeps -# https://github.com/openpeeps/tim - -import std/[tables, strutils, json, - jsonutils, options, terminal] - -import pkg/jsony -import ../ast, ../logging - -from std/xmltree import escape -from ../meta import TimEngine, TimTemplate, TimTemplateType, - getType, getSourcePath - -import ./tim # TimCompiler object - -type - JSCompiler* = object of TimCompiler - ## Object of a TimCompiler to transpile Tim templates - ## to `JavaScript` HtmlElement nodes for client-side - ## rendering. - globalScope: ScopeTable = ScopeTable() - data: JsonNode - jsOutputCode: string = "{" - jsCountEl: uint - targetElement: string - -const - domCreateElement = "let $1 = document.createElement('$2');" - domSetAttribute = "$1.setAttribute('$2','$3');" - domInsertAdjacentElement = "$1.insertAdjacentElement('beforeend',$2);" - domInnerText = "$1.innerText=\"$2\";" - -# Forward Declaration -proc evaluateNodes(c: var JSCompiler, nodes: seq[Node], elp: string = "") - - -proc toString(c: var JSCompiler, x: Node): string = - result = - case x.nt - of ntLitString: - if x.sVals.len == 0: - x.sVal - else: "" - else: "" - -proc getAttrs(c: var JSCompiler, attrs: HtmlAttributes, elx: string): string = - let len = attrs.len - for k, attrNodes in attrs: - var attrStr: seq[string] - for attrNode in attrNodes: - case attrNode.nt - of ntAssignableSet: - add attrStr, c.toString(attrNode) - else: discard # todo - add result, domSetAttribute % [elx, k, attrStr.join(" ")] - # add result, attrStr.join(" ") - -proc createHtmlElement(c: var JSCompiler, x: Node, elp: string) = - ## Create a new HtmlElement - let elx = "el" & $(c.jsCountEl) - add c.jsOutputCode, domCreateElement % [elx, x.getTag()] - if x.attrs != nil: - add c.jsOutputCode, c.getAttrs(x.attrs, elx) - inc c.jsCountEl - if x.nodes.len > 0: - c.evaluateNodes(x.nodes, elx) - if elp.len > 0: - add c.jsOutputCode, domInsertAdjacentElement % [elp, elx] - else: - add c.jsOutputCode, domInsertAdjacentElement % ["document.querySelector('" & c.targetElement & "')", elx] - -# proc evaluatePartials(c: var HtmlCompiler, includes: seq[string], scopetables: var seq[ScopeTable]) = -# # Evaluate included partials -# for x in includes: -# if likely(c.ast.partials.hasKey(x)): -# c.evaluateNodes(c.ast.partials[x][0].nodes, scopetables) - -proc evaluateNodes(c: var JSCompiler, nodes: seq[Node], elp: string = "") = - for i in 0..nodes.high: - case nodes[i].nt - of ntHtmlElement: - c.createHtmlElement(nodes[i], elp) - of ntLitString, ntLitInt, ntLitFloat, ntLitBool: - add c.jsOutputCode, domInnerText % [elp, c.toString(nodes[i])] - else: discard # todo - -proc newCompiler*(nodes: seq[Node], clientTargetElement: string): JSCompiler = - ## Create a new instance of `JSCompiler` - result = JSCompiler(targetElement: clientTargetElement) - result.evaluateNodes(nodes) - -proc getOutput*(c: var JSCompiler): string = - add c.jsOutputCode, "}" # end block statement - result = c.jsOutputCode