Módulo:Aligned table
-- Este módulo implementa a predefinição {{#invoke:aligned table|table}} local p = {}
local function isnotempty(s) return s and s:match( '^%s*(.-)%s*$' ) ~= end
local function getarg(args, ...) for i = 1, select('#', ...) do local key = select(i, ...) local value = args[key] if isnotempty(value) then return value end end return nil end
local function getpresentarg(args, ...) for i = 1, select('#', ...) do local key = select(i, ...) if args[key] ~= nil then return args[key] end end return nil end
function p.table(frame) local args = (frame.args[3] ~= nil) and frame.args or frame:getParent().args local entries = {} local colclass = {} local colstyle = {} local cols = tonumber(getarg(args, 'cols', 'colunas')) or 2
-- cria a tabela raiz local root = mw.html.create('table')
-- adiciona estilo de tabela para largura total if getarg(args, 'fullwidth', 'largura total') then root :css('width', '100%') :css('border-collapse', 'collapse') :css('border-spacing', '0px 0px') :css('border', 'none') end
-- adiciona classes de tabela local tableclass = getarg(args, 'class', 'classe') if tableclass then root:addClass(tableclass) end
-- adiciona estilo de tabela local tablestyle = getarg(args, 'style', 'estilo') if tablestyle then root:cssText(tablestyle) end
-- constrói arranjos com os estilos e classes das colunas if getarg(args, 'leftright', 'esquerda-direita') then colstyle[1] = 'text-align:left;' colstyle[2] = 'text-align:right;' end if getarg(args, 'rightleft', 'direita-esquerda') then colstyle[1] = 'text-align:right;' colstyle[2] = 'text-align:left;' end for i = 1,cols do local n = tostring(i) colclass[i] = colclass[i] or colstyle[i] = colstyle[i] or
local commoncolstyle = getarg(args, 'colstyle', 'estilo das colunas') if commoncolstyle then colstyle[i] = commoncolstyle .. ';' .. colstyle[i] end
local colalign = getarg( args, 'colalign' .. n, 'col' .. n .. 'align', 'align' .. n, 'alinhamento da coluna ' .. n ) if colalign then colstyle[i] = 'text-align:' .. colalign .. ';' .. colstyle[i] end
if getarg( args, 'colnowrap' .. n, 'col' .. n .. 'nowrap', 'nowrap' .. n, 'sem quebra na coluna ' .. n ) then colstyle[i] = 'white-space:nowrap;' .. colstyle[i] end
local colwidth = getarg( args, 'colwidth' .. n, 'col' .. n .. 'width', 'largura da coluna ' .. n ) if colwidth then colstyle[i] = 'width:' .. colwidth .. ';' .. colstyle[i] else local commoncolwidth = getarg(args, 'colwidth', 'largura das colunas') if commoncolwidth then colstyle[i] = 'width:' .. commoncolwidth .. ';' .. colstyle[i] end end
local specificcolstyle = getarg( args, 'colstyle' .. n, 'col' .. n .. 'style', 'style' .. n, 'estilo da coluna ' .. n ) if specificcolstyle then colstyle[i] = colstyle[i] .. specificcolstyle end
local specificcolclass = getarg( args, 'colclass' .. n, 'col' .. n .. 'class', 'class' .. n, 'classe da coluna ' .. n ) if specificcolclass then colclass[i] = specificcolclass end end
-- calcula o índice máximo da célula local cellcount = 0 for k, v in pairs(args) do if type(k) == 'number' then cellcount = math.max(cellcount, k) end end
-- calcula o número de linhas local rows = math.ceil(cellcount / cols)
-- constrói o conteúdo da tabela local title = getarg(args, 'title', 'título') if title then local caption = root:tag('caption') caption:cssText(getarg(args, 'titlestyle', 'estilo do título')) caption:wikitext(title) end
local above = getarg(args, 'above', 'cabeçalho superior') if above then local row = root:tag('tr') local cell = row:tag('th') cell:attr('colspan', cols) cell:cssText(getarg(args, 'abovestyle', 'estilo do cabeçalho superior')) cell:wikitext(above) end
for j = 1,rows do local rownumber = tostring(j)
-- inicia uma nova linha local row = root:tag('tr') local commonrowstyle = getarg(args, 'rowstyle', 'estilo das linhas') if commonrowstyle then row:cssText(commonrowstyle) else row:css('vertical-align', 'top') end
local commonrowclass = getarg(args, 'rowclass', 'classe das linhas') if commonrowclass then row:addClass(commonrowclass) end
-- faz um loop nas células da linha for i = 1,cols do local columnnumber = tostring(i) local cell
if getarg( args, 'row' .. rownumber .. 'header', 'cabeçalho da linha ' .. rownumber ) then cell = row:tag('th'):attr('scope','col') elseif getarg( args, 'col' .. columnnumber .. 'header', 'cabeçalho da coluna ' .. columnnumber ) then cell = row:tag('th'):attr('scope','row') else cell = row:tag('td') end
local cellclass = getpresentarg( args, 'class' .. rownumber .. '.' .. columnnumber, 'classe da célula ' .. rownumber .. '.' .. columnnumber )
if cellclass ~= nil then cell:addClass(cellclass) else local specificrowclass = getpresentarg( args, 'rowclass' .. rownumber, 'row' .. rownumber .. 'class', 'classe da linha ' .. rownumber )
if specificrowclass ~= nil then cell:addClass(specificrowclass) else local evenclass = getpresentarg( args, 'rowevenclass', 'classe das linhas pares' ) local oddclass = getpresentarg( args, 'rowoddclass', 'classe das linhas ímpares' )
if evenclass ~= nil and math.fmod(j,2) == 0 then cell:addClass(evenclass) elseif oddclass ~= nil and math.fmod(j,2) == 1 then cell:addClass(oddclass) end end
if colclass[i] ~= then cell:addClass(colclass[i]) end end
local cellstyle = getpresentarg( args, 'style' .. rownumber .. '.' .. columnnumber, 'estilo da célula ' .. rownumber .. '.' .. columnnumber )
if cellstyle ~= nil then cell:cssText(cellstyle) else local specificrowstyle = getpresentarg( args, 'rowstyle' .. rownumber, 'estilo da linha ' .. rownumber )
if specificrowstyle ~= nil then cell:cssText(specificrowstyle) else local evenstyle = getpresentarg( args, 'rowevenstyle', 'estilo das linhas pares' ) local oddstyle = getpresentarg( args, 'rowoddstyle', 'estilo das linhas ímpares' ) local alternativerowstyle = getpresentarg( args, 'row' .. rownumber .. 'style' )
if evenstyle ~= nil and math.fmod(j,2) == 0 then cell:cssText(evenstyle) elseif oddstyle ~= nil and math.fmod(j,2) == 1 then cell:cssText(oddstyle) elseif alternativerowstyle ~= nil then cell:cssText(alternativerowstyle) end end
if isnotempty(colstyle[i]) then cell:cssText(colstyle[i]) end end
cell:wikitext(mw.ustring.gsub( args[cols*(j - 1) + i] or , '^(.-)%s*$', '%1' ) or ) end end
-- retorna a tabela raiz return tostring(root) end
return p