Module:names

From Laenkea
Revision as of 03:23, 25 August 2023 by TheNightAvl (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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 .. " names]][[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, data.gender)
	end
	table.insert(out, "[[Appendix:Glossary#" .. string.gsub(data.type, " ", "_") .. "|" .. data.type .. "]]")
	if data.from then
		local from_lang = m_languages.find_by_code(data.from)
		local from_name = (from_lang and ("[[" .. from_lang.link .. "|" .. from_lang.name .. "]]") or data.from)
		local from_name_raw = (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_raw .. "]]"
		if data.gender then
			cat = cat .. "[[Category:" .. data.language.name .. " " .. data.gender .. " " .. data.type .. "s from " .. from_name_raw .. "]]"
		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