local m_links = require("Module:links")
local m_utilities = require("Module:utilities")
local m_common = require("Module:sl-common")

local export = {}

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

-- Functions that do the actual inflecting by creating the forms of a basic term.
local inflections = {}

-- The main entry point.
-- This is the only function that can be invoked from a template.
function export.show(frame)
	local infl_type = frame.args[1] or error("Inflection type has not been specified.")
	local args = frame:getParent().args
	local num = args["n"]; if num == "" then num = nil end
	
	local data = {forms = {}, title = nil, categories = {}, num = num}
	
	if inflections[infl_type] then
		inflections[infl_type](args, data)
	else
		error("Unknown inflection type '" .. infl_type .. "'")
	end
	
	check_accents(args, data)
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end

-- Checks each form for the presence of accent marks.
-- If any form doesn't contain any accent marks, flag the entry for attention.
function check_accents(args, data)
	-- Do we need to check?
	local noacc = args["noacc"]; if noacc == "" then noacc = nil end
	
	if noacc then
		return
	end
	
	-- Yes, we do need to check.
	for key, subforms in pairs(data.forms) do
		for key2, subform in ipairs(subforms) do
			if not m_common.has_accents(subform) then
				table.insert(data.categories, "Slovene nouns needing accents")
				return  -- So that we don't add the category more than once
			end
		end
	end
end

-- Masculine o-stem
inflections["m"] = function(args, data)
	local stem_noending = args[1]; if stem_noending == "" then stem_noending = nil end
	local stem = args[2]; if stem == "" then stem = nil end
	local anim = args["a"]; if not anim or anim == "" then anim = false else anim = true end
	
	local nom_pl = args["nom_pl"]; if nom_pl == "" then nom_pl = nil end
	local nom_pl2 = args["nom_pl2"]; if nom_pl2 == "" then nom_pl2 = nil end
	
	if not stem_noending then
		if mw.title.getCurrentTitle().nsText == "Template" then
			stem_noending = "-"
		else
			error("1st parameter (stem) has not been specified.")
		end
	end
	
	if not stem then
		stem = stem_noending
		stem_noending = make_endingless_stem(stem)
	end
	
	-- Is this a hard stem or a soft stem?
	local oe = "o"
	local hs = "hard"
	data.title = "masculine " .. (anim and "anim." or "inan.")
	
	if m_common.is_soft(stem) then
		oe = "e"
		hs = "soft"
	end
	
	data.title = data.title .. ", " .. hs .. " o-stem"
	table.insert(data.categories, "Slovene masculine " .. hs .. " o-stem nouns")
	
	-- Categorize further
	local stem_noaccents = m_common.remove_accents(stem)
	local stem_noending_noaccents = m_common.remove_accents(stem_noending)
	local corrected_stem = nil
	
	if stem_noaccents == stem_noending_noaccents then
		corrected_stem = stem_noending
	elseif stem_noaccents == mw.ustring.gsub(stem_noending_noaccents, "e([ln]j)$", "%1") then
		corrected_stem = mw.ustring.gsub(stem_noending, "e([ln]j)$", "%1")
	elseif stem_noaccents == mw.ustring.gsub(stem_noending_noaccents, "e(.)$", "%1") then
		corrected_stem = mw.ustring.gsub(stem_noending, "e(.)$", "%1")
	elseif stem_noaccents == mw.ustring.gsub(stem_noending_noaccents, "[aeiou]$", "") then
		table.insert(data.categories, "Slovene masculine o-stem nouns with final vowel")
		corrected_stem = mw.ustring.gsub(stem_noending, "[aeiou]$", "")
	elseif stem_noaccents == stem_noending_noaccents .. "j" then
		table.insert(data.categories, "Slovene masculine soft o-stem nouns with j-infix")
		corrected_stem = stem_noending .. "j"
	elseif stem_noaccents == stem_noending_noaccents .. "-j" then
		table.insert(data.categories, "Slovene masculine soft o-stem nouns with j-infix")
		corrected_stem = stem_noending .. "-j"
	elseif stem_noaccents == stem_noending_noaccents .. "n" then
		table.insert(data.categories, "Slovene masculine hard o-stem nouns with n-infix")
		corrected_stem = stem_noending .. "n"
	elseif stem_noaccents == stem_noending_noaccents .. "t" then
		table.insert(data.categories, "Slovene masculine hard o-stem nouns with t-infix")
		corrected_stem = stem_noending .. "t"
	else
		table.insert(data.categories, "Slovene irregular nouns")
	end
	
	if corrected_stem and stem ~= corrected_stem then
		table.insert(data.categories, "Slovene nouns with accent alternations")
	end
	
	-- Singular
	data.forms["nom_sg"] = {stem_noending}
	data.forms["acc_sg"] = {anim and stem .. "a" or stem_noending}
	data.forms["gen_sg"] = {stem .. "a"}
	data.forms["dat_sg"] = {stem .. "u"}
	data.forms["loc_sg"] = {stem .. "u"}
	data.forms["ins_sg"] = {stem .. oe .. "m"}
	
	-- Dual
	data.forms["nom_du"] = {stem .. "a"}
	data.forms["acc_du"] = {stem .. "a"}
	data.forms["gen_du"] = {stem .. oe .. "v"}
	data.forms["dat_du"] = {stem .. oe .. "ma"}
	data.forms["loc_du"] = {stem .. "ih"}
	data.forms["ins_du"] = {stem .. oe .. "ma"}
	
	-- Plural
	data.forms["nom_pl"] = {nom_pl or stem .. "i", nom_pl2}
	data.forms["acc_pl"] = {stem .. "e"}
	data.forms["gen_pl"] = {stem .. oe .. "v"}
	data.forms["dat_pl"] = {stem .. oe .. "m"}
	data.forms["loc_pl"] = {stem .. "ih"}
	data.forms["ins_pl"] = {stem .. "i"}
