მოდული:gender and number

ვიქსიკონი გვერდიდან

შეგიძლიათ შექმნათ დოკუმენტაცია ამ მოდულისათვის: მოდული:gender and number/ინფო

--[=[
	This module creates standardised displays for gender and number.
	It converts a gender specification into Wiki/HTML format.
	
	A gender specification is a list of one of the elements listed below,
	separated by hyphens. Examples are: "c", "n", "f-p", "m-an-p"
]=]--

local export = {}

local codes = {}

-- A list of all possible "parts" that a specification can be made out of.

codes["?"] = {type = "other", display = '<small><i><abbr title="არასრული სქესი">?</abbr></i></small>'}

-- სქესები
codes["m"] = {type = "gender", display = '<small><i><abbr title="მამრობითი სქესი">მამრ.</abbr></i></small>'}
codes["f"] = {type = "gender", display = '<small><i><abbr title="მდედრობითი სქესი">მდედრ.</abbr></i></small>'}
codes["n"] = {type = "gender", display = '<small><i><abbr title="საშუალო სქესი">საშ.</abbr></i></small>'}
codes["c"] = {type = "gender", display = '<small><i><abbr title="საერთო სქესი">საერთ.</abbr></i></small>'}

-- სულიერება
codes["an"] = {type = "animacy", display = '<small><i><abbr title="სულიერი">სულ.</abbr></i></small>'}
codes["in"] = {type = "animacy", display = '<small><i><abbr title="უსულო">უსულ.</abbr></i></small>'}

-- ცხოველი (უკრაინულისა და პოლონურისათვის)
codes["anml"] = {type = "animal", display = '<small><i><abbr title="ცხოველი">ცხოვ.</abbr></i></small>'}

-- Personal
codes["pr"] = {type = "personal", display = '<small><i><abbr title="პირადული">პირად.</abbr></i></small>'}
codes["np"] = {type = "personal", display = '<small><i><abbr title="არაპირადული">არაპირად.</abbr></i></small>'}

-- Virility (for Polish)
codes["vr"] = {type = "virility", display = '<small><i><abbr title="virile">vir</abbr></i></small>'}
codes["nv"] = {type = "virility", display = '<small><i><abbr title="nonvirile">nvir</abbr></i></small>'}

-- რიცხვები
codes["s"] = {type = "number", display = '<small><i><abbr title="მხოლობითი რიცხვი">მხოლ.</abbr></i></small>'}
codes["d"] = {type = "number", display = '<small><i><abbr title="მრჩობლობითი რიცხვი">მრჩობლ.</abbr></i></small>'}
codes["p"] = {type = "number", display = '<small><i><abbr title="მრავლობითი რიცხვი">მრავლ.</abbr></i></small>'}

-- Verb qualifiers
codes["impf"] = {type = "perfectivity", display = '<small><i><abbr title="imperfective aspect">impf</abbr></i></small>'}
codes["pf"] = {type = "perfectivity", display = '<small><i><abbr title="perfective aspect">pf</abbr></i></small>'}

-- Version of format_list that can be invoked from a template.
function export.show_list(frame)
	local args = frame.args
	local lang = args["lang"]; if lang == "" then lang = nil end
	local list = {}
	local i = 1
	
	while args[i] and args[i] ~= "" do
		table.insert(list, args[i])
		i = i + 1
	end
	
	return export.format_list(list, lang)
end

-- Format one or more gender specifications, in the form of a table of specifications.
function export.format_list(list, lang)
	local is_nounclass = nil
	
	-- Iterate over each specification and format it
	for key, spec in ipairs(list) do
		local nc
		list[key], nc = export.format_specification(spec, lang)
		
		-- Ensure that the specifications are either all noun classes, or none are.
		if is_nounclass == nil then
			is_nounclass = nc
		elseif is_nounclass ~= nc then
			error("Noun classes and genders cannot be mixed. Please use either one or the other.")
		end
	end
	
	if is_nounclass then
		-- Add the processed codes together with slashes
		return "<span class=\"gender\">class " .. table.concat(list, "/") .. "</small>"
	else
		-- Add the processed codes together with commas
		return "<span class=\"gender\">" .. table.concat(list, " or ") .. "</small>"
	end
end

-- Format the sub-parts of a single gender specification.
function export.format_specification(spec, lang)
	local categories = ""
	local ret = ""
	local is_nounclass = false
	
	-- If the specification starts with cX, then it is a noun class specification.
	if spec:find("^[1-9]") or spec:find("^c[^-]") then
		is_nounclass = true
		code = spec:gsub("^c", "")
		
		if code == "?" then
			ret = "<abbr class=\"noun-class\" title=\"noun class missing\">?</abbr></i></small>"
		else
			ret = "<abbr class=\"noun-class\" title=\"noun class " .. code .. "\">" .. code .. "</abbr></i></small>"
		end
	else
		local types = {}
		
		-- Split the parts and iterate over each part, converting it into its display form
		local parts = mw.text.split(spec, "-")
		
		for key, code in ipairs(parts) do
			-- Is this code valid?
			if not codes[code] then
				error("The gender specification \"" .. spec .. "\" is not valid.")
			end
			
			if codes[code].type ~= "other" and types[codes[code].type] then
				--require("Module:debug").track("gender and number/multiple")
				--require("Module:debug").track("gender and number/multiple/" .. spec)
				error("The gender specification \"" .. spec .. "\" contains multiple tags of type \"" .. codes[code].type .. "\".")
			end
				
			parts[key] = codes[code].display
			types[codes[code].type] = true
		end
		
		-- Add the processed codes together with non-breaking spaces
		ret = table.concat(parts, "&nbsp;")
	end
	
	-- Do some additional checks if a language was given
	if lang then
		-- Is this an incomplete gender?
		if spec:find("?") then
			local m_utilities = require("Module:utilities")
			categories = m_utilities.format_categories({"Requests for gender in " .. lang:getCanonicalName() .. " entries"}, nil)
		end
		
		-- Check if the specification is valid
		--elseif langinfo.genders then
		--	local valid_genders = {}
		--	for _, g in ipairs(langinfo.genders) do valid_genders[g] = true end
		--	
		--	if not valid_genders[spec] then
		--		local valid_string = {}
		--		for i, g in ipairs(langinfo.genders) do valid_string[i] = g end
		--		error("The gender specification \"" .. spec .. "\" is not valid for " .. langinfo.names[1] .. ". Valid are: " .. table.concat(valid_string, ", "))
		--	end
		--end
	end
	
	return ret .. categories, is_nounclass
end

return export