Module:names: Difference between revisions

From Laenkea
Jump to navigation Jump to search
No edit summary
mNo edit summary
 
(2 intermediate revisions by one other user not shown)
Line 30: Line 30:
function export.format_name(data)
function export.format_name(data)
local out = {"A"}
local out = {"A"}
local cat = "[[Category:" .. data.language.name .. " " .. data.type .. "s]]"
local cat = "[[Category:" .. data.language.name .. " names]][[Category:" .. data.language.name .. " " .. data.type .. "s]]"
if data.gender then
if data.gender then
cat = cat .. "[[Category:" .. data.language.name .. " " .. data.gender .. " " .. data.type .. "s]]"
cat = cat .. "[[Category:" .. data.language.name .. " " .. data.gender .. " " .. data.type .. "s]]"
Line 38: Line 38:
if data.from then
if data.from then
local from_lang = m_languages.find_by_code(data.from)
local from_lang = m_languages.find_by_code(data.from)
local from_name = (from_lang and from_lang.name or 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)
table.insert(out, "from " .. from_name)
cat = cat .. "[[Category:" .. data.language.name .. " " .. data.type .. "s from " .. from_name .. "]]"
cat = cat .. "[[Category:" .. data.language.name .. " " .. data.type .. "s from " .. from_name_raw .. "]]"
if data.gender then
if data.gender then
cat = cat .. "[[Category:" .. data.language.name .. " " .. data.gender .. " " .. data.type .. "s from " .. from_name .. "]]"
cat = cat .. "[[Category:" .. data.language.name .. " " .. data.gender .. " " .. data.type .. "s from " .. from_name_raw .. "]]"
end
end
end
end

Latest revision as of 03:23, 25 August 2023

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