end

-- Masculine o-stem, plural in -ôv-
inflections["m-ov"] = function(args, data)
	local stem_noending = args[1]; if stem_noending == "" then stem_noending = nil end
	local stem = args[2]; if stem == "" then stem = nil end
	local gen_sg_ending = args[3]; if gen_sg_ending == "" then gen_sg_ending = nil end
	local anim = args["a"]; if not anim or anim == "" then anim = false else anim = true end
	
	if not stem_noending then
		if mw.title.getCurrentTitle().nsText == "Template" then
			stem_noending = "-"
		else
			error("1st parameter (stem) has not been specified.")
		end
	end
	
	if not stem then
		stem = stem_noending
		stem_noending = make_endingless_stem(stem)
	end
	
	local stem_noaccents = m_common.remove_accents(stem)
	
	local gen_sg = stem .. "a"
	
	if gen_sg_ending == "á" or gen_sg_ending == "ú" then
		gen_sg = stem_noaccents .. gen_sg_ending
	elseif gen_sg_ending then
		error("3rd parameter (genitive singular ending) must be \"á\" or \"ú\".")
	end
	
	-- Is this a hard stem or a soft stem?
	local oe = "o"
	local hs = "hard"
	data.title = "masculine " .. (anim and "anim." or "inan.")
	
	if m_common.is_soft(stem) then
		oe = "e"
		hs = "soft"
	end
	
	data.title = data.title .. ", " .. hs .. " o-stem"
	table.insert(data.categories, "Slovene masculine " .. hs .. " o-stem nouns")
	
	if gen_sg_ending then
		data.title = data.title .. ", mobile accent"
		table.insert(data.categories, "Slovene masculine o-stem nouns with mobile accent")
	end
	
	if data.num ~= "sg" then
		data.title = data.title .. ", plural in " .. m_links.full_link({lang = lang, alt = "-ôv-"}, "term")
		table.insert(data.categories, "Slovene masculine o-stem nouns with plural in -ov-")
	end
	
	-- Categorize further
	local stem_noending_noaccents = m_common.remove_accents(stem_noending)
	local corrected_stem = nil
	
	if stem_noaccents == stem_noending_noaccents then
		corrected_stem = stem_noending
	elseif stem_noaccents == mw.ustring.gsub(stem_noending_noaccents, "e([ln]j)$", "%1") then
		corrected_stem = mw.ustring.gsub(stem_noending, "e([ln]j)$", "%1")
	elseif stem_noaccents == mw.ustring.gsub(stem_noending_noaccents, "e(.)$", "%1") then
		corrected_stem = mw.ustring.gsub(stem_noending, "e(.)$", "%1")
	elseif stem_noaccents == mw.ustring.gsub(stem_noending_noaccents, "[aeiou]$", "") then
		table.insert(data.categories, "Slovene masculine o-stem nouns with final vowel")
		corrected_stem = mw.ustring.gsub(stem_noending, "[aeiou]$", "")
	elseif stem_noaccents == stem_noending_noaccents .. "j" then
		table.insert(data.categories, "Slovene masculine soft o-stem nouns with j-infix")
		corrected_stem = stem_noending .. "j"
	elseif stem_noaccents == stem_noending_noaccents .. "-j" then
		table.insert(data.categories, "Slovene masculine soft o-stem nouns with j-infix")
		corrected_stem = stem_noending .. "-j"
	elseif stem_noaccents == stem_noending_noaccents .. "n" then
		table.insert(data.categories, "Slovene masculine hard o-stem nouns with n-infix")
		corrected_stem = stem_noending .. "n"
	elseif stem_noaccents == stem_noending_noaccents .. "t" then
		table.insert(data.categories, "Slovene masculine hard o-stem nouns with t-infix")
		corrected_stem = stem_noending .. "t"
	else
		table.insert(data.categories, "Slovene irregular nouns")
	end
	
	if corrected_stem and stem ~= corrected_stem then
		table.insert(data.categories, "Slovene nouns with accent alternations")
	end
	
	-- Singular
	data.forms["nom_sg"] = {stem_noending}
	data.forms["acc_sg"] = {anim and gen_sg or stem_noending}
	data.forms["gen_sg"] = {gen_sg}
	data.forms["dat_sg"] = {stem .. "u"}
	data.forms["loc_sg"] = {stem .. "u"}
	data.forms["ins_sg"] = {stem .. oe .. "m"}
	
	-- Dual
	data.forms["nom_du"] = {stem_noaccents .. "ôva"}
	data.forms["acc_du"] = {stem_noaccents .. "ôva"}
	data.forms["gen_du"] = {stem_noaccents .. "ôv"}
	data.forms["dat_du"] = {stem_noaccents .. "ôvoma"}
	data.forms["loc_du"] = {stem_noaccents .. "ôvih"}
	data.forms["ins_du"] = {stem_noaccents .. "ôvoma"}
	
	-- Plural
	data.forms["nom_pl"] = {stem_noaccents .. "ôvi"}
	data.forms["acc_pl"] = {stem_noaccents .. "ôve"}
	data.forms["gen_pl"] = {stem_noaccents .. "ôv"}
	data.forms["dat_pl"] = {stem_noaccents .. "ôvom"}
	data.forms["loc_pl"] = {stem_noaccents .. "ôvih"}
	data.forms["ins_pl"] = {stem_noaccents .. "ôvi"}
end

-- Masculine a-stem
inflections["m-a"] = function(args, data)
	local stem = args[1]; if stem == "" then stem = nil end
	local anim = args["a"]; if not anim or anim == "" then anim = false else anim = true end
	
	if not stem then
		if mw.title.getCurrentTitle().nsText == "Template" then
			stem = "-"
		else
			error("1st parameter (stem) has not been specified.")
		end
	end
	
	local stem_noending = make_endingless_stem(stem)
	
	data.title = "masculine " .. (anim and "anim." or "inan.") .. ", a-stem"
	table.insert(data.categories, "Slovene masculine a-stem nouns")
	
	-- Singular
	data.forms["nom_sg"] = {stem .. "a"}
	data.forms["acc_sg"] = {stem .. "o"}
	data.forms["gen_sg"] = {stem .. "e"}
	data.forms["dat_sg"] = {stem .. "i"}
	data.forms["loc_sg"] = {stem .. "i"}
	data.forms["ins_sg"] = {stem .. "o"}
	
	-- Dual
	data.forms["nom_du"] = {stem .. "i"}
	data.forms["acc_du"] = {stem .. "i"}
	data.forms["gen_du"] = {stem_noending}
	data.forms["dat_du"] = {stem .. "ama"}
	data.forms["loc_du"] = {stem .. "ah"}
	data.forms["ins_du"] = {stem .. "ama"}
	
	-- Plural
	data.forms["nom_pl"] = {stem .. "e"}
	data.forms["acc_pl"] = {stem .. "e"}
	data.forms["gen_pl"] = {stem_noending}
	data.forms["dat_pl"] = {stem .. "am"}
	data.forms["loc_pl"] = {stem .. "ah"}
	data.forms["ins_pl"] = {stem .. "ami"}
