Module:LuaLink: Difference between revisions

From Polcompball Wiki
Jump to navigationJump to search
Content added Content deleted
(Created page with "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...")
 
No edit summary
Line 49: Line 49:


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


Line 56: Line 57:
local page, disp
local page, disp


if emptyCheck(frame.args[2]) or emptyCheck(frame:getParent().args[2]) then
if emptyCheck(parentArgs[2]) then
page = frame.args[2] or frame:getParent().args[2]
page = parentArgs[2]
else
else
page = "User:" .. name
page = "User:" .. name
end
end


if emptyCheck(frame.args[3]) or emptyCheck(frame:getParent().args[3]) then
if emptyCheck(parentArgs[3]) then
disp = frame.args[3] or frame:getParent().args[3]
disp = parentArgs[3]
elseif emptyCheck(frame.args[2]) or emptyCheck(frame:getParent().args[2]) then
elseif emptyCheck(parentArgs[2]) then
disp = page
disp = page
elseif emptyCheck(params["nick"]) then
elseif emptyCheck(params["nick"]) then
Line 79: Line 80:


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


Line 86: Line 88:
local disp, page = nil, ":Category:" .. name
local disp, page = nil, ":Category:" .. name


if emptyCheck(frame.args[2]) or emptyCheck(frame:getParent().args[2]) then
if emptyCheck(parentArgs[2]) then
disp = frame.args[2] or frame:getParent().args[2]
disp = parentArgs[2]
elseif emptyCheck(params["nick"]) then
elseif emptyCheck(params["nick"]) then
disp = params["nick"]
disp = params["nick"]
Line 101: Line 103:


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


Line 108: Line 111:
local disp
local disp


if emptyCheck(frame.args[2]) or emptyCheck(frame:getParent().args[2]) then
if emptyCheck(parentArgs[2]) then
disp = frame.args[2] or frame:getParent().args[2]
disp = parentArgs[2]
elseif emptyCheck(params["nick"]) then
elseif emptyCheck(params["nick"]) then
disp = params["nick"]
disp = params["nick"]

Revision as of 20:18, 18 March 2023

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 .. "</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]
        else
            page = "User:" .. 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