Module:Table Wrapper
Jump to navigation
Jump to search
Documentation for this module may be created at Module:Table Wrapper/doc
local p = {}
local lib = require('Module:Feature')
local ne = lib.isNotEmpty
local ie = lib.isEmpty
function attrTable(attrString)
local out = {}
for val in string.gmatch(attrString, '([^;]+)') do
local attr, tag = string.match(val, '(.+):(.+)')
attr = lib.trim(attr)
tag = lib.trim(tag)
if ne(attr) and ne(tag) then out[attr] = tag end
end
return out
end
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame, {
parentFirst = true,
removeBlanks = false,
wrapper = { 'Template:Table Wrapper' }
})
return p._main(frame, args)
end
function p._main(frame, args)
local class = args['class'] or 'article-table'
local tableCss = args['style'] or ''
local size = args['size']
if ie(size) then
return mw.html.create('span')
:addClass('error')
:wikitext('Table size is invalid. Please enter a valid size in the form r;c.')
end
local row, col = string.match(size, '(%d+);(%d+)')
if row == nil or col == nil then
return mw.html.create('span')
:addClass('error')
:wikitext('Table size is invalid: ', size, '. Please enter a valid size in the form r;c.')
end
row = tonumber(row)
col = tonumber(col)
-- Check if table has any zero size rows or columns
-- or if it is extremely large rows or columns
-- (don't want to eat up all the lua time on
-- massive tables that should be split or restructured)
if row <= 0 or col <= 0 or row > 99 or col > 99 then
return mw.html.create('span')
:addClass('error')
:wikitext('Table size is invalid: ', size, '. Minimum size is 1 row/column, maximum size is 99 rows/columns.')
end
local out = mw.html.create('table')
:addClass(class)
:css(attrTable(tableCss))
local tableID = args['tableID'] or nil
if ne(tableID) then
out:attr('id', tableID)
end
local caption = args['caption'] or nil
if ne(caption) then
out:tag('caption')
:wikitext(caption)
end
--mw.log(out) -- debug
for i = 1, row do
local tr = out:tag('tr')
local rowID = args['id-' .. i] or nil
if ne(rowID) then tr:attr('id', rowID) end
for j = 1, col do
local header = args['header-' .. i .. '-' .. j]
local cell = args['cell-' .. i .. '-' .. j]
local css = args['style-' .. i .. '-' .. j]
local attr = args['attr-' .. i .. '-' .. j]
local cellOrHeader = nil
if header ~= nil then
cellOrHeader = tr:tag('th')
:wikitext(header)
elseif cell ~= nil then
cellOrHeader = tr:tag('td')
:wikitext(cell)
end
-- add any css attributes
if ne(css) and cellOrHeader ~= nil then
cellOrHeader:css(attrTable(css))
end
-- add any html attributes
if ne(attr) and cellOrHeader ~= nil then
cellOrHeader:attr(attrTable(attr))
end
end
end
--mw.log(out) --debug
return out
end
return p