Module:Aligned table: Difference between revisions
No edit summary |
m (59 revisions imported from wikipedia:Module:Aligned_table: see Topic:Vtixlm0q28eo6jtf) |
||
| (55 intermediate revisions by 7 users not shown) | |||
| Line 1: | Line 1: | ||
-- This module implements {{aligned table}} | -- This module implements {{aligned table}} | ||
local p = {} | |||
local | local function isnotempty(s) | ||
return s and s:match( '^%s*(.-)%s*$' ) ~= '' | |||
end | |||
function p.table(frame) | function p.table(frame) | ||
local args = (frame.args[3] ~= nil) and frame.args or frame:getParent().args | local args = (frame.args[3] ~= nil) and frame.args or frame:getParent().args | ||
local entries = {} | local entries = {} | ||
local | local colclass = {} | ||
local cols = tonumber(args['cols'] or ' | local colstyle = {} | ||
local cols = tonumber(args['cols']) or 2 | |||
if style | -- create the root table | ||
style = ' | local root = mw.html.create('table') | ||
-- add table style for fullwidth | |||
if isnotempty(args['fullwidth']) then | |||
root | |||
:css('width', '100%') | |||
:css('border-collapse', 'collapse') | |||
:css('border-spacing', '0px 0px') | |||
:css('border', 'none') | |||
end | |||
-- add table classes | |||
if isnotempty(args['class']) then | |||
root:addClass(args['class']) | |||
end | |||
-- add table style | |||
if isnotempty(args['style']) then | |||
root:cssText(args['style']) | |||
end | |||
-- build arrays with the column styles and classes | |||
if isnotempty(args['leftright']) then | |||
colstyle[1] = 'text-align:left;' | |||
colstyle[2] = 'text-align:right;' | |||
end | |||
if isnotempty(args['rightleft']) then | |||
colstyle[1] = 'text-align:right;' | |||
colstyle[2] = 'text-align:left;' | |||
end | end | ||
for i = 1,cols do | for i = 1,cols do | ||
if args['align' .. tostring(i)] then | colclass[ i ] = colclass[ i ] or '' | ||
align[ i ] = args['align' .. tostring(i)] | colstyle[ i ] = colstyle[ i ] or '' | ||
if isnotempty(args['colstyle']) then | |||
colstyle[ i ] = args['colstyle'] .. ';' .. colstyle[ i ] | |||
end | |||
if isnotempty(args['colalign' .. tostring(i)]) then | |||
colstyle[ i ] = 'text-align:' .. args['colalign' .. tostring(i)] .. ';' .. colstyle[ i ] | |||
elseif isnotempty(args['col' .. tostring(i) .. 'align']) then | |||
colstyle[ i ] = 'text-align:' .. args['col' .. tostring(i) .. 'align'] .. ';' .. colstyle[ i ] | |||
elseif isnotempty(args['align' .. tostring(i)]) then | |||
colstyle[ i ] = 'text-align:' .. args['align' .. tostring(i)] .. ';' .. colstyle[ i ] | |||
end | |||
if isnotempty(args['colnowrap' .. tostring(i)]) then | |||
colstyle[ i ] = 'white-space:nowrap;' .. colstyle[ i ] | |||
elseif isnotempty(args['col' .. tostring(i) .. 'nowrap']) then | |||
colstyle[ i ] = 'white-space:nowrap;' .. colstyle[ i ] | |||
elseif isnotempty(args['nowrap' .. tostring(i)]) then | |||
colstyle[ i ] = 'white-space:nowrap;' .. colstyle[ i ] | |||
end | |||
if isnotempty(args['colwidth' .. tostring(i)]) then | |||
colstyle[ i ] = 'width:' .. args['colwidth' .. tostring(i)] .. ';' .. colstyle[ i ] | |||
elseif isnotempty(args['col' .. tostring(i) .. 'width']) then | |||
colstyle[ i ] = 'width:' .. args['col' .. tostring(i) .. 'width'] .. ';' .. colstyle[ i ] | |||