Module:User:Theknightwho/test

From Wiktionary, the free dictionary
Jump to navigation Jump to search

This is a private module sandbox of Theknightwho, for their own experimentation. Items in this module may be added and removed at Theknightwho's discretion; do not rely on this module's stability.


local export = {}
local get = require("Module:languages").getByCode
local languages = require("Module:languages/data/all")

function export.show(frame)
	local self_lang = require("Module:languages").getByCode(frame.args[1], nil, true)
	local family = self_lang:getFamily()
	
	local descendants = {}
	if family:getProtoLanguageCode() == self_lang:getCode() then
		for code in pairs(languages) do
			local lang = get(code, nil, true, true)
			if code ~= self_lang:getCode() and lang:inFamily(family) then
				table.insert(descendants, code)
			end
		end
	else
		local family_members = {}
		for code, lang in pairs(languages) do
			local lang = get(code, nil, true, true)
			if code ~= self_lang:getCode() and lang:inFamily(family) then
				table.insert(family_members, lang)
			end
		end
		for _, relative in ipairs(family_members) do
			if relative:hasAncestor(self_lang) then
				table.insert(descendants, relative:getCode())
			end
		end
	end
	return table.concat(descendants, ", ")
end

function export.getLetters(lang)
	lang = require("Module:languages").getByCode(lang)
	local chars = lang:getStandardCharacters()
	local find = mw.ustring.find
	local u = mw.ustring.char
	local upper = mw.ustring.upper
	local letters = {}
	for i = 1, 0x1FFFF do
		local letter = u(i)
		if (i < 0xD800 or i > 0xDFFF) and find(letter, "[" .. chars .. "]") then
			table.insert(letters, letter)
		end
	end
	
	table.sort(letters, function (k1, k2)
		if upper(k1) == k2 then
			return false
		elseif k1 == upper(k2) then
			return true
		else
			return lang:makeSortKey(k1) < lang:makeSortKey(k2)
		end
	end)
	
	return table.concat(letters)
end

function export.alternate_caps(text)
	text = mw.ustring.gsub(text, ".", function(m)
		return m .. mw.ustring.lower(m)
	end)
	return text
end

function export.sortkey(frame)
	local text = frame.args[1]
	
	local concat = table.concat
	local insert = table.insert
	local c = "[%z\1-\127\194-\244][\128-\191]*"
	local cp = mw.ustring.codepoint
	local u = mw.ustring.char
	local data = require("Module:User:Theknightwho/sortkey/serialized")
	
	local chars = mw.text.split(text, "")
	
	local function table_insert(t, v)
		if v ~= "\0" then
			insert(t, u(cp(v)+0xF0000))
		end
	end
		
	local primary = {}
	local secondary = {}
	local tertiary = {}	
	for _, char in ipairs(chars) do
		local char_data = data:match("\255(" .. char .. "[\253\254][^\255]+)\255")
		for p, s, t in char_data:gmatch("(" .. c .. ")(" .. c .. ")(" .. c .. ")") do
			table_insert(primary, p)
			table_insert(secondary, s)
			table_insert(tertiary, t)
		end
	end
	return concat(primary) .. concat(secondary) .. concat(tertiary)
end

function export.toNFD(frame)
	return mw.ustring.toNFD(frame.args[1])
end

return export