Difference between revisions of "Module:Alternating rows table section"

From blackwiki
Jump to navigation Jump to search
blackwiki>Wugapodes
m (Changed protection level for "Module:Alternating rows table section": High-risk Lua module ([Edit=Require template editor access] (indefinite) [Move=Require template editor access] (indefinite)))
m (3 revisions imported)
 
(No difference)

Latest revision as of 13:08, 26 September 2020

Usage

See Template:Alternating rows table section for details.


-- This module implements [[Template:Alternating rows table section]]
local p = {}

function p._buildrows(args)
	local ostyle = args['os'] and ' style="' .. args['os'] .. '"' or ''
	local estyle = args['es'] and ' style="' .. args['es'] .. '"' or ''
	
	local rownums = {}
	for k, _ in pairs( args ) do
		local i = tonumber(tostring(k):match( '^%s*([%d]+)%s*$' ) or '0')
		if( i > 0) then
			table.insert( rownums, i )
		end
	end
	-- sort the row numbers
	table.sort(rownums)
	
	local res = {}
	for k, idx in ipairs( rownums ) do
		table.insert(res, '|-' .. ((k % 2 == 0) and estyle or ostyle) )
		table.insert(res, args[idx])
	end
	
	return res
end

function p.main(frame)
	local getArgs = require('Module:Arguments').getArgs
	local args = getArgs(frame, {parentFirst = true})
	return table.concat(p._buildrows(args), '\n')
end

return p