มอดูล:cy-mut

จาก วิกิพจนานุกรม พจนานุกรมเสรี
local export = {}

local lang = require("Module:languages").getByCode("cy")

local rfind = mw.ustring.find
local usub = mw.ustring.sub
local ulower = mw.ustring.lower

local PAGENAME = mw.title.getCurrentTitle().text
local IRREGMARKER = "<sup>△</sup>"

local mutation_rules = {
	['b'] = {"f", "m"},
	['c'] = {"g", "ngh", "ch"},
	['ch'] = {},
	['d'] = {"dd", "n"},
	['dd'] = {},
	['f'] = {},
	['g'] = {"", "ng"},
	['h'] = {},
	['j'] = {},
	['k'] = {},
	['l'] = {},
	['ll'] = {"l"},
	['m'] = {"f"},
	['n'] = {},
	['p'] = {"b", "mh", "ph"},
	['ph'] = {},
	['q'] = {},
	['r'] = {},
	['rh'] = {"r"},
	['s'] = {},
	['t'] = {"d", "nh", "th"},
	['th'] = {},
	['v'] = {},
	['x'] = {},
	['z'] = {}
}

function export.get_mutation_data(term)
	local data = {}
	data.radical = term
	data.initial = usub(term, 1, 2)
	data.is_uppercase = ulower(data.initial) ~= data.initial
	data.initial = ulower(data.initial)
	data.final = usub(term, 3, -1)
	
	local mut = mutation_rules[data.initial]
	if not mut then
		data.final = usub(data.initial, 2) .. data.final
		data.initial = usub(data.initial, 1, 1)
		mut = mutation_rules[data.initial]
	end
	
	data.vowel = false
	data.mut1 = nil
	data.mut2 = nil
	data.mut3 = nil
	
	if mut then
		data.mut1 = mut[1]
		data.mut2 = mut[2]
		data.mut3 = mut[3]
	elseif rfind(mw.ustring.toNFD(data.initial), "[aeiouwy]") then
		data.vowel = true
		data.mut3 = "h" .. data.initial
	else
		error("no mutation rule found for this term: " .. term)
	end

	return data
end

function export.show(frame)
	local params = {
		[1] = {},
		["soft"] = {},
		["nasal"] = {},
		["aspirate"] = {},
		["nocat"] = {type = "boolean"},
	}
	local parargs = frame:getParent().args
    local args = require("Module:parameters").process(parargs, params)
	
	local data = export.get_mutation_data(args[1] or PAGENAME)

	local function construct_mutation(accel_form, initial, override)
		local normal_mutation
		if not initial then
			normal_mutation = nil
		else
			normal_mutation = initial .. data.final
			if data.is_uppercase then
				normal_mutation = require("Module:string utilities").ucfirst(normal_mutation)
			end
		end
		local has_override = override
		if override then
			if override == "no" or override == "0" then
				override = nil
			end
		end
		local has_irreg_mutation = has_override and override ~= normal_mutation
		local mutation
		-- don't combine the following into A and B or C because `override` may be nil
		if has_override then
			mutation = override
		else
			mutation = normal_mutation
		end
		local irreg_marker = has_irreg_mutation and IRREGMARKER or ""
		if mutation then
			return require("Module:links").full_link({lang = lang, accel = {form = accel_form, lemma = data.radical}, term = mutation}) .. irreg_marker, true, has_irreg_mutation
		else
			return "''คงเดิม''" .. irreg_marker, false, has_irreg_mutation
		end
	end

	local soft, has_soft, has_irreg_soft = construct_mutation("soft", data.mut1, args.soft)
	local nasal, has_nasal, has_irreg_nasal  = construct_mutation("nasal", data.mut2, args.nasal)
	local aspirate, has_aspirate, has_irreg_aspirate = construct_mutation(data.vowel and "h-prothesis" or "aspirate", data.mut3, args.aspirate)

	result = '{| class="inflection-table" style="display:table; width:60%; border:1px solid #b3b3b3; font-size:95%; text-align:center;"'
	result = result .. '\n|-'
	result = result .. '\n! style="background:#dafdda;" colspan=4 | [[ภาคผนวก:การกลายรูปภาษาเวลส์|การกลายรูปภาษาเวลส์]]'
	result = result .. '\n|-'
	result = result .. '\n! style="padding-top:4px;" | [[ราก]]'
	result = result .. '\n! style="padding-top:4px;" | [[soft mutation|อ่อน]]'
	result = result .. '\n! style="padding-top:4px;" | [[nasal mutation|นาสิก]]'
	result = result .. '\n! style="padding-top:4px;" | ' .. (data.vowel and '[[h-prothesis|เติม h]]' or '[[aspirate mutation|พ่นลม]]')
	result = result .. '\n|-'
	result = result .. '\n| style="padding-bottom:4px;" | ' .. require("Module:links").full_link({lang = lang, term = data.radical})
	result = result .. '\n| style="padding-bottom:4px;" | ' .. soft
	result = result .. '\n| style="padding-bottom:4px;" | ' .. nasal
	result = result .. '\n| style="padding-bottom:4px;" | ' .. aspirate
	if has_irreg_soft or has_irreg_nasal or has_irreg_aspirate then
		result = result .. '\n|-'
		result = result .. '\n| colspan=4 | <small style="font-size:85%;">' .. IRREGMARKER .. 'ไม่ปรกติ</small>'
	end
	if has_soft or has_nasal or has_aspirate then
		result = result .. '\n|-'
		result = result .. "\n| colspan=4 | <small style=\"font-size:85%;\">''หมายเหตุ:'' รูปเหล่านี้บางส่วนอาจเป็นการสันนิษฐาน มิใช่ว่าทุกรูปที่เป็นไปได้จะเกิดขึ้นจริง</small>"
	end
	result = result .. '\n|}'
	if not args.nocat and (has_irreg_soft or has_irreg_nasal or has_irreg_aspirate) then
		result = result .. require("Module:utilities").format_categories({"Welsh terms with irregular mutation"}, lang)
	end
	return result
end

return export