end

-- Feminine a-stem
inflections["f"] = function(args, data)
	local stem = args[1]; if stem == "" then stem = nil end
	
	if not stem then
		if mw.title.getCurrentTitle().nsText == "Template" then
			stem = "-"
		else
			error("1st parameter (stem) has not been specified.")
		end
	end
	
	local stem_noending = make_endingless_stem(stem)
	
	data.title = "feminine, a-stem"
	table.insert(data.categories, "Slovene feminine a-stem nouns")
	
	-- Singular
	data.forms["nom_sg"] = {stem .. "a"}
	data.forms["acc_sg"] = {stem .. "o"}
	data.forms["gen_sg"] = {stem .. "e"}
	data.forms["dat_sg"] = {stem .. "i"}
	data.forms["loc_sg"] = {stem .. "i"}
	data.forms["ins_sg"] = {stem .. "o"}
	
	-- Dual
	data.forms["nom_du"] = {stem .. "i"}
	data.forms["acc_du"] = {stem .. "i"}
	data.forms["gen_du"] = {stem_noending}
	data.forms["dat_du"] = {stem .. "ama"}
	data.forms["loc_du"] = {stem .. "ah"}
	data.forms["ins_du"] = {stem .. "ama"}
	
	-- Plural
	data.forms["nom_pl"] = {stem .. "e"}
	data.forms["acc_pl"] = {stem .. "e"}
	data.forms["gen_pl"] = {stem_noending}
	data.forms["dat_pl"] = {stem .. "am"}
	data.forms["loc_pl"] = {stem .. "ah"}
	data.forms["ins_pl"] = {stem .. "ami"}
end

-- Feminine a-stem, mobile accent paradigm
inflections["f-mobile"] = function(args, data)
	local stem = args[1]; if stem == "" then stem = nil end
	local gen_pl = args["gen_pl"]; if gen_pl == "" then gen_pl = nil end
	
	if not stem then
		if mw.title.getCurrentTitle().nsText == "Template" then
			stem = "-"
		else
			error("1st parameter (stem) has not been specified.")
		end
	end
	
	local stem_noaccents = m_common.remove_accents(stem)
	gen_pl = gen_pl or stem_noaccents .. "á"
	
	data.title = "feminine, a-stem, mobile accent"
	table.insert(data.categories, "Slovene feminine a-stem nouns")
	table.insert(data.categories, "Slovene feminine a-stem nouns with mobile accent")
	
	-- Singular
	data.forms["nom_sg"] = {stem .. "a"}
	data.forms["acc_sg"] = {stem_noaccents .. "ó"}
	data.forms["gen_sg"] = {stem_noaccents .. "é"}
	data.forms["dat_sg"] = {stem .. "i"}
	data.forms["loc_sg"] = {stem .. "i"}
	data.forms["ins_sg"] = {stem_noaccents .. "ó"}
	
	-- Dual
	data.forms["nom_du"] = {stem_noaccents .. "é"}
	data.forms["acc_du"] = {stem_noaccents .. "é"}
	data.forms["gen_du"] = {gen_pl}
	data.forms["dat_du"] = {stem_noaccents .. "áma"}
	data.forms["loc_du"] = {stem_noaccents .. "àh"}
	data.forms["ins_du"] = {stem_noaccents .. "áma"}
	
	-- Plural
	data.forms["nom_pl"] = {stem_noaccents .. "é"}
	data.forms["acc_pl"] = {stem_noaccents .. "é"}
	data.forms["gen_pl"] = {gen_pl}
	data.forms["dat_pl"] = {stem_noaccents .. "àm"}
	data.forms["loc_pl"] = {stem_noaccents .. "àh"}
	data.forms["ins_pl"] = {stem_noaccents .. "ámi"}
end

