Modul:Nyelvtani modul

A Wikiszótárból, a nyitott szótárból
Ugrás a navigációhoz Ugrás a kereséshez

A modult a Modul:Nyelvtani modul/doc lapon tudod dokumentálni

local p = {}
local lang = mw.getContentLanguage()

-- allow to call functions with a single string or number parameter
local function getArgs(frame, options)
	if type(frame) ~= "table" then frame = { frame } end
	return require('Modul:Arguments').getArgs(frame, options)
end

-- needed to be called from pcall
local function formatDateLocal(form, tstamp, loc)
   return lang:formatDate(form, tstamp, loc)
end

-- returns "a" or "az" depending on the first character of the word passed to the template
function p.az(frame)
	local str = getArgs(frame)[1]
	if not str then return "a(z)" end

	local firstChar = mw.ustring.sub(str, 1, 1)
	local firstCharLower = mw.ustring.lower(firstChar)

	if (nil ~= mw.ustring.find("aáeéiíoóöőuúüű", firstCharLower, 1, true)) then
		return "az"
	elseif (nil ~= mw.ustring.find("bcdfghjklmnpqrstvwxyz", firstCharLower, 1, true)) then
		return "a"
	else
		return "a(z)"
	end

end

-- Return true if first argument starts with A or Az, otherwise empty string.
function p.startsWithAz(frame)
	local s = getArgs(frame)[1]
	return (s == nil and '') or tostring(mw.ustring.upper(mw.ustring.sub(s, 1, 2)) == 'A '
		or mw.ustring.upper(mw.ustring.sub(s, 1, 3)) == 'AZ '
		or ''
	)
end

-- returns month name in Hungarian + "ából" / "jából" / "éből" / "jéből" accordingly
function p.fromMonthSuffix(frame)
	local dateStr = getArgs(frame)[1]
	local suffixes = {
		[1] = "jából",
		[2] = "jából",
		[3] = "ából",
		[4] = "ából",
		[5] = "ából",
		[6] = "ából",
		[7] = "ából",
		[8] = "ából",
		[9] = "éből",
		[10] = "éből",
		[11] = "éből",
		[12] = "éből"
	}


	if (pcall(formatDateLocal, 'n', dateStr)) then
		local monthIndex = lang:formatDate('n', dateStr)
		return lang:formatDate('Y F', dateStr)..suffixes[tonumber(monthIndex)]
	else
		return dateStr
	end

end

-- returns month name in Hungarian + "i"
function p.ofMonthSuffix(frame)
	local dateStr = getArgs(frame)[1]

	if (pcall(formatDateLocal, 'n', dateStr)) then
		return lang:formatDate('Y F', dateStr).."i"
	else
		return dateStr
	end

end

