יחידה:Election results – הבדלי גרסאות

מתוך ויקיפדיה, האנציקלופדיה החופשית
תוכן שנמחק תוכן שנוסף
?
?
שורה 168: שורה 168:
:css('text-align', 'right')
:css('text-align', 'right')
:tag('td')
:tag('td')
:wikitext('Source:', source)
:wikitext('Source: (source)')
:attr('colspan', cols - 0)
:attr('colspan', cols - 0)
:css('text-align', 'left')
:css('text-align', 'left')

גרסה מ־23:01, 24 ביולי 2020


local p = {}
function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame)
	local index, cols = {}, 3
	local party, vp = false, false
	local winner, winner_votes = 0, 0
	local valid, invalid, electorate = 0, tonumber(args.invalid) or 0, tonumber(args.electorate) or 0
	for i = 1, 20 do
		if args['cand' .. i] then
			table.insert(index, i)
			if not party and args['party' .. i] then
				party = true
				cols = cols + 2
			end
			if not vp and args['vp' .. i] then
				vp = true
				cols = cols + 1
			end
			if args['votes' .. i] then
				args['votes' .. i] = tonumber(args['votes' .. i])
				if args['votes' .. i] > winner_votes then
					winner = i
					winner_votes = args['votes' .. i]
				end
				valid = valid + args['votes' .. i]
			end
		end
	end
	local root = mw.html.create('table')
	root
		:addClass('wikitable sortable')
		:tag('caption')
			:wikitext(args.caption)
	local row = mw.html.create('tr')
	if vp then
		row
			:tag('th')
				:wikitext('President')
				:attr('colspan', 2)
			:tag('th')
				:wikitext('Vice president')
	else
		row
			:tag('th')
				:wikitext('Candidate')
				:attr('colspan', 2)
	end
	if party then
		row
			:tag('th')
				:wikitext('Party')
	end
	row
		:tag('th')
			:wikitext('Votes')
		:tag('th')
			:wikitext('%')
	root:node(row)
	local lang = mw.getContentLanguage()
	local function fmt(n)
		return lang:formatNum(n)
	end
	for i, v in ipairs(index) do
		local row = mw.html.create('tr')
		row
			:tag('td')
				:css('background-color', frame:expandTemplate{title = args['party' .. v] .. '/meta/color'})
		if vp then
			row
				:tag('td')
					:wikitext(string.format('\[\[%s\]\]', args['cand' .. v]))
			row
				:tag('td')
					:wikitext(string.format('\[\[%s\]\]', args['vp' .. v]))
		else
			row
				:tag('td')
					:wikitext(string.format('\[\[%s\]\]', args['cand' .. v]))
		end
		if args['party_name' .. v] then
			row
				:tag('td')
					:wikitext(string.format('\[\[%s\|%s\]\]', args['party' .. v], args['party_name' .. v]))
		else
			row
				:tag('td')
					:wikitext(string.format('\[\[%s\]\]', args['party' .. v]))
		end
		local cell = mw.html.create('td')
		cell
			:css('text-align', 'right')
		if args['votes' .. v] then
		cell
			:wikitext(fmt(args['votes' .. v]))
		end
		if v == winner then
			cell
				:css('font-weight', 'bold')
		end
		row:node(cell)
		local cell = mw.html.create('td')
		cell
			:css('text-align', 'right')
		if args['votes' .. v] then
		cell
			:wikitext(string.format('%.2f%%', args['votes' .. v] / valid * 100))
		end
		if v == winner then
			cell
				:css('font-weight', 'bold')
		end
		row:node(cell)
		root:node(row)
	end
	root
		:tag('tr')
			:addClass('sortbottom')
			:css('background', '#eaecf0')
			:css('text-align', 'right')
			:tag('td')
				:wikitext('Valid votes')
				:attr('colspan', cols - 2)
 			        :css('text-align', 'left')
			:tag('td')
				:wikitext(fmt(valid))
			:tag('td')
				:wikitext(string.format('%.2f%%', valid / (valid + invalid) * 100))
		:tag('tr')
			:addClass('sortbottom')
			:css('background', '#eaecf0')
			:css('text-align', 'right')
			:tag('td')
				:wikitext('Invalid votes')
				:attr('colspan', cols - 2)
 			        :css('text-align', 'left')
			:tag('td')
				:wikitext(fmt(invalid))
			:tag('td')
				:wikitext(string.format('%.2f%%', invalid / (valid + invalid) * 100))
		:tag('tr')
			:addClass('sortbottom')
			:css('font-weight', 'bold')
			:css('background', '#eaecf0')
			:css('text-align', 'right')
			:tag('td')
				:wikitext('Total votes')
				:attr('colspan', cols - 2)
 			        :css('text-align', 'left')
			:tag('td')
				:wikitext(fmt(valid + invalid))
			:tag('td')
				:wikitext('100%')
		:tag('tr')
			:addClass('sortbottom')
			:css('background', '#eaecf0')
			:css('text-align', 'right')
			:tag('td')
				:wikitext('Registered voters/turnout')
				:attr('colspan', cols - 2)
 			        :css('text-align', 'left')
			:tag('td')
				:wikitext(fmt(electorate))
			:tag('td')
				:wikitext(string.format('%.2f%%', (valid + invalid) / electorate * 100))
		:tag('tr')
			:addClass('sortbottom')
			:css('background', '#eaecf0')
			:css('text-align', 'right')
			:tag('td')
				:wikitext('Source: (source)')
				:attr('colspan', cols - 0)
 			        :css('text-align', 'left')
	return tostring(root)
end
return p