-- Feminine v-stem
inflections["f-v"] = function(args, data)
	local stem = args[1]; if stem == "" then stem = nil end
	
	if not stem then
		if mw.title.getCurrentTitle().nsText == "Template" then
			stem = "-"
		else
			error("1st parameter (stem) has not been specified.")
		end
	end
	
	local stem_noending = stem .. "ev"
	stem = stem .. "v"
	
	data.title = "feminine, v-stem"
	table.insert(data.categories, "Slovene feminine v-stem nouns")
	
	-- Singular
	data.forms["nom_sg"] = {stem_noending}
	data.forms["acc_sg"] = {stem_noending}
	data.forms["gen_sg"] = {stem .. "e"}
	data.forms["dat_sg"] = {stem .. "i"}
	data.forms["loc_sg"] = {stem .. "i"}
	data.forms["ins_sg"] = {stem .. "ijo"}
	
	-- Dual
	data.forms["nom_du"] = {stem .. "i"}
	data.forms["acc_du"] = {stem .. "i"}
	data.forms["gen_du"] = {stem_noending}
	data.forms["dat_du"] = {stem .. "ama"}
	data.forms["loc_du"] = {stem .. "ah"}
	data.forms["ins_du"] = {stem .. "ama"}
	
	-- Plural
	data.forms["nom_pl"] = {stem .. "e"}
	data.forms["acc_pl"] = {stem .. "e"}
	data.forms["gen_pl"] = {stem_noending}
	data.forms["dat_pl"] = {stem .. "am"}
	data.forms["loc_pl"] = {stem .. "ah"}
	data.forms["ins_pl"] = {stem .. "ami"}
end

-- Feminine i-stem
inflections["f-i"] = function(args, data)
	local stem = args[1]; if stem == "" then stem = nil end
	
	if not stem then
		if mw.title.getCurrentTitle().nsText == "Template" then
			stem = "-"
		else
			error("1st parameter (stem) has not been specified.")
		end
	end
	
	local stem_noending = make_endingless_stem(stem)
	
	data.title = "feminine, i-stem"
	table.insert(data.categories, "Slovene feminine i-stem nouns")
	
	-- Singular
	data.forms["nom_sg"] = {stem_noending}
	data.forms["acc_sg"] = {stem_noending}
	data.forms["gen_sg"] = {stem .. "i"}
	data.forms["dat_sg"] = {stem .. "i"}
	data.forms["loc_sg"] = {stem .. "i"}
	data.forms["ins_sg"] = {stem .. (stem ~= stem_noending and "i" or "") .. "jo"}
	
	-- Dual
	data.forms["nom_du"] = {stem .. "i"}
	data.forms["acc_du"] = {stem .. "i"}
	data.forms["gen_du"] = {stem .. "i"}
	data.forms["dat_du"] = {stem .. (stem ~= stem_noending and "i" or "") .. "ma"}
	data.forms["loc_du"] = {stem .. "ih"}
	data.forms["ins_du"] = {stem .. (stem ~= stem_noending and "i" or "") .. "ma"}
	
	-- Plural
	data.forms["nom_pl"] = {stem .. "i"}
	data.forms["acc_pl"] = {stem .. "i"}
	data.forms["gen_pl"] = {stem .. "i"}
	data.forms["dat_pl"] = {stem .. "im"}
	data.forms["loc_pl"] = {stem .. "ih"}
	data.forms["ins_pl"] = {stem .. (stem ~= stem_noending and "i" or "") .. "mi"}
end

