Module:hierarchy
Jump to navigation
Jump to search
- This module lacks a documentation subpage. Please create it.
- Useful links: subpage list • links • transclusions • testcases • sandbox
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[val] then
current[val] = data[val]
table.insert(make_nil, key)
data[val] = nil
end
else
current[key] = nested(val)
end
end
for _, key in ipairs(make_nil) do
current[key] = 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[key] = true
table.insert(result, key)
end
end
table.sort(result)
for i=1,#result do
if is_table[result[i]] then
local name = result[i]
result[i] = export.deep_sort(data[result[i]])
result[i].name = name
else
result[i] = {name = result[i]}
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[name_of_upper_rank] then
for _, upper_rank in ipairs(val[name_of_upper_rank]) do
if type(upper_rank) == "table" then
upper_rank = upper_rank.name or upper_rank.template or ("error: "..key)
end
if children[upper_rank] and not exempt[upper_rank] then
--mw.log(key,val,upper_rank,require("Module:debug").dump(children[upper_rank]))
table.insert(children[upper_rank], key)
else
children[upper_rank] = {key}
end
end
end
end
children = export.make_nested(children)
return export.dump(children, " ", link)
end
return export