Módulo:Changelog

De Wiki Gla
Revisão de 14h16min de 10 de setembro de 2025 por Gurren1 (discussão | contribs)
Ir para navegação Ir para pesquisar

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

local p = {}

local function N(s)
    return s and mw.text.trim(tostring(s)) or ''
end
local function has(s)
    return N(s) ~= ''
end

local function clean_url(s)
    s = N(s)
    -- remove CR/LF/TAB e espaços dentro do atributo
    s = s:gsub('[\r\n\t]', ''):gsub(' +', '')
    return s
end

-- Uso: {{#invoke:Changelog|item|title=...|description=...|date=...|link=...|icon=...|color=#FCE5CD}}
function p.item(frame)
    local parent = frame:getParent()
    local a = (parent and parent.args) or frame.args or {}

    local title = N(a.title)
    local link = clean_url(a.link)
    local date = N(a.date)
    local desc = N(a.description)
    local icon = N(a.icon)
    local color = N(a.color)

    -- container do item
    local item = mw.html.create('div'):addClass('notice-item')
    if has(color) then
        item:css('--notice-color', color)
    end

    -- preparar ícones (inline e overlay), sem link
    local overlay_icon_wikitext, inline_icon_wikitext
    if has(icon) then
        local lower = icon:lower()
        local is_url = lower:find('://', 1, true) ~= nil
        local is_prefixed = lower:match('^file:') or lower:match('^image:')

        if is_url then
            overlay_icon_wikitext = string.format('<img src="%s" alt="%s" class="notice-icon notice-icon--overlay" />',
                icon, title)
            inline_icon_wikitext = string.format('<img src="%s" alt="%s" class="notice-icon notice-icon--inline" />',
                icon, title)
        elseif is_prefixed then
            overlay_icon_wikitext = string.format('[[%s|alt=%s|class=notice-icon notice-icon--overlay|link=]]', icon,
                title)
            inline_icon_wikitext = string.format('[[%s|alt=%s|class=notice-icon notice-icon--inline|link=]]', icon,
                title)
        else
            overlay_icon_wikitext = string.format('[[File:%s|alt=%s|class=notice-icon notice-icon--overlay|link=]]',
                icon, title)
            inline_icon_wikitext = string.format('[[File:%s|alt=%s|class=notice-icon notice-icon--inline|link=]]', icon,
                title)
        end
    end

    local content = item:tag('div'):addClass('notice-content'):css('flex', '1')

    -- header com esquerda (ícone+titulo) e direita (data)
    if has(title) or has(date) then
        local header = content:tag('div'):addClass('notice-header')
        local left = header:tag('div'):addClass('notice-header-left')
        local right = header:tag('div'):addClass('notice-header-right')

        if inline_icon_wikitext then
            left:wikitext(inline_icon_wikitext)
        end

        if has(title) then
            local holder = left:tag('span'):addClass('notice-title')
            if has(link) then
                holder:tag('a'):attr('href', link):attr('target', '_blank') -- também forço nova aba aqui
                :attr('rel', 'noopener noreferrer'):wikitext(title)
            else
                holder:wikitext(title)
            end
        end

        if has(date) then
            right:tag('span'):addClass('notice-date'):wikitext(date)
        end
    end

    if has(desc) then
        content:tag('p'):addClass('notice-desc'):wikitext(desc)
    end

    if overlay_icon_wikitext then
        item:wikitext(overlay_icon_wikitext)
    end

    return tostring(item)
end

return p