Модуль:debug
Зовнішній вигляд
Документацію для цього модуля можна створити у Модуль:debug/документація
local export = {}
-- Convert a value to a string
function export.dump(value, prefix, tsort)
local t = type(value)
prefix = prefix or ""
if t == "string" then
return '"' .. value .. '"'
elseif t == "table" then
local str_table = {}
table.insert(str_table, " {")
for key, val in require("Module:table").sortedPairs(value, tsort) do
table.insert(str_table, " " .. prefix .. "\t[" .. export.dump(key, prefix .. "\t") .. "] = " .. mw.ustring.gsub(export.dump(val, prefix .. "\t"), "^ ", "") .. ",")
end
table.insert(str_table, " " .. prefix .. "}")
return table.concat(str_table, "\n")
else
return tostring(value)
end
end
function export.highlight_dump(value, prefix, tsort, options)
options = options or {}
local func = options.modified and "modified_dump" or "dump"
local dump = export[func](value, prefix, tsort)
-- Remove spaces at beginnings of lines (which are simply to force a <pre></pre> tag).
dump = mw.ustring.gsub(dump, "^ ", "")
dump = mw.ustring.gsub(dump, "\n ", "\n")
return export.highlight(dump)
end
-- Returns true if all keys are numbers, and they are consecutive.
local function isConsecutive(t)
local keys = require("Module:table").keysToList(t)
for _, key in pairs(keys) do
if type(key) == "number" then
if not (key == 1 or t[key - 1]) then
return false
end
else
return false
end
end
return true
end
-- Returns true if table contains a table as one of its values
local function containsTable(t)
for key, value in pairs(t) do
if type(value) == "table" then
return true
end
end
return false
end
local function containsTablesWithSize(t, size)
for key, value in pairs(t) do
if type(value) == "table" and require("Module:table").size(value) ~= size then
return false
end
end
return true
end
--[=[
Convert a value to a string.
Like dump below, but if a table has consecutive numbered keys and does not
have a table as one of its values, it will be placed on a single line.
Used by [[Module:User:Erutuon/script recognition]].
]=]
function export.modified_dump(value, prefix, tsort)
local t = type(value)
prefix = prefix or ""
if t == "string" then
return '"' .. value .. '"'
elseif t == "table" then
local str_table = {}
local containsTable = containsTable(value)
local consecutive = isConsecutive(value)
if consecutive and not containsTable or containsTable and containsTablesWithSize(value, 3) then
table.insert(str_table, "{")
for key, val in require("Module:table").sortedPairs(value, tsort) do
if containsTable then
table.insert(str_table, "\n\t" .. prefix)
else
table.insert(str_table, " ")
end
if type(key) == "string" then
table.insert(str_table, "[" .. export.modified_dump(key) .. "] = ")
end
table.insert(str_table, type(key) == "number" and type(val) == "number" and string.format("0x%X", val) or export.modified_dump(val))
if not (consecutive and #value == 3) or type(key) == "number" and value[key + 1] then
table.insert(str_table, ",")
end
end
if containsTable then
table.insert(str_table, "\n" .. prefix)
else
table.insert(str_table, " ")
end
table.insert(str_table, "}")
return table.concat(str_table)
end
table.insert(str_table, " {")
for key, val in require("Module:table").sortedPairs(value, tsort) do
table.insert(str_table, " " .. prefix .. "\t[" .. export.modified_dump(key, prefix .. "\t") .. "] = " .. mw.ustring.gsub(export.modified_dump(val, prefix .. "\t"), "^ ", "") .. ",")
end
table.insert(str_table, " " .. prefix .. "}")
return table.concat(str_table, "\n")
elseif t == "number" and value > 46 then
return string.format("0x%X", value)
else
return tostring(value)
end
end
function export.track(key)
local frame = mw.getCurrentFrame()
if key then
if type(key) ~= "table" then
key = { key }
end
for i, value in pairs(key) do
pcall(frame.expandTemplate, frame, { title = 'tracking/' .. value })
end
else
error('No tracking key supplied to the function "' .. track .. '".')
end
end
-- Trigger a script error from a template
function export.error(frame)
error(frame.args[1] or "(no message specified)")
end
--[[
Convenience function for generating syntaxhighlight tags.
Display defaults to block.
Options is a table. To display inline text with HTML highlighting:
{ inline = true, lang = "html" }
]]
function export.highlight(content, options)
return mw.getCurrentFrame():extensionTag{
name = "syntaxhighlight",
content = content,
args = {
lang = options and options.lang or "lua",
inline = options and options.inline and true
}
}
end
return export