Mudanças entre as edições de "Módulo:Character.Utils"

De Wiki Gla
Ir para navegação Ir para pesquisar
m
m
 
(Uma revisão intermediária pelo mesmo usuário não está sendo mostrada)
Linha 83: Linha 83:
         M._moduleCache[char] = data
         M._moduleCache[char] = data
         return data
         return data
    end
    -- Tenta com espaços (underscores → espaço)
    local spaced = char:gsub("_", " ")
    if spaced ~= char then
        ok, data = pcall(function()
            return require("Módulo:" .. spaced)
        end)
        if ok and type(data) == "table" then
            M._moduleCache[char] = data
            return data
        end
    end
    -- Tenta com underscores (espaços → _)
    local underscored = char:gsub(" ", "_")
    if underscored ~= char then
        ok, data = pcall(function()
            return require("Módulo:" .. underscored)
        end)
        if ok and type(data) == "table" then
            M._moduleCache[char] = data
            return data
        end
    end
    -- Fallback: tenta última palavra do nome (ex: "Trafalgar Law" → "Law")
    local lastWord = char:match("(%S+)%s*$")
    if lastWord and lastWord ~= char then
        ok, data = pcall(function()
            return require("Módulo:" .. lastWord)
        end)
        if ok and type(data) == "table" then
            M._moduleCache[char] = data
            return data
        end
     end
     end


Linha 101: Linha 137:
         M._moduleCache[char] = data
         M._moduleCache[char] = data
         return data
         return data
    end
    -- Tenta com underscores (espaços → _)
    local underscored = char:gsub(" ", "_")
    if underscored ~= char then
        ok, data = pcall(function()
            return require("Módulo:" .. underscored)
        end)
        if ok and type(data) == "table" then
            M._moduleCache[char] = data
            return data
        end
     end
     end



Edição atual tal como às 19h56min de 3 de dezembro de 2025

A documentação para este módulo pode ser criada em Módulo:Character.Utils/doc

-- Módulo:Character.Utils — funções utilitárias compartilhadas
local M = {}

-- Cache de módulos carregados
M._moduleCache = {}

--------------------------------------------------------------------------------
-- Utils
--------------------------------------------------------------------------------

function M.trim(s)
    return (tostring(s or ""):gsub("^%s+", ""):gsub("%s+$", ""))
end

-- Gera URL pública de arquivo (tenta "Special:" e "Especial:" por compat)
function M.fileURL(name)
    name = M.trim(name or "")
    if name == "" then
        return ""
    end
    name = name:gsub("^Arquivo:", ""):gsub("^File:", "")
    local t1 = mw.title.new("Special:FilePath/" .. name)
    if t1 then
        local u = t1:fullUrl()
        if u and u ~= "" then
            return u
        end
    end
    local t2 = mw.title.new("Especial:FilePath/" .. name)
    if t2 then
        local u = t2:fullUrl()
        if u and u ~= "" then
            return u
        end
    end
    return ""
end

-- Normaliza dimensões (mantém %, px, rem; números viram %)
function M.normalizeDim(x)
    x = mw.text.trim(tostring(x or ""))
    if x == "" then
        return nil
    end
    if x:match("%%$") then
        return x
    end
    if x:match("^%d+%.?%d*$") then
        return x .. "%"
    end
    return x
end

-- Monta string dos atributos (PVE, PvP, Energia, CD), usando '-' quando vazio
function M.makeAttrString(pve, pvp, energy, cd)
    local function nzOrDash(s)
        s = M.trim(s or "")
        return (s ~= "" and s or "-")
    end
    return table.concat({ nzOrDash(pve), nzOrDash(pvp), nzOrDash(energy), nzOrDash(cd) }, ", ")
end

-- Carrega o módulo do personagem (Módulo:<Nome>/Modulo/Module) com cache
-- Carrega APENAS pelo nome exato passado, sem fallbacks
function M.requireCharModule(char)
    char = M.trim(char or "")
    if char == "" then
        return nil
    end

    -- Verifica cache primeiro
    if M._moduleCache[char] then
        return M._moduleCache[char]
    end

    local ok, data

    -- Tenta Módulo:Nome
    ok, data = pcall(function()
        return require("Módulo:" .. char)
    end)
    if ok and type(data) == "table" then
        M._moduleCache[char] = data
        return data
    end

    -- Tenta com espaços (underscores → espaço)
    local spaced = char:gsub("_", " ")
    if spaced ~= char then
        ok, data = pcall(function()
            return require("Módulo:" .. spaced)
        end)
        if ok and type(data) == "table" then
            M._moduleCache[char] = data
            return data
        end
    end

    -- Tenta com underscores (espaços → _)
    local underscored = char:gsub(" ", "_")
    if underscored ~= char then
        ok, data = pcall(function()
            return require("Módulo:" .. underscored)
        end)
        if ok and type(data) == "table" then
            M._moduleCache[char] = data
            return data
        end
    end

    -- Fallback: tenta última palavra do nome (ex: "Trafalgar Law" → "Law")
    local lastWord = char:match("(%S+)%s*$")
    if lastWord and lastWord ~= char then
        ok, data = pcall(function()
            return require("Módulo:" .. lastWord)
        end)
        if ok and type(data) == "table" then
            M._moduleCache[char] = data
            return data
        end
    end

    -- Tenta Modulo:Nome (sem acento)
    ok, data = pcall(function()
        return require("Modulo:" .. char)
    end)
    if ok and type(data) == "table" then
        M._moduleCache[char] = data
        return data
    end

    -- Tenta Module:Nome (inglês)
    ok, data = pcall(function()
        return require("Module:" .. char)
    end)
    if ok and type(data) == "table" then
        M._moduleCache[char] = data
        return data
    end

    return nil
end

-- Separa uma sequência de {} (JSONs colados) em uma tabela de chunks
function M.collectJsonObjects(s)
    s = tostring(s or "")
    local out = {}
    for chunk in s:gmatch("%b{}") do
        table.insert(out, chunk)
    end
    return out
end

return M