Module:names

Revision as of 18:26, 6 August 2023 by Maria (talk | contribs)

Documentation for this module may be created at Module:names/documentation

local export = {}

local m_languages = require("Module:languages")
local m_parameters = require("Module:parameters")

local params = {
	[1] = {required = true},
	[2] = {alias_of = "gender"},
	["gender"] = {},
	["from"] = {},
	["nocat"] = {type = "boolean"},
}

local function check_gender(gender)
	if gender == nil then return nil end
	if gender == "male" or gender == "female" or gender == "unisex" then
		return gender
	end
	error("Invalid gender was supplied")
end

local function get_data(frame, name_type)
	local args = m_parameters.process(frame:getParent().args, params)
	args["language"] = m_languages.get_by_code(args[1])
	args["gender"] = check_gender(args["gender"])
	args["type"] = name_type
	return args
end

function export.format_name(data)
	local out = {"A"}
	local cat = "[[Category:" .. data.language.name .. " " .. data.type .. "s]]"
	if data.gender then
		cat = cat .. "[[Category:" .. data.language.name .. " " .. data.gender .. " " .. data.type .. "s]]"
		table.insert(out, gender)
	end
	table.insert(out, data.type)
	if data.from then
		local from_lang = m_languages.find_by_code(data.from)
		local from_name = (from_lang and from_lang.name or data.from)
		table.insert(out, "from " .. from_name)
		cat = cat .. "[[Category:" .. data.language.name .. " " .. data.type .. "s from " .. from_name .. "]]"
		if data.gender then
			cat = cat .. "[[Category:" .. data.language.name .. " " .. data.gender .. " " .. data.type .. "s from " .. from_name .. "]]"
		end
	end
	return table.concat(out, " ") .. (data.nocat and "" or cat)
end

function export.givenName(frame)
	return export.format_name(get_data(frame, "given name"))
end

function export.surname(frame)
	return export.format_name(get_data(frame, "surname"))
end

return export