FANDOM


local UH = {}
local lang = mw.getContentLanguage()
local contribs = {
    en = 'Contriutions',
    pl = 'Wkład',
}
 
-- Dependencies
local getArgs = require('Module:Translate').getArgs
 
-- Presets
local function nsPreset(ns, user, tpl)
    local res, name = {}, mw.ustring.gsub(mw.site.namespaces[ns].name, ' ', '_')
    if mw.site.namespaces[ns].isCapitalized then user = lang:ucfirst(user) end
 
    res[#res+1] = mw.ustring.format(tpl or 'a[href="/wiki/%s:%s"]', name, user)
    res[#res+1] = mw.ustring.format(tpl or 'a[href="/wiki/%s:%s"]', mw.uri.encode(name, 'WIKI'), mw.uri.encode(user, 'WIKI'))
    return res
end
local customPresets, customPattern = {'a[data-user="%username%"]'}
local defaultPresets = 'userlink, user'
local presets = setmetatable({
    userlink = function(user)
        return unpack(nsPreset(2, user, 'a[href="/wiki/%s:%s"].mw-userlink'))
    end,
    user = function(user)
        return unpack(nsPreset(2, user))
    end,
    talk = function(user)
        return unpack(nsPreset(3, user))
    end,
    wall = function(user)
        return unpack(nsPreset(1200, user))
    end,
    contribs = function(user)
        local res, name, cont = {}, mw.ustring.gsub(mw.site.namespaces[-1].name, ' ', '_'), mw.ustring.gsub(contribs[lang:getCode()] or contribs.en, ' ', '_')
 
        res[#res+1] = mw.ustring.format('a[href="/wiki/%s:%s/%s"]', name, cont, user)
        res[#res+1] = mw.ustring.format('a[href="/wiki/%s:%s/%s"]', mw.uri.encode(name, 'WIKI'), mw.uri.encode(cont, 'WIKI'), mw.uri.encode(user, 'WIKI'))
        return unpack(res)
    end,
    any = function(user)
        if mw.site.namespaces[2].isCapitalized then user = lang:ucfirst(user) end
        return
            mw.ustring.format('a[href$=":%s"]', user),
            mw.ustring.format('a[href$=":%s"]', mw.uri.encode(user, 'WIKI'))
    end
}, {
    __index = function(self, key)
        return function()
            local no = tonumber(mw.ustring.match(key, customPattern))
            if no and customPresets[no] then
                return customPresets[no]
            end
        end
    end,
})
 
local function ins(tab, unique, ...)
    for i,v in ipairs(arg) do
        if not unique then
            tab[#tab+1] = v
        else
            local found = false
            for i,v2 in ipairs(tab) do
                if v == v2 then
                    found = true
                    break
                end
            end
            if not found then tab[#tab+1] = v end
        end
    end
    return tab
end
local function makeGroup(group)
    if not group.users or not (group.css or group.icon) then return end
    local highlight = mw.text.split(group.highlight or defaultPresets, '%s*,%s*')
 
    local users = mw.ustring.match(group.users, '^%%(.*)$')
    if users then
        users = tonumber(users) or users
        if type(users) == 'number' then
            users = {'Nanaki', 'Nanaki.bot', 'ąć'}
        else
            users = mw.loadData('Module:UserHighlight/globals')[users]
        end
    elseif group.users then
        users = mw.text.split(group.users or '', '%s*\n%s*')
    end
 
    local count = 0
    for i,v in ipairs(users or {}) do count = i end
    if not users or count == 0 then return end
 
    local res = {}
    if group.css then
        local res2 = {}
        for i,user in ipairs(users) do
            local res3 = {}
            for i,v in ipairs(highlight) do
                ins(res3, true, presets[v](user))
            end
            res2[#res2+1] = table.concat(res3, ',\n')
        end
        res[#res+1] = table.concat(res2, ',\n') .. ' {\n\t' .. mw.ustring.gsub(group.css, '\n%s*', '\n\t') .. '\n}'
    end
    if group.icon then
        local res2 = {}
        for i,user in ipairs(users) do
            local a, b = unpack(nsPreset(2, user))
            ins(res2, true, a .. '::before', b .. '::before')
        end
        local styles = {
            'content: \'\';',
            'display: inline-block;',
            'margin: 0 .2em;',
            'vertical-align: text-bottom;',
        }
        local ratio = 1
        if group.size then
            local w, h = mw.ustring.match(tostring(group.size), '(%d+)%s*[×x]%s*(%d+)')
            ratio = tonumber(w)/tonumber(h)
        end
        local height = 1.119;
        styles[#styles+1] = mw.ustring.format('height: %.3fem;', height)
        styles[#styles+1] = mw.ustring.format('width: %.3fem;', height*ratio)
        styles[#styles+1] = mw.ustring.format('background: url(\'/wiki/Special:FilePath/%s?width=480&height=48\') no-repeat center;', mw.uri.encode(group.icon, 'WIKI'))
        styles[#styles+1] = 'background-size: 100% 100%;'
 
 
 
        res[#res+1] = table.concat(res2, ',\n') .. ' {\n\t' .. table.concat(styles, '\n\t') .. '\n}'
    end
 
    res = table.concat(res, '\n')
    if group.name then res = mw.ustring.format('\n/* %s - %d */\n', group.name, count) .. res end
    return res
end
UH.makeGroup = makeGroup
 
 
function dump(tab, indent, func)
    if type(tab) ~= 'table' then return end
    func = func or pairs
    indent = indent or ''
    for k,v in func(tab) do
        mw.log(indent..'['..tostring(k)..'] = '..tostring(v))
        --dump(v, indent..'['..tostring(k)..'] ', func)
    end
end
UH.dump = dump
 
 
UH.args = {
    ['contribs'] = 'Specjalna:Wkład',
    ['1:name'] = 'Helperzy',
    ['1:css'] = 'color:blue;text-decoration:underline;',
    ['3:icon'] = 'Nunchaku.svg',
    ['3:size'] = '512x345',
    ['1:users'] = '',
    ['2:name'] = 'Boty',
    ['2:highlight'] = 'any',
    ['2:css'] = 'color:green;',
    ['2:icon'] = 'Elliot.png',
    ['2:users'] = '',
    ['3:name'] = 'Administratorzy',
    ['3:order'] = '1',
    ['3:highlight'] = 'userpage',
    ['3:css'] = 'color:red;text-decoration:underline;',
    ['1:icon'] = 'Wiki-wordmark.png',
    ['1:size'] = '250x65',
    ['3:users'] = 'Nanaki\nMyslec'
}
function UH.css(frame)
    local args, md = getArgs(frame or UH.args, {
        translate = {
            custom = 'custom #',
            name = '#:name',
            highlight = '#:highlight',
            css = '#:css',
            icon = '#:icon',
            size = '#:size',
            users = '#:users',
        },
        trim = true,
        removeBlank = true,
    })
    mw.log(rawget(args.custom, 'pattern'))
    customPattern = rawget(md.custom, 'pattern')
 
    local res, groups = {}, args:group({'users'}, {'css', 'icon', 'highlight', 'name', 'size'})
    for i,v in pairs(groups) do res[#res+1] = makeGroup(v) end
 
    return mw.text.trim(table.concat(res, '\n'))
end
return UH

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.