Merge branch 'next' of github.com:h3rald/min into next
h3rald h3rald@h3rald.com
Sun, 30 Jul 2023 09:23:23 +0000
2 files changed,
50 insertions(+),
5 deletions(-)
M
min.nimble
→
min.nimble
@@ -14,7 +14,7 @@ installDirs = @["minpkg"]
# Dependencies -requires "nim >= 1.6.12, zippy >= 0.5.6" +requires "nim >= 1.6.12, zippy >= 0.5.6, nimquery >= 2.0.1" before install: exec "nimble install -y nifty"
M
minpkg/lib/min_xml.nim
→
minpkg/lib/min_xml.nim
@@ -3,7 +3,9 @@ import
std/xmlparser, std/xmltree, std/parsexml, - std/strtabs + std/strtabs, + std/critbits, + nimquery import ../core/parser, ../core/value,@@ -24,6 +26,7 @@ result.objType = "xml-verbatim-text"
i.dset(result, "text", xml.text.newVal) of xnElement: result.objType = "xml-element" + i.dset(result, "tag", xml.tag.newVal) var children = newSeq[MinValue](0) var attributes = newDict(i.scope) for child in xml.items:@@ -42,11 +45,39 @@ of xnComment:
result.objType = "xml-comment" i.dset(result, "text", xml.text.newVal) +proc newXml(i: In, xdict: MinValue): XmlNode = + case xdict.objType: + of "xml-text": + result = newText(i.dget(xdict, "text").getString) + of "xml-verbatim-text": + result = newVerbatimText(i.dget(xdict, "text").getString) + of "xml-element": + let tag = i.dget(xdict, "tag").getString + let attributes = i.dget(xdict, "attributes") + let children = i.dget(xdict, "children") + result = newElement(tag) + var attrs = newSeq[tuple[key, val: string]](0) + for attr in i.keys(attributes).qVal: + let key = attr.getString + let val = i.dget(attributes, attr).getString + attrs.add {key: key, val: val} + result.attrs = attrs.toXmlAttributes + for child in children.qVal: + result.add i.newXml(child) + of "xml-cdata": + result = newCdata(i.dget(xdict, "text").getString) + of "xml-entity": + result = newEntity(i.dget(xdict, "text").getString) + of "xml-comment": + result = newComment(i.dget(xdict, "text").getString) + proc xml_module*(i: In) = let def = i.define() - def.symbol("xparse") do (i: In): + i.scope.symbols["xml-node"] = MinOperator(kind: minValOp, val: xmltypes.newVal, sealed: false, quotation: false) + + def.symbol("from-xml") do (i: In): let vals = i.expect("str") let s = vals[0].getString() try:@@ -76,7 +107,21 @@ def.symbol("xentity") do (i: In):
let vals = i.expect("'sym") i.push i.newXDict(newEntity(vals[0].getString)) - def.symbol("xchildren") do (i: In): - let vals = i.expect("dict:xml") + def.symbol("xelement") do (i: In): + let vals = i.expect("'sym") + i.push i.newXDict(newElement(vals[0].getString)) + + def.symbol("to-xml") do (i: In): + let vals = i.expect("dict:xml-node") + let xdict = vals[0] + let xml = i.newXml(xdict) + i.push ($xml).newVal + + def.symbol("xquery") do (i: In): + let vals = i.expect("dict:xml-element", "'sym") + let xdict = vals[0] + let query = vals[1].getString + let root = i.newXml(xdict) + i.push i.newXDict(root.querySelector(query)) def.finalize("xml")