Module:VL-noun

From Wiktionary, the free dictionary
Jump to navigation Jump to search

This module needs documentation.
Please document this module by describing its purpose and usage on the documentation page.

local m_links = require("Module:links")
local m_utils = require("Module:la-utilities")
local m_vl_translit = require("Module:VL-translit")
local title = mw.title.getCurrentTitle()
local lang = require("Module:languages").getByCode("la")

local export = {}

local branch_links = {
	["It-W"] = "[[w:Italo-Western languages|Italo-Western]]",
	["E"] = "[[w:Eastern Romance languages|Eastern]]",
	["S"] = "[[w:Sardinian language|Sardinian]]",
}

local decl_names = {
	["1st"] = "First declension",
	["2nd"] = "Second declension",
	["2nd-er"] = "Second declension, nominative singular in ''-er''",
	["2nd-N"] = "Second declension neuter",
	["3rd"] = "Third declension",
}

local decls = {}

decls["1st"] = {
	params = {
		[1] = {},
		},
}
setmetatable(decls["1st"], {__call = function(self, args, data)
	data.forms["nom-sg"] = args[1] .. "a"
	data.forms["nom-pl"] = args[1] .. "ae"
	
	data.forms["gen-sg"] = args[1] .. "ae"
	data.forms["gen-pl"] = args[1] .. "ārum"
	
	data.forms["dat-sg"] = args[1] .. "ae"
	data.forms["dat-pl"] = args[1] .. "īs"
	
	data.forms["acc-abl-sg"] = args[1] .. "am"
	data.forms["acc-abl-pl"] = args[1] .. "ās"
end
})

decls["2nd"] = {
	params = {
		[1] = {},
		},
}
setmetatable(decls["2nd"], {__call = function(self, args, data)
	data.forms["nom-sg"] = args[1] .. "us"
	data.forms["nom-pl"] = args[1] .. "ī"
	
	data.forms["gen-sg"] = args[1] .. "ī"
	data.forms["gen-pl"] = args[1] .. "ōrum"
	
	data.forms["dat-sg"] = args[1] .. "ō"
	data.forms["dat-pl"] = args[1] .. "īs"
	
	data.forms["acc-abl-sg"] = args[1] .. "um"
	data.forms["acc-abl-pl"] = args[1] .. "ōs"
end
})

decls["2nd-er"] = {
	params = {
		[1] = {},
		[2] = {},
		},
}
setmetatable(decls["2nd-er"], {__call = function(self, args, data)
	data.forms["nom-sg"] = args[1]
	data.forms["nom-pl"] = args[2] .. "ī"
	
	data.forms["gen-sg"] = args[2] .. "ī"
	data.forms["gen-pl"] = args[2] .. "ōrum"
	
	data.forms["dat-sg"] = args[2] .. "ō"
	data.forms["dat-pl"] = args[2] .. "īs"
	
	data.forms["acc-abl-sg"] = args[2] .. "um"
	data.forms["acc-abl-pl"] = args[2] .. "ōs"
end
})

decls["2nd-N"] = {
	params = {
		[1] = {},
		},
}
setmetatable(decls["2nd-N"], {__call = function(self, args, data)
	data.forms["nom-sg"] = args[1] .. "um"
	data.forms["nom-pl"] = args[1] .. "a"
	
	data.forms["gen-sg"] = args[1] .. "ī"
	data.forms["gen-pl"] = args[1] .. "ōrum"
	
	data.forms["dat-sg"] = args[1] .. "ō"
	data.forms["dat-pl"] = args[1] .. "īs"
	
	data.forms["acc-abl-sg"] = args[1] .. "um"
	data.forms["acc-abl-pl"] = args[1] .. "a"
end
})

decls["3rd"] = {
	params = {
		[1] = {},
		[2] = {},
		},
}
setmetatable(decls["3rd"], {__call = function(self, args, data)
	if not args[2] then args[2] = m_utils.make_stem2(args[1]) end
	
	data.forms["nom-sg"] = args[1]
	data.forms["nom-pl"] = args[2] .. "is"
	
	data.forms["gen-sg"] = args[2] .. "is"
	data.forms["gen-pl"] = args[2] .. "um"
	
	data.forms["dat-sg"] = args[2] .. "ī"
	data.forms["dat-pl"] = args[2] .. "ibus"
	
	data.forms["acc-abl-sg"] = args[2] .. "em"
	data.forms["acc-abl-pl"] = args[2] .. "ēs"
end
})

