This repository was archived by the owner on Feb 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathluahtml.lua
76 lines (59 loc) · 1.61 KB
/
luahtml.lua
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
local lines = {}
local alive, die_reason = false, ""
local sbox = setmetatable({}, { __index = _G })
local function printLine(...)
local ret = {}
for i = 1, select("#", ...) do
local arg = tostring(select(i, ...))
table.insert(ret, arg)
end
return table.concat(ret, "\t")
end
sbox.PrintTable = function(t)
table.insert(lines, table.ToPlain(t))
end
sbox.print = function(...)
table.insert(lines, printLine(...))
end
sbox.die = function(str)
alive = false
die_reason = str or ""
end
function EvalLuaHTML(luahtml)
local error = false
alive, die_reason = true, ""
local output = string.gsub(luahtml, "<lua>(.-)</lua>", function(code)
lines = {}
local fn, syntaxError = load(code, "<lua>...</lua>", "t", sbox)
if not fn then
error = (error and error .."\n" or "") .. syntaxError
return ""
end
local success, runtimeError = pcall(fn)
if not success then
error = (error and error .."\n" or "") .. runtimeError
return ""
end
return table.concat(lines, "\n")
end)
if alive == false then return die_reason end
return error or output, not error
end
--[[
print(
EvalLuaHTML("<h1><lua> print('Test') </lua></h1>")
)
]]--
function EvalLuaHTMLFile(path)
local f = io.open(path, "r")
if not f then return false, "" end
local luahtml = f:read("*a")
io.close(f)
return true, EvalLuaHTML(luahtml)
end
--[[
print(
EvalLuaHTMLFile("var/www/site.com/pages/index.lua.html")
)
]]--
return {eval = EvalLuaHTML, evalfile = EvalLuaHTMLFile}