Module:LuaLink

From Polcompball Wiki
Jump to navigationJump to search

The LuaLink module contains several functions to create user/ideology/category links that do not require the creation of a dedicated template.

user

category

ideology


local utils = {}

local function emptyCheck(value)
    if value == nil then
        return false
    elseif value == "" then
        return false
    elseif value == {} then
        return false
    else
        return true
    end
end

local function parseIcons(params, link)
    local left, right, i = "", "", 1
    while emptyCheck(params["icon" .. i]) do
        local icon = "[[File:" .. params["icon" .. i] .. "|link=" .. link .. "]]"
        if i % 2 == 0 then
            right = icon .. right
        else
            left = left .. icon
        end
        i = i + 1
    end
    return {left, right}
end

local function createElement(name, params, page, disp)
    local color, style = "#000", ""

    if emptyCheck(params["color"]) then
        color = params["color"]
    end

    if emptyCheck(params["style"]) then
        style = params["style"]
    end

    style = "color: " .. color .. "; " .. style

    local icons = parseIcons(params, page)

    local coreLink = "[[" .. page .. '|<span style="' .. style .. '">&nbsp;' .. disp .. "&nbsp;</span>]]"

    return "'''" .. icons[1] .. coreLink .. icons[2] .. "'''"

end

function utils.user(frame)
    local parentArgs = frame:getParent().args
    local name = parentArgs[1]
    local users = mw.loadData("Module:LuaLink/users")

    local params = users[name]
    if emptyCheck(params) then
        local page, disp

        if emptyCheck(parentArgs[2]) then
            page = parentArgs[2]
        elseif emptyCheck(params["link"]) then
            page = params["link"]
        else
            page = "UserWiki:" .. name
        end

        if emptyCheck(parentArgs[3]) then
            disp = parentArgs[3]
        elseif emptyCheck(parentArgs[2]) then
            disp = page
        elseif emptyCheck(params["nick"]) then
            disp = params["nick"]
        else
            disp = name
        end

        return createElement(name, params, page, disp)
    else
        return "Invalid user"
    end
end

function utils.category(frame)
    local parentArgs = frame:getParent().args
    local name = parentArgs[1]
    local cats = mw.loadData("Module:LuaLink/categories")

    local params = cats[name]
    if emptyCheck(params) then
        local disp, page = nil, ":Category:" .. name

        if emptyCheck(parentArgs[2]) then
            disp = parentArgs[2]
        elseif emptyCheck(params["nick"]) then
            disp = params["nick"]
        else
            disp = name
        end

        return createElement(name, params, page, disp)
    else
        return "Invalid category"
    end
end

function utils.ideology(frame)
    local parentArgs = frame:getParent().args
    local name = parentArgs[1]
    local ideo = mw.loadData("Module:LuaLink/ideologies")

    local params = ideo[name]
    if emptyCheck(params) then
        local disp

        if emptyCheck(parentArgs[2]) then
            disp = parentArgs[2]
        elseif emptyCheck(params["nick"]) then
            disp = params["nick"]
        else
            disp = name
        end

        return createElement(name, params, name, disp)
    else
        return "Invalid ideology"
    end
end

return utils