-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclean.lua
99 lines (87 loc) · 2.83 KB
/
clean.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
function SS(inputSnippet)
local simplifiedSnippet = {}
-- Function to recursively simplify nested tables
local function simplifyTable(srcTable, destTable)
for key, value in pairs(srcTable) do
if type(value) == "table" then
destTable[key] = {}
simplifyTable(value, destTable[key])
else
destTable[key] = value
end
end
end
-- Call the recursive function to simplify the input snippet
simplifyTable(inputSnippet, simplifiedSnippet)
return simplifiedSnippet
end
function get_source_code(f)
local t = debug.getinfo (f)
if t.linedefined < 0 then print("source",t.source); return end
local name = t.source:gsub("^@","")
local i = 0
local text = {}
for line in io.lines(name) do
i=i+1
if i >= t.linedefined then text[#text+1] = line end
if i >= t.lastlinedefined then break end
end
return table.concat(text,"\n")
end
local function tableToString(tbl, indent)
indent = indent or 0
local result = "{\n"
local nextIndent = indent + 2
local doIndent = true;
for k, v in pairs(tbl) do
result = result .. string.rep(" ", nextIndent)
-- print(v)
if type(v) == "table" then
if type(k) == "string" then
result = result .. k .. " = " .. tableToString(v, nextIndent) .. ",\n"
else
result = result .. tableToString(v, nextIndent) .. ",\n"
end
else
if type(k) == "string" and type(v) == "string" then
result = result .. k .. " = \"" .. v .. "\",\n"
elseif type(k) == "string" and type(v) ~= "string" then
if type(v) ~= 'function' then
if type(v) == 'boolean' then
v = tostring(v)
end
result = result .. k .. " = " .. v .. ",\n"
else
result = result .. k .. " = " .. get_source_code(v) .. ",\n"
end
elseif type(k) == "number" and type(v) == "string" then
result = result .. "\"".. v .. "\",\n"
else
result = result .. tostring(v) .. ",\n"
end
end
end
if doIndent then
result = result .. string.rep(" ", indent) .. "}"
else
result = result .. "}"
end
return result
end
local function writeTableToFile(filename, tbl)
local file = io.open(filename, "w")
if file then
file:write("return " .. tableToString(tbl))
file:close()
print("Table successfully written to " .. filename)
else
print("Failed to open file " .. filename .. " for writing.")
end
end
if #arg < 1 then
print("Usage: lua clean.lua <filename>")
os.exit(1)
end
local filename = arg[1]
local c = dofile(filename)
writeTableToFile("clean_"..filename, SS(c))