Mudanças entre as edições de "Módulo:Character.Utils"
Ir para navegação
Ir para pesquisar
m |
m |
||
| Linha 62: | Linha 62: | ||
-- Carrega o módulo do personagem (Módulo:<Nome>/Modulo/Module) com cache | -- 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) | function M.requireCharModule(char) | ||
char = M.trim(char or "") | char = M.trim(char or "") | ||
| Linha 84: | Linha 85: | ||
end | end | ||
-- Tenta Modulo:Nome | -- Tenta Modulo:Nome (sem acento) | ||
ok, data = pcall(function() | ok, data = pcall(function() | ||
return require("Modulo:" .. char) | return require("Modulo:" .. char) | ||
| Linha 93: | Linha 94: | ||
end | end | ||
-- Tenta Module:Nome | -- Tenta Module:Nome (inglês) | ||
ok, data = pcall(function() | ok, data = pcall(function() | ||
return require("Module:" .. char) | return require("Module:" .. char) | ||
| Linha 102: | Linha 103: | ||
end | end | ||
-- | -- Tenta com underscores (espaços → _) | ||
local | local underscored = char:gsub(" ", "_") | ||
if | if underscored ~= char then | ||
ok, data = pcall(function() | ok, data = pcall(function() | ||
return require("Módulo:" .. | return require("Módulo:" .. underscored) | ||
end) | end) | ||
if ok and type(data) == "table" then | if ok and type(data) == "table" then | ||
Edição das 19h33min 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 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
-- 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
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