Jump to content

Module:wlm-sortkey

From Wiktionary, the free dictionary

This module will sort Middle Welsh language text. The module should preferably not be called directly from templates or other modules. To use it from a template, use {{sortkey}}. Within a module, use Module:languages#Language:makeSortKey.

For testcases, see Module:wlm-sortkey/testcases.

Functions

makeSortKey(text, lang, sc)
Generates a sortkey for a given piece of text written in the script specified by the code sc, and language specified by the code lang.
When the sort fails, returns nil.
local export = {}
local u = mw.ustring.char
local a = u(0xF000)

local remove_diacritics = u(0x0300) .. "-" .. u(0x0302) .. u(0x0308) .. "'" -- grave, acute, circumflex, diaeresis, apostrophe

local oneChar = {
	["k"] = "c"
}

local twoChars = {
	["ch"] = "c" .. a, ["dd"] = "d" .. a, ["ff"] = "f" .. a, ["ll"] = "l" .. a, ["ph"] = "p" .. a, ["rh"] = "r" .. a, ["th"] = "t" .. a
}

local threeChars = {
	["ngh"] = "g" .. a
}

function export.makeSortKey(text, lang, sc)
	text = mw.ustring.lower(text)
	
	for from, to in pairs(threeChars) do
		text = mw.ustring.gsub(text, from, to)
	end
	
	for from, to in pairs(twoChars) do
		text = mw.ustring.gsub(text, from, to)
	end
	
	return mw.ustring.upper(mw.ustring.toNFC(mw.ustring.gsub(mw.ustring.toNFD(mw.ustring.gsub(text, ".", oneChar)), "[" .. remove_diacritics .. "]", ""))) -- decompose, remove appropriate diacritics, then recompose again
end

return export