local export = {}
local m_links = require("Module:links")
local m_utils = require("Module:utilities")
local lang = require("Module:languages").getByCode("iir-pro")
local decl_data = require("Module:User:Victar/iir-decl-noun/data")
local decl_data_irreg = require("Module:User:Victar/iir-decl-noun/data/irreg")
local endings = {
= "a-m", = "a-m", = "a-n", = "a-n",
= "aH-f", = "aH-f",
= "i-mf", = "i-mf", = "i-n",
= "yaH-f", = "yaH-f",
= "iH-f", = "iH-f",
= "u-mf", = "u-n",
= "uH-f", = "uH-f",
}
local endings_reverse = {
= "ás", = "as", = "ám", = "am",
= "áH", = "aH",
= "íš", = "iš", = "íš", = "iš", = "íš", = "iš", = "i",
= "íH", = "iH",
= "íHs", = "iHs",
= "uš", = "uš", = "uš", = "u",
= "úHs", = "uHs",
= "ā́", = "ā",
= "",
}
local function detect_decl(word, stem, gender)
if stem and gender then
local decl = stem .. "-" .. gender
return decl, {mw.ustring.sub(word, 1, -(mw.ustring.len(endings_reverse) + 1))}
elseif stem == "r" and mw.ustring.sub(word, -2) == "ā́" then --r-stem
return "r-n", {mw.ustring.sub(word, 1, -3)}
elseif mw.ustring.find(word, "ô$") then -- an-stem
if gender then
return "n-" .. gender, {mw.ustring.sub(word, 1, -2)}
else
error("Gender must be specified for an-stems.")
end
else
for ending, decl in pairs(endings) do
if mw.ustring.find(word, ending .. "$") then
return decl, {mw.ustring.sub(word, 1, -(mw.ustring.len(ending) + 1))}
end
end
-- No matches, assume consonant stem. Now check for s-stems
local stem = ""
if mw.ustring.sub(word, -1, -1) == "s" and not mw.ustring.find(mw.ustring.sub(word, -2, -2), "") then
stem = word
else
stem = mw.ustring.sub(word, 1, -2)
end
return (gender and "cons-" .. gender or "cons-mf"), {stem}
end
end
function destress_if_stressed(ending)
local word = mw.title.getCurrentTitle().subpageText
if mw.ustring.find(mw.ustring.sub(word, 1, -3), "") then
ending = mw.ustring.gsub(ending, "á", "a")
ending = mw.ustring.gsub(ending, "í", "i")
ending = mw.ustring.gsub(ending, "ó", "o")
ending = mw.ustring.gsub(ending, "ú", "u")
ending = mw.ustring.gsub(ending, "ā́", "ā")
ending = mw.ustring.gsub(ending, "́", "")
end
return ending
end
local function add_asterisks(forms, data)
for _, form in ipairs(forms) do
for i, subform in ipairs(data.forms) do
data.forms = "*" .. subform
end
end
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 stems = nil
local decl_type = {}
local word = mw.title.getCurrentTitle().subpageText
local args = {}
if not decl_data_irreg then
if frame.args.decl then
decl_type = frame.args.decl
else
if parent_args.stem and parent_args.g and parent_args then
decl_type = parent_args.stem .. "-" .. parent_args.g
stems = {parent_args}
else
decl_type, stems = detect_decl(word, parent_args.stem, parent_args.g)
end
end
if not decl_type then
error("Unknown declension '" .. decl_type .. "'")
end
args = require("Module:parameters").process(parent_args, decl_data.params, true)
if stems then
for i, stem in ipairs(stems) do
args = stem
end
end
end
local data = {forms = {}, categories = {}}
data.head = parent_args or nil
-- Generate the forms
if decl_data_irreg then
table.insert(data.categories, "Proto-Indo-Iranian irregular nouns")
decl_data_irreg(parent_args, data)
else
decl_data(args, data)
end
-- Make the table
return make_table(data)
end
function make_table(data)
local function show_form(form)
if not form then
return "—"
end
local ret = {}
for key, subform in ipairs(form) do
if subform ~= "—" then
subform = "*" .. subform
end
table.insert(ret, subform)
end
return table.concat(ret, ", ")
end
local function repl(param)
if param == "decl_type" then
return data.decl_type
elseif param == "title" then
return "*" .. data.forms.nom_sg
else
return show_form(data.forms)
end
end
local function make_cases(data)
local cases = {"nominative", "vocative", "accusative", "instrumental", "ablative", "dative", "genitive", "locative"}
local ret = {}
for _, case in ipairs(cases) do
local case_short = mw.ustring.sub(case, 1, 3)
-- assert(false, case_short)
table.insert(ret, "|- " .. ((case_short == "nom" or case_short == "gen") and "" or "class=\"vsHide\"") .. " \n! style=\"background: #DBEDFF\" | " .. case .. "\n")
table.insert(ret, "| style=\"background: #ECF5FF\" | " .. show_form(data.forms) .. "\n")
table.insert(ret, "| class=\"vsHide\" style=\"background: #ECF5FF\" | " .. show_form(data.forms) .. "\n")
if data.forms then
table.insert(ret, "| class=\"vsHide\" style=\"background: #ECF5FF\" | " .. show_form(data.forms) .. "\n")
end
end
return table.concat(ret)
end
local no_plural = data.forms.nom_pl == nil
local wikicode = [=[
{| class="inflection-table vsSwitcher" data-toggle-category="inflection" style="background: #F8FBff; border: 1px solid #d0d0d0; text-align: left;" cellspacing="1" cellpadding="2"
|-
! style="background: #B5D9FF;" class="vsToggleElement" colspan="]=] .. (no_plural and "3" or "4") .. [=[" | {{{decl_type}}}
|-
| style="min-width: 8em; background-color:#B5D9FF" |
! style="min-width: 11em; background-color:#B5D9FF" | singular
! class="vsHide" style="min-width: 11em; background-color:#B5D9FF" | dual]=] .. (no_plural and "\n" or [=[
! class="vsHide" style="min-width: 11em; background-color:#B5D9FF" | plural
]=]) .. make_cases(data) .. [=[
|}]=]
return (mw.ustring.gsub(wikicode, "{{{(+)}}}", repl)) .. m_utils.format_categories(data.categories, lang)
end
return export