Module:Infobox election

local p = {}

local InfoboxImage = require('Module:InfoboxImage').InfoboxImage
local delink = require('Module:Delink')._delink
local fetch = require('Module:Political party')._fetch
	
-- Helper functions --
local notblank = function (v) return (mw.text.trim(v or "") ~= "") end
local ifblank = function (v, alt1, alt2) return notblank(v) and v or (notblank(alt1) and alt1 or alt2) end
local ucfirst = function (s) return mw.ustring.upper(mw.ustring.sub(s,1,1)) .. mw.ustring.gsub(mw.ustring.sub(s,2), '_', ' ') end

local function export(func, frame)
	-- Read args from input --
	local args = {}
	local frameArgs = false
	for k,v in pairs(frame.args) do
		if notblank(v) then
			args[k] = mw.text.trim(v)
			frameArgs = true
		end
	end
	if not frameArgs then
		for k,v in pairs(frame:getParent().args) do
			if notblank(v) then args[k] = mw.text.trim(v) end
		end
	end
	
	return p[func](args, frame)
end

-- Template:Infobox election/shortname --
function p._shortname(sargs)
	local link = sargs.link
	local name = sargs.name
	if link ~= 'no' and notblank(name) and delink({[1] = name, wikilinks = 'target'}) == name then
		-- the below array items are lowercase and intentionally missing the training 's'
		local names = {
			independent = '[[Independent politician|' .. name .. ']]',
			miscellaneou = '[[Independent politician|DIV]]',					-- the 's' is intentionally left off
			other = 'Others',
			['other parties and independent'] = 'Others & [[Independent politician|IND]]',
			regionalist = '[[Regionalism (politics)|Regionalists]]',
			tbc = '[[To be announced|TBC]]',
			tbd = '[[To be announced|TBD]]',
			vacant = '[[Casual vacancy|Vacant]]'
		}
		return names[mw.ustring.gsub(mw.ustring.lower(name), 's$', '')] or 
			('[[' .. name .. '|' .. fetch({[1] = name, [2] = 'shortname'})  .. ']]')
	end
	return name or ''
end

p.shortname = function (frame) return export('_shortname', frame) end

