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

De Wiki Gla
Ir para navegação Ir para pesquisar
m
m
 
(6 revisões intermediárias pelo mesmo usuário não estão sendo mostradas)
Linha 1: Linha 1:
local p = {}
--[[
  Módulo: opcional para inserir o widget via {{#invoke}}.


-- 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).
  Na wiki: criar página Módulo:GlaReportEmbed com este código.
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)
  Uso num modelo ou página:
     local args = frame:getParent().args
     {{#invoke:GlaReportEmbed|embed|api_base=https://teu-deploy.com}}
    local cards = {}


     local i = 1
  Parâmetros nomeados (opcionais):
     while true do
     |heading=Título da caixa
        local nome = args['nome' .. i]
     |wid=2
        if not nome or mw.text.trim(nome) == '' then break end


        local link        = mw.text.trim(args['link' .. i] or '')
  Requer a extensão Scribunto e o widget {{#widget:ReportModal|...}} registrado.
        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)
local p = {}
        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 .. '|220px|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')
function p.embed(frame)
        if comum > 0 then
local pargs = (frame:getParent() and frame:getParent().args) or {}
            chests:tag('span'):attr('class', 'island-chest-group')
local args = frame.args or {}
                :node(mw.html.create(''):wikitext(comumHtml))
local base = (
                :tag('span'):attr('class', 'island-chest-count'):wikitext(tostring(comum))
args.api_base
        end
or pargs.api_base
        if ouro > 0 then
or args[1]
            chests:tag('span'):attr('class', 'island-chest-group')
or pargs[1]
                :node(mw.html.create(''):wikitext(ouroHtml))
or ""
                :tag('span'):attr('class', 'island-chest-count'):wikitext(tostring(ouro))
):gsub("^%s+", ""):gsub("%s+$", "")
        end
if base == "" then
return '<span class="error">GlaReportEmbed: falta <code>api_base</code>.</span>'
end


        cards[#cards + 1] = card
local heading = args.heading or pargs.heading or ""
        i = i + 1
local wid = args.wid or pargs.wid or ""
    end


    local grid = mw.html.create('div')
local wikitext = "{{#widget:ReportModal|api_base=" .. base
    grid:attr('class', 'island-grid')
if heading ~= "" then
    for _, card in ipairs(cards) do
wikitext = wikitext .. "|heading=" .. heading
        grid:node(card)
end
    end
if wid ~= "" then
wikitext = wikitext .. "|wid=" .. wid
end
wikitext = wikitext .. "}}"


    return tostring(grid)
return frame:preprocess(wikitext)
end
end


return p
return p

Edição atual tal como às 16h07min de 28 de abril de 2026

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

--[[
  Módulo: opcional para inserir o widget via {{#invoke}}.

  Na wiki: criar página Módulo:GlaReportEmbed com este código.

  Uso num modelo ou página:
    {{#invoke:GlaReportEmbed|embed|api_base=https://teu-deploy.com}}

  Parâmetros nomeados (opcionais):
    |heading=Título da caixa
    |wid=2

  Requer a extensão Scribunto e o widget {{#widget:ReportModal|...}} registrado.
]]

local p = {}

function p.embed(frame)
	local pargs = (frame:getParent() and frame:getParent().args) or {}
	local args = frame.args or {}
	local base = (
		args.api_base
		or pargs.api_base
		or args[1]
		or pargs[1]
		or ""
	):gsub("^%s+", ""):gsub("%s+$", "")
	if base == "" then
		return '<span class="error">GlaReportEmbed: falta <code>api_base</code>.</span>'
	end

	local heading = args.heading or pargs.heading or ""
	local wid = args.wid or pargs.wid or ""

	local wikitext = "{{#widget:ReportModal|api_base=" .. base
	if heading ~= "" then
		wikitext = wikitext .. "|heading=" .. heading
	end
	if wid ~= "" then
		wikitext = wikitext .. "|wid=" .. wid
	end
	wikitext = wikitext .. "}}"

	return frame:preprocess(wikitext)
end

return p