सामग्री पर जाएँ

मॉड्यूल:zlw-opl-noun

विक्षनरी से

"इस मॉड्यूल हेतु प्रलेख मॉड्यूल:zlw-opl-noun/doc पर बनाया जा सकता है"

local export = {}

local m_links = require('Module:links')
local m_g = require('Module:gender and number')
local lang = require("Module:languages").getByCode("zlw-opl")

-- case information
local cases = {
	{ key = "nom"; en = "nominative"; pl = "mianownik (kto? co?)" },
	{ key = "gen"; en = "genitive"; pl = "dopełniacz (kogo? czego?)" },
	{ key = "dat"; en = "dative"; pl = "celownik (komu? czemu?)" },
	{ key = "acc"; en = "accusative"; pl = "biernik (kogo? co?)" },
	{ key = "ins"; en = "instrumental"; pl = "narzędnik (kim? czym?)" },
	{ key = "loc"; en = "locative"; pl = "miejscownik (o kim? o czym?)" },
	{ key = "voc"; en = "vocative"; pl = "wołacz (o!)" },
}

-- columns for Old Polish nouns
local noun_dual_cols = {
	{ key = "s"; title = "singular" },
	{ key = "d"; title = "dual" },
	{ key = "p"; title = "plural" },
}

local altsep = "/"

function empty_item(text)
	return (not text) or text == "" or text == "-" or text == "–" or text == "—"
end

-- add link markers, with links separated by any of splitchars
-- exported for use in testcases
-- normally the separator is altsep
function export.make_links(text, splitchars, title)
	if not title then
		title = mw.title.getCurrentTitle().fullText
	end
	if empty_item(text) then
		return "—"
	elseif not splitchars or splitchars == "" then
		return ("[[%s#Old Polish|%s]]"):format(text, text)
	else
		items = {}
		for word in mw.ustring.gmatch(text, "[^" .. splitchars .. "]+") do
			add_archaic = ""
			if word:sub(-string.len(" (archaic)")) == " (archaic)" then
				word = (mw.text.split(word, "% %(archaic%)"))[1]
				add_archaic = " (archaic)"
			end
			
			if word == title then
				table.insert(items, ("[[%s]]"):format(word) .. add_archaic)
			else
				table.insert(items, ("[[%s#Old Polish|%s]]"):format(word, word) .. add_archaic)
			end
		end
		
		return table.concat(items, "/")
	end
end

local function linkify_info(declinfo, splitchars, nolinks)
	local linked = {}
	local title = mw.title.getCurrentTitle().fullText
	for k, v in pairs(declinfo) do
		if v == title then
			linked[k] = "[[" .. v .. "]]"
		elseif nolinks then
			linked[k] = v
		else
			linked[k] = export.make_links(v, splitchars, title)
		end
	end
	return linked
end

local function override_col_titles(heads, cols)
	if not heads then
		return cols or {}
	end
	local new_cols = {}
	local index = 1
	for word in mw.ustring.gmatch(heads, "[^,]+") do
		if cols[index] then
			table.insert(new_cols, { key = cols[index].key; title = word } )
		else
			table.insert(new_cols, { key = tostring(index); title = word } )
		end
		index = index + 1
	end
	for ci, col in ipairs(cols) do
		if ci >= index then
			table.insert(new_cols, cols[index])
		end
	end
	return new_cols
end

local function normalize_tantum(pargs)
	-- support "num" as fallback for "tantum" to match Latin templates
	local tantum = pargs.tantum or pargs.num
	if not tantum then
		return nil
	end
	if tantum == "sg" then
		tantum = "s"
	elseif tantum == "pl" then
		tantum = "p"
	end
	return tantum
end

local function guess_width(declinfo, cols)
	local maxl = 0
	for k, v in pairs(declinfo) do
		local l = mw.ustring.len(mw.text.trim(v))
		if maxl < l then
			maxl = l
		end
	end
	local width = math.floor(maxl * 0.78) -- number obtained by anecdotal evidence
	width = (width < 10) and 10 or width
	width = 9 + (width * #cols)
	return width
end

-- generate the HTML code of an inflection table
-- each entry in heads must have "key" and "title"
local function make_table(declinfo, cols, preproc, width, title, tantum, nolinks)
	local result = {}
	if not cols or not cols[1] then
		error("make_table: invalid cols parameter")
	end

	local lemma_key = cases[1].key .. cols[1].key
	local lemma = m_links.remove_links(declinfo[lemma_key])
	title = title or ('Declension of <span class="Latn mention" lang="zlw-opl" xml:lang="zlw-opl">%s</span>'):format(lemma)

	local emwidth = width or guess_width(declinfo, cols)

	--if preproc == "nowiki" then
	--	declinfo = nowiki_info(declinfo)
	--else
	if preproc == "linkify" then
		declinfo = linkify_info(declinfo, altsep, nolinks)
	end

	if cols and (#cols > 0) then
		table.insert(result, '|-\n! style="width: 8em;" |\n')
		for i, col in ipairs(cols) do
			table.insert(result, ('! scope="col" | %s\n'):format(col.title))
		end
	end

	local maxl = 0
	for i, case in ipairs(cases) do
		table.insert(result, ('|-\n! title="%s" scope="row" | %s\n'):format(case.pl, case.en))
		for _, col in ipairs(cols) do
			local declkey = case.key .. col.key
			local item = mw.text.trim(declinfo[declkey])
			if empty_item(item) then
				table.insert(result, '| —\n')
			else
				table.insert(result, ('| <span class="Latn" lang="zlw-opl" xml:lang="zlw-opl">%s</span>\n'):format(item))
			end
		end
	end

	local outtext = ([=[<div class="NavFrame inflection-table-noun" style="width: %uem">
<div class="NavHead">%s</div>
<div class="NavContent">
{| style="width: %uem; margin: 0;" class="wikitable inflection-table"
]=]):format(emwidth, title, emwidth) .. table.concat(result, "") .. "|}</div></div>"

	if tantum == "s" then
		outtext = outtext .. "[[Category:Polish singularia tantum]]"
	elseif tantum == "p" then
		outtext = outtext .. "[[Category:Polish pluralia tantum]]"
	end
	return outtext
end

local function make_table_from_pargs(pargs, cols, tantum)
	local width = pargs.width and tonumber(pargs.width)
	
	local declinfo = {}
	cols = override_col_titles(pargs.heads, cols or {})
	for i = 1, #cases do
		for j, col in ipairs(cols) do
			local case_key = cases[i].key .. col.key
			local argn = ((i-1) * #cols) + j
			declinfo[case_key] = m_links.remove_links(mw.text.trim(pargs[case_key] or pargs[argn] or "-"))
		end
	end
	return make_table(declinfo, cols, "linkify", pargs.width, pargs.title, normalize_tantum(pargs), pargs.nolinks)
end

function export.template_decl_manual(frame)
	local pargs = frame:getParent().args
	return make_table_from_pargs(pargs, noun_dual_cols, nil)
end

return export