Moduuli:Viikon kilpailu

Wikipediasta
Siirry navigaatioon Siirry hakuun



uk = {}

function uk.nimi(frame)
    local year
    local week
    if frame.args[1] == nil then
        return uk.naa(frame)
    end
    if frame.args[2] == nil then
        local spl = mw.text.split(frame.args[1], '-', true)
        year = tonumber(spl[1])
        week = tonumber(spl[2])
    else
        year = tonumber(frame.args[1])
        week = tonumber(frame.args[2])
    end
    local modname = 'Module:Viikon kilpailu luettelo ' .. year
    local topics = require(modname)
    local topic = topics[week]
    return topic
end

function uk.vanha(fname)
    fname.args[1] = - tonumber(fname.args[1])
    return uk.uusi(fname)
end

function uk.naa(frame)
    local lang = mw.getContentLanguage()
    local week = tonumber(lang:formatDate('W', now, true))
    local year = tonumber(lang:formatDate('o', now, true))
    local modname = 'Module:Viikon kilpailu luettelo ' .. year
    local topics = require(modname)
    local topic = topics[week]
    return topic
end

function uk.fremtid(frame)
    local lang = mw.getContentLanguage()
    local diff = frame.args[1]
    local week = tonumber(lang:formatDate('W', diff..' week', true))
    local year = tonumber(lang:formatDate('o', diff..' week', true))
    local modname = 'Module:Viikon kilpailu luettelo ' .. year
    local topics = require(modname)
    local topic = topics[week]
    return topic
end 

-- ########################################################
-- Funksjoner for {{Ukens konkurranse kriterium}}
-- ########################################################

function uk.format_kriterium(pre, main, main_fl, desc, items)
    if #items > 1 then
        return pre .. ' ' .. main_fl .. ' ' .. mw.text.listToText(items, ', ', ' eller ')
    elseif #items == 1 then 
        return pre .. ' ' .. main .. ' ' .. items[1]
    else
        return '{{Feil|Ingen ' .. desc ..' ble angitt}}'
    end
end

function uk.pagelist(args)
    local r = {}
    for i, v in ipairs(args) do
        v = mw.text.trim(v)
        if v ~= '' then
            local lang, page = string.match(v, '^([a-z\]+):(.+)$')
            if lang then
                table.insert(r, string.format('[[:%s:%s|%s]]', lang, page, page))
            else
                table.insert(r, string.format('[[:%s]]', v))
            end
        end
    end 
    return r
end

function uk.kriterium_maler(args)
    local r = {}
    for i, v in ipairs(args) do
        v = mw.text.trim(v)
        if v ~= '' then
            local m, n = string.match(v, '^([a-z\]+):(.+)$')
            if m then
                table.insert(r, string.format('{{Mal|%s|%s}}', n, m))
            else
                table.insert(r, string.format('{{Mal|%s}}', v))
            end
        end
    end
    return uk.format_kriterium('merket med', 'malen', 'minst en av malene', 'maler', r)
end

function uk.kriterium_kategorier(args)
    local prefix = {
        ['se'] = 'se:Kategoriija',
        ['nn'] = 'nn:Kategori',
        ['no'] = 'Kategori'
    }
    local r = {}
    for i, v in ipairs(args) do
        v = mw.text.trim(v)
        if v ~= '' then
            local lang = 'no'
            local name = v
            local m, n = string.match(v, '^([a-z\]+):(.+)$')
            if m then
                lang = m
                name = n
            end
            table.insert(r, string.format('[[:%s:%s|%s]]', prefix[lang], name, name, name))
        end
    end
    return uk.format_kriterium('ligger i', 'kategorien', 'minst en av kategoriene', 'kategorier', r)
end

function uk.kriterium_tilbakelenke(args)
    return uk.format_kriterium('lenkes fra', '', '', 'sider', uk.pagelist(args))
end

function uk.kriterium_fremlenke(args)
    return uk.format_kriterium('lenker til', '', '', 'sider', uk.pagelist(args))
end

function uk.kriterium_enkeltsider(args)
    return uk.format_kriterium('', 'siden', 'sidene', 'sider', uk.pagelist(args))
end

function uk.kriterium_bytes(args) 
   return 'utvides med minst ' .. args[1] .. ' bytes' 
end

function uk.kriterium_navnerom(args) 
   return 'ligger i navnerommet {{ns:' .. args[1] .. '}}' 
end

function uk.kriterium(frame)
    local t = {
        ['ny'] = 'opprettes denne uka',
        ['eksisterende'] = 'er opprettet før denne uka',
        ['stubb'] = 'stubbmerket fjernes',
        ['bytes'] = function(args) return uk.kriterium_bytes(args) end,
        ['navnerom'] = function(args) return uk.kriterium_navnerom(args) end,
        ['mal'] = function(args) return uk.kriterium_maler(args) end,
        ['kategori'] = function(args) return uk.kriterium_kategorier(args) end,
        ['tilbakelenke'] = function(args) return uk.kriterium_tilbakelenke(args) end,
        ['fremlenke'] = function(args) return uk.kriterium_tilbakelenke(args) end,
        ['enkeltsider'] = function(args) return uk.kriterium_enkeltsider(args) end
    }
    local args = {}
    for i, v in ipairs(frame.args) do
        if tonumber(i) ~= nil then
            args[i] = v
        end
    end
    local k = frame.args.kriterium
    local s = type(t[k]) == "function" and t[k](args) or t[k] or '{{Feil|Intet gyldig kriterium ble angitt}}'
    return frame:preprocess(s)
end

return uk