Mudanças entre as edições de "Módulo:Teste"

De Wiki Gla
Ir para navegação Ir para pesquisar
m
m
 
(5 revisões intermediárias pelo mesmo usuário não estão sendo mostradas)
Linha 1: Linha 1:
--[[
Módulo:Teste — exemplo didático (colar na página Módulo:Teste).
Uso: {{Teste|titulo=...|texto=...}}
]]
local p = {}
local p = {}
-- Parseia bau no formato "2 comum, 8 ouro" ou "8 ouro, 2 comum". Retorna comum, ouro (números) ou nil,nil se for só tipo (comum/ouro).
local function parseBau(s)
    if not s or s == '' then return nil, nil end
    s          = mw.text.trim(s)
    local comum = tonumber(s:match('(%d+)%s*[Cc]omum')) or 0
    local ouro  = tonumber(s:match('(%d+)%s*[Oo]uro')) or 0
    if comum > 0 or ouro > 0 then
        return comum, ouro
    end
    return nil, nil
end


function p.main(frame)
function p.main(frame)
     local args = frame:getParent().args
     local args = frame:getParent().args
     local cards = {}
     local titulo = mw.text.trim(args.titulo or '')
 
    local texto = mw.text.trim(args.texto or '')
    local i = 1
    if titulo == '' then
    while true do
         titulo = 'Sem título'
        local nome = args['nome' .. i]
        if not nome or mw.text.trim(nome) == '' then break end
 
        local link        = mw.text.trim(args['link' .. i] or '')
        local img        = mw.text.trim(args['img' .. i] or '')
        local qtd        = mw.text.trim(args['qtd' .. i] or '0')
        local bauParam    = mw.text.trim(args['bau' .. i] or 'comum')
 
        local comum, ouro = parseBau(bauParam)
        if comum == nil and ouro == nil then
            -- Modo antigo: bau = comum ou ouro, qtd = quantidade
            local n = tonumber(qtd) or 0
            if bauParam:lower() == 'ouro' then
                comum, ouro = 0, n
            else
                comum, ouro = n, 0
            end
        end
 
        local imgHtml  = frame:preprocess('[[File:' .. img .. '|150px|link=]]')
        local href      = tostring(mw.uri.localUrl(link))
        local comumHtml = frame:preprocess('[[File:Chest_normal.png|link=]]')
        local ouroHtml  = frame:preprocess('[[File:Chest_gold.png|link=]]')
 
        -- Usar span + data-href para evitar que a tag <a> seja escapada pelo parser/Widgets
        local card      = mw.html.create('span')
         card:attr('class', 'island-card')
            :attr('data-href', href)
            :attr('role', 'link')
            :attr('tabindex', '0')
 
        card:tag('span'):attr('class', 'island-img')
            :node(mw.html.create(''):wikitext(imgHtml))
 
        card:tag('span'):attr('class', 'island-name')
            :wikitext(nome)
 
        local chests = card:tag('span'):attr('class', 'island-chests')
        if comum > 0 then
            chests:tag('span'):attr('class', 'island-chest-group')
                :node(mw.html.create(''):wikitext(comumHtml))
                :tag('span'):attr('class', 'island-chest-count'):wikitext(tostring(comum))
        end
        if ouro > 0 then
            chests:tag('span'):attr('class', 'island-chest-group')
                :node(mw.html.create(''):wikitext(ouroHtml))
                :tag('span'):attr('class', 'island-chest-count'):wikitext(tostring(ouro))
        end
 
        cards[#cards + 1] = card
        i = i + 1
     end
     end


     local grid = mw.html.create('div')
     local root = mw.html.create('div'):addClass('teste-caixa')
     grid:attr('class', 'island-grid')
     root:tag('h3'):wikitext(titulo)
     for _, card in ipairs(cards) do
    root:tag('p')
         grid:node(card)
        :addClass('teste-corpo')
    end
        :addClass('teste-colapsado')
        :wikitext(texto)
     -- <button> na saída do #invoke costuma ser escapado pelo sanitizer; usar span + role (igual data-href no lugar de <a>)
    root:tag('span')
        :addClass('teste-toggle')
        :attr('role', 'button')
        :attr('tabindex', '0')
         :wikitext('Expandir')


     return tostring(grid)
     return tostring(root)
end
end


return p
return p

Edição atual tal como às 00h23min de 1 de abril de 2026

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

--[[
Módulo:Teste — exemplo didático (colar na página Módulo:Teste).

Uso: {{Teste|titulo=...|texto=...}}
]]

local p = {}

function p.main(frame)
    local args = frame:getParent().args
    local titulo = mw.text.trim(args.titulo or '')
    local texto = mw.text.trim(args.texto or '')
    if titulo == '' then
        titulo = 'Sem título'
    end

    local root = mw.html.create('div'):addClass('teste-caixa')
    root:tag('h3'):wikitext(titulo)
    root:tag('p')
        :addClass('teste-corpo')
        :addClass('teste-colapsado')
        :wikitext(texto)
    -- <button> na saída do #invoke costuma ser escapado pelo sanitizer; usar span + role (igual data-href no lugar de <a>)
    root:tag('span')
        :addClass('teste-toggle')
        :attr('role', 'button')
        :attr('tabindex', '0')
        :wikitext('Expandir')

    return tostring(root)
end

return p