Module:Utilities

From ema
Revision as of 13:31, 30 June 2017 by Cicalese (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Documentation for this module may be created at Module:Utilities/doc

local p = {}

function p.process_param(param)
 
	s = mw.text.trim( param )
 
	s = string.gsub( s, '%s*,%s*', ',' )
	s = string.gsub( s, ',+', ',' )
	if ( string.match( s, ',$' ) ) then
		s = string.sub( s, 1, #s - 1 )
	end
	if ( string.find( s, ',' ) == 1 ) then
		length = 0 - #s + 1
		s = string.sub( s, length )
	end
	if #s == 0 then
		return ''
	end
 
	return mw.text.split( s, ",", true )
end
 
function p.gridify(frame)
 
	items = p.process_param(frame.args[1])
 
	out = '{| style="width:100%;"'
 
	if ( frame.args["class"] ~= nil ) then
		out = out .. ' class="' .. frame.args["class"] .. '"'
	end
 
	out = out .. "\n"
 
	if ( frame.args["cols"] == nil ) then
		cols = 5
	else
		cols = tonumber(frame.args["cols"])
		if ( cols < 0 or cols > 5 ) then
			cols = 5
		end
	end
 
	rows = math.ceil(#items/cols)
 
	for row=1,rows do
		for col=1,cols do
			index = (col-1)*rows+row
			if ( index <= #items ) then
				out = out .. "|" .. items[index] .. "\n"
			else
				out = out .. "| &nbsp;\n"
			end
		end
		out = out .. "|-\n"
	end
 
	out = out .. "|}\n"
 
	return out
end

return p