-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
37 lines (33 loc) · 671 Bytes
/
api.go
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
package gluaxmlpath
import (
"bytes"
"github.com/yuin/gopher-lua"
xmlpath "gopkg.in/xmlpath.v2"
)
var api = map[string]lua.LGFunction{
"loadxml": loadXml,
"compile": compile,
}
func loadXml(L *lua.LState) int {
xmlStr := L.CheckString(1)
r := bytes.NewReader([]byte(xmlStr))
node, err := xmlpath.ParseHTML(r)
if err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
L.Push(newNode(L, node))
return 1
}
func compile(L *lua.LState) int {
xpathStr := L.CheckString(1)
path, err := xmlpath.Compile(xpathStr)
if err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
L.Push(newPath(L, path))
return 1
}