local cases = {
	["nominative"] = {short_form = "nom", link = "''[[nominative case|nominative]]''"},
	["accusative-ablative"] = {short_form = "acc-abl", link = "''[[accusative case|accusative]]-[[ablative case|ablative]]''"},
	["genitive"] = {short_form = "gen", link = "''[[genitive case|genitive]]''"},
	["dative"] = {short_form = "dat", link = "''[[dative case|dative]]''"},
}

local function make_headers(data, wikicode)
	table.insert(wikicode, '|- class="vsHide"\n')
	table.insert(wikicode, '! style="background:#' .. data.latincolour[3] .. '; font-style:italic;" | Number\n')
	table.insert(wikicode, '! style="background:#' .. data.latincolour[3] .. '; font-style:italic;" | Singular\n')
	table.insert(wikicode, '! style="background:#' .. data.latincolour[3] .. '; font-style:italic;" | Plural\n')
end

local function show_form(form, data, branch)
	local function convert_word(form, branch)
		if mw.ustring.find(form, "{{{") then
			return form
		else
			return m_vl_translit.convert_words(form, branch)
		end
	end

	if not form then
		return "—"
	end
	
	
	if title:inNamespace("") then
		return m_links.full_link({lang = lang, alt = convert_word(form, branch), term = form})
	else
		return m_links.full_link({lang = lang, alt = convert_word(form, branch), term = "*" .. form})
	end
end

local function make_case(data, wikicode, case, branch)
	local case_short = cases[case].short_form
	table.insert(wikicode, '|- class="vsHide"\n')
	table.insert(wikicode, '! style="background:#' .. data.latincolour[1] .. ';" | ' .. cases[case].link .. '\n')
	table.insert(wikicode, '| style="background:#' .. data.latincolour[2] .. ';" | ' .. show_form(data.forms[case_short .. '-sg'], data, branch) .. '\n')
	table.insert(wikicode, '| style="background:#' .. data.latincolour[2] .. ';" | ' .. show_form(data.forms[case_short .. '-pl'], data, branch) .. '\n')
end

local function make_table(data, branch)
	local wikicode = {'{| class="prettytable inflection-table vsSwitcher" data-toggle-category="inflection"\n'}
	table.insert(wikicode, '! colspan="5" class="vsToggleElement" style="text-align:left;min-width: 25em;background:#' ..
				 data.latincolour[1] .. '" | ')
	table.insert(wikicode, branch_links[branch] .. ' declension of #head\n')

	make_headers(data, wikicode)
	make_case(data, wikicode, "nominative", branch)
	make_case(data, wikicode, "genitive", branch)
	make_case(data, wikicode, "dative", branch)
	make_case(data, wikicode, "accusative-ablative", branch)

	table.insert(wikicode, "|}")
	
	return table.concat(wikicode)
end

-- The main entry point.
-- This is the only function that can be invoked from a template.
function export.show(frame)
	local parent_args = frame:getParent().args

	local decl_type = (frame.args["decl"] or parent_args["decl"]) or "1st"
	
	if not decls[decl_type] then
		error("Unknown declension type '" .. decl_type .. "'")
	end
	
	local data = {forms = {}}
	data.head = parent_args["head"] or nil
	
	data.latincolour = {
		frame:expandTemplate({title = "Latincolour1"}),
		frame:expandTemplate({title = "Latincolour2"}),
		frame:expandTemplate({title = "Latincolour3"}),
	}

	local args = require("Module:parameters").process(parent_args, decls[decl_type].params, true)
	
	if not args[1] then
		setmetatable(args, {__index = function(self, key)
			return "{{{" .. key .. "}}}"
		end
		})
	end
	
	-- Generate the forms
	decls[decl_type](args, data)
	data["decl"] = decl_type
	
	-- Make the table
	t = decl_names[data.decl] .. '.\n\n'
	if parent_args.family then
		t = t .. make_table(data, parent_args.family)
		if parent_args.family2 then
			t = t .. "\n" .. make_table(data, parent_args.family2)
		end
	else
		t = t .. make_table(data, "It-W") .. "\n" .. make_table(data, "E") .. "\n" .. make_table(data, "S")
	end
	if title:inNamespace("") then
		t = mw.ustring.gsub(t, "#head", m_links.full_link({lang = lang, alt = data.forms["nom-sg"]}, "term"))
	else
		t = mw.ustring.gsub(t, "#head", m_links.full_link({lang = lang, alt = "*" .. data.forms["nom-sg"]}, "term"))
	end
	return t
end

return export