Pumunta sa nilalaman

Module:gender and number

Mula Wiktionary

Ang dokumentasyon ng ng modyul na ito ay maaaring likhain sa Module:gender and number/doc

--[=[
	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["?"] = '<abbr title="kasariang kulang">?</abbr>'

-- Genders (Kasarian)
codes["m"] = '<abbr title="kasariang panlalaki">m</abbr>'
codes["f"] = '<abbr title="kasariang pambabae">f</abbr>'
codes["n"] = '<abbr title="walang kasarian">n</abbr>'
codes["c"] = '<abbr title="kasariang di-tiyak">c</abbr>'

-- Additional qualifiers
codes["an"] = '<abbr title="animado">anim</abbr>'
codes["in"] = '<abbr title="di-animado">inan</abbr>'
codes["pr"] = '<abbr title="panao">pers</abbr>'
codes["np"] = '<abbr title="di-panao">npers</abbr>'

-- Numbers (Kailanan)
codes["s"] = '<abbr title="isahan">sg</abbr>'
codes["d"] = '<abbr title="dalawahan">du</abbr>'
codes["p"] = '<abbr title="maramihan">pl</abbr>'

-- Verb qualifiers
codes["impf"] = '<abbr title="aspetong imperpektibo">impf</abbr>'
codes["pf"] = '<abbr title="aspetong perpektibo">pf</abbr>'

-- 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("Ang mga uri ng pangngalan at kasarian ay hindi maaaring magkakahalo. Isa lamang ang pwedeng gamitin alinman sa dalawa.")
		end
	end
	
	if is_nounclass then
		-- Add the processed codes together with slashes
		return "<span class=\"gender\">class " .. table.concat(list, "/") .. "</span>"
	else
		-- Add the processed codes together with commas
		return "<span class=\"gender\">" .. table.concat(list, ", ") .. "</span>"
	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>"
		else
			ret = "<abbr class=\"noun-class\" title=\"noun class " .. code .. "\">" .. code .. "</abbr>"
		end
	else
		-- 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 codes[code] then
				parts[key] = codes[code]
			else
				error("Ang kasariang \"" .. spec .. "\" ay inbalido.")
			end
		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({lang:getCanonicalName() .. " terms with incomplete gender"}, 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("Ang kasariang \"" .. spec .. "\" ay inbalido sa wikang " .. langinfo.names[1] .. ". Ang mga pwede ay: " .. table.concat(valid_string, ", "))
		--	end
		--end
	end
	
	return ret .. categories, is_nounclass
end

return export