Módulo:Info.Expand

De Wiki Gla
Ir para navegação Ir para pesquisar

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

-- Módulo:Info.Expand — getTier(), getTags(), expandTier(), expandTags()
local p = {}

local utils = require("Módulo:Info.Utils")
local trim = utils.trim
local safeArgs = utils.safeArgs
local requireCharacterModule = utils.requireCharacterModule
local resolveCharFromFrames = utils.resolveCharFromFrames

-- ===== Tier textual (i18n) =====

function p.getTier(frame)
    local a = safeArgs(frame)
    local char = trim(frame.args[1] or a.nome)
    if char == "" then
        char = resolveCharFromFrames(frame, a)
    end
    local data = requireCharacterModule(char) or {}
    local lang = trim((a.lang or (frame:getParent() and frame:getParent().args.lang) or "pt"):lower())
    if data.tier_i18n and data.tier_i18n[lang] then
        return data.tier_i18n[lang]
    end
    return trim(data.tier or "")
end

-- ===== Tags textuais (i18n) =====

function p.getTags(frame)
    local a = safeArgs(frame)
    local char = trim(frame.args[1] or a.nome)
    if char == "" then
        char = resolveCharFromFrames(frame, a)
    end
    local data = requireCharacterModule(char) or {}
    local lang = trim((a.lang or (frame:getParent() and frame:getParent().args.lang) or "pt"):lower())
    local tags = (data.tags_i18n and data.tags_i18n[lang]) or data.tags or {}
    if type(tags) ~= "table" then
        return trim(tags or "")
    end
    return trim(table.concat(tags, " / "))
end

-- ===== Gambis: expandir tier/tags via token "tierNome", "tagsNome" =====

function p.expandTier(frame)
    local token = trim(frame.args[1] or "")
    local nome = trim(frame.args[2] or (frame:getParent() and frame:getParent().args.nome) or "")
    if token == "" then
        return ""
    end
    if token:lower():sub(1, 4) ~= "tier" then
        return token
    end
    local char = nome ~= "" and nome or token:match("^tier(.+)$") or ""
    if char == "" then
        local title = mw.title.getCurrentTitle()
        char = title and trim(title.text) or ""
    end
    local data = requireCharacterModule(char) or {}
    local lang = trim((frame.args.lang or "pt"):lower())
    if data.tier_i18n and data.tier_i18n[lang] then
        return data.tier_i18n[lang]
    end
    return trim(data.tier or "")
end

function p.expandTags(frame)
    local token = trim(frame.args[1] or "")
    local nome = trim(frame.args[2] or (frame:getParent() and frame:getParent().args.nome) or "")
    if token == "" then
        return ""
    end
    if token:lower():sub(1, 4) ~= "tags" then
        return token
    end
    local char = nome ~= "" and nome or token:match("^tags(.+)$") or ""
    if char == "" then
        local title = mw.title.getCurrentTitle()
        char = title and trim(title.text) or ""
    end
    local data = requireCharacterModule(char) or {}
    local arr = (data.tags_i18n and data.tags_i18n.pt) or data.tags
    if type(arr) == "table" then
        return table.concat(arr, " / ")
    end
    return trim(arr or "")
end

return p