Module:mg-spel

From Linguifex
Jump to navigation Jump to search

Implements {{mg-mut}} and {{mg-alter}}.
local gsub = mw.ustring.gsub
local PAGENAME = gsub(mw.title.getCurrentTitle().text, "%s", " ")
local match = mw.ustring.match
local gmatch = mw.ustring.gmatch

local lang = require("Module:languages").getByCode("cel-gal")
local m_l = require('Module:links')
local m_q = require('Module:qualifier')

local export = {}

local function link(term)
	return "[[Contionary:" .. term .. "|" .. term .. "]]"	
end

function export.alter(frame)
	local ret = {}; local qs = {}
	local term = mw.title.getCurrentTitle().nsText == "Template" and "cosmaile" or frame:getParent().args[1] or PAGENAME
	
	local e = term; local a = term; local c = term; local s = term
	
	
	-- Spanish
	e = e:gsub("il(.)", "ll%1")
	e = e:gsub("v", "b")
	e = e:gsub("([^c])[gh]", "%1j")
	
	-- Asturian	  
	a = a:gsub("il(.)", "ll%1")
	a = a:gsub("v", "b")
	
	-- Southern
	s = s:gsub("ñ", "nh")
	s = s:gsub("il(.)", "lh%1")
	s = s:gsub("v", "b")
	local lh = match(s, "lh")
	
	-- Celtic
	c = c:gsub("c([aou])", "k%1")
	c = c:gsub("qu([ei])", "k%1")
	c = c:gsub("([^i])ñ", "%1in")
	c = c:gsub("([^i])x", "%1is")
	
	-- disclaimer: awful approach below, your physical and mental integrity may be at risk
	
	if e == a and a == s then
		qs = {"Spanish", "Asturian", "Southern"}
		if e ~= term then table.insert(ret, "* " .. link(e) .. " " .. m_q.format_qualifier(qs) .. "\n") end
	elseif e == a then
		table.insert(qs, "Spanish")
		table.insert(qs, "Asturian")
		if e ~= term then table.insert(ret, "* " .. link(e) .. " " .. m_q.format_qualifier(qs) .. "\n") end
		if s ~= term then table.insert(ret, "* " .. link(s) .. (lh and ", " .. link(s:gsub("lh", "j")) or "")
												 .. " " .. m_q.format_qualifier({"Southern"}) .. "\n") end
	elseif e == s then
		table.insert(qs, "Spanish")
		table.insert(qs, "Southern")
		if e ~= term then table.insert(ret, "* " .. link(e) .. " " .. m_q.format_qualifier(qs) .. "\n") end
		if a ~= term then table.insert(ret, "* " .. link(a) .. " " .. m_q.format_qualifier({"Asturian"}) .. "\n") end
	elseif a == s then
		table.insert(qs, "Asturian")
		table.insert(qs, "Southern")
		if a ~= term then table.insert(ret, "* " .. link(a) .. " " .. m_q.format_qualifier(qs) .. "\n") end
		if e ~= term then table.insert(ret, "* " .. link(e) .. " " .. m_q.format_qualifier({"Spanish"}) .. "\n") end
	else
		if e ~= term then table.insert(ret, "* " .. link(e) .. " " .. m_q.format_qualifier({"Spanish"}) .. "\n") end
		if a ~= term then table.insert(ret, "* " .. link(a) .. " " .. m_q.format_qualifier({"Asturian"}) .. "\n") end
		if s ~= term then table.insert(ret, "* " .. link(s) .. (lh and ", " .. link(s:gsub("lh", "j")) or "")
												 .. " " .. m_q.format_qualifier({"Southern"}) .. "\n") end
	end
	
	if c ~= term then
		table.insert(ret, "* " .. link(c) .. " " .. m_q.format_qualifier({"Celtic"}) .. "\n")
	end
	
	table.insert(ret, 1, "===Alternative forms===")
	table.insert(ret, 2, "\n")
	
	if e == term and a == term and c == term and s == term then
		ret = {"aa", "aa"}
	end
	
	return table.concat(ret)
end

function export.mutate(frame)
	
	local term = mw.title.getCurrentTitle().nsText == "Template" and "bea" or frame:getParent().args[1] or PAGENAME
	local unchanged = "''unchanged''"
	
	local soft = {
		["p"] = "b", ["b"] = "v", ["d"] = "z", ["t"] = "d", ["g"] = "h", ["c"] = "g", ["q"] = "g", 
		["P"] = "B", ["B"] = "V", ["D"] = "Z", ["T"] = "D", ["G"] = "H", ["C"] = "G", ["Q"] = "G", 
	}
	local nasal = {
		["m"] = "v", ["b"] = "m", ["d"] = "n", ["g"] = "k",
		["M"] = "V", ["B"] = "M", ["D"] = "N", ["G"] = "K"
	}
	
	local function aw(term, mutation)
		local mutated = ""
		for regex, repl in pairs(mutation) do
			mutated = gsub(term, "^" .. regex, repl, 1)
			mutated = gsub(mutated, "^k(.)", function(foo) return (match(foo, "[aou]") and "c" or "qu") .. foo end)
			if mutated ~= term then return mutated end
		end
		
		return unchanged
	end
	
	local function link(term)
		local links = {}
		for alt in gmatch(term, "([^%s,]+)") do
			if alt == unchanged then return unchanged end
			alt = "[[Contionary:" .. alt .. "|" .. alt .. "]]"
			table.insert(links, alt)
		end
		return table.concat(links, ", ")
	end
	
	local reference = ""
	
	local wikicode = [=[
	{| border="1" cellpadding="4" cellspacing="0" class="inflection-table" style="text-align: center; margin: 0.5em 0 0 0; border-style: solid; border: 1px solid #7f7f7f; border-right-width: 2px; border-bottom-width: 2px; border-collapse: collapse; background-color: #F8F8F8; font-size: 95%;"
	|-
	! colspan="3" | [[Modern Gallaecian mutation]]<sup class="reference nowrap ltr" style="color:#002bb8;">[1]</sup>
	|-
	! Radical
	! Soft
	! Nasal
	|-
	| ]=] .. link(term, uc) .. [=[&#8203;
	| ]=] .. link(aw(term, soft)) .. [=[&#8203;
	| ]=] .. link(aw(term, nasal)) .. [=[&#8203;
	|-
	| colspan=3 style="font-size: 89%; text-align: left;" | <div class="dummy-footnote" style="font-size: 90%; margin-bottom: 0.5em; margin-left: 2em;">1.&#32;<span style="color: #002bb8;">^</span>&#32;<span class="citation">Christian Evans, (2021). <span title="See link">[[Template:mg-mut/doc#References|“Mutation in Modern Gallaecian”]]</span>, ''Segments'' (01-07), April 2021.</span></div>
	|}]=]
	
	return wikicode
end

return export