/
lua.go
230 lines (205 loc) · 4.47 KB
/
lua.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
import (
"bytes"
"html"
"os/exec"
"sync"
"github.com/yuin/gluamapper"
lua "github.com/yuin/gopher-lua"
)
type lStatePool struct {
m sync.Mutex
saved []*lua.LState
}
func (pl *lStatePool) Get() *lua.LState {
pl.m.Lock()
defer pl.m.Unlock()
n := len(pl.saved)
if n == 0 {
return pl.New()
}
x := pl.saved[n-1]
pl.saved = pl.saved[0 : n-1]
return x
}
func (pl *lStatePool) New() *lua.LState {
L := lua.NewState()
loadConfig(L)
return L
}
func (pl *lStatePool) Put(L *lua.LState) {
pl.m.Lock()
defer pl.m.Unlock()
pl.saved = append(pl.saved, L)
}
func (pl *lStatePool) Shutdown() {
for _, L := range pl.saved {
L.Close()
}
}
var luaPool = &lStatePool{
saved: make([]*lua.LState, 0, 4),
}
// LuaModuleLoader loads lua functions.
func LuaModuleLoader(L *lua.LState) int {
mod := L.SetFuncs(L.NewTable(), exports)
L.Push(mod)
return 1
}
var exports = map[string]lua.LGFunction{
"runprocessor": luaRunProcessor,
"htmlescape": luaHTMLEscape,
"htmlunescape": luaHTMLUnescape,
"urlencode": luaURLEncode,
"formatmarkup": luaFormatMarkup,
"title": luaTitle,
"path": luaPath,
"url": luaURL,
"fullurl": luaFullURL,
"copyfile": luaCopyFile,
"copytree": luaCopyTree,
"isdir": luaIsDir,
"isfile": luaIsFile,
"pathexists": luaPathExists,
}
func luaRunProcessor(L *lua.LState) int {
cmdline := []string{}
text := L.CheckString(-1)
for i := 1; i < L.GetTop(); i++ {
cmdline = append(cmdline, L.Get(i).String())
}
cmd := exec.Command(cmdline[0], cmdline[1:]...)
stdin, err1 := cmd.StdinPipe()
if err1 != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err1.Error()))
return 2
}
var buf bytes.Buffer
cmd.Stdout = &buf
if err := cmd.Start(); err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
if _, err := stdin.Write(([]byte)(text)); err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
_ = stdin.Close()
if err := cmd.Wait(); err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
L.Push(lua.LString(buf.String()))
return 1
}
func luaHTMLEscape(L *lua.LState) int {
str := L.CheckString(1)
L.Push(lua.LString(html.EscapeString(str)))
return 1
}
func luaHTMLUnescape(L *lua.LState) int {
str := L.CheckString(1)
L.Push(lua.LString(html.UnescapeString(str)))
return 1
}
func luaURLEncode(L *lua.LState) int {
str := L.CheckString(1)
L.Push(lua.LString(urlEncode(str)))
return 1
}
func luaFormatMarkup(L *lua.LState) int {
app := appInstance()
text := L.CheckString(1)
format := L.CheckString(2)
html, err := app.convertArticleText(L, text, format)
if err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
L.Push(lua.LString(html))
return 1
}
func luaMapArg(L *lua.LState, idx int) map[interface{}]interface{} {
app := appInstance()
data := gluamapper.ToGoValue(L.CheckTable(idx),
gluamapper.Option{NameFunc: gluamapper.Id}).(map[interface{}]interface{})
data["App"] = app
return data
}
func luaTitle(L *lua.LState) int {
app := appInstance()
name := L.CheckString(1)
data := luaMapArg(L, 2)
L.Push(lua.LString(app.Title(name, data)))
return 1
}
func luaPath(L *lua.LState) int {
app := appInstance()
name := L.CheckString(1)
data := luaMapArg(L, 2)
L.Push(lua.LString(app.Path(name, data)))
return 1
}
func luaURL(L *lua.LState) int {
app := appInstance()
name := L.CheckString(1)
data := luaMapArg(L, 2)
L.Push(lua.LString(app.Url(name, data)))
return 1
}
func luaFullURL(L *lua.LState) int {
app := appInstance()
name := L.CheckString(1)
data := luaMapArg(L, 2)
L.Push(lua.LString(app.FullURL(name, data)))
return 1
}
func luaCopyFile(L *lua.LState) int {
src, dst := L.CheckString(1), L.CheckString(2)
if err := copyFile(src, dst); err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
L.Push(lua.LTrue)
return 1
}
func luaCopyTree(L *lua.LState) int {
src, dst := L.CheckString(1), L.CheckString(2)
if err := copyTree(src, dst); err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
L.Push(lua.LTrue)
return 1
}
func luaIsDir(L *lua.LState) int {
if isDir(L.CheckString(1)) {
L.Push(lua.LTrue)
} else {
L.Push(lua.LFalse)
}
return 1
}
func luaIsFile(L *lua.LState) int {
if isFile(L.CheckString(1)) {
L.Push(lua.LTrue)
} else {
L.Push(lua.LFalse)
}
return 1
}
func luaPathExists(L *lua.LState) int {
if pathExists(L.CheckString(1)) {
L.Push(lua.LTrue)
} else {
L.Push(lua.LFalse)
}
return 1
}