Editing Module:Aligned table
From MINR.ORG WIKI
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.
The edit can be undone.
Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 1: | Line 1: | ||
-- This module implements {{aligned table}} | -- This module implements {{aligned table}} | ||
local p = {} | local p = {} | ||
− | |||
− | |||
− | |||
− | |||
function p.table(frame) | function p.table(frame) | ||
Line 12: | Line 8: | ||
local colstyle = {} | local colstyle = {} | ||
local cols = tonumber(args['cols']) or 2 | local cols = tonumber(args['cols']) or 2 | ||
− | + | local class = args['class'] or '' | |
+ | local style = args['style'] or '' | ||
+ | local leftright = args['leftright'] or '' | ||
+ | local fullwidth = args['fullwidth'] or '' | ||
+ | |||
+ | if leftright ~= '' then | ||
+ | colstyle[1] = 'text-align:left;' | ||
+ | colstyle[2] = 'text-align:right;' | ||
+ | end | ||
-- create the root table | -- create the root table | ||
local root = mw.html.create('table') | local root = mw.html.create('table') | ||
− | + | -- add table style for fullwidth | |
− | -- add table style for fullwidth | + | if fullwidth ~= '' then |
− | if | ||
root | root | ||
:css('width', '100%') | :css('width', '100%') | ||
:css('border-collapse', 'collapse') | :css('border-collapse', 'collapse') | ||
− | :css('border-spacing', ' | + | :css('border-spacing', '0px') |
:css('border', 'none') | :css('border', 'none') | ||
end | end | ||
− | |||
-- add table classes | -- add table classes | ||
− | if | + | if class ~= '' then |
− | root:addClass( | + | root:addClass(class) |
end | end | ||
− | |||
-- add table style | -- add table style | ||
− | if | + | if style ~= '' then |
− | root:cssText( | + | root:cssText(style) |
end | end | ||
− | |||
-- build arrays with the column styles and classes | -- build arrays with the column styles and classes | ||
− | |||
− | |||
− | |||
− | |||
for i = 1,cols do | for i = 1,cols do | ||
colclass[ i ] = colclass[ i ] or '' | colclass[ i ] = colclass[ i ] or '' | ||
colstyle[ i ] = colstyle[ i ] or '' | colstyle[ i ] = colstyle[ i ] or '' | ||
− | if | + | if args['align' .. tostring(i)] then |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
colstyle[ i ] = 'text-align:' .. args['align' .. tostring(i)] .. ';' .. colstyle[ i ] | colstyle[ i ] = 'text-align:' .. args['align' .. tostring(i)] .. ';' .. colstyle[ i ] | ||
end | end | ||
− | if | + | if args['nowrap' .. tostring(i)] and args['nowrap' .. tostring(i)] ~= '' then |
− | |||
− | |||
− | |||
− | |||
colstyle[ i ] = 'white-space:nowrap;' .. colstyle[ i ] | colstyle[ i ] = 'white-space:nowrap;' .. colstyle[ i ] | ||
end | end | ||
− | if | + | if args['style' .. tostring(i)] then |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
colstyle[ i ] = colstyle[ i ] .. args['style' .. tostring(i)] | colstyle[ i ] = colstyle[ i ] .. args['style' .. tostring(i)] | ||
end | end | ||
− | if | + | if args['class' .. tostring(i)] then |
− | |||
− | |||
− | |||
− | |||
colclass[ i ] = args['class' .. tostring(i)] | colclass[ i ] = args['class' .. tostring(i)] | ||
end | end | ||
Line 91: | Line 61: | ||
-- compute the number of rows | -- compute the number of rows | ||
local rows = math.ceil(cellcount / cols) | local rows = math.ceil(cellcount / cols) | ||
− | + | ||
-- build the table content | -- build the table content | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
for j=1,rows do | for j=1,rows do | ||
-- start a new row | -- start a new row | ||
local row = root:tag('tr') | local row = root:tag('tr') | ||
− | + | row:css('vertical-align', 'top') | |
− | + | -- loop over the cells in each row | |
− | |||
− | |||
− | |||
− | -- loop over the cells in | ||
for i=1,cols do | for i=1,cols do | ||
− | local | + | local cell = row:tag('td') |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
if args['class' .. tostring(j) .. '.' .. tostring(i)] then | if args['class' .. tostring(j) .. '.' .. tostring(i)] then | ||
cell:addClass(args['class' .. tostring(j) .. '.' .. tostring(i)]) | cell:addClass(args['class' .. tostring(j) .. '.' .. tostring(i)]) | ||
− | + | elseif args['rowclass' .. tostring(j)] then | |
− | + | cell:addClass(args['rowclass' .. tostring(j)] .. '"') | |
− | + | elseif colclass[i] ~= '' then | |
− | + | cell:addClass(colclass[i]) | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
if args['style' .. tostring(j) .. '.' .. tostring(i)] then | if args['style' .. tostring(j) .. '.' .. tostring(i)] then | ||
cell:cssText(args['style' .. tostring(j) .. '.' .. tostring(i)]) | cell:cssText(args['style' .. tostring(j) .. '.' .. tostring(i)]) | ||
− | + | elseif args['rowstyle' .. tostring(j)] then | |
− | + | cell:cssText(args['rowstyle' .. tostring(j)]) | |
− | + | elseif colstyle[i] ~= '' then | |
− | + | cell:cssText(colstyle[i]) | |
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
− | cell:wikitext | + | cell:wikitext(args[cols*(j - 1) + i] or '') |
− | + | end | |
− | + | end | |
-- return the root table | -- return the root table | ||
− | + | return tostring(root) | |
− | end | + | end |
return p | return p |