Ugrás a tartalomhoz

Modul:Nyelvtani modul

Ellenőrzött
A Wikipédiából, a szabad enciklopédiából

Nyelvtani modul[mi ez?] • [dokumentáció: mutat, szerkeszt] • [tesztek: sikeres: 2, sikertelen: 0, kihagyva: 0 (részletek)]

Előfordulások

[szerkesztés]
require('strict')
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 args = getArgs(frame, {
		removeBlanks = false
	})
	local str, errortext = args[1], args[2]
	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 errortext or 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 args = getArgs(frame)
	local list = {}
	for n, v in pairs(args) do
		if type(n) == 'number' then table.insert(list, v) end  -- insert unnamed parameters into the table
	end
	return mw.text.listToText(list, args['elválasztó'], args['utolsó'])
end

return p