local export = {}
local function color(text)
return "<span style=\"color:green;\">" .. text .. "</span>"
end
function export.dump(data, prefix, link)
local res={}
dumpinner(data, prefix, link,res)
return table.concat(res, "")
end
function dumpinner(data, prefix, link, result)
prefix = prefix or " "
if type(data) == "string" then
table.insert(result, link(data))
else
local length = 0
for _,_ in pairs(data) do
length = length + 1
end
local branch = color("├──")
local next_level = prefix .. color("│ ")
for key,val in pairs(data) do
length = length - 1
if length == 0 then
branch = color("└──")
next_level = prefix .. " "
end
if type(key) == "number" then
table.insert(result, prefix)
table.insert(result, branch)
dumpinner(val, nil, link, result)
table.insert(result,"\n")
else
table.insert(result, prefix)
table.insert(result, branch)
dumpinner(key, nil, link, result)
table.insert(result,"\n")
dumpinner(val, next_level, link, result)
end
end
end
end
function export.make_nested(data)
local function nested(current)
local make_nil = {}
for key,val in pairs(current) do
if type(key) == "number" then
if data then
current = data
table.insert(make_nil, key)
data = nil
end
else
current = nested(val)
end
end
for _, key in ipairs(make_nil) do
current = nil
end
return current
end
data = nested(mw.clone(data))
return data
end
function export.deep_sort(data)
local result = {}
local is_table = {}
for key,val in pairs(data) do
if type(key) == "number" then
table.insert(result, val)
else
is_table = true
table.insert(result, key)
end
end
table.sort(result)
for i=1,#result do
if is_table] then
local name = result
result = export.deep_sort(data])
result.name = name
else
result = {name = result}
end
end
return result
end
function export.show(data, name_of_upper_rank, link, exempt)
local children = {}
exempt = exempt or {}
for key,val in pairs(data) do
if val then
for _, upper_rank in ipairs(val) do
if type(upper_rank) == "table" then
upper_rank = upper_rank.name or upper_rank.template or ("error: "..key)
end
if children and not exempt then
--mw.log(key,val,upper_rank,require("Module:debug").dump(children))
table.insert(children, key)
else
children = {key}
end
end
end
end
children = export.make_nested(children)
return export.dump(children, " ", link)
end
return export