יחידה:Election results


require('Module:No globals')

local ElectionResults = {}

function ElectionResults.new(frame, args)
	local args = args or {}
	local index, headings, showtotal = {}, {}, {}
	local cols, rounds = 0, 1
	local winner, winner_votes = {0, 0}, {0, 0}
	local valid = {0, 0}
	local invalid = {tonumber(args.invalid) or 0, tonumber(args.invalid2) or 0}
	local majority = {tonumber(args.majority)}
	local majority2 = {tonumber(args.majority2)}
	local majoritypct = {tonumber(args.majoritypct)}
	local majoritypct2 = {tonumber(args.majoritypct2)}
	local totalvotes = {tonumber(args.totalvotes), tonumber(args.totalvotes2)}
	local electorate = {tonumber(args.electorate), tonumber(args.electorate2)}
	local turnout = {tonumber(args.turnout), tonumber(args.turnout2)}
	local seats = 0
	local row, secondrow
	local tracking = ''
	local max_rows = 0

	-- helper functions
	local lang = mw.getContentLanguage()
	local function fmt(n)
		return n and tonumber(n) and lang:formatNum(tonumber(n)) or nil
	end
	local function pct(n, d)
		n, d = tonumber(n), tonumber(d)
		if n and d and d > 0 then
			return string.format('%.2f', n / d * 100)
		end
		return '–'
	end
	local function tonumdash(s)
		if s then
			s = mw.ustring.gsub(s, '&[MmNn][Dd][Aa][Ss][Hh];', '-')
			s = mw.ustring.gsub(s, '&[Mm][Ii][Nn][Uu][Ss];', '-')
			s = mw.ustring.gsub(s, '[—–−]', '-')
			return tonumber(s) or 0
		end
	end
	local function unlink(s)
		if s then
			s = s:match("^[^%[]-%[%[([^%]]-)|[^%]]-%]%].*$") or s
			s = s:match("^[^%[]-%[%[([^%]]-)%]%].*$") or s
		end
		return s
	end

	-- preprocess the input
	local stop_flag = false
	local i = 0
	local has_votes = args['invalid'] or args['totalvotes'] or args['electorate'] or args['turnout']
	while stop_flag == false do
		stop_flag = true
		for kk = 1, 20 do
			i = i + 1
			for k, key in ipairs({'cand', 'vp', 'party', 'ivotes', 'ipct', 'iseats', 'sc', 'st1t', 'st2t', 'st3t', 'st4t', 'st5t', 'sw', 'seats', 'totalvotes', 'acolor'}) do
				if args[key .. i] then
					headings[key] = true
					stop_flag = false
					max_rows = i > max_rows and i or max_rows
				end
			end
			if args['row' .. i] then
				stop_flag = false
				max_rows = i > max_rows and i or max_rows
			end

			if args['row' .. i] or args['cand' .. i] or args['party' .. i] then
				table.insert(index, i)
				if args['votes' .. i] then
					has_votes = true
					if tonumber(args['votes' .. i]) then showtotal.votes = 1 end
					local votesi = tonumber(args['votes' .. i]) or 0
					args['votes' .. i] = votesi
					if votesi > winner_votes[1] then
						winner[1] = i
						winner_votes[1] = votesi
					end
					valid[1] = valid[1] + votesi
				end
				if args['ivotes' .. i] then
					has_votes = true
				end
				if args['ipct' .. i] then
					has_votes = true
				end
				if args['votes' .. i .. '_2'] then
					has_votes = true
					rounds = 2
					if tonumber(args['votes' .. i .. '_2']) then showtotal.votes_2 = 1 end
					local votesi = tonumber(args['votes' .. i .. '_2']) or 0
					args['votes' .. i .. '_2'] = votesi
					if votesi > winner_votes[2] then
						winner[2] = i
						winner_votes[2] = votesi
					end
					valid[2] = valid[2] + votesi
				end
				if args['seats' .. i] then
					if tonumber(args['seats' .. i]) then showtotal.seats = 1 end
					seats = seats + (tonumber(args['seats' .. i]) or 0)
				end
			end
		end
	end

	local ovalid = {valid[1], valid[2]}
	seats = ((args['total_seats'] or '') == 'TOTAL' and seats) or args['total_seats'] or seats
	if seats or args['total_sc'] or args['total_st1t'] or args['total_st2t'] or args['total_st3t'] or args['total_st4t'] or args['total_st5t'] or args['valid'] or ((rounds > 1) and args['valid2']) then
		max_rows = max_rows + 1
		local i = max_rows
		table.insert(index, i)
		args['votes' .. i] = showtotal.votes and valid[1] or nil
		args['votes' .. i .. '_2'] = showtotal.votes_2 and valid[2] or nil
		args['colour' .. i] = 'inherit'
		args['color' .. i] = 'inherit'
		args['row' .. i] = 'סך הכל'
		args['sw' .. i] = '–'
		args['seats' .. i] = showtotal.seats and seats or nil
		args['sc' .. i] = args['total_sc']
		args['st1t' .. i] = args['total_st1t']
		args['st2t' .. i] = args['total_st2t']
		args['st3t' .. i] = args['total_st3t']
		args['st4t' .. i] = args['total_st4t']
		args['st5t' .. i] = args['total_st5t']
		args['font-weight' .. i] = 'bold'
		args['class' .. i] = 'sortbottom'
		ovalid[1] = tonumber(args['valid']) or valid[1]
		ovalid[2] = tonumber(args['valid2']) or valid[2]
	end

	-- build the table
	local root = mw.html.create('table')
	root
		:addClass('wikitable sortable')
		:tag('caption')
			:wikitext(args.caption)
			:done()

	local topcell = nil
	if args['image'] then
		topcell = root
			:tag('tr')
				:tag('td')
					:wikitext(args['image'])
					:css('text-align', 'center')
	end

	local rowspan = (rounds > 1) and 2 or headings['st1t'] and 2 or nil
	row = root:tag('tr')
	if args['dsv'] then
		row
			:tag('th')
				:wikitext(args['partytitle'] or 'מפלגה')
				:attr('scope', 'col')
				:attr('colspan', 2)
				:attr('rowspan', rowspan)
				:done()
		cols = cols + 2
		row
			:tag('th')
			:wikitext(args.candtitle or 'מועמד')
			:attr('scope', 'col')
			:attr('rowspan', rowspan)
			:done()
		cols = cols + 1
	elseif args['israel'] then
		row
			:tag('th')
				:wikitext(args['partytitle'] or 'רשימה')
				:attr('scope', 'col')
				:attr('colspan', 2)
				:attr('rowspan', rowspan)
				:done()
		cols = cols + 2
		row
			:tag('th')
			:wikitext('סימן')
			:attr('scope', 'col')
			:attr('rowspan', rowspan)
			:done()
		cols = cols + 1
		row
			:tag('th')
			:wikitext(args.candtitle or 'יו"ר')
			:attr('scope', 'col')
			:attr('rowspan', rowspan)
			:done()
		cols = cols + 1
	elseif headings['cand'] then
		row
			:tag('th')
				:wikitext('מועמד')
				:attr('scope', 'col')
				:attr('colspan', 2)
				:attr('rowspan', rowspan)
				:done()
		cols = cols + 2
		if headings['vp'] then
			row
				:tag('th')
						:wikitext('עמית למרוץ')
						:attr('scope', 'col')
						:attr('rowspan', rowspan)
						:done()
			cols = cols + 1
		end
		if headings['party'] then
			row
				:tag('th')
				:wikitext(args['partytitle'] or 'מפלגה')
				:attr('scope', 'col')
				:attr('rowspan', rowspan)
				:done()
			cols = cols + 1
		end
	else
		row
			:tag('th')
				:wikitext(headings['party'] and 'מפלגה' or '')
				:attr('scope', 'col')
				:attr('colspan', 2)
				:attr('rowspan', rowspan)
				:done()
		cols = cols + 2
	end
	if rounds > 1 then
		if has_votes then
			row
				:tag('th')
					:wikitext(args.firstround or 'סיבוב ראשון')
					:attr('scope', 'col')
					:attr('colspan', 2)
					:done()
				:tag('th')
					:wikitext(args.secondround or 'סיבוב שני')
					:attr('scope', 'col')
					:attr('colspan', 2)
					:done()
		end
		secondrow = root:tag('tr')
	else
		secondrow = row
	end
	rowspan = (rounds > 1) and 1 or (rounds < 2 and headings['st1t']) and 2 or nil
	if has_votes then
		for k=1, rounds do
			secondrow
				:tag('th')
					:wikitext('קולות')
					:attr('scope', 'col')
					:attr('rowspan', rowspan)
					:done()
				:tag('th')
					:wikitext('%')
					:attr('scope', 'col')
					:attr('rowspan', rowspan)
					:done()
			cols = cols + 2
		end
	end
	if headings['sw'] then
		row
			:tag('th')
				:wikitext('&pm;')
				:attr('scope', 'col')
				:attr('rowspan', rowspan)
				:done()
		cols = cols + 1
	end
	rowspan = (rounds > 1) and 2 or nil
	if headings['seats'] then
		row
			:tag('th')
				:wikitext(args['seatstitle'] or 'מושבים')
				:attr('scope', 'col')
				:attr('rowspan', rowspan)
				:done()
		cols = cols + 1
	end
	if headings['sc'] then
		row
			:tag('th')
				:wikitext(headings['seats'] and '&pm;' or 'מושבים&pm;')
				:attr('scope', 'col')
				:attr('rowspan', rowspan)
				:done()
		cols = cols + 1
	end
	if headings['st1t'] then
		row
			:tag('th')
				:wikitext(args['seatstitle'] or 'מושבים')
				:attr('colspan', 5)
				:attr('scope', 'col')
				:done()
	if rounds < 2 then secondrow = root:tag('tr')
	end
		secondrow
			:tag('th')
				:wikitext(args.seattype1)
				:attr('scope', 'col')
				:done()
		cols = cols + 1
	if headings['st2t'] then
		secondrow
			:tag('th')
				:wikitext(args.seattype2)
				:attr('scope', 'col')
				:done()
		cols = cols + 1
	end
	if headings['st3t'] then
		secondrow
			:tag('th')
				:wikitext(args.seattype3)
				:attr('scope', 'col')
				:done()
		cols = cols + 1
	end
	if headings['st4t'] then
		secondrow
			:tag('th')
				:wikitext(args.seattype4)
				:attr('scope', 'col')
				:done()
		cols = cols + 1
	end
	if headings['st5t'] then
		secondrow
			:tag('th')
				:wikitext(args.seattype5)
				:attr('scope', 'col')
				:done()
		cols = cols + 1
	end
	end
	if topcell then
		topcell:attr('colspan', cols)
	end

	local cs = cols - 2*rounds
			- (headings['sw'] and 1 or 0)
			- (headings['seats'] and 1 or 0)
			- (headings['sc'] and 1 or 0)
			- (headings['st1t'] and 1 or 0)
			- (headings['st2t'] and 1 or 0)
			- (headings['st3t'] and 1 or 0)
			- (headings['st4t'] and 1 or 0)
			- (headings['st5t'] and 1 or 0)
			+ (has_votes and 0 or 2)
	local rsuff = (rounds > 1) and {'', '_2'} or {''}
	for i, v in ipairs(index) do
		row = root:tag('tr')
			:addClass(args['class' .. v])
			:css('font-weight', args['font-weight' .. v])

		-- determine the color
		local color = args['colour' .. v] or args['color' .. v] or nil
		if color == nil then
			local party = unlink(args['party' .. v]) or ''
			if party ~= '' and mw.title.new('תבנית:' .. party .. '/meta/color').exists then
				color = frame:expandTemplate{title = party .. '/meta/color'}
			end
		end

		if args['row' .. v] then
			row
				:css('background-color', args['acolor' .. v])
				:tag('td')
					:wikitext(args['row' .. v])
					:attr('colspan', cs)
		else
			-- create the empty color cell
			row
				:tag('td')
					:css('background-color', color)

			-- add the rest of the row
			if args['dsv'] then
				row
					:tag('td')
						:wikitext(args['party' .. v])
				row
					:tag('td')
						:wikitext(args['cand' .. v])
			end
			if args['israel'] then
				row
					:tag('td')
						:wikitext(args['party' .. v])
				row
					:tag('td')
						:wikitext(args['symbol' .. v])
				row
					:tag('td')
						:wikitext(args['cand' .. v])
			end
			if headings['cand'] and not args['dsv'] and not args['israel'] then
				row
					:tag('td')
						:wikitext(args['cand' .. v])
				if headings['vp'] then
					row
						:tag('td')
							:wikitext(args['vp' .. v])
				end
			end
			if headings['party'] and not args['dsv'] and not args['israel'] then
				row
					:tag('td')
						:wikitext(args['party' .. v])
			end
		end
		for kk, suf in ipairs(rsuff) do
			if args['votes' .. v .. suf] then
				row:tag('td')
					:css('text-align', 'right')
					:wikitext(fmt(args['votes' .. v .. suf]))
				row:tag('td')
					:css('text-align', 'right')
					:wikitext(pct(args['votes' .. v .. suf], valid[kk]))
			elseif args['ivotes' .. v] or args['ipct' .. v] then
				row:tag('td')
					:css('text-align', 'right')
					:css('font-style', 'italic')
					:wikitext(fmt(args['ivotes' .. v]))
				row:tag('td')
					:css('text-align', 'right')
					:css('font-style', 'italic')
					:wikitext(args['ipct' .. v])
			elseif headings['sw'] then
				row:tag('td'):attr('colspan', 3)
			elseif has_votes then
				row:tag('td'):attr('colspan', 2)
			end
		end
		if headings['sw'] and (args['votes' .. v]) then
			row
				:tag('td')
					:css('text-align', 'center')
					:wikitext(args['sw' .. v])
		end
		if headings['sw'] and (args['ivotes' .. v]) then
			row
				:tag('td')
					:css('text-align', 'center')
					:wikitext(args['sw' .. v])
		end
		if headings['seats'] and (args['seats' .. v]) then
			row
				:tag('td')
					:css('text-align', 'center')
					:wikitext(fmt(args['seats' .. v]))
		elseif headings['seats'] and (args['iseats' .. v]) then
			row
				:tag('td')
					:css('text-align', 'center')
					:css('font-style', 'italic')
					:wikitext(fmt(args['iseats' .. v]))
		elseif headings['seats'] then
			row
				:tag('td')
					:css('text-align', 'center')
					:wikitext('–')
		end
		if headings['sc'] and (args['seats' .. v]) then
			row
				:tag('td')
					:css('text-align', 'center')
					:css('direction', 'ltr')
					:wikitext(args['sc' .. v])
		elseif headings['sc'] and (args['iseats' .. v]) then
			row
				:tag('td')
					:css('text-align', 'center')
					:css('font-style', 'italic')
					:wikitext(args['sc' .. v])
		elseif headings['sc'] then
			row
				:tag('td')
					:css('text-align', 'center')
					:wikitext('–')
		end
		if headings['st1t'] then
			row
				:tag('td')
					:css('text-align', 'center')
					:wikitext(args['st1t' .. v])
		end
		if headings['st2t'] then
			row
				:tag('td')
					:css('text-align', 'center')
					:wikitext(args['st2t' .. v])
		end
		if headings['st3t'] then
			row
				:tag('td')
					:css('text-align', 'center')
					:wikitext(args['st3t' .. v])
		end
		if headings['st4t'] then
			row
				:tag('td')
					:css('text-align', 'center')
					:wikitext(args['st4t' .. v])
		end
		if headings['st5t'] then
			row
				:tag('td')
					:css('text-align', 'center')
					:wikitext(args['st5t' .. v])
		end
	end
	-- separating line
	row = root
		:tag('tr')
			:addClass('sortbottom')
	row
		:tag('td')
			:css('background', '#eaecf0')
			:attr('colspan', cols)
	-- valid votes
	if args['invalid'] then
	row = root
		:tag('tr')
			:addClass('sortbottom')
			:css('text-align', 'right')
	row
		:tag('th')
			:wikitext('קולות כשרים')
			:attr('scope', 'row')
			:attr('colspan', cs)
			:css('text-align', 'left')
			:css('font-weight', 'normal')
			:css('background', 'inherit')
	for k=1, rounds do
		row
			:tag('td')
				:wikitext(fmt(ovalid[k]))
			:tag('td')
				:wikitext(pct(ovalid[k], ovalid[k] + invalid[k]))
	end
	if args['invalidsw'] then
		row:tag('td')
			:wikitext(args['validsw'])
		local cspan = (headings['seats'] and 1 or 0) + (headings['sc'] and 1 or 0) + (headings['st1t'] and 1 or 0) + (headings['st2t'] and 1 or 0) + (headings['st3t'] and 1 or 0) + (headings['st4t'] and 1 or 0) + (headings['st5t'] and 1 or 0)
	if headings['seats'] or headings['sc'] then
		local cspan = (headings['seats'] and 1 or 0) + (headings['sc'] and 1 or 0) + (headings['st1t'] and 1 or 0) + (headings['st2t'] and 1 or 0) + (headings['st3t'] and 1 or 0) + (headings['st4t'] and 1 or 0) + (headings['st5t'] and 1 or 0)
		row
			:tag('td')
				:attr('colspan', cspan > 1 and cspan or nil)
	end
	elseif headings['sw'] or headings['seats'] or headings['sc'] or headings['st1t'] or headings['st2t'] or headings['st3t'] or headings['st4t'] or headings['ss51t'] then
		local cspan = (headings['sw'] and 1 or 0) + (headings['seats'] and 1 or 0) + (headings['sc'] and 1 or 0) + (headings['st1t'] and 1 or 0) + (headings['st2t'] and 1 or 0) + (headings['st3t'] and 1 or 0) + (headings['st4t'] and 1 or 0) + (headings['st5t'] and 1 or 0)
		row
			:tag('td')
				:attr('colspan', cspan > 1 and cspan or nil)
	end
	-- invalid votes
	row = root:tag('tr')
			:addClass('sortbottom')
			:css('text-align', 'right')
		if args['blank'] then
	row
		:tag('th')
			:wikitext('קולות פסולים')
			:attr('scope', 'row')
			:attr('colspan', cs)
			:css('text-align', 'left')
			:css('font-weight', 'normal')
			:css('background', 'inherit')
	else
	row
		:tag('th')
			:wikitext('קולות פסולים/פתק לבן')
			:attr('scope', 'row')
			:attr('colspan', cs)
			:css('text-align', 'left')
			:css('font-weight', 'normal')
			:css('background', 'inherit')
	end
	for k=1, rounds do
		row
			:tag('td')
				:wikitext(fmt(invalid[k]))
			:tag('td')
				:wikitext(pct(invalid[k], ovalid[k] + invalid[k]))
	end
	if args['invalidsw'] then
		row:tag('td')
			:wikitext(args['invalidsw'])
	if headings['seats'] or headings['sc'] or headings['st1t'] or headings['st2t'] or headings['st3t'] or headings['st4t'] or headings['ss51t'] then
		local cspan = (headings['seats'] and 1 or 0) + (headings['sc'] and 1 or 0)
		row
			:tag('td')
				:attr('colspan', cspan > 1 and cspan or nil)
	end
	elseif headings['sw'] or headings['seats'] or headings['sc'] or headings['st1t'] or headings['st2t'] or headings['st3t'] or headings['st4t'] or headings['ss51t'] then
		local cspan = (headings['sw'] and 1 or 0) + (headings['seats'] and 1 or 0) + (headings['sc'] and 1 or 0) + (headings['st1t'] and 1 or 0) + (headings['st2t'] and 1 or 0) + (headings['st3t'] and 1 or 0) + (headings['st4t'] and 1 or 0) + (headings['st5t'] and 1 or 0)
		row
			:tag('td')
				:attr('colspan', cspan > 1 and cspan or nil)
	end
	-- total
	if not args['totalvotes'] then
	row = root:tag('tr')
			:addClass('sortbottom')
			:css('font-weight', 'bold')
			:css('text-align', 'right')
	row
		:tag('th')
			:wikitext('סך כל הקולות')
			:attr('scope', 'row')
			:attr('colspan', cs)
			:css('text-align', 'left')
			:css('background', 'inherit')
	for k=1, rounds do
		row
			:tag('td')
				:wikitext(fmt(ovalid[k] + invalid[k]))
			:tag('td')
				:wikitext(pct(1, 1))
	end
	if args['invalidsw'] then
		row:tag('td')
			:wikitext('–')
	if headings['seats'] or headings['sc'] or headings['st1t'] or headings['st2t'] or headings['st3t'] or headings['st4t'] or headings['st5t'] then
		local cspan = (headings['seats'] and 1 or 0) + (headings['sc'] and 1 or 0) + (headings['st1t'] and 1 or 0) + (headings['st2t'] and 1 or 0) + (headings['st3t'] and 1 or 0) + (headings['st4t'] and 1 or 0) + (headings['st5t'] and 1 or 0)
		row
			:tag('td')
				:attr('colspan', cspan > 1 and cspan or nil)
	end
	elseif headings['sw'] or headings['seats'] or headings['sc'] or headings['st1t'] or headings['st2t'] or headings['st3t'] or headings['st4t'] or headings['st5t'] then
		local cspan = (headings['sw'] and 1 or 0) + (headings['seats'] and 1 or 0) + (headings['sc'] and 1 or 0) + (headings['st1t'] and 1 or 0) + (headings['st2t'] and 1 or 0) + (headings['st3t'] and 1 or 0) + (headings['st4t'] and 1 or 0) + (headings['st5t'] and 1 or 0)
		row
			:tag('td')
				:attr('colspan', cspan > 1 and cspan or nil)
	end
	end
	end
	if args['totalvotes'] then
	row = root:tag('tr')
			:addClass('sortbottom')
			:css('font-weight', 'bold')
			:css('text-align', 'right')
	row
		:tag('th')
			:wikitext('סך כל הקולות')
			:attr('scope', 'row')
			:attr('colspan', cs)
			:css('text-align', 'left')
			:css('background', 'inherit')
	if args['invalid'] then
	for k=1, rounds do
		row
			:tag('td')
				:wikitext(fmt(totalvotes[k]))
			:tag('td')
				:wikitext('100')
	end
	else
	for k=1, rounds do
		row
			:tag('td')
				:wikitext(fmt(totalvotes[k]))
			:tag('td')
				:wikitext('–')
	end
	end
	if headings['sw'] or headings['seats'] or headings['sc'] or headings['st1t'] or headings['st2t'] or headings['st3t'] or headings['st4t'] or headings['st5t'] then
		local cspan = (headings['sw'] and 1 or 0) + (headings['seats'] and 1 or 0) + (headings['sc'] and 1 or 0) + (headings['st1t'] and 1 or 0) + (headings['st2t'] and 1 or 0) + (headings['st3t'] and 1 or 0) + (headings['st4t'] and 1 or 0) + (headings['st5t'] and 1 or 0)
		row
			:tag('td')
				:attr('colspan', cspan > 1 and cspan or nil)
	end
	end
	-- registered
	if args['electorate'] or args['turnout'] then
	row = root:tag('tr')
			:addClass('sortbottom')
			:css('text-align', 'right')
	row
		:tag('th')
			:wikitext('מצביעים רשומים/שיעור ההצבעה')
			:attr('scope', 'row')
			:attr('colspan', cs)
			:css('text-align', 'left')
			:css('font-weight', 'normal')
			:css('background', 'inherit')
	if args['electorate'] and args['turnout'] then
		for k=1, rounds do
		row:tag('td')
			:wikitext(fmt(electorate[k]))
		row:tag('td')
			:wikitext(fmt(turnout[k]))
	end
	elseif args['turnout'] then
		for k=1, rounds do
		row:tag('td')
		row:tag('td')
			:wikitext(fmt(turnout[k]))
	end
	elseif args['invalid'] then
		for k=1, rounds do
		row:tag('td')
			:wikitext(fmt(electorate[k]))
		row:tag('td')
			:wikitext(pct(ovalid[k] + invalid[k], electorate[k]))
	end
	elseif args['totalvotes'] then
		for k=1, rounds do
		row:tag('td')
			:wikitext(fmt(electorate[k]))
		row:tag('td')
			:wikitext(pct(totalvotes[k], electorate[k]))
	end
	else
		for k=1, rounds do
		row:tag('td')
			:wikitext(fmt(electorate[k]))
		row:tag('td')
			:wikitext('–')
	end
	end
	if args['turnoutsw'] then
		row:tag('td')
			:wikitext(args['turnoutsw'])
	if headings['seats'] or headings['sc'] or headings['st1t'] or headings['st2t'] or headings['st3t'] or headings['st4t'] or headings['st5t'] then
		local cspan = (headings['seats'] and 1 or 0) + (headings['sc'] and 1 or 0) + (headings['st1t'] and 1 or 0) + (headings['st2t'] and 1 or 0) + (headings['st3t'] and 1 or 0) + (headings['st4t'] and 1 or 0) + (headings['st5t'] and 1 or 0)
		row
			:tag('td')
				:attr('colspan', cspan > 1 and cspan or nil)
	end
	elseif headings['sw'] or headings['seats'] or headings['sc'] or headings['st1t'] or headings['st2t'] or headings['st3t'] or headings['st4t'] or headings['st5t'] then
		local cspan = (headings['sw'] and 1 or 0) + (headings['seats'] and 1 or 0) + (headings['sc'] and 1 or 0) + (headings['st1t'] and 1 or 0) + (headings['st2t'] and 1 or 0) + (headings['st3t'] and 1 or 0) + (headings['st4t'] and 1 or 0) + (headings['st5t'] and 1 or 0)
		row
			:tag('td')
				:attr('colspan', cspan > 1 and cspan or nil)
	end
	end
	if args['majority'] then
	if args['invalid'] or args['electorate'] then
	row = root
		:tag('tr')
			:addClass('sortbottom')
	row
		:tag('td')
			:css('background', '#eaecf0')
			:attr('colspan', cols)
	end
	row = root
		:tag('tr')
			:addClass('sortbottom')
			:css('text-align', 'right')
	row
		:tag('th')
			:wikitext('רוב')
			:attr('scope', 'row')
			:attr('colspan', cs)
			:css('text-align', 'left')
			:css('font-weight', 'normal')
			:css('background', 'inherit')
	for k=1, rounds do
	row
		:tag('td')
			:wikitext(fmt(majority[k]))
		:tag('td')
			:wikitext(fmt(majoritypct[k]))
	end
	if args['majoritysw'] then
	row
		:tag('td')
			:wikitext(args['majoritysw'])
	end
	end
	if args['result'] then
	row = root:tag('tr')
			:addClass('sortbottom')
		-- determine the color
		local color = args['resultcolour'] or nil
		if color == nil then
			local result = unlink(args['result']) or ''
			if result ~= '' and mw.title.new('תבנית:' .. result .. '/meta/color').exists then
				color = frame:expandTemplate{title = result .. '/meta/color'}
			end
			end
	if args['resultsw'] then
		row
			:tag('td')
				:css('background-color', color)
		row
			:tag('td')
				:attr('colspan', 2)
				:wikitext(args['result'])
		row
			:tag('td')
				:attr('colspan', 2)
				:css('text-align', 'right')
				:wikitext('שינוי')
		row
			:tag('td')
				:css('text-align', 'right')
				:wikitext(args['resultsw'])
	else
		row
			:tag('td')
				:css('background-color', color)
		row
			:tag('td')
				:attr('colspan', cols - 1)
				:wikitext(args['result'])
	end
	end
	if args['majority2'] then
	row = root
		:tag('tr')
			:addClass('sortbottom')
			:css('text-align', 'right')
	row
		:tag('th')
			:wikitext('רוב')
			:attr('scope', 'row')
			:attr('colspan', cs)
			:css('text-align', 'left')
			:css('font-weight', 'normal')
			:css('background', 'inherit')
	for k=1, rounds do
	row
		:tag('td')
			:wikitext(fmt(majority2[k]))
		:tag('td')
			:wikitext(fmt(majoritypct2[k]))
	end
	if args['majoritysw2'] then
	row
		:tag('td')
			:wikitext(args['majoritysw2'])
	end
	end
	if args['result2'] then
	row = root:tag('tr')
			:addClass('sortbottom')
		-- determine the color
		local color = args['resultcolour'] or nil
		if color == nil then
			local result = unlink(args['result2']) or ''
			if result ~= '' and mw.title.new('תבנית:' .. result .. '/meta/color').exists then
				color = frame:expandTemplate{title = result .. '/meta/color'}
			end
			end
	if args['resultsw2'] then
		row
			:tag('td')
				:css('background-color', color)
		row
			:tag('td')
				:attr('colspan', 2)
				:wikitext(args['result2'])
		row
			:tag('td')
				:attr('colspan', 2)
				:css('text-align', 'right')
				:wikitext('שינוי')
		row
			:tag('td')
				:css('text-align', 'right')
				:wikitext(args['resultsw2'])
	else
		row
			:tag('td')
				:css('background-color', color)
		row
			:tag('td')
				:attr('colspan', cols - 1)
				:wikitext(args['result2'])
	end
	end
	if args['source'] then
	row = root:tag('tr')
			:addClass('sortbottom')
			:css('text-align', 'right')
	row:tag('td')
		:wikitext('מקור: ', args.source)
		:attr('colspan', cols)
		:css('text-align', 'left')
	end
	return tostring(root) .. tracking
end
--------------------------------------------------------------------------------
-- Exports
--------------------------------------------------------------------------------
local p = {}

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame)
    -- translate params
	local translatedArgs = {};
	for k,v in pairs(args) do
		if mw.ustring.match(k, '[א-ת]') ~=nil then
			local enKey = mw.ustring.gsub( k, 'צבע ', 'color' )
			enKey = mw.ustring.gsub( enKey, 'תמונה', 'image' )
			enKey = mw.ustring.gsub( enKey, 'כותרת ראשית', 'caption' )
			enKey = mw.ustring.gsub( enKey, 'כותרת מפלגות', 'partytitle' )
			enKey = mw.ustring.gsub( enKey, 'כותרת מועמדים', 'candtitle' )
			enKey = mw.ustring.gsub( enKey, 'כותרת מושבים', 'seatstitle' )
			enKey = mw.ustring.gsub( enKey, 'כותרת סיבוב ראשון', 'firstround' )
			enKey = mw.ustring.gsub( enKey, 'כותרת סיבוב שני', 'secondround' )
			enKey = mw.ustring.gsub( enKey, 'סימן ', 'symbol' )
			enKey = mw.ustring.gsub( enKey, 'מפלגה ', 'party' )
			enKey = mw.ustring.gsub( enKey, 'מועמד ', 'cand' )
			enKey = mw.ustring.gsub( enKey, 'סגן ', 'vp' )
			enKey = mw.ustring.gsub( enKey, 'קולות ', 'votes' )
			enKey = mw.ustring.gsub( enKey, 'שינוי כללי', 'total_sc' )
			enKey = mw.ustring.gsub( enKey, 'שינוי מושבים ', 'sc' )
			enKey = mw.ustring.gsub( enKey, 'מושבים ', 'seats' )
			enKey = mw.ustring.gsub( enKey, 'שינוי בכשרים', 'validsw' )
			enKey = mw.ustring.gsub( enKey, 'כשרים בשני', 'valid2' )
			enKey = mw.ustring.gsub( enKey, 'כשרים', 'valid' )
			enKey = mw.ustring.gsub( enKey, 'שינוי בפסולים', 'invalidsw' )
			enKey = mw.ustring.gsub( enKey, 'פסולים בשני', 'invalid2' )
			enKey = mw.ustring.gsub( enKey, 'פסולים', 'invalid' )
			enKey = mw.ustring.gsub( enKey, 'לבן', 'blank' )
			enKey = mw.ustring.gsub( enKey, 'פתקים', 'totalvotes' )
			enKey = mw.ustring.gsub( enKey, 'רשומים בשני', 'electorate2' )
			enKey = mw.ustring.gsub( enKey, 'רשומים', 'electorate' )
			enKey = mw.ustring.gsub( enKey, 'שינוי בשיעור הצבעה', 'turnoutsw' )
			enKey = mw.ustring.gsub( enKey, 'שיעור הצבעה בשני', 'turnout2' )
			enKey = mw.ustring.gsub( enKey, 'שיעור הצבעה', 'turnout' )
			enKey = mw.ustring.gsub( enKey, 'שיעור רוב בשני', 'majoritypct2' )
			enKey = mw.ustring.gsub( enKey, 'שיעור רוב', 'majoritypct' )
			enKey = mw.ustring.gsub( enKey, 'שינוי רוב בשני', 'majoritysw2' )
			enKey = mw.ustring.gsub( enKey, 'שינוי רוב', 'majoritysw' )
			enKey = mw.ustring.gsub( enKey, 'שינוי תוצאה בשני', 'resultsw2' )
			enKey = mw.ustring.gsub( enKey, 'שינוי תוצאה', 'resultsw' )
			enKey = mw.ustring.gsub( enKey, 'תוצאה בשני', 'result2' )
			enKey = mw.ustring.gsub( enKey, 'תוצאה', 'result' )
			enKey = mw.ustring.gsub( enKey, 'רוב בשני', 'majority2' )
			enKey = mw.ustring.gsub( enKey, 'רוב', 'majority' )
			enKey = mw.ustring.gsub( enKey, 'שינוי ', 'sw' )
			enKey = mw.ustring.gsub( enKey, 'ממוזגת', 'dsv' )
			enKey = mw.ustring.gsub( enKey, 'ישראל', 'israel' )
			enKey = mw.ustring.gsub( enKey, 'חסימה', 'threshold' )
			enKey = mw.ustring.gsub( enKey, 'מקור', 'source' )
			
			translatedArgs[enKey] = v
		else
			translatedArgs[k] = v
		end
	end

	return ElectionResults.new(frame, translatedArgs)
end

return p