Module:User:Erutuon/headword

local export = {}

local is_sequence = require "Module:table".isArray

local function parse_args(args) local parsed = {} for k, v in pairs(args) do		if type(k) == "string" then local word, number = k:match "^(%l+)(%d*)$" if not word then error("Invalid parameter key " .. k)			end number = tonumber(number) or 1 parsed[word] = parsed[word] or {} if parsed[word][number] then error("Two values for " .. k)			end parsed[word][number] = v		else parsed[k] = v		end end return parsed end

local function validate_args(key_info, args) local invalid_keys = {} local should_not_be_list = {} local should_be_sequence = {} for key, values in pairs(args) do		if not key_info[key] then table.insert(invalid_keys, key) elseif not (key_info[key] == "multiple" or key_info[key] == "sequence") and require "Module:table".length(values) > 1 then table.insert(should_not_be_list, key) end end for key, info in pairs(key_info) do		if info == "sequence" and args[k] and not is_sequence(args[k]) then table.insert(should_be_sequence, key) end end local msg = {} if #invalid_keys > 0 then table.insert(msg, "Invalid parameters: " .. table.concat(invalid_keys, ", ")) end if #should_not_be_list > 0 then table.insert(msg, "The following arguments don't accept numbered parameters: "			.. table.concat(should_not_be_list, ", ")) end if #should_be_sequence > 0 then table.insert(msg, "The following arguments have gaps: "			.. table.concat(should_be_sequence, ", ")) end if #msg > 0 then error(table.concat(msg, " ")) end for key in pairs(key_info) do		args[key] = args[key] or {} end end

local function headword_with_forms(forms, suffixes, args, lang) assert(is_sequence(forms)) assert(is_sequence(suffixes)) local parameters = { head = "sequence", g = "multiple", tr = "multiple", cat = "sequence", sort = true, }	local sequential_keys = {} local form_keys = {} local labels = {} for _, form_data in ipairs(forms) do		assert(type(form_data) == "table") local form = assert(form_data.key) labels[form] = assert(form_data.label) parameters[form] = "sequence" table.insert(form_keys, form) for _, suffix in ipairs(suffixes) do local key = form .. suffix parameters[key] = "sequence" sequential_keys[key] = form end end local args = frame:getParent.args local parsed_args = parse_args(args) validate_args(parameters, parsed_args) for suffixed_key, key in pairs(sequential_keys) do		for i in pairs(parsed_args[suffixed_key]) do			if not parsed_args[key][i] then error("|" .. suffixed_key .. i .. " with no corresponding |" .. key .. i)			end end end local headword_forms = { enable_auto_translit = true } for _, form in ipairs(form_keys) do		local headword_form = { label = labels[form] } for i, term in ipairs(parsed_args[form]) do			headword_form[i] = { term = term, translit = parsed_args[form .. "tr"][i], }		end if #headword_form > 0 then table.insert(headword_forms, headword_form) end end local data = { lang = lang, heads = parsed_args.head or {}, translits = parsed_args.tr, genders = parsed_args.g,		inflections = headword_forms, pos_category = "nouns", categories = parsed_args.cat, sort_key = parsed_args.sort[1], }	return require("Module:headword").full_headword(data) end

function export.noun(frame) local parameters = { head = "sequence", g = "multiple", tr = "multiple", pl = "sequence", pltr = "multiple", sort = true, }	local args = frame:getParent.args local parsed_args = parse_args(args) validate_args(parameters, parsed_args) local lang = require "Module:languages".getByCode("ti") local plurals = { label = "plural" } for i, plural in pairs(parsed_args.pl) do		plurals[i] = { term = plural, translit = parsed_args.pltr[i], }	end for i in pairs(parsed_args.pltr) do		if not parsed_args.pl[i] then error("|pltr" .. i .. " with no corresponding |pl" .. i)		end end local data = { lang = lang, heads = parsed_args.head or {}, translits = parsed_args.tr, genders = parsed_args.g,		inflections = #plurals > 0 and { enable_auto_translit = true, plurals } or {}, pos_category = "nouns", categories = {}, sort_key = parsed_args.sort[1], }	return require("Module:headword").full_headword(data) end

return export