Skip to content

Commit a17e43f

Browse files
committed
Adding debug.lua
1 parent 486acaa commit a17e43f

1 file changed

Lines changed: 212 additions & 0 deletions

File tree

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
local export = {}
2+
3+
local escape
4+
do
5+
local escapes = {
6+
["\a"] = "a", ["\b"] = "b", ["\f"] = "f", ["\n"] = "n", ["\r"] = "r",
7+
["\t"] = "t", ["\v"] = "v", ["\\"] = "\\", ["\""] = '"', ["'"] = "'",
8+
}
9+
10+
local function helper(char)
11+
return escapes[char] and "\\" .. escapes[char]
12+
or ("\\%03d"):format(char:byte())
13+
end
14+
15+
-- Escape control characters, backslash, double quote, and bytes that aren't
16+
-- used in UTF-8.
17+
-- Escape stuff that can't be saved in a MediaWiki page, like invalid UTF-8
18+
-- and NFD character sequences? Hard.
19+
-- Similar to string.format("%q", str), which does not use C-like simple
20+
-- escapes and does not escape bytes that are not used in UTF-8.
21+
escape = function(str)
22+
return (str:gsub("[%z\1-\31\\\"\127\192\193\245-\255]", helper))
23+
end
24+
end
25+
26+
export.escape = escape
27+
28+
-- Convert a value to a string
29+
function export.dump(value, prefix, tsort)
30+
local t = type(value)
31+
32+
prefix = prefix or ""
33+
34+
if t == "string" then
35+
return '"' .. escape(value) .. '"'
36+
elseif t == "table" then
37+
local str_table = {}
38+
39+
table.insert(str_table, " {")
40+
41+
for key, val in require("table").sortedPairs(value, tsort) do
42+
table.insert(str_table, " " .. prefix .. "\t[" .. export.dump(key, prefix .. "\t") .. "] = " .. export.dump(val, prefix .. "\t"):gsub("^ ", "") .. ",")
43+
end
44+
45+
table.insert(str_table, " " .. prefix .. "}")
46+
47+
return table.concat(str_table, "\n")
48+
else
49+
return tostring(value)
50+
end
51+
end
52+
53+
function export.highlight_dump(value, prefix, tsort, options)
54+
options = options or {}
55+
56+
local func = options.modified and "modified_dump" or "dump"
57+
58+
local dump = export[func](value, prefix, tsort)
59+
60+
-- Remove spaces at beginnings of lines (which are simply to force a <pre></pre> tag).
61+
dump = dump:gsub("%f[^%z\n] ", "")
62+
63+
return export.highlight(dump)
64+
end
65+
66+
67+
-- Returns true if table contains a table as one of its values
68+
local function containsTable(t)
69+
for key, value in pairs(t) do
70+
if type(value) == "table" then
71+
return true
72+
end
73+
end
74+
return false
75+
end
76+
77+
local function containsTablesWithSize(t, size)
78+
for key, value in pairs(t) do
79+
if type(value) == "table" and require("table").size(value) ~= size then
80+
return false
81+
end
82+
end
83+
return true
84+
end
85+
86+
87+
--[=[
88+
Convert a value to a string.
89+
Like dump below, but if a table has consecutive numbered keys and does not
90+
have a table as one of its values, it will be placed on a single line.
91+
Used by [[Module:User:Erutuon/script recognition]].
92+
]=]
93+
function export.modified_dump(value, prefix, tsort)
94+
local t = type(value)
95+
96+
prefix = prefix or ""
97+
98+
if t == "string" then
99+
return '"' .. value .. '"'
100+
elseif t == "table" then
101+
local str_table = {}
102+
103+
local containsTable = containsTable(value)
104+
local consecutive = require("table").isArray(value)
105+
if consecutive and not containsTable or containsTable and containsTablesWithSize(value, 3) then
106+
table.insert(str_table, "{")
107+
108+
for key, val in require("table").sortedPairs(value, tsort) do
109+
if containsTable then
110+
table.insert(str_table, "\n\t" .. prefix)
111+
else
112+
table.insert(str_table, " ")
113+
end
114+
115+
if type(key) == "string" then
116+
table.insert(str_table, "[" .. export.modified_dump(key) .. "] = ")
117+
end
118+
119+
table.insert(str_table, type(key) == "number" and type(val) == "number" and string.format("0x%05X", val) or export.modified_dump(val))
120+
121+
if not (consecutive and #value == 3) or type(key) == "number" and value[key + 1] then
122+
table.insert(str_table, ",")
123+
end
124+
end
125+
126+
if containsTable then
127+
table.insert(str_table, "\n" .. prefix)
128+
else
129+
table.insert(str_table, " ")
130+
end
131+
132+
table.insert(str_table, "}")
133+
return table.concat(str_table)
134+
end
135+
136+
table.insert(str_table, " {")
137+
138+
for key, val in require("table").sortedPairs(value, tsort) do
139+
table.insert(str_table, " " .. prefix .. "\t[" .. export.modified_dump(key, prefix .. "\t") .. "] = " .. export.modified_dump(val, prefix .. "\t"):gsub("^ ", "") .. ",")
140+
end
141+
142+
table.insert(str_table, " " .. prefix .. "}")
143+
144+
return table.concat(str_table, "\n")
145+
elseif t == "number" and value > 46 then
146+
return string.format("0x%05X", value)
147+
else
148+
return tostring(value)
149+
end
150+
end
151+
152+
export.track = require("debug/track")
153+
154+
155+
-- Trigger a script error from a template
156+
function export.error(frame)
157+
error(frame.args[1] or "(no message specified)")
158+
end
159+
160+
--[[
161+
Convenience function for generating syntaxhighlight tags.
162+
Display defaults to block.
163+
Options is a table. To display inline text with HTML highlighting:
164+
{ inline = true, lang = "html" }
165+
]]
166+
function export.highlight(content, options)
167+
if type(content) == "table" then
168+
options = content
169+
options = {
170+
lang = options.lang or "lua",
171+
inline = options.inline and true
172+
}
173+
return function(content)
174+
return mw.getCurrentFrame():extensionTag {
175+
name = "syntaxhighlight",
176+
content = content,
177+
args = options
178+
}
179+
end
180+
else
181+
return mw.getCurrentFrame():extensionTag {
182+
name = "syntaxhighlight",
183+
content = content,
184+
args = {
185+
lang = options and options.lang or "lua",
186+
inline = options and options.inline and true or nil
187+
}
188+
}
189+
end
190+
end
191+
192+
function export.track_unrecognized_args(args, template_name)
193+
local function track(code)
194+
export.track(template_name .. "/" .. code)
195+
end
196+
197+
track("unrecognized arg")
198+
199+
local arg_list = {}
200+
for arg, value in pairs(args) do
201+
track("unrecognized arg/" .. arg)
202+
table.insert(arg_list, ("|%s=%s"):format(arg, value))
203+
end
204+
205+
mw.log(
206+
("Unrecognized parameter%s in {{%s}}: %s."):format(
207+
arg_list[2] and "s" or "",
208+
template_name,
209+
table.concat(arg_list, ", ")))
210+
end
211+
212+
return export

0 commit comments

Comments
 (0)