-- Feminine i-stem, mobile accent paradigm
inflections["f-i-mobile"] = function(args, data)
	local stem = args[1]; if stem == "" then stem = nil end
	local stem_dat_loc_sg = args[2]; if stem_dat_loc_sg == "" then stem_dat_loc_sg = nil end
	
	if not stem then
		if mw.title.getCurrentTitle().nsText == "Template" then
			stem = "-"
		else
			error("1st parameter (stem) has not been specified.")
		end
	end
	
	local stem_noending = make_endingless_stem(stem)
	local stem_noaccents = m_common.remove_accents(stem)
	stem_dat_loc_sg = stem_dat_loc_sg or stem
	
	data.title = "feminine, i-stem, mobile accent"
	table.insert(data.categories, "Slovene feminine i-stem nouns")
	table.insert(data.categories, "Slovene feminine i-stem nouns with mobile accent")
	
	-- Singular
	data.forms["nom_sg"] = {stem_noending}
	data.forms["acc_sg"] = {stem_noending}
	data.forms["gen_sg"] = {stem_noaccents .. "í"}
	data.forms["dat_sg"] = {stem_dat_loc_sg .. "i"}
	data.forms["loc_sg"] = {stem_dat_loc_sg .. "i"}
	data.forms["ins_sg"] = {stem_noaccents .. (stem ~= stem_noending and "i" or "") .. "jó"}
	
	-- Dual
	data.forms["nom_du"] = {stem_noaccents .. "í"}
	data.forms["acc_du"] = {stem_noaccents .. "í"}
	data.forms["gen_du"] = {stem_noaccents .. "í"}
	data.forms["dat_du"] = {stem_noaccents .. "éma"}
	data.forms["loc_du"] = {stem_noaccents .. "éh"}
	data.forms["ins_du"] = {stem_noaccents .. "éma"}
	
	-- Plural
	data.forms["nom_pl"] = {stem_noaccents .. "í"}
	data.forms["acc_pl"] = {stem_noaccents .. "í"}
	data.forms["gen_pl"] = {stem_noaccents .. "í"}
	data.forms["dat_pl"] = {stem_noaccents .. "ém"}
	data.forms["loc_pl"] = {stem_noaccents .. "éh"}
	data.forms["ins_pl"] = {stem_noaccents .. (stem ~= stem_noending and "i" or "") .. "mí"}
end

-- Neuter o-stem
inflections["n"] = function(args, data)
	local stem = args[1]; if stem == "" then stem = nil end
	
	if not stem then
		if mw.title.getCurrentTitle().nsText == "Template" then
			stem = "-"
		else
			error("1st parameter (stem) has not been specified.")
		end
	end
	
	local stem_noending = make_endingless_stem(stem)
	
	-- Is this a hard stem or a soft stem?
	local oe = "o"
	data.title = "neuter"
	
	if m_common.is_soft(stem) then
		oe = "e"
		data.title = data.title .. ", soft"
		table.insert(data.categories, "Slovene neuter soft o-stem nouns")
	else
		data.title = data.title .. ", hard"
		table.insert(data.categories, "Slovene neuter hard o-stem nouns")
	end
	
	-- Singular
	data.forms["nom_sg"] = {stem .. oe}
	data.forms["acc_sg"] = {stem .. oe}
	data.forms["gen_sg"] = {stem .. "a"}
	data.forms["dat_sg"] = {stem .. "u"}
	data.forms["loc_sg"] = {stem .. "u"}
	data.forms["ins_sg"] = {stem .. oe .. "m"}
	
	-- Dual
	data.forms["nom_du"] = {stem .. "i"}
	data.forms["acc_du"] = {stem .. "i"}
	data.forms["gen_du"] = {stem_noending}
	data.forms["dat_du"] = {stem .. oe .. "ma"}
	data.forms["loc_du"] = {stem .. "ih"}
	data.forms["ins_du"] = {stem .. oe .. "ma"}
	
	-- Plural
	data.forms["nom_pl"] = {stem .. "a"}
	data.forms["acc_pl"] = {stem .. "a"}
	data.forms["gen_pl"] = {stem_noending}
	data.forms["dat_pl"] = {stem .. oe .. "m"}
	data.forms["loc_pl"] = {stem .. "ih"}
	data.forms["ins_pl"] = {stem .. "i"}
end

function make_endingless_stem(stem)
	local stem_noaccents = mw.ustring.lower(m_common.remove_accents(stem))
	
	if mw.ustring.find(stem_noaccents, "[^aeioujlrv][ln]j$") then
		return mw.ustring.gsub(stem, "([ln]j)$", "e%1")
	elseif mw.ustring.find(stem_noaccents, "[^aeiouln]j$") then
		return mw.ustring.gsub(stem, "j$", "ij")
	elseif mw.ustring.find(stem_noaccents, "[^aeiou]r$") or mw.ustring.find(stem_noaccents, "[^aeioujlrv][lmnv]$") then
		return mw.ustring.gsub(stem, "(.)$", "e%1")
	else
		return stem
	end
end

-- Make the table
function make_table(data)
	if data.num then
		return make_table_single(data)
	else
		return make_table_full(data)
	end
end

