all repos — min @ 2e8cb4237a3d94d3e5ee5c9f416a50af458f489b

A small but practical concatenative programming language.

minpkg/lib/min_xml.nim

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
import
    std/[xmlparser,
    xmltree,
    parsexml,
    htmlparser,
    strtabs,
    critbits]
import
    nimquery
import
    ../core/parser,
    ../core/value,
    ../core/interpreter,
    ../core/utils


let xmltypes = "dict:xml-text|dict:xml-element|dict:xml-cdata|dict:xml-comment|dict:xml-entity"

proc newXDict(i: In, xml: XmlNode): MinValue =
    result = newDict(i.scope)
    case xml.kind:
        of xnText:
            result.objType = "xml-text"
            i.dset(result, "text", xml.text.newVal)
        of xnVerbatimText:
            result.objType = "xml-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:
                children.add i.newXDict(child)
            i.dset(result, "children", children.newVal)
            if not xml.attrs.isNil:
                for attr in xml.attrs.pairs:
                    i.dset(attributes, attr.key, attr.value.newVal)
            i.dset(result, "attributes", attributes)
        of xnCData:
            result.objType = "xml-cdata"
            i.dset(result, "text", xml.text.newVal)
        of xnEntity:
            result.objType = "xml-entity"
            i.dset(result, "text", xml.text.newVal)
        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-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
                var tattr: tuple[key, val: string]
                tattr.key = key
                tattr.val = val
                attrs.add tattr
            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()

    i.scope.symbols["typealias: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:
            let xml = parseXml(s, {reportComments, allowUnquotedAttribs,
                    allowEmptyAttribs})
            i.push(i.newXDict(xml))
        except CatchableError:
            let msg = getCurrentExceptionMsg()
            raiseInvalid(msg)

    def.symbol("from-html") do (i: In):
        let vals = i.expect("str")
        let s = vals[0].getString()
        try:
            let xml = parseHtml(s)
            i.push(i.newXDict(xml))
        except CatchableError:
            let msg = getCurrentExceptionMsg()
            raiseInvalid(msg)

    def.symbol("comment") do (i: In):
        let vals = i.expect("'sym")
        i.push i.newXDict(newComment(vals[0].getString))

    def.symbol("cdata") do (i: In):
        let vals = i.expect("'sym")
        i.push i.newXDict(newCData(vals[0].getString))

    def.symbol("text") do (i: In):
        let vals = i.expect("'sym")
        i.push i.newXDict(newText(vals[0].getString))

    def.symbol("entity") do (i: In):
        let vals = i.expect("'sym")
        i.push i.newXDict(newEntity(vals[0].getString))

    def.symbol("entity2utf8") do (i: In):
        let vals = i.expect("dict:xml-entity")
        var entity = i.dget(vals[0], "text").getString
        # Strip & and ;
        entity = entity[1..entity.len-2]
        i.push entity.entityToUtf8.newVal

    def.symbol("escape") do (i: In):
        let vals = i.expect("'sym")
        i.push vals[0].getString.escape.newVal

    def.symbol("element") 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("xml-node")
        let xdict = vals[0]
        let xml = i.newXml(xdict)
        i.push ($xml).newVal

    def.symbol("query") do (i: In):
        let vals = i.expect("'sym", "dict:xml-element")
        let xdict = vals[1]
        let query = vals[0].getString
        let root = i.newXml(xdict)
        i.push i.newXDict(root.querySelector(query))

    def.symbol("query-all") do (i: In):
        let vals = i.expect("'sym", "dict:xml-element")
        let xdict = vals[1]
        let query = vals[0].getString
        let root = i.newXml(xdict)
        let xresults = root.querySelectorAll(query)
        var results = newSeq[MinValue](0)
        for e in xresults:
            results.add i.newXDict(e)
        i.push results.newVal

    def.finalize("xml")