FANDOM


local lib = {}
 
-- Zwraca zrzut wszystkich podanych parametrów
-- {{#invoke:Funkcje|dump}}
function lib.dump(frame)
    local getArgs = require('Dev:Arguments').getArgs
    local args = getArgs(frame)
    local keys = lib.getSortedKeys(args)
    local list = ''
    for i,k in ipairs(keys) do
        list = list .. '\n<u>' .. mw.text.nowiki(k) .. '</u> = ' .. tostring(mw.text.nowiki(args[tonumber(k) or k]) or 'nil')
    end
    return '<pre>' .. list  .. '</pre>'
end
 
-- Zwraca i czyści bufor mw.log()
-- {{#invoke:Funkcje|log}}
function lib.log(frame)
    local buffer = mw.getLogBuffer()
    mw.clearLogBuffer()
    if frame.args[1] then return buffer end
    return '<pre>' .. mw.text.nowiki(buffer)  .. '</pre>'
end
 
-- Meta
function lib.mergeFrames(frame, parent)
    local args = {}
    if frame then
        for k,v in pairs(frame.args) do
            args[k] = v
        end
    end
    if parent then
        for k,v in pairs(parent.args) do
            args[k] = v
        end
    end
    return args
end
function lib.arguments(origArgs)
    local args = {}
    for k, v in pairs(origArgs) do
        if type(v) == 'string' then v = mw.text.trim(v) end
        if v ~= '' then args[k] = v end
    end
    return args
end
function lib.table2array(tab, addkey)
    addkey = not not addkey
    local keys, res = {}, {}
    for k,v in pairs(tab) do keys[#keys+1] = k end
    table.sort(keys, function(a,b)
        if type(a) == type(b) then
            return a < b
        else
            return type(a) < type(b)
        end
    end)
    for i,k in ipairs(keys) do
        res[#res+1] = tab[k]
        if addkey and type(res[#res]) == 'table' then res[#res]._k = k end
    end
    return res
end
function lib.getSortedKeys(tab)
    local keys = {}
    for k,_ in pairs(tab) do keys[#keys+1] = k end
    table.sort(keys, function(a,b)
        if type(a) == type(b) then
            return a < b
        else
            return type(a) < type(b)
        end
    end)
    return keys
end
function lib.groupedArguments(args, numeric)
    if numeric == nil then numeric = true end
    numeric = not not numeric
 
    local base = {}
    local groups = {}
 
    for k, v in pairs(args) do
        v = mw.text.trim(v) or ''
        if v ~= '' then
            if type(k) == 'string' then
                k = mw.text.trim(k) or ''
                if k ~= '' then
                    local split = mw.text.split(k, ':')
                    if #split == 1 then base[k] = v
                    else
                        local group = mw.text.trim(split[1]) or ''
                        local key = mw.text.trim(table.concat(split, ':', 2)) or ''
                        if key ~= '' and group ~= '' then
                            if numeric then
                                group = tonumber(group)
                                if group ~= nil then
                                    if groups[group] == nil then groups[group] = {} end
                                    groups[group][key] = v
                                else
                                    base[k] = v
                                end
                            else
                                if groups[group] == nil then groups[group] = {} end
                                groups[group][key] = v
                            end
                        else
                            base[k] = v
                        end
                    end
                end
            elseif v ~= '' then
                base[k] = v
            end
        end
    end
    return base, groups
end
function lib.ternary(cond, T, F) if cond then return T else return F end end
 
 
function lib.split(text, pattern, plain, capture)
	local ret = {}
	for m, n in lib.gsplit( text, pattern, plain ) do
		ret[#ret+1] = m
		if capture then ret[#ret+1] = n end
	end
	return ret
end
function lib.esplit(text, pattern, exceptin, plain, capture)
    if not exceptin then return lib.split(text, pattern, plain, capture) end
    local ret, a = {}
	for m, n in lib.gsplit( text, exceptin, plain ) do
    	for o, p in lib.gsplit( m, pattern, plain ) do
    	    if a then
    	        ret[a] = ret[a] .. o
    	        a = nil
    	    else
    		    ret[#ret+1] = o
    		end
    		if capture then ret[#ret+1] = p end
    	end
		if n then
	        ret[#ret] = ret[#ret] .. n
	        a = #ret
	    end
	end
	return ret
end
function lib.gsplit( text, pattern, plain )
	local s, l = 1, mw.ustring.len( text )
	return function ()
		if s then
			local e, n = mw.ustring.find( text, pattern, s, plain )
			local ret1, ret2
			if not e then
				ret1 = mw.ustring.sub( text, s )
				s = nil
			elseif n < e then
				-- Empty separator!
				ret1 = mw.ustring.sub( text, s, e )
				ret2 = ''
				if e < l then
					s = e + 1
				else
					s = nil
				end
			else
				ret1 = e > s and mw.ustring.sub( text, s, e - 1 ) or ''
				ret2 = mw.ustring.sub( text, e, n )
				s = n + 1
			end
			return ret1, ret2
		end
	end, nil, nil
end
function lib.gsub(text, pattern, replacement, n, exceptin)
    if not exceptin then return mw.ustring.gsub(text, pattern, replacement, n) end
    local res = lib.split(text, exceptin, false, true)
    local sum, no = 0
    for i=1,#res,2 do
        res[i], no = mw.ustring.gsub(res[i], pattern, replacement, n)
        sum = sum + no
        if n and n-sum < 1 then break end
    end
 
    return table.concat(res, ''), sum
end
 
return lib
Treści społeczności są dostępne na podstawie licencji CC-BY-SA , o ile nie zaznaczono inaczej.