Ir para o conteúdo

Módulo:Caixa lateral

De Wiki TokuDrive
Revisão de 06h16min de 26 de julho de 2026 por Tavoraadmin (discussão | contribs) (Importando predefinição/módulo da Wikipédia em português para manter layout)
(dif) ← Edição anterior | Revisão atual (dif) | Versão posterior → (dif)

local yesno = require('Módulo:Yesno') local p = {}

-- Tabela de mapeamento: "nome_em_portugues" = "English_name" local argAliases = {

   metadados      = 'metadata',
   ['posição']        = 'position',
   ['colapsável']     = 'collapsible',
   classe         = 'class',
   imagem         = 'image',
   ['classe-texto'] = 'textclass',
   estilo         = 'style',
   textoestilo    = 'textstyle',
   ['texto-estilo'] = 'textstyle',
   estilotexto    = 'textstyle',
   ['estilo-texto'] = 'textstyle',
   acima          = 'above',
   acimaestilo    = 'abovestyle',
   ['acima-estilo'] = 'abovestyle',
   estiloacima    = 'abovestyle',
   ['estilo-acima'] = 'abovestyle',
   texto          = 'text',
   imagemdireita  = 'imageright',
   ['imagem-direita'] = 'imageright',
   abaixo         = 'below',

}

-- Normaliza os argumentos (tradução + tratamento do valor "nenhuma") local function processArgs(rawArgs)

   local args = {}
   for k, v in pairs(rawArgs) do
       if type(k) == 'string' then
           v = v:match('^%s*(.-)%s*$') -- Remove espaços no início e fim
           if v ~=  then
               -- Traduz o nome do parâmetro se houver alias registrado
               local canonicalKey = argAliases[k] or k
               
               -- Trata o caso especial do valor da imagem em PT-BR
               if canonicalKey == 'image' and v:lower() == 'nenhuma' then
                   v = 'none'
               end
               
               args[canonicalKey] = v
           end
       end
   end
   return args

end

local function makeData(args)

   local data = {}
   -- Main table classes
   data.classes = {}
   if yesno(args.metadata) ~= false then
       table.insert(data.classes, 'metadata')
   end
   if args.position and args.position:lower() == 'left' then
       table.insert(data.classes, 'side-box-left')
   else
       table.insert(data.classes, 'side-box-right')
   end
   
   if args.collapsible then
       table.insert(data.classes, 'mw-collapsible')
       if args.collapsible == "collapsed" then
           table.insert(data.classes, 'mw-collapsed')
       end
       data.collapsible = true
   end
   -- CORREÇÃO IMPORTANTE: Só insere se args.class existir para não quebrar a tabela
   if args.class then
       table.insert(data.classes, args.class)
   end
   
   -- Image
   if args.image and args.image ~= 'none' then
       data.image = args.image
   end
   
   if args.textclass == 'plainlist' or not args.textclass then
       data.textclass = 'plainlist'
       data.plainlist_templatestyles = 'Lista simples/styles.css'
   else
       data.textclass = args.textclass
   end
   -- Copy over data that does not need adjusting
   local argsToCopy = {
       'role',
       'labelledby',
       'style',
       'textstyle',
       'templatestyles',
       'above',
       'abovestyle',
       'text',
       'imageright',
       'below',
   }
   for _, key in ipairs(argsToCopy) do
       data[key] = args[key]
   end
   return data

end

local function renderSidebox(data)

   local root = mw.html.create('div')
   root:attr('role', data.role)
       :attr('aria-labelledby', data.labelledby)
       :addClass('side-box')
       
   for _, class in ipairs(data.classes or {}) do
       root:addClass(class)
   end
   
   if data.style then
       root:cssText(data.style)
   end
   
   local frame = mw.getCurrentFrame()
   if data.plainlist_templatestyles then
       root:wikitext(frame:extensionTag{
           name = 'templatestyles', args = { src = data.plainlist_templatestyles }
       })
   end
   -- Above row
   if data.above then
       local above = root:newline():tag('div')
       above:addClass('side-box-abovebelow')
           :newline()
           :wikitext(data.above)
       if data.textstyle then above:cssText(data.textstyle) end
       if data.abovestyle then above:cssText(data.abovestyle) end
   end
   -- Body row
   local body = root:newline():tag('div')
   body:addClass('side-box-flex')
       :addClass(data.collapsible and 'mw-collapsible-content')
       :newline()
       
   if data.image then
       body:tag('div')
           :addClass('side-box-image')
           :wikitext(data.image)
   end
   
   local text = body:newline():tag('div')
   text:addClass('side-box-text')
       :addClass(data.textclass)
   if data.textstyle then
       text:cssText(data.textstyle)
   end
   text:wikitext(data.text)
   
   if data.imageright then
       body:newline():tag('div')
           :addClass('side-box-imageright')
           :wikitext(data.imageright)
   end
   -- Below row
   if data.below then
       local below = root:newline():tag('div')
       below:addClass('side-box-abovebelow')
           :wikitext(data.below)
       if data.textstyle then
           below:cssText(data.textstyle)
       end
   end
   root:newline()
   local templatestyles = 
   if data.templatestyles then
       templatestyles = frame:extensionTag{
           name = 'templatestyles', args = { src = data.templatestyles }
       }
   end
   return frame:extensionTag{
       name = 'templatestyles', args = { src = 'Módulo:Caixa lateral/styles.css' }
   } .. templatestyles .. tostring(root)

end

function p._main(args)

   local data = makeData(args)
   return renderSidebox(data)

end

function p.main(frame)

   -- Captura tanto chamadas diretas (#invoke) quanto parâmetros da predefinição chamadora
   local origArgs = frame:getParent() and frame:getParent().args or frame.args
   local args = processArgs(origArgs)
   
   return p._main(args)

end

return p