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 9: | ||
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 | ||
− | |||
local root = mw.html.create('table') | local root = mw.html.create('table') | ||
− | + | ||
− | + | 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 | ||
− | + | if class ~= '' then | |
− | + | root:addClass(class) | |
− | if | ||
− | root:addClass( | ||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
− | + | if style ~= '' then | |
− | + | root:cssText(style) | |
− | if | ||
− | |||
− | |||
end | end | ||
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 | ||
end | end | ||
− | + | ||
− | |||
for k, v in pairs( args ) do | for k, v in pairs( args ) do | ||
if type( k ) == 'number' then | if type( k ) == 'number' then | ||
− | + | i = math.fmod(k-1,cols) + 1 | |
− | + | local j = (k - i) / cols + 1 | |
− | + | local tdstyle = '' | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | local | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
if args['class' .. tostring(j) .. '.' .. tostring(i)] then | if args['class' .. tostring(j) .. '.' .. tostring(i)] then | ||
− | + | tdstyle = tdstyle .. ' class="' .. args['class' .. tostring(j) .. '.' .. tostring(i)] .. '"' | |
− | + | elseif args['rowclass' .. tostring(j)] then | |
− | + | tdstyle = tdstyle .. ' class="' .. args['rowclass' .. tostring(j)] .. '"' | |
− | + | elseif colclass[i] ~= '' then | |
− | + | tdstyle = tdstyle .. ' class="' .. colclass[i] .. '"' | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
if args['style' .. tostring(j) .. '.' .. tostring(i)] then | if args['style' .. tostring(j) .. '.' .. tostring(i)] then | ||
− | + | tdstyle = tdstyle .. ' style="' .. args['style' .. tostring(j) .. '.' .. tostring(i)] .. '"' | |
− | + | elseif args['rowstyle' .. tostring(j)] then | |
− | + | tdstyle = tdstyle .. ' style="' .. args['rowstyle' .. tostring(j)] .. '"' | |
− | + | elseif colstyle[i] ~= '' then | |
− | + | tdstyle = tdstyle .. ' style="' .. colstyle[i] .. '"' | |
− | + | end | |
− | + | entries[ k ] = '<td' .. tdstyle .. '>' .. v .. '</td>' | |
− | + | if i == 1 then | |
− | + | entries[ k ] = '<tr style="vertical-align:top">' .. entries[ k ] | |
− | |||
end | end | ||
− | + | if i == cols then | |
− | + | entries[ k ] = entries[k] .. '</tr>' | |
− | + | end | |
− | + | end | |
− | + | end | |
− | end | + | root:wikitext(table.concat( entries, '\n' )) |
+ | |||
+ | return tostring(root) | ||
+ | end | ||
return p | return p |