မဝ်ဂျူ:Jpan-sortkey

နူ ဝိက်ရှေန်နရဳ

Documentation for this module may be created at မဝ်ဂျူ:Jpan-sortkey/doc

local export = {}
local u = mw.ustring.char
local UTF8_char = "[%z\1-\127\194-\244][\128-\191]*"

function export.sortkey_from_string(text, lang, sc)
	text = mw.ustring.toNFD(require("Module:ja").kata_to_hira(text))
	
	-- If the first character has dakuten, replace it with the corresponding character without dakuten and add an apostrophe to the end, e.g. がす > かす'
	text = text:gsub("^(" .. UTF8_char .. ")" .. u(0x3099) .. "(.*)", "%1%2'")
	-- Similar thing, but with handuken and two apostrophes, e.g. ぱす -> はす''
	text = text:gsub("^(" .. UTF8_char .. ")" .. u(0x309A) .. "(.*)", "%1%2''")
	
	-- Replace the long vowel mark with the vowel that it stands for
	if text:match("ー") then
		local from = {"([あぁかさたなはまやゃらわ])ー", "([いぃきしちにひみり])ー", "([うぅくすつぬふむゆゅる])ー", "([えぇけせてねへめれ])ー", "([おぉこそとのほもよょろ])ー"}
		local to = {"%1あ", "%1い", "%1う", "%1え", "%1お"}
		for i, v in ipairs(from) do
			text = mw.ustring.gsub(text, v, to[i])
		end
	end
	
	local ret = require("Module:Hani-sortkey").makeSortKey(text, lang, sc)
	
	if ret ~= text then
		require("Module:debug/track"){"Jpan-sortkey/fallback", "Jpan-sortkey/fallback/" .. lang}
	end
	
	return ret
end

function export.makeSortKey(text, lang, sc)
	local check_pattern = '^' .. lang:upper() .. '_SORTKEY:(.+)'
	local i = tonumber(mw.getCurrentFrame():extensionTag('nowiki', ''):match'([%dA-F]+)', 16)
	while i > 0 do
		i = i - 1
		local sortkey = mw.text.unstripNoWiki(('\127\'"`UNIQ--nowiki-%08X-QINU`"\'\127'):format(i)):match(check_pattern)
		if sortkey then
			return sortkey
		end
	end
	
	return export.sortkey_from_string(text, lang, sc)
end

return export