Módulo:Aligned table: mudanças entre as edições
Importando predefinição/módulo da Wikipédia em português para manter layout |
Importando predefinição/módulo da Wikipédia em português para manter layout |
||
| Linha 1: | Linha 1: | ||
-- Este módulo implementa a {{ | -- Este módulo implementa a predefinição {{Tabela alinhada}} | ||
local p = {} | local p = {} | ||
local function isnotempty(s) | local function isnotempty(s) | ||
return s and s:match( '^%s*(.-)%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 | end | ||
| Linha 11: | Linha 32: | ||
local colclass = {} | local colclass = {} | ||
local colstyle = {} | local colstyle = {} | ||
local cols = tonumber(args | local cols = tonumber(getarg(args, 'cols', 'colunas')) or 2 | ||
-- cria a tabela raiz | -- cria a tabela raiz | ||
| Linha 17: | Linha 38: | ||
-- adiciona estilo de tabela para largura total | -- adiciona estilo de tabela para largura total | ||
if | if getarg(args, 'fullwidth', 'largura total') then | ||
root | root | ||
:css('width', '100%') | :css('width', '100%') | ||
| Linha 26: | Linha 47: | ||
-- adiciona classes de tabela | -- adiciona classes de tabela | ||
local tableclass = getarg(args, 'class', 'classe') | |||
root:addClass( | if tableclass then | ||
root:addClass(tableclass) | |||
end | end | ||
-- adiciona estilo de tabela | -- adiciona estilo de tabela | ||
local tablestyle = getarg(args, 'style', 'estilo') | |||
root:cssText( | if tablestyle then | ||
root:cssText(tablestyle) | |||
end | end | ||
-- constrói arranjos com os estilos e classes das colunas | -- constrói arranjos com os estilos e classes das colunas | ||
if | if getarg(args, 'leftright', 'esquerda-direita') then | ||
colstyle[1] = 'text-align:left;' | colstyle[1] = 'text-align:left;' | ||
colstyle[2] = 'text-align:right;' | colstyle[2] = 'text-align:right;' | ||
end | end | ||
if | if getarg(args, 'rightleft', 'direita-esquerda') then | ||
colstyle[1] = 'text-align:right;' | colstyle[1] = 'text-align:right;' | ||
colstyle[2] = 'text-align:left;' | colstyle[2] = 'text-align:left;' | ||
end | end | ||
for i = 1,cols do | for i = 1,cols do | ||
colclass[ i ] = colclass[ i ] or '' | local n = tostring(i) | ||
colstyle[ i ] = colstyle[ i ] or '' | colclass[i] = colclass[i] or '' | ||
colstyle[i] = colstyle[i] or '' | |||
colstyle[ i ] = | |||
local commoncolstyle = getarg(args, 'colstyle', 'estilo das colunas') | |||
if commoncolstyle then | |||
colstyle[i] = commoncolstyle .. ';' .. colstyle[i] | |||
end | end | ||
local colalign = getarg( | |||
args, | |||
'colalign' .. n, | |||
'col' .. n .. 'align', | |||
colstyle[ i ] = 'text-align:' .. | 'align' .. n, | ||
'alinhamento da coluna ' .. n | |||
) | |||
if colalign then | |||
colstyle[i] = 'text-align:' .. colalign .. ';' .. colstyle[i] | |||
end | end | ||
if | |||
if getarg( | |||
args, | |||
'colnowrap' .. n, | |||
'col' .. n .. 'nowrap', | |||
colstyle[ i ] = 'white-space:nowrap;' .. colstyle[ i ] | 'nowrap' .. n, | ||
'sem quebra na coluna ' .. n | |||
) then | |||
colstyle[i] = 'white-space:nowrap;' .. colstyle[i] | |||
end | end | ||
local colwidth = getarg( | |||
args, | |||
colstyle[ i ] = 'width:' .. | '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 | end | ||
local specificcolstyle = getarg( | |||
args, | |||
'colstyle' .. n, | |||
'col' .. n .. 'style', | |||
colstyle[ i ] = colstyle[ i ] .. | 'style' .. n, | ||
'estilo da coluna ' .. n | |||
) | |||
if specificcolstyle then | |||
colstyle[i] = colstyle[i] .. specificcolstyle | |||
end | end | ||
local specificcolclass = getarg( | |||
args, | |||
'colclass' .. n, | |||
'col' .. n .. 'class', | |||
colclass[ i ] = | 'class' .. n, | ||
'classe da coluna ' .. n | |||
) | |||
if specificcolclass then | |||
colclass[i] = specificcolclass | |||
end | end | ||
end | end | ||
-- calcula o índice máximo da célula | -- calcula o índice máximo da célula | ||
local cellcount = 0 | local cellcount = 0 | ||
for k, v in pairs( args ) do | for k, v in pairs(args) do | ||
if type( k ) == 'number' then | if type(k) == 'number' then | ||
cellcount = math.max(cellcount, k) | cellcount = math.max(cellcount, k) | ||
end | end | ||
end | end | ||
-- calcula o número de linhas | -- calcula o número de linhas | ||
local rows = math.ceil(cellcount / cols) | local rows = math.ceil(cellcount / cols) | ||
-- constrói o conteúdo da tabela | -- constrói o conteúdo da tabela | ||
local title = getarg(args, 'title', 'título') | |||
if title then | |||
local caption = root:tag('caption') | local caption = root:tag('caption') | ||
caption:cssText(args | caption:cssText(getarg(args, 'titlestyle', 'estilo do título')) | ||
caption:wikitext( | caption:wikitext(title) | ||
end | end | ||
local above = getarg(args, 'above', 'cabeçalho superior') | |||
if above then | |||
local row = root:tag('tr') | local row = root:tag('tr') | ||
local cell = row:tag('th') | local cell = row:tag('th') | ||
cell:attr('colspan', cols) | cell:attr('colspan', cols) | ||
cell:cssText(args | cell:cssText(getarg(args, 'abovestyle', 'estilo do cabeçalho superior')) | ||
cell:wikitext( | cell:wikitext(above) | ||
end | end | ||
for j=1,rows do | |||
for j = 1,rows do | |||
local rownumber = tostring(j) | |||
-- inicia uma nova linha | -- inicia uma nova linha | ||
local row = root:tag('tr') | local row = root:tag('tr') | ||
local commonrowstyle = getarg(args, 'rowstyle', 'estilo das linhas') | |||
row:cssText( | if commonrowstyle then | ||
row:cssText(commonrowstyle) | |||
else | else | ||
row:css('vertical-align', 'top') | row:css('vertical-align', 'top') | ||
end | end | ||
row:addClass( | local commonrowclass = getarg(args, 'rowclass', 'classe das linhas') | ||
if commonrowclass then | |||
row:addClass(commonrowclass) | |||
end | end | ||
-- faz um loop nas células da linha | -- faz um loop nas células da linha | ||
for i=1,cols do | for i = 1,cols do | ||
local columnnumber = tostring(i) | |||
local cell | local cell | ||
if | |||
if getarg( | |||
args, | |||
'row' .. rownumber .. 'header', | |||
'cabeçalho da linha ' .. rownumber | |||
) then | |||
cell = row:tag('th'):attr('scope','col') | cell = row:tag('th'):attr('scope','col') | ||
elseif | elseif getarg( | ||
args, | |||
'col' .. columnnumber .. 'header', | |||
'cabeçalho da coluna ' .. columnnumber | |||
) then | |||
cell = row:tag('th'):attr('scope','row') | cell = row:tag('th'):attr('scope','row') | ||
else | else | ||
cell = row:tag('td') | cell = row:tag('td') | ||
end | end | ||
local cellclass = getpresentarg( | |||
args, | |||
'class' .. rownumber .. '.' .. columnnumber, | |||
'classe da célula ' .. rownumber .. '.' .. columnnumber | |||
) | |||
if cellclass ~= nil then | |||
cell:addClass(cellclass) | |||
else | else | ||
local specificrowclass = getpresentarg( | |||
args, | |||
'rowclass' .. rownumber, | |||
cell:addClass(args | '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 | end | ||
if colclass[i] ~= '' then | if colclass[i] ~= '' then | ||
cell:addClass(colclass[i]) | cell:addClass(colclass[i]) | ||
end | end | ||
end | end | ||
local cellstyle = getpresentarg( | |||
args, | |||
'style' .. rownumber .. '.' .. columnnumber, | |||
'estilo da célula ' .. rownumber .. '.' .. columnnumber | |||
) | |||
if cellstyle ~= nil then | |||
cell:cssText(cellstyle) | |||
else | else | ||
local specificrowstyle = getpresentarg( | |||
cell:cssText(args | 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 | end | ||
if isnotempty(colstyle[i]) then | if isnotempty(colstyle[i]) then | ||
cell:cssText(colstyle[i]) | cell:cssText(colstyle[i]) | ||
end | end | ||
end | end | ||
cell:wikitext(mw.ustring.gsub(args[cols*(j - 1) + i] or '', '^(.-)%s*$', '%1') or '') | |||
cell:wikitext(mw.ustring.gsub( | |||
args[cols*(j - 1) + i] or '', | |||
'^(.-)%s*$', | |||
'%1' | |||
) or '') | |||
end | end | ||
end | end | ||
-- retorna a tabela raiz | -- retorna a tabela raiz | ||
return tostring(root) | return tostring(root) | ||
Edição atual tal como às 04h59min de 27 de setembro de 2026
-- 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