League of Legends Wiki
Advertisement
League of Legends Wiki

Dokumentacja dla tego modułu może zostać utworzona pod nazwą Moduł:Dane/opis

local D = {}
local getArgs = require('Dev:Arguments').getArgs
local json = require('Dev:Json')

D.titles = {
    champion = 'Dane/%s'
}
function getData(t, val)
    assert(mw.getCurrentFrame(), 'Frame is required')
    local res = mw.getCurrentFrame():expandTemplate{
        title = mw.ustring.format(D.titles[t], val),
        args = { 'JSON' }
    }
    res = json.decode(res)
    for k,v in pairs(res) do
        local n = tonumber(k)
        if n and n == math.floor(n) then
            res[n] = v
            res[k] = nil
        end
    end
    return res
end

for k,v in pairs(D.titles) do
    D[k] = function(frame)
        local args = getArgs(frame, { frameOnly = true })
        local data = getData(k, args[1])
        local t, m, f = args['template'], args['module'], args['function']
        for i,k in ipairs{'template','module','function'} do args[k] = nil end
        
        for i,v in ipairs(args) do
            if data[i] == nil and i > 1 then
                data[i] = v
            end
        end
        for k,v in pairs(args) do
            if data[k] == nil then
                data[k] = v
            end
        end
        
        if t then
            local s, res = pcall(function()
                return frame:expandTemplate{ title = t, args = data }
            end)
            if not s then
                return mw.ustring.format('[[:%s]]', mw.title.new( t, mw.site.namespaces[10].name ).prefixedText)
            end
            return res
        elseif m and f then
            local func = require('Module:' .. m)[f]
            assert(func, 'Function doesn\'t exist')
            return func(data)
        end
        error('Need to specify a template or module/function combo')
    end
end

function D.dump(frame)
    local args = getArgs(frame, {frameOnly = true})
    return json.encode(args)
end
function D.dumpParent(frame)
    local args = getArgs(frame, {parentOnly = true})
    return json.encode(args)
end

return D
Advertisement