پودمان:names

از ویکی‌واژه

توضیحات این پودمان می‌تواند در پودمان:names/توضیحات قرار گیرد.

local m_links = require("Module:links")
local m_utilities = require("Module:utilities")

local export = {}

local rfind = mw.ustring.find

local function ine(x)
	if x == "" then
		return nil
	else
		return x
	end
end

-- Clone parent's args while also assigning nil to empty strings.
local function clone_args(frame)
	local args = {}
	for pname, param in pairs(frame:getParent().args) do
		args[pname] = ine(param)
	end
	return args
end

local function join_names(lang, args, param, paramalt, paramtr)
	local words
	local val = args[param]
	local alt = args[paramalt]
	local tr = paramtr and args[paramtr]
	local i = 2

	while val do
		words = words or {} -- Don't create new table unless it's actually needed
		local sep
		if i == 2 then
			sep = ""
		elseif not args[param .. i] then
			sep = " or "
		else
			sep = ", "
		end
		
		local link = m_links.full_link({lang = lang, term = val, alt = alt, tr = tr}, nil, true)
		
		table.insert(words, sep)
		table.insert(words, link)
		val = args[param .. i]
		alt = args[paramalt .. i]
		tr = paramtr and args[paramtr .. i]
		i = i + 1
	end
	
	return words and table.concat(words, "") or "", i - 2
end

-- The main entry point.
function export.given_name(frame)
	local params = {
		["lang"] = { default = "fa" },
		["gender"] = { default = "{{{1}}}" },
		[1] = { alias_of = "gender" },
		-- second gender
		["or"] = {},
		["from"] = {},
		[2] = { alias_of = from },
		["diminutive"] = { list = true },
		["diminutivealt"] = { list = true },
		["diminutivetr"] = { list = true },
		["dim"] = { alias_of = "diminutive" },
		["dimalt"] = { alias_of = "diminutivealt" },
		["dimtr"] = { alias_of = "diminutivetr" },
		["eq"] = { list = true },
		["eqalt"] = { list = true },
		["xlit"] = { list = true },
		["xlitalt"] = { list = true },
		-- initial article: A or An
		["A"] = {},
		["sort"] = {},
		["dimtype"] = {},
	}
	
	local args = clone_args(frame)
	local textsegs = {}
	local lang = require("Module:languages").getByCode(args["lang"] or "fa")
	local en = require("Module:languages").getByCode("fa")
	local gender = args["gender"] or args[1] or "{{{1}}}"
	local from = args["from"] or args[2]

	local dimtext, numdims
	if args["diminutive"] then
		dimtext, numdims = join_names(lang, args, "diminutive",
			"diminutivealt", "diminutivetr")
	else
		dimtext, numdims = join_names(lang, args, "dim", "dimalt", "dimtr")
	end
	local xlittext = join_names(en, args, "xlit", "xlitalt")
	local eqtext = join_names(en, args, "eq", "eqalt")

	table.insert(textsegs, "<span class='use-with-mention'>")
	local dimtype = args["dimtype"]
	local article = args["یک"] or
		dimtype and rfind(dimtype, "^[aeiouAEIOU]") and "یک" or "یک"

	table.insert(textsegs, article .. " ")
	if numdims > 0 then
		table.insert(textsegs,
			(dimtype and dimtype .. " " or "") ..
			"[[diminutive]]" ..
			(xlittext ~= "" and ", " .. xlittext .. "," or "") ..
			" of the ")
	end
	table.insert(textsegs, numdims > 1 and "[[اسم کوچک]]" or
		"[[اسم کوچک]]")
	table.insert(textsegs, gender .. " " .. (
		args["or"] and ("or " .. args["or"] .. " ") or ""))
	if numdims > 0 then
		table.insert(textsegs, " " .. dimtext)
	elseif xlittext ~= "" then
		table.insert(textsegs, ", " .. xlittext)
	end
	--if from then
	--	table.insert(textsegs, ", from " .. from)
	--end
	if eqtext ~= "" then
		table.insert(textsegs, ", equivalent to English " .. eqtext)
	end
	table.insert(textsegs, "</span>")

	local categories = {}
	local langname = lang:getCanonicalName() .. " "
	local function insert_cats(isdim)
		if isdim == "" then
			-- No category such as "English diminutives of given names"
			table.insert(categories, langname .. isdim .. "given names")
		end
		local function insert_cats_gender(g)
			table.insert(categories, langname .. isdim .. g .. " اسم‌های کوچک")
			if from then
				table.insert(categories, langname .. isdim .. g .. " اسم‌های کوچک برگرفته از " .. from)
			end
		end
		insert_cats_gender(gender)
		if args["or"] then
			insert_cats_gender(args["or"])
		end
	end
	insert_cats("")
	if numdims > 0 then
		insert_cats("diminutives of ")
	end

	if from and not require("Module:languages").getByCanonicalName(from) 
			and not require("Module:etymology languages").getByCanonicalName(from) then
		removed, count = mw.ustring.gsub(from, " languages$", "")
		if count == 0 or not require("Module:families").getByCanonicalName(removed) then
			table.insert(categories,"Kenny's testing category 6")
		end
	end

	return table.concat(textsegs, "") ..
		m_utilities.format_categories(categories, lang, args["sort"])
end

return export

-- For Vim, so we get 4-space tabs
-- vim: set ts=4 sw=4 noet: