Documentation for this module may be created at Modul:Infobox 2/doc

local p = {};

local yesno = require('Module:Yesno')

local function _renderLine( frame, args, i )
	if args[ 'title' .. i ] and args[ 'title' .. i ] == '-' then
		return ''
	elseif args[ 'title' .. i ] and args[ 'title' .. i ] ~= '' then
		local style = ( args[ 'labelstyles' ] or '' ) .. ( args[ 'labelstyle' .. i ] or '' );
		local class = ( args[ 'class' .. i ] or '' );
		return '\n<tr>\n<th colspan="2" class="infobox-header ' .. class .. '" style="' .. style .. '">' ..
			args[ 'title' .. i ] ..
			'</th>\n</tr>';
	end
	
	if args[ 'block' .. i ] and args[ 'block' .. i ] ~= '' then
		return args[ 'block' .. i ];
	end

	local text = args[ 'data' .. i ] or '';
	if args[ 'wikidata' .. i ] and args[ 'wikidata' .. i ] ~= '' then
		text = frame:expandTemplate{ title = 'Wikidata', args = {
			args[ 'wikidata' .. i ],
			text,
			from = args[ 'from' ] or ''
		} };
	end

if text ~= '' then
		local label = args[ 'label' .. i ] or '';

	local class = args[ 'class' .. i ] or '';
	if string.find(class, 'noplainlist') == nil and string.find(class, 'nofirstlevel') == nil then
		class = class .. ' plainlist';
	end
	if class ~= '' then
			class = ' class="' .. class .. '"';
	end

	local style = ( args[ 'datastyles' ] or '' ) .. ( args[ 'datastyle' ] or '' ) .. ( args[ 'textstyle' .. i ] or '' );
		if label == '' then
			style = 'text-align:center;' .. style;
		end
	if style ~= '' then
		style = ' style="' .. style .. '"';
		end

		if label ~= '' then
	 	local labelClass = args[ 'labelsclass' ] or '';
	 	if string.find(labelClass, 'noplainlist') == nil and string.find(labelClass, 'nofirstlevel') == nil then
			labelClass = labelClass .. ' plainlist';
			end
	 	if labelClass ~= '' then
	 		labelClass = ' class="' .. labelClass .. '"';
			end
	
	 	local labelStyle = ( args[ 'labelsstyle' ] or '' ) .. ( args[ 'labelstyle' .. i ] or '' );
	 	if labelStyle ~= '' then
	 		labelStyle = ' style="' .. labelStyle .. '"';
			end

			return '\n<tr>\n<th scope="row"' .. labelClass .. labelStyle .. '>' .. label .. '</th>' ..
	 		'\n<td' .. class .. style .. '>\n' .. text .. '</td>\n</tr>';
		end
		
	 return '\n<tr>\n<td colspan="2"' .. class .. style .. '>\n' .. text .. '</td>\n</tr>';
	end	 

	return '';
end

local function maxNumber ( args )
	local maxNumber = 0
	for argName, _ in pairs(args) do
		local argNumber = mw.ustring.match(argName, '^[^0-9]+([0-9]+)$')
		if argNumber and tonumber(argNumber) > maxNumber then
			maxNumber = tonumber(argNumber)
		end
	end
	return maxNumber
end

function p.renderLine( frame )
	local args = frame:getParent().args;
	return _renderLine(frame, args, '')
end

function p.renderLines( frame )
	local args = frame:getParent().args;
	
	local res = ''
	local header, text = '', ''
	
	local autoHeaders = yesno(args [ 'autotitles' ] or 'false', false)
	
	for i = 1, maxNumber(args) do
		if args[ 'title' .. i ] and args[ 'title' .. i ] ~= '' then
			if text ~= '' or not autoHeaders then
				res = res .. header .. text
			end
			header, text = _renderLine(frame, args, i), ''
		else
			text = text .. _renderLine(frame, args, i)
		end
	end
	if text ~= '' or not autoHeaders then
		res = res .. header .. text
	end
	
	return res
end

return p;