Module:User:Aryamanarora/ANC

local p = {}

local lang = mw.loadData("Module:languages/data/all") local fam = mw.loadData("Module:families/data") local etyl = mw.loadData("Module:etymology languages/data")

local function format(code) if lang[code] then return " " elseif etyl[code] then return " " end end

local function dump(data, prefix) if type(data) == "string" then return format(data) else local result = "" local branch = "├───" local next_level = prefix .. "│　　　　"		local current = "" for i,val in ipairs(data) do			if i == #data then branch = "└───" next_level = prefix .. "　　　 　"			end if #val == 0 then result = result .. prefix .. branch .. dump(val.name) .. " "			else result = result .. "{| class=mw-collapsible style=border-collapse:collapse\n|" result = result .. prefix .. branch .. dump(val.name) result = result .. "\n|-\n|" result = result .. dump(val, next_level) result = result .. "\n|}\n" end end return result end end

local function deep_sort(current) local result = {} local is_table = {} for key,val in pairs(current) do		if type(key) == "number" then table.insert(result, val) else is_table[key] = true table.insert(result, key) end end table.sort(result, function(a,b)		return (lang[a] or etyl[a] or error(a)).canonicalName < (lang[b] or etyl[b] or error(b)).canonicalName	end) local i = 2 while i<#result do		while (lang[result[i-1]] or etyl[result[i-1]]) == (lang[result[i]] or etyl[result[i]]) do			table.remove(result,i) end i = i + 1 end for i=1,#result do		if is_table[result[i]] then local name = result[i] result[i] = deep_sort(current[result[i]]) result[i].name = name else result[i] = {name = result[i]} end end return result end

function p.show(frame) local children = {} local function find_ancestors(origin,key,val) if val.ancestors then return val.ancestors elseif etyl[key] and val.parent and mw.ustring.match(val.parent,"%-pro$") then return {val.parent} elseif val.family then key = val.family val = fam[key] local function find_ancestor_in_family if not key or key == "qfa-not" or key == "qfa-und" then return nil elseif val.protoLanguage and origin ~= val.protoLanguage then return {val.protoLanguage} elseif origin ~= key.."-pro" and lang[key.."-pro"] then return {key.."-pro"} else key = val.family val = fam[key] return find_ancestor_in_family end end return find_ancestor_in_family end end for _,data in ipairs{lang, etyl} do		for key,val in pairs(data) do			local ancestors = find_ancestors(key,key,val) if ancestors then for _, ancestor in ipairs(ancestors) do					if ancestor ~= key then if children[ancestor] then table.insert(children[ancestor], key) else children[ancestor] = {key} end end end end end end local function make_nested(data) local make_nil = {} for key,val in pairs(data) do			if type(key) == "number" then if children[val] then data[val] = make_nested(children[val]) table.insert(make_nil, key) children[val] = nil end else data[key] = make_nested(val) end end for _,key in ipairs(make_nil) do			data[key] = nil end return data end local nested = make_nested(children) nested = deep_sort(nested) local result = "" for i=1,#nested do result = result .. "\n\n\n{| class=mw-collapsible style=border-collapse:collapse\n|" .. format(nested[i].name) .. "\n|-\n|" result = result .. dump(nested[i], "　　") result = result .. "\n|}" end return frame:preprocess(result) end

return p