-- Template:Infobox election/row --
function p._row(rargs, frame)
	local rtype = mw.ustring.lower(rargs.type or '')
	local ongoing = rargs.ongoing == 'yes'
	local output = {'|-'}
	setmetatable(output, {__call = function (t, v) t[#t+1] = v end })				-- Append to array by calling it
	
	local function threeCells(cellType, title, always, shortname)
		local centeredCell = function (key)
			local name = shortname and p._shortname({link = rargs[key .. '_name'], name = rargs[key]}) or (rargs[key] or '')
			return ('| style="' .. (notblank(rargs[key]) and 'width:75px;' or '') .. 'text-align:center" | ' .. name )
		end

		if always or ( notblank(rargs[cellType .. '1']) or notblank(rargs[cellType .. '2']) or notblank(rargs[cellType .. '3']) ) then
			output('|-')
			output('! style="width:75px;text-align:left" |' .. (ifblank(rargs[cellType .. '_label'], title, ucfirst(cellType or ''))))
			output(centeredCell(cellType .. '1'))
			output(centeredCell(cellType .. '2'))
			if always or notblank(rargs.col3) then
				output(centeredCell(cellType .. '3'))
			end
		end
	end
	
	if notblank(rargs.divider) and (
		notblank(rargs.party1) or notblank(rargs.color1) or notblank(rargs.leader1) or notblank(rargs.party2) or notblank(rargs.color2) or notblank(rargs.leader2) or notblank(rargs.party3) or notblank(rargs.color3) or notblank(rargs.leader3)
	) then
		output('| colspan=4 | ' .. rargs.divider)
	end
	output('|- class="sr-only"')
	if not ongoing and (
		rtype == "parliamentary" or rtype == "legislative"
	)  and (
		notblank(rargs.party1) or notblank(rargs.party2) or notblank(rargs.party2)
	) then
		output('! style="text-align:left;" |  ')
		output('! style="text-align:center" | ' .. (notblank(rargs.party1) and rargs.header1 or ''))
		output('! style="text-align:center" | ' .. (notblank(rargs.party2) and rargs.header2 or ''))
		if notblank(rargs.col3) then
			output('! style="text-align:center" | ' .. (notblank(rargs.party3) and rargs.header3 or ''))
		end
	end
	-- Images --
	output('|-')
	if notblank(rargs.party1) or notblank(rargs.color1) or notblank(rargs.image1) or notblank(rargs.party2) or notblank(rargs.color2) or notblank(rargs.image2) or notblank(rargs.party3) or notblank(rargs.color3) or notblank(rargs.image3) then
		if notblank(rargs.filler) then
			output('! <br />' .. rargs.filler)
		else
			output('| &nbsp;')
		end
		local image = {}
		for i = 1, notblank(rargs['col3']) and 3 or 2, 1 do
			image[i] = '| class="notheme" style="text-align:center;'
			if not notblank(rargs['party' .. i]) and not notblank(rargs['color' .. i]) then
				image[i] = image[i] .. 'width:0;'
			end
			if notblank(rargs['color' .. i]) then
				local color =  mw.text.trim(mw.text.decode(rargs['color' .. i]))
				if (#color == 3 or #color == 6) and string.match(color, "^[0-9a-fA-F]*$") then
					color = '#' .. color
				end
				image[i] = image[i] .. 'border-bottom: 6px solid ' .. color
			elseif rargs.party_color ~= 'no' and notblank(rargs['party' .. i]) then
				local color = mw.ustring.gsub(rargs['party' .. i], '^(%[%[%s*[^%]%[]*%]%]).*$', '%1')
				color = delink({[1] = color, wikilinks = 'target'})
				color = fetch({[1] = color, [2] = 'color', error = 'NOTHERE'})
				if color ~= 'NOTHERE' then
					image[i] = image[i] .. 'border-bottom: 6px solid ' .. color
				end
			end
			image[i] = image[i] .. ';" | ' .. InfoboxImage(	frame:newChild{
				title = frame:getTitle(), args = {
					image = rargs['image' .. i] or '',
					size = notblank(rargs['image' .. i .. '_size']) and rargs['image' .. i .. '_size'] or (rargs.image_size or ''),
					alt = rargs['alt' .. i] or '',
					upright = notblank(rargs['image' .. i .. '_upright']) and rargs['image' .. i .. '_upright'] or (
						(notblank(rargs['col3']) and notblank(rargs['image3'])) and '0.4' or '0.6'
					),
					suppressplaceholder = 'yes',
					class = notblank(rargs['pageimage' .. i]) and '' or 'notpageimage noresize',
				}
			} )
			output(image[i])
		end
	end
	-- Presidential & by-election nominee --
	if notblank(rargs['nominee1']) or notblank(rargs['nominee2']) or notblank(rargs['nominee3']) then
		if ({primary = 1, presidential = 1, ['by-election'] = 1})[rtype] == 1 then
			threeCells('nominee', '', true) -- nominee, no alternative title, always show
		end
	else
		threeCells('candidate') -- nominee
	end
	-- Party leader --
	if rtype == 'parliamentary' or rtype == 'legislative' then
		threeCells('leader')
	end
	-- Party name --
	threeCells('party', ifblank(rargs.party_label, 'Party'), false, true) -- party, alternative name, don't always show, use shortname
	-- Alliance name --
	threeCells('alliance', '', false, true) -- alliance, no alternative title, don't always show, use shortname
	-- Primary or presidential
	if rtype == 'primary' or rtype == 'presidential' then
		-- Home state --
		threeCells('home_state')
		-- Running mate --
		if rtype == 'presidential' then
			threeCells('running_mate')
			-- Electoral vote --
			if not ongoing then
				threeCells('electoral_vote', (rargs.college_voted == 'no' and 'Projected e' or 'E') .. 'lectoral&nbsp;vote')
			end
		end
		-- Delegate count --
		threeCells('delegate_count', (ongoing and 'Estimated d' or 'D') .. 'elegate&nbsp;count')
		-- Primary only --
		if rtype == 'primary' then
			-- Contests won
			threeCells('states_carried', 'Contests&nbsp;won')
		-- Presidential not ongoing --
		elseif not ongoing then
			-- States carried --
			threeCells('states_carried', 'States&nbsp;carried')
		end
	end
	-- Parliamentary or legislative --
	if rtype == 'parliamentary' or rtype == 'legislative' then
		-- Leader since --
		threeCells('leader_since')
		-- Leader's seat --
		threeCells('leaders_seat', "Leader's&nbsp;seat")
		-- Last election --
		threeCells('last_election')
		-- Seats before --
		threeCells('seats_before', ongoing and 'Current&nbsp;seats' or 'Seats&nbsp;before')
		-- Ongoing --
		if ongoing then
			-- Seats needed --
			threeCells('seats_needed')
		-- Not ongoing --
		else
			-- Seats won --
			threeCells('seats', 'Seats&nbsp;won')
			-- Seats after --
			threeCells('seats_after')
			-- Seat change --
			threeCells('seat_change')
		end
	end
	-- Primary or not ongoing --
	if not ongoing or rtype == 'primary' then
		-- Popular vote --
		threeCells('popular_vote', (rargs.vote_type or 'Popular') .. '&nbsp;vote')
		-- Percentage --
		threeCells('percentage')
		-- First round --
		threeCells('first_round')
		-- Final round --
		threeCells('final_round')
		-- Primary or presidential --
		if rtype == 'primary' or rtype == 'presidential' then
			-- Counties won --
			threeCells('counties_won')
			-- Counties 25% threshold --
			threeCells('counties_threshold', 'Counties&nbsp;with&nbsp;25%&nbsp;vote')
		end
	end
	-- Swing --
	if (rtype == 'parliamentary' or rtype == 'legislative' or rtype == 'by-election') and not ongoing then
		threeCells('swing')
	end
	-- Blank fields --
	for i = 1,6,1 do
		if notblank(rargs[i .. 'blank']) then
			threeCells(i ..'data', rargs[i .. 'blank'])
		end
	end
	
	return table.concat(output, '\n')
end

p.row = function (frame) return export('_row', frame) end

return p

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.