function make_table_full(data)
	return [=[
<div class="NavFrame" style="width: 70em">
<div class="NavHead" style="background:#d9ebff; text-align: left">การผันรูปของ ]=] .. m_links.full_link({lang = lang, alt = data.forms["nom_sg"][1]}, "term") .. (data.title and " (" .. data.title .. ")" or "") .. [=[</div>
<div class="NavContent">
{| style="background:#F9F9F9;text-align:center;width:100%" class="inflection-table"
|-
! style="background:#d9ebff;width:16%" | 
! style="background:#d9ebff;width:28%" | singular
! style="background:#d9ebff;width:28%" | dual
! style="background:#d9ebff;width:28%" | plural
|-
! style="background:#eff7ff" | nominative
| ]=] .. show_forms(data.forms["nom_sg"]) .. [=[

| ]=] .. show_forms(data.forms["nom_du"]) .. [=[

| ]=] .. show_forms(data.forms["nom_pl"]) .. [=[

|-
! style="background:#eff7ff" | accusative
| ]=] .. show_forms(data.forms["acc_sg"]) .. [=[

| ]=] .. show_forms(data.forms["acc_du"]) .. [=[

| ]=] .. show_forms(data.forms["acc_pl"]) .. [=[

|-
! style="background:#eff7ff" | genitive
| ]=] .. show_forms(data.forms["gen_sg"]) .. [=[

| ]=] .. show_forms(data.forms["gen_du"]) .. [=[

| ]=] .. show_forms(data.forms["gen_pl"]) .. [=[

|-
! style="background:#eff7ff" | dative
| ]=] .. show_forms(data.forms["dat_sg"]) .. [=[

| ]=] .. show_forms(data.forms["dat_du"]) .. [=[

| ]=] .. show_forms(data.forms["dat_pl"]) .. [=[

|-
! style="background:#eff7ff" | locative
| ]=] .. show_forms(data.forms["loc_sg"]) .. [=[

| ]=] .. show_forms(data.forms["loc_du"]) .. [=[

| ]=] .. show_forms(data.forms["loc_pl"]) .. [=[

|-
! style="background:#eff7ff" | instrumental
| ]=] .. show_forms(data.forms["ins_sg"]) .. [=[

| ]=] .. show_forms(data.forms["ins_du"]) .. [=[

| ]=] .. show_forms(data.forms["ins_pl"]) .. [=[

|}</div></div>]=]
end

function make_table_single(data)
	return [=[
<div class="NavFrame" style="width: 31em">
<div class="NavHead" style="background:#d9ebff; text-align: left">Declension of ]=] .. m_links.full_link({lang = lang, alt = data.forms["nom_" .. data.num][1]}, "term") .. (data.title and " (" .. data.title .. ")" or "") .. [=[</div>
<div class="NavContent">
{| style="background:#F9F9F9;text-align:center;width:100%" class="inflection-table"
|-
! style="background:#d9ebff;width:36%" | 
! style="background:#d9ebff;width:64%" | ]=] .. (data.num == "pl" and "plural" or data.num == "du" and "dual" or "singular") .. [=[

|-
! style="background:#eff7ff" | nominative
| ]=] .. show_forms(data.forms["nom_" .. data.num]) .. [=[

|-
! style="background:#eff7ff" | accusative
| ]=] .. show_forms(data.forms["acc_" .. data.num]) .. [=[

|-
! style="background:#eff7ff" | genitive
| ]=] .. show_forms(data.forms["gen_" .. data.num]) .. [=[

|-
! style="background:#eff7ff" | dative
| ]=] .. show_forms(data.forms["dat_" .. data.num]) .. [=[

|-
! style="background:#eff7ff" | locative
| ]=] .. show_forms(data.forms["loc_" .. data.num]) .. [=[

|-
! style="background:#eff7ff" | instrumental
| ]=] .. show_forms(data.forms["ins_" .. data.num]) .. [=[

|}</div></div>]=]
end

function show_forms(subforms)
	if not subforms then
		return "&mdash;"
	elseif type(subforms) ~= "table" then
		subforms = {subforms}
	end
	
	local ret = {}
	
	for key, subform in ipairs(subforms) do
		table.insert(ret, m_links.full_link({lang = lang, term = subform}))
	end
	
	return table.concat(ret, ",<br/>")
end

return export