Jump to content

Module:cs-ijpdecl

From Wiktionary, the free dictionary


local export = {}

local rsplit = mw.text.split
local rmatch = mw.ustring.match

local recognized_cases = require("Module:table").listToSet {
	"nominative",
	"genitive",
	"dative",
	"accusative",
	"vocative",
	"locative",
	"instrumental",
}

local slot_to_index = {
	["nom_s"] = 1,
	["gen_s"] = 2,
	["dat_s"] = 3,
	["acc_s"] = 4,
	["voc_s"] = 5,
	["loc_s"] = 6,
	["ins_s"] = 7,
	["nom_p"] = 8,
	["gen_p"] = 9,
	["dat_p"] = 10,
	["acc_p"] = 11,
	["voc_p"] = 12,
	["loc_p"] = 13,
	["ins_p"] = 14,
}

function export.ijpdecl(frame)
	local params = {
		[1] = {required = true}
	}
	local parargs = frame:getParent().args
	local args = require("Module:parameters").process(parargs, params)
	local function get_orig()
		return "original follows: {{temp|cs-ijpdecl|" .. args[1] .. "}}"
	end

	local function normalize_caseform(caseform)
		local forms = rsplit(caseform, " *, *")
		local need_brackets = #forms > 1
		for i, form in ipairs(forms) do
			-- remove footnotes
			form = form:gsub("[0-9]*$", "")
			if need_brackets then
				form = "[[" .. form .. "]]"
			end
			forms[i] = form
		end
		return table.concat(forms, ", ")
	end

	local outargs = {}
	local function process_line(line)
		local case, sg, pl = rmatch(line, "^(.-)\t(.-)\t(.-)$")
		if not case then
			return ("Not enough parts on line '%s'; "):format(line) .. get_orig()
		end
		if not recognized_cases[case] then
			return ("Unrecognized case '%s'; "):format(case) .. get_orig()
		end
		case = case:sub(1, 3)
		outargs[slot_to_index[case .. "_s"]] = normalize_caseform(sg)
		outargs[slot_to_index[case .. "_p"]] = normalize_caseform(pl)
	end

	local forms = mw.text.trim(args[1])

	for _, line in ipairs(rsplit(forms, "\n")) do
		local errmsg = process_line(line)
		if errmsg then
			return errmsg
		end
	end

	local parts = {}
	for i = 1, 7 do
		table.insert(parts, outargs[i] or "")
	end
	local sg = table.concat(parts, "|")
	parts = {}
	for i = 8, 14 do
		table.insert(parts, outargs[i] or "")
	end
	local pl = table.concat(parts, "|")
	return ("{{cs-decl-noun\n|%s\n|%s\n}}"):format(sg, pl)
end

return export