Module:Table

local schema = require( 'Module:Stats/SCHEMA' ) local utils = require( 'Module:Utils' )

local p = {}

function toLowerAndRemoveSpaces( str ) if type( str ) ~= 'string' then return nil end return mw.ustring.lower(mw.ustring.gsub( str, ' ', '' ) ) end

function p.makeTable( frame ) local columns = {} local options = {} local dataSource = nil local numCols = 0 -- Extract column names and ids for i, v in ipairs( frame.args ) do		local v_lower = toLowerAndRemoveSpaces( v ) if v_lower == nil or v_lower == '' then break end columns[ i ] = { [ 'name' ] = v,			[ 'id' ] = v_lower }		if numCols < i then numCols = i		end end -- Extract options and parameters for k, v in pairs( frame.args ) do		local k_lower = toLowerAndRemoveSpaces( k ) if k_lower == 'data' then dataSource = v		elseif k_lower ~= nil then options[ k_lower ] = v		end end -- Fail if no data source is provided if dataSource == nil or type( dataSource ) ~= 'string' then return utils.error('No data source was specified!') end -- Fail if data source is invalid local data = utils.loadDataIfExists( 'Module:Stats/' .. dataSource ) if data == nil then return utils.error('The specified data source was invalid!') end -- Fail if no columns are provided if numCols < 1 then return utils.error('No column names were provided.') end if true then return columns end -- Generate table local text = '{| class="wikitable sortable"\n' for i = 1, numCols, 1 do text = text .. '! ' .. columns[ i ][ 'name' ] .. '\n' end for k, v in pairs( data ) do text = text .. '|-\n' for i = 1, numCols, 1 do			local schemaFunc = schema[ columns[ i ][ 'id' ] ] if schemaFunc == nil then text = text .. '| \n' else text = text .. '| ' .. ( schemaFunc( v, options ) or '' ) .. '\n' end end end text = text .. '|}' end

return p