This module displays the table of families in Wiktionary:List of families. It makes a list of families from Module:families/data and compiles information about each family from there and from the language data modules.


local concat = table.concat
local insert = table.insert
local sort = table.sort

local export = {}
local filters = {}

local function populate(t, stat, families, stats)
	for code, data in pairs(t) do
		local fam = data[3]
		while fam do
			stats[fam][stat][code] = true
			if fam == "qfa-not" then
				break
			end
			fam = families[fam]
			fam = fam and fam[3] or nil
		end
	end
	for code, fam in pairs(stats) do
		local list = {}
		for k in pairs(fam[stat]) do
			insert(list, k)
		end
		stats[code][stat] = list
	end
end

function export.show(frame)
	local args = frame.args
	local filter = filters[args[1]]
	local families = require("Module:families/data")
	local languages = require("Module:languages/data/all")
	local stats = {}

	for code, _ in pairs(families) do
		stats[code] = {langs = {}, subfams = {}}
	end
	
	populate(languages, "langs", families, stats)
	populate(families, "subfams", families, stats)

	local codes = {}
 
	for code, _ in pairs(families) do
		insert(codes, code)
	end 
	sort(codes)
 
	local rows = {}
	for i, code in ipairs(codes) do
		local data = families[code]

		local rt = nil
		if data.otherNames then
			local i = 1
			rt = {}
			while data.otherNames[i] do
				insert(rt, data.otherNames[i])
				i = i + 1
			end
		end

		if (not filter) or filter(code, data, args) then
			local catname = data[1] .. (data[1]:find("[Ll]anguages") and "" or " languages")
			catname = catname:sub(1, 1):upper() .. catname:sub(2)

			insert(rows, string.format(
				' id="%s"\n' ..
				'| <code>%s</code>\n' ..
				'| [[:Category:%s|%s]]\n' ..
				'| %s\n' ..
				'| %s\n' ..
				'| %u\n' ..
				'| %u\n',
				code, code,
				catname, data[1],
				(data[3] ~= nil) and ('[[#' .. data[3] .. '|' .. families[data[3]][1] .. ']]') or '',
				rt and concat(rt, ", ") or "&nbsp;",
				#stats[code].subfams,
				#stats[code].langs
			))
		end
	end

	return
		"{| class=\"wikitable sortable\"\n" ..
		"! Code\n" ..
		"! Canonical name\n" ..
		"! Parent family\n" ..
		"! Other names\n" ..
		"! Subfamilies\n" ..
		"! Languages\n" ..
		"|-" .. concat(rows, "\n|-") .. "\n|}"
end

filters["three-letter code"] = function (code, data, args)
	local firstletter = args[2]
	return data[3] ~= "qfa-not" and code:find("^" .. (firstletter or "[a-z]") .. "[a-z][a-z]$") ~= nil
end

filters["exceptional"] = function (code, data, args)
	return data[3] ~= "qfa-not" and code:find("-") ~= nil
end

filters["special"] = function (code, data, args)
	return data[3] == "qfa-not"
end

return export