Módulo:Character.Utils
Ir para navegação
Ir para pesquisar
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
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
ok, data = pcall(function()
return require("Módulo:" .. char)
end)
if ok and type(data) == "table" then
M._moduleCache[char] = data
return data
end
ok, data = pcall(function()
return require("Modulo:" .. char)
end)
if ok and type(data) == "table" then
M._moduleCache[char] = data
return data
end
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