|
@@ -6,6 +6,8 @@ import (
|
|
|
"fmt"
|
|
|
"log"
|
|
|
qu "qfw/util"
|
|
|
+ "regexp"
|
|
|
+ "strings"
|
|
|
|
|
|
ljson "github.com/yuin/gopher-json"
|
|
|
"github.com/yuin/gopher-lua"
|
|
@@ -59,7 +61,73 @@ func (s *LuaScript) RunScript(stype string) map[string]interface{} {
|
|
|
data := map[string]interface{}{}
|
|
|
qu.Try(func() {
|
|
|
s.L = lua.NewState()
|
|
|
+ //s.L.DoFile("lua/comm.lua")
|
|
|
s.L.PreloadModule("json", ljson.Loader)
|
|
|
+ s.L.SetGlobal("findOneText", s.L.NewFunction(func(S *lua.LState) int {
|
|
|
+ nodetype := S.ToString(-3)
|
|
|
+ gpath := S.ToString(-2)
|
|
|
+ content := S.ToString(-1)
|
|
|
+ ret := FindOneText(gpath, content, nodetype)
|
|
|
+ S.Push(ret)
|
|
|
+ return 1
|
|
|
+ }))
|
|
|
+ s.L.SetGlobal("findOneHtml", s.L.NewFunction(func(S *lua.LState) int {
|
|
|
+ nodetype := S.ToString(-3)
|
|
|
+ gpath := S.ToString(-2)
|
|
|
+ content := S.ToString(-1)
|
|
|
+ ret := FindOneHtml(gpath, content, nodetype)
|
|
|
+ S.Push(ret)
|
|
|
+ return 1
|
|
|
+ }))
|
|
|
+ s.L.SetGlobal("findListText", s.L.NewFunction(func(S *lua.LState) int {
|
|
|
+ gpath := S.ToString(-2)
|
|
|
+ content := S.ToString(-1)
|
|
|
+ ret := s.L.NewTable()
|
|
|
+ FindListText(gpath, content, ret)
|
|
|
+ S.Push(ret)
|
|
|
+ return 1
|
|
|
+ }))
|
|
|
+ s.L.SetGlobal("findListHtml", s.L.NewFunction(func(S *lua.LState) int {
|
|
|
+ gpath := S.ToString(-2)
|
|
|
+ content := S.ToString(-1)
|
|
|
+ ret := s.L.NewTable()
|
|
|
+ FindListHtml(gpath, content, ret)
|
|
|
+ S.Push(ret)
|
|
|
+ return 1
|
|
|
+ }))
|
|
|
+ s.L.SetGlobal("findMap", s.L.NewFunction(func(S *lua.LState) int {
|
|
|
+ qmap := S.ToTable(-2)
|
|
|
+ content := S.ToString(-1)
|
|
|
+ ret := s.L.NewTable()
|
|
|
+ FindMap(qmap, content, ret)
|
|
|
+ S.Push(ret)
|
|
|
+ return 1
|
|
|
+ }))
|
|
|
+ //支持正则
|
|
|
+ s.L.SetGlobal("regexp", s.L.NewFunction(func(S *lua.LState) int {
|
|
|
+ index := int(S.ToNumber(-1))
|
|
|
+ regstr := S.ToString(-2)
|
|
|
+ text := S.ToString(-3)
|
|
|
+ reg := regexp.MustCompile(regstr)
|
|
|
+ reps := reg.FindAllStringSubmatchIndex(text, -1)
|
|
|
+ ret := s.L.NewTable()
|
|
|
+ number := 0
|
|
|
+ for _, v := range reps {
|
|
|
+ number++
|
|
|
+ ret.Insert(number, lua.LString(text[v[index]:v[index+1]]))
|
|
|
+ }
|
|
|
+ S.Push(ret)
|
|
|
+ return 1
|
|
|
+ }))
|
|
|
+ //支持替换
|
|
|
+ s.L.SetGlobal("replace", s.L.NewFunction(func(S *lua.LState) int {
|
|
|
+ text := S.ToString(-3)
|
|
|
+ old := S.ToString(-2)
|
|
|
+ repl := S.ToString(-1)
|
|
|
+ text = strings.Replace(text, old, repl, -1)
|
|
|
+ S.Push(lua.LString(text))
|
|
|
+ return 1
|
|
|
+ }))
|
|
|
defer s.L.Close()
|
|
|
if err := s.L.DoString(s.Script); err != nil {
|
|
|
data["err"] = err.Error()
|
|
@@ -83,6 +151,7 @@ func (s *LuaScript) RunScript(stype string) map[string]interface{} {
|
|
|
Protect: true,
|
|
|
}, lua.LString(s.Code), tab, lua.LString(block), kvMap); err != nil {
|
|
|
data["err"] = err.Error()
|
|
|
+ log.Println(err.Error())
|
|
|
}
|
|
|
} else if stype == "back" {
|
|
|
result := MapToLuaTable2(s.L, s.Result)
|