-- returns number + "-s" / "-as" / "-es" / "-os" / "-ós" / "-ös"
function p.hanyas(frame)
	local str = getArgs(frame)[1];

	if (nil == str) then
		return ""
	end

	local number = tonumber(str)
	if (nil == number) then
		number = mw.ustring.match(str, "%d*$")
	end

	local suffixesNonRound = {
		[1] = "es",
		[2] = "es",
		[3] = "as",
		[4] = "es",
		[5] = "ös",
		[6] = "os",
		[7] = "es",
		[8] = "as",
		[9] = "es"
	}

	local suffixesRoundBelow100 = {
		[1] = "es",
		[2] = "as",
		[3] = "as",
		[4] = "es",
		[5] = "es",
		[6] = "as",
		[7] = "es",
		[8] = "as",
		[9] = "es"
	}

	local lastChar = mw.ustring.sub(str, -1)

	local suffixIndex = tonumber(lastChar)

	if (nil ~= suffixIndex) then
		if (0 ~= suffixIndex) then
			return str.."-"..suffixesNonRound[suffixIndex]
		else
			if (0 == tonumber(str) or 0 == tonumber(number)) then
				-- return the parameter value + "-s" if
				-- the parameter value is exactly 0 or
				-- a NaN ending with a number that evaluates to 0 (like "M0" or "dsdadsa000")
				return str.."-s"
			elseif (0 ~= (number % 100)/10) then
				suffixIndex = (number % 100)/10
				return str.."-"..suffixesRoundBelow100[suffixIndex]
			elseif (0 ~= (number % 1000)/100) then
				return str.."-as"
			elseif (0 == number % 10^36) then
				return str.."-s"
			elseif (0 == number % 10^33) then
				return str.."-os"
			elseif (0 == number % 10^30) then
				return str.."-s"
			elseif (0 == number % 10^27) then
				return str.."-os"
			elseif (0 == number % 10^24) then
				return str.."-s"
			elseif (0 == number % 10^21) then
				return str.."-os"
			elseif (0 == number % 10^18) then
				return str.."-s"
			elseif (0 == number % 10^15) then
				return str.."-os"
			elseif (0 == number % 10^12) then
				return str.."-s"
			elseif (0 == number % 10^9) then
				return str.."-os"
			elseif (0 == number % 10^6) then
				return str.."-s"
			elseif (0 == number % 10^3) then
				return str.."-es"
			end
		end
	end

	return str

end

-- returns "-s" / "-as" / "-es" / "-os" / "-ós" / "-ös"
function p.hanyas2(frame)
	local str = getArgs(frame)[1];

	if (nil == str) then
		return ""
	end

	local number = tonumber(str)
	if (nil == number) then
		number = mw.ustring.match(str, "%d*$")
	end

	local suffixesNonRound = {
		[1] = "es",
		[2] = "es",
		[3] = "as",
		[4] = "es",
		[5] = "ös",
		[6] = "os",
		[7] = "es",
		[8] = "as",
		[9] = "es"
	}

	local suffixesRoundBelow100 = {
		[1] = "es",
		[2] = "as",
		[3] = "as",
		[4] = "es",
		[5] = "es",
		[6] = "as",
		[7] = "es",
		[8] = "as",
		[9] = "es"
	}

	local lastChar = mw.ustring.sub(str, -1)

	local suffixIndex = tonumber(lastChar)

	if (nil ~= suffixIndex) then
		if (0 ~= suffixIndex) then
			return "-"..suffixesNonRound[suffixIndex]
		else
			if (0 == tonumber(str) or 0 == tonumber(number)) then
				-- return the parameter value + "-s" if
				-- the parameter value is exactly 0 or
				-- a NaN ending with a number that evaluates to 0 (like "M0" or "dsdadsa000")
				return "-s"
			elseif (0 ~= (number % 100)/10) then
				suffixIndex = (number % 100)/10
				return "-"..suffixesRoundBelow100[suffixIndex]
			elseif (0 ~= (number % 1000)/100) then
				return "-as"
			elseif (0 == number % 10^36) then
				return "-s"
			elseif (0 == number % 10^33) then
				return "-os"
			elseif (0 == number % 10^30) then
				return "-s"
			elseif (0 == number % 10^27) then
				return "-os"
			elseif (0 == number % 10^24) then
				return "-s"
			elseif (0 == number % 10^21) then
				return "-os"
			elseif (0 == number % 10^18) then
				return "-s"
			elseif (0 == number % 10^15) then
				return "-os"
			elseif (0 == number % 10^12) then
				return "-s"
			elseif (0 == number % 10^9) then
				return "-os"
			elseif (0 == number % 10^6) then
				return "-s"
			elseif (0 == number % 10^3) then
				return "-es"
			end
		end
	end

	return ""

end

-- returns number + "-ban" / "-ben"
function p.hanyban(frame)
	local str = getArgs(frame)[1];

	if (nil == str) then
		return ""
	end

	local number = tonumber(str)
	if (nil == number) then
		number = mw.ustring.match(str, "%d*$")
	end

	local suffixesNonRound = {
		[1] = "ben",
		[2] = "ben",
		[3] = "ban",
		[4] = "ben",
		[5] = "ben",
		[6] = "ban",
		[7] = "ben",
		[8] = "ban",
		[9] = "ben"
	}

	local suffixesRoundBelow100 = {
		[1] = "ben",
		[2] = "ban",
		[3] = "ban",
		[4] = "ben",
		[5] = "ben",
		[6] = "ban",
		[7] = "ben",
		[8] = "ban",
		[9] = "ben"
	}

	local lastChar = mw.ustring.sub(str, -1)

	local suffixIndex = tonumber(lastChar)

	if (nil ~= suffixIndex) then
		if (0 ~= suffixIndex) then
			return str.."-"..suffixesNonRound[suffixIndex]
		else
			if (0 == tonumber(str) or 0 == tonumber(number)) then
				-- return the parameter value + "-s" if
				-- the parameter value is exactly 0 or
				-- a NaN ending with a number that evaluates to 0 (like "M0" or "dsdadsa000")
				return str.."-ban"
			elseif (0 ~= (number % 100)/10) then
				suffixIndex = (number % 100)/10
				return str.."-"..suffixesRoundBelow100[suffixIndex]
			elseif (0 ~= (number % 1000)/100) then
				return str.."-ban"
			elseif (0 == number % 10^6) then
				return str.."-ban"
			elseif (0 == number % 10^3) then
				return str.."-ben"
			end
		end
	end

	return str
end

-- converts default signature timestamp to YYYY-MM-DD HH:MM (T) format for
-- further processing via {{#time:}}
function p.alairasIdobelyeg(frame)
	local str = getArgs(frame)[1]
	if type(str) ~= 'string' then return str, nil end;
	local months = {
		['január']		=  1,
		['február']		=  2,
		['március']		=  3,
		['április']		=  4,
		['május']		=  5,
		['június']		=  6,
		['július']		=  7,
		['augusztus']	=  8,
		['szeptember']	=  9,
		['október']		= 10,
		['november']	= 11,
		['december']	= 12
	}
	local match = {
		tonumber(mw.ustring.match(str, '(%d%d%d%d)%. %a+ %d?%d%.%, %d%d%:%d%d %(CES?T%)')),
		months  [mw.ustring.match(str, '%d%d%d%d%. (%a+) %d?%d%.%, %d%d%:%d%d %(CES?T%)')],
		tonumber(mw.ustring.match(str, '%d%d%d%d%. %a+ (%d?%d)%.%, %d%d%:%d%d %(CES?T%)')),
		tostring(mw.ustring.match(str, '%d%d%d%d%. %a+ %d?%d%.%, (%d%d%:%d%d %(CES?T%))'))
	}
	if #match ~= 4 or match[2] == nil then
		return str, nil
	end
	str = mw.ustring.format( '%d-%d-%d %s', match[1], match[2], match[3], match[4])
	return mw.getContentLanguage():formatDate('Y-m-d H:i (T)', str, true)
end

-- creates a list from (any number of) unnamed arguments using separators
-- in named arguments ("elválasztó" and "utolsó" for last) or default values
-- with Scribunto function mw.text.listToText()
function p.lista(frame)
	local format_params = 0
	local function get_params(str)
		local params = {}
		for i = 1, format_params do
			table.insert(params, str)
		end
		return unpack(params)
	end
	local args = getArgs(frame)
	local list = {}
	local format = args['formátum']
	if format then
		for _ in format:gmatch('%%') do
			format_params = format_params + 1
		end
	end
	local max_i = 0
	for n, _ in pairs(args) do
		if type(n) == 'number' and n > max_i then
			max_i = n
		end
	end
	for i = 1, max_i do
		if args[i] then
			if format then
				table.insert(list, format:format(get_params(args[i])))
			else
				table.insert(list, args[i])
			end
		end
	end
	return mw.text.listToText(list, args['elválasztó'], args['utolsó'])
end

return p