Module:ibe-nouns



local m_utilities = require("Module:utilities")
local m_links = require("Module:links")

local export = {}

local lang = require("Module:languages").getByCode("ibe-pro")


local function postprocess(args, data)
	data.sg = true
	data.du = true
	data.pl = true
	
	if args["n"] then
		if not mw.ustring.find(args["n"], "s") then
			data.sg = false
		end
		
		if not mw.ustring.find(args["n"], "d") then
			data.du = false
		end
		
		if not mw.ustring.find(args["n"], "p") then
			data.pl = false
		end
	end
	
	for key, form in pairs(data.forms) do
		-- Do not show singular, dual or plural forms for nouns that don't have them
		if (not data.sg and key:find("_sg$")) or (not data.du and key:find("_du$")) or (not data.pl and key:find("_pl$")) then
			form = nil
		end
		
		data.forms[key] = form
	end
end


-- Inflection functions

export["a-f"] = function(frame)
	local params = {
		[1] = {required = true, default = "{{{1}}}"},
		
		["n"] = {},
		["nopl"] = {},
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local data = {forms = {}, info = "feminine ā-stem", categories = {lang:getCanonicalName() .. " ā-stem nouns"}}
	
	data.forms["nom_sg"] = {args[1] .. "ā"}
	data.forms["voc_sg"] = {args[1] .. "e"}
	data.forms["acc_sg"] = {args[1] .. "ām"}
	data.forms["gen_sg"] = {args[1] .. "ās"}
	data.forms["dat_sg"] = {args[1] .. "āe"}
	data.forms["com_sg"] = {args[1] .. "āē"}

	if not args.nopl then	
		data.forms["nom_du"] = {args[1] .. "aī"}
		data.forms["voc_du"] = {args[1] .. "aī"}
		data.forms["acc_du"] = {args[1] .. "aī"}
		data.forms["gen_du"] = {args[1] .. "ous"}
		data.forms["dat_du"] = {args[1] .. "āvām"}
		data.forms["com_du"] = {args[1] .. "āvām"}
		
		data.forms["nom_pl"] = {args[1] .. "ās"}
		data.forms["voc_pl"] = {args[1] .. "ās"}
		data.forms["acc_pl"] = {args[1] .. "āns"}
		data.forms["gen_pl"] = {args[1] .. "om"}
		data.forms["dat_pl"] = {args[1] .. "āvos"}
		data.forms["com_pl"] = {args[1] .. "āvis"}
	end
	
	postprocess(args, data)
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end

export["cons-mf"] = function(frame)
	local params = {
		[1] = {required = true, default = "{{{1}}}"},
		[2] = {},
		
		["n"] = {},
		["nopl"] = {},
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local data = {forms = {}, info = "masculine/feminine/neuter consonant stem", categories = {lang:getCanonicalName() .. " consonant stem nouns"}}
	
	data.forms["nom_sg"] = {args[1] .. "s"}
	data.forms["voc_sg"] = {args[1]}
	data.forms["acc_sg"] = {args[1] .. "am"}
	data.forms["gen_sg"] = {args[1] .. "es"}
	data.forms["dat_sg"] = {args[1] .. "e"}
	data.forms["com_sg"] = {args[1] .. "ē"}

	if not args.nopl then	
		data.forms["nom_du"] = {args[1] .. "e"}
		data.forms["voc_du"] = {args[1] .. "e"}
		data.forms["acc_du"] = {args[1] .. "e"}
		data.forms["gen_du"] = {args[1] .. "om"}
		data.forms["dat_du"] = {args[1] .. "emos"}
		data.forms["com_du"] = {args[1] .. "evi(s)"}
		
		data.forms["nom_pl"] = {args[1] .. "es"}
		data.forms["voc_pl"] = {args[1] .. "es"}
		data.forms["acc_pl"] = {args[1] .. "ans"}
		data.forms["gen_pl"] = {args[1] .. "om"}
		data.forms["dat_pl"] = {args[1] .. "emos"}
		data.forms["com_pl"] = {args[1] .. "evi(s)"}
	end
	
	postprocess(args, data)
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end


export["i-mf"] = function(frame)
	local params = {
		[1] = {required = true, default = "{{{1}}}"},
		
		["n"] = {},
		["nopl"] = {},
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local data = {forms = {}, info = "masculine/feminine i-stem", categories = {lang:getCanonicalName() .. " i-stem nouns"}}
	
	data.forms["nom_sg"] = {args[1] .. "is"}
	data.forms["voc_sg"] = {args[1] .. "e"}
	data.forms["acc_sg"] = {args[1] .. "im"}
	data.forms["gen_sg"] = {args[1] .. "ois"}
	data.forms["dat_sg"] = {args[1] .. "eie"}
	data.forms["com_sg"] = {args[1] .. "iē"}

	if not args.nopl then	
		data.forms["nom_du"] = {args[1] .. "ī"}
		data.forms["voc_du"] = {args[1] .. "ī"}
		data.forms["acc_du"] = {args[1] .. "ī"}
		data.forms["gen_du"] = {args[1] .. "jom"}
		data.forms["dat_du"] = {args[1] .. "ivos"}
		data.forms["com_du"] = {args[1] .. "ivis"}
		
		data.forms["nom_pl"] = {args[1] .. "eies"}
		data.forms["voc_pl"] = {args[1] .. "eies"}
		data.forms["acc_pl"] = {args[1] .. "ins"}
		data.forms["gen_pl"] = {args[1] .. "jom"}
		data.forms["dat_pl"] = {args[1] .. "ivos"}
		data.forms["com_pl"] = {args[1] .. "ivis"}
	end
	
	postprocess(args, data)
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end


export["i-n"] = function(frame)
	local params = {
		[1] = {required = true, default = "{{{1}}}"},
		
		["n"] = {},
		["nopl"] = {},
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local data = {forms = {}, info = "neuter i-stem", categories = {lang:getCanonicalName() .. " i-stem nouns"}}
	
	data.forms["nom_sg"] = {args[1]}
	data.forms["voc_sg"] = {args[1]}
	data.forms["acc_sg"] = {args[1]}
	data.forms["gen_sg"] = {args[1] .. "ois"}
	data.forms["dat_sg"] = {args[1] .. "eie"}
	data.forms["com_sg"] = {args[1] .. "iē"}

	if not args.nopl then	
		data.forms["nom_du"] = {args[1] .. "ī"}
		data.forms["voc_du"] = {args[1] .. "ī"}
		data.forms["acc_du"] = {args[1] .. "ī"}
		data.forms["gen_du"] = {args[1] .. "jom"}
		data.forms["dat_du"] = {args[1] .. "ivos"}
		data.forms["com_du"] = {args[1] .. "ivis"}
		
		data.forms["nom_pl"] = {args[1] .. "ī"}
		data.forms["voc_pl"] = {args[1] .. "ī"}
		data.forms["acc_pl"] = {args[1] .. "ī"}
		data.forms["gen_pl"] = {args[1] .. "jom"}
		data.forms["dat_pl"] = {args[1] .. "ivos"}
		data.forms["com_pl"] = {args[1] .. "ivis"}
	end
	
	postprocess(args, data)
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end


export["n-mf"] = function(frame)
	local params = {
		[1] = {required = true, default = "{{{1}}}"},
		
		["n"] = {},
		["nopl"] = {},
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
--	local delabial = mw.ustring.gsub(args[1], "ʷ$", "")
	
	local data = {forms = {}, info = "masculine/feminine n-stem", categories = {lang:getCanonicalName() .. " n-stem nouns"}}
	
	data.forms["nom_sg"] = {args[1] .. "ō"}
	data.forms["voc_sg"] = {args[1] .. "on"}
	data.forms["acc_sg"] = {args[1] .. "onam"}
	data.forms["gen_sg"] = {args[1] .. "ōnes"}
	data.forms["dat_sg"] = {args[1] .. "one"}
	data.forms["com_sg"] = {args[1] .. "ōne"}

	if not args.nopl then	
		data.forms["nom_du"] = {args[1] .. "one"}
		data.forms["voc_du"] = {args[1] .. "one"}
		data.forms["acc_du"] = {args[1] .. "one"}
		data.forms["gen_du"] = {args[1] .. "ōnom"}
		data.forms["dat_du"] = {args[1] .. "ōmos"}
		data.forms["com_du"] = {args[1] .. "ōvi(s)"}
		
		data.forms["nom_pl"] = {args[1] .. "ones"}
		data.forms["voc_pl"] = {args[1] .. "ones"}
		data.forms["acc_pl"] = {args[1] .. "onans"}
		data.forms["gen_pl"] = {args[1] .. "ōnom"}
		data.forms["dat_pl"] = {args[1] .. "ōmos"}
		data.forms["com_pl"] = {args[1] .. "ōvi(s)"}
	end
	
	postprocess(args, data)
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end


export["n-n"] = function(frame)
	local params = {
		[1] = {required = true, default = "{{{1}}}"},
		
		["n"] = {},
		["nopl"] = {},
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local data = {forms = {}, info = "neuter n-stem", categories = {lang:getCanonicalName() .. " n-stem nouns"}}
	
	data.forms["nom_sg"] = {args[1] .. "an"}
	data.forms["voc_sg"] = {args[1] .. "an"}
	data.forms["acc_sg"] = {args[1] .. "an"}
	data.forms["gen_sg"] = {args[1] .. "an(e)s"}
	data.forms["dat_sg"] = {args[1] .. "ane"}
	data.forms["com_sg"] = {args[1] .. "ane"}

	if not args.nopl then	
		data.forms["nom_du"] = {args[1] .. "anī"}
		data.forms["voc_du"] = {args[1] .. "anī"}
		data.forms["acc_du"] = {args[1] .. "anī"}
		data.forms["gen_du"] = {args[1] .. "ōnom"}
		data.forms["dat_du"] = {args[1] .. "ōmos"}
		data.forms["com_du"] = {args[1] .. "ōvi(s)"}
		
		data.forms["nom_pl"] = {args[1] .. "anā"}
		data.forms["voc_pl"] = {args[1] .. "anā"}
		data.forms["acc_pl"] = {args[1] .. "anā"}
		data.forms["gen_pl"] = {args[1] .. "ōnom"}
		data.forms["dat_pl"] = {args[1] .. "ōmos"}
		data.forms["com_pl"] = {args[1] .. "ōvi(s)"}
	end
	
	postprocess(args, data)
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end


export["o-m"] = function(frame)
	local params = {
		[1] = {required = true, default = "{{{1}}}"},
		
		["n"] = {},
		["nopl"] = {},
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
--	local delabial = mw.ustring.gsub(args[1], "ʷ$", "")
	
	local data = {forms = {}, info = "masculine o-stem", categories = {lang:getCanonicalName() .. " o-stem nouns"}}
	
	data.forms["nom_sg"] = {args[1] .. "os"}
	data.forms["voc_sg"] = {args[1] .. "e"}
	data.forms["acc_sg"] = {args[1] .. "om"}
	data.forms["gen_sg"] = {args[1] .. "osjo"}
	data.forms["dat_sg"] = {args[1] .. "ō"}
	data.forms["com_sg"] = {args[1] .. "ō"}
	
	if not args.nopl then
		data.forms["nom_du"] = {args[1] .. "ō"}
		data.forms["voc_du"] = {args[1] .. "ō"}
		data.forms["acc_du"] = {args[1] .. "ō"}
		data.forms["gen_du"] = {args[1] .. "ous"}
		data.forms["dat_du"] = {args[1] .. "ovām"}
		data.forms["com_du"] = {args[1] .. "ovām"}
	
		data.forms["nom_pl"] = {args[1] .. "ōs"}
		data.forms["voc_pl"] = {args[1] .. "ōs"}
		data.forms["acc_pl"] = {args[1] .. "ons"}
		data.forms["gen_pl"] = {args[1] .. "ōm"}
		data.forms["dat_pl"] = {args[1] .. "ōis"}
		data.forms["com_pl"] = {args[1] .. "ōis"}
	end
	postprocess(args, data)
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end


export["o-n"] = function(frame)
	local params = {
		[1] = {required = true, default = "{{{1}}}"},
		
		["n"] = {},
		["nopl"] = {},
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
--	local delabial = mw.ustring.gsub(args[1], "ʷ$", "")
	
	local data = {forms = {}, info = "neuter o-stem", categories =  {lang:getCanonicalName() .. " o-stem nouns"}}
	
	data.forms["nom_sg"] = {args[1] .. "om"}
	data.forms["voc_sg"] = {args[1] .. "om"}
	data.forms["acc_sg"] = {args[1] .. "om"}
	data.forms["gen_sg"] = {args[1] .. "osjo"}
	data.forms["dat_sg"] = {args[1] .. "ō"}
	data.forms["com_sg"] = {args[1] .. "ō"}

	if not args.nopl then	
		data.forms["nom_du"] = {args[1] .. "oī"}
		data.forms["voc_du"] = {args[1] .. "oī"}
		data.forms["acc_du"] = {args[1] .. "oī"}
		data.forms["gen_du"] = {args[1] .. "ous"}
		data.forms["dat_du"] = {args[1] .. "ovām"}
		data.forms["com_du"] = {args[1] .. "ovām"}
	
		data.forms["nom_pl"] = {args[1] .. "ā"}
		data.forms["voc_pl"] = {args[1] .. "ā"}
		data.forms["acc_pl"] = {args[1] .. "ā"}
		data.forms["gen_pl"] = {args[1] .. "ōm"}
		data.forms["dat_pl"] = {args[1] .. "ōis"}
		data.forms["com_pl"] = {args[1] .. "ōis"}
	end
	
	postprocess(args, data)
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end


export["r-mf"] = function(frame)
	local params = {
		[1] = {required = true, default = "{{{1}}}"},
		
		["n"] = {},
		["nopl"] = {},
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local data = {forms = {}, info = "masculine/feminine/neuter r-stem", categories = {lang:getCanonicalName() .. " r-stem nouns"}}
	
	data.forms["nom_sg"] = {args[1] .. "er"}
	data.forms["voc_sg"] = {args[1] .. "er"}
	data.forms["acc_sg"] = {args[1] .. "eram"}
	data.forms["gen_sg"] = {args[1] .. "eres"}
	data.forms["dat_sg"] = {args[1] .. "ere"}
	data.forms["com_sg"] = {args[1] .. "ere"}

	if not args.nopl then
		data.forms["nom_du"] = {args[1] .. "ere"}
		data.forms["voc_du"] = {args[1] .. "ere"}
		data.forms["acc_du"] = {args[1] .. "ere"}
		data.forms["gen_du"] = {args[1] .. "erom"}
		data.forms["dat_du"] = {args[1] .. "ermos"}
		data.forms["com_du"] = {args[1] .. "ervi(s)"}

		data.forms["nom_pl"] = {args[1] .. "eres"}
		data.forms["voc_pl"] = {args[1] .. "eres"}
		data.forms["acc_pl"] = {args[1] .. "erans"}
		data.forms["gen_pl"] = {args[1] .. "rom"}
		data.forms["dat_pl"] = {args[1] .. "ermos"}
		data.forms["com_pl"] = {args[1] .. "ervi(s)"}
	end
	
	postprocess(args, data)
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end


export["u-mf"] = function(frame)
	local params = {
		[1] = {required = true, default = "{{{1}}}"},
		
		["n"] = {},
		["nopl"] = {},
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local data = {forms = {}, info = "masculine/feminine u-stem", categories = {lang:getCanonicalName() .. " u-stem nouns"}}
	
	data.forms["nom_sg"] = {args[1] .. "us"}
	data.forms["voc_sg"] = {args[1] .. "u"}
	data.forms["acc_sg"] = {args[1] .. "um"}
	data.forms["gen_sg"] = {args[1] .. "eus"}
	data.forms["dat_sg"] = {args[1] .. "ewei"}
	data.forms["com_sg"] = {args[1] .. "ū"}

	if not args.nopl then	
		data.forms["nom_du"] = {args[1] .. "ū"}
		data.forms["voc_du"] = {args[1] .. "ū"}
		data.forms["acc_du"] = {args[1] .. "ū"}
		data.forms["gen_du"] = {args[1] .. "ewom"}
		data.forms["dat_du"] = {args[1] .. "umos"}
		data.forms["com_du"] = {args[1] .. "uvi(s)"}
		
		data.forms["nom_pl"] = {args[1] .. "ewes"}
		data.forms["voc_pl"] = {args[1] .. "ewes"}
		data.forms["acc_pl"] = {args[1] .. "uns"}
		data.forms["gen_pl"] = {args[1] .. "ewom"}
		data.forms["dat_pl"] = {args[1] .. "umos"}
		data.forms["com_pl"] = {args[1] .. "uvi(s)"}
	end
	
	postprocess(args, data)
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end


export["u-n"] = function(frame)
	local params = {
		[1] = {required = true, default = "{{{1}}}"},
		
		["n"] = {},
		["nopl"] = {},
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	if string.find(args[1], "a") then
	    vowel = mw.ustring.gsub(args[1], "ar$", "r")
	elseif string.find(args[1], "e") then
	    vowel = mw.ustring.gsub(args[1], "er$", "r")
	elseif string.find(args[1], "i") then
	    vowel = mw.ustring.gsub(args[1], "ir$", "r")
	elseif string.find(args[1], "o") then
	    vowel = mw.ustring.gsub(args[1], "or$", "r")
	elseif string.find(args[1], "u") then
	    vowel = mw.ustring.gsub(args[1], "ur$", "r")
	end
	
	local data = {forms = {}, info = "neuter u-stem", categories = {lang:getCanonicalName() .. " u-stem nouns"}}
	
	data.forms["nom_sg"] = {args[1] .. "u"}
	data.forms["voc_sg"] = {args[1] .. "u"}
	data.forms["acc_sg"] = {args[1] .. "u"}
	data.forms["gen_sg"] = {vowel .. "eus"}
	data.forms["dat_sg"] = {vowel .. "ewei"}
	data.forms["com_sg"] = {args[1] .. "ū"}

	if not args.nopl then	
		data.forms["nom_du"] = {args[1] .. "wī"}
		data.forms["voc_du"] = {args[1] .. "wī"}
		data.forms["acc_du"] = {args[1] .. "wī"}
		data.forms["gen_du"] = {vowel .. "ewom"}
		data.forms["dat_du"] = {vowel .. "umos"}
		data.forms["com_du"] = {args[1] .. "uvi(s)"}
		
		data.forms["nom_pl"] = {args[1] .. "ū"}
		data.forms["voc_pl"] = {args[1] .. "ū"}
		data.forms["acc_pl"] = {args[1] .. "ū"}
		data.forms["gen_pl"] = {vowel .. "ewom"}
		data.forms["dat_pl"] = {vowel .. "umos"}
		data.forms["com_pl"] = {args[1] .. "uvi(s)"}
	end
	
	postprocess(args, data)
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end


-- Irregular nouns

local names = {
	["nom"] = "nominative",
	["voc"] = "vocative",
	["acc"] = "accusative",
	["gen"] = "genitive",
	["dat"] = "dative",
	["com"] = "comitative",
	
	["sg"] = "singular",
	["du"] = "dual",
	["pl"] = "plural",
	
	["m"] = "masculine",
	["f"] = "feminine",
	["n"] = "neuter",
}

-- Make the table
function make_table(data)
	local function repl(param)
		if param == "info" then
			return mw.getContentLanguage():ucfirst(data.info or "")
		end
		
		local form = data.forms[param]
		
		if not form or #form == 0 then
			return "—"
		end
		
		local ret = {}
		
		for key, subform in ipairs(form) do
			table.insert(ret, m_links.full_link({lang = lang, alt = "" .. subform}))
		end
		
		return table.concat(ret, ", ")
	end
	
	local numbers = {"sg", "du", "pl"}
	local cases = {"nom", "voc", "acc", "gen", "dat", "com"}
	
	local wikicode = {}
	
	table.insert(wikicode, "{| class=\"inflection-table vsSwitcher mw-collapsible mw-collapsed\" data-toggle-category=\"inflection\" style=\"background: #FAFAFA; border: 1px solid #d0d0d0; text-align: left;\" cellspacing=\"1\" cellpadding=\"2\"")
	table.insert(wikicode, "|- style=\"background: #FFCCCC;\"\n! class=\"vsToggleElement\" style=\"min-width: 41em;\" colspan=\"" .. (#numbers + 1) .. "\" | {{{info}}}")
	
	table.insert(wikicode, "|- class=\"vsHide\" style=\"background: #FFCCCC;\"")
	table.insert(wikicode, "!")
	
	for _, number in ipairs(numbers) do
		table.insert(wikicode, "! style=\"min-width: 11em; background: #FFCCCC;\" | " .. names[number])
	end
	
	for _, case in ipairs(cases) do
		table.insert(wikicode, "|- class=\"vsHide\" style=\"background-color: #FFF2F2;\"\n! style=\"min-width: 8em; background-color: #FFE6E6;\" | " .. names[case])
		
		for _, number in ipairs(numbers) do
			table.insert(wikicode, "| {{{" .. case .. "_" .. number .. "}}}")
		end
	end
	
	table.insert(wikicode, "|}")
	
	wikicode = table.concat(wikicode, "\n")
	
	return (mw.ustring.gsub(wikicode, "{{{([a-z0-9_]+)}}}", repl))
end

return export