Jump to content

Module:US elections imagemap

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Elli (talk | contribs) at 06:18, 19 January 2021. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

-- the goal of this module is to standardize creating imagemaps for yearly Senate and Gubernatorial elections in the United States.
local utils = require("Module:US elections imagemap/utils") -- utilities such as a string splitting function
local data = require("Module:US elections imagemap/data") -- data such as shapes of states

local stateshapes = data.stateshapes
local cycles = data.cycles
local statenames = data.statenames

local p = {}

function p.makeMap(frame) -- limitations: currently no way to add custom shapes, or to add custom states to cycles, or to add custom links not following the general template
	local states = cycles[frame.args.cycle] or error("error: invalid/no cycle specified") -- require to specify a cycle
	local extrastates = utils.split(frame.args.extra_states or "", ";") -- extra states to add
	states = utils.listmerge(states, extrastates)
	local extralines = utils.split(frame.args.extra_lines or "", ";") -- extra raw lines to add
	local stateoverrides = utils.split(frame.args.state_overrides or "", ";") -- state overrides for links (for example, linking to a recall election)
	local overrides = {}
	for _, override in stateoverrides do
		overrides[utils.split(override,"=")[0]] = utils.split(override,"=")[1]
	end
	local linktemplatearg = frame.args.link_template or error("error: no link template specified") -- require a link template
	local linktemplate = " [[" .. linktemplatearg .. "]]\r\n\r\n" -- make it a wikilink, also the space is necessary to separate from the shape data
	local imagemap = "\r\n\r\n" -- start the imagemap
	for _, line in ipairs(extralines) do -- for each extra line. these have to come before normal lines to override them (otherwise, special elections for example don't work)
		imagemap = imagemap .. line .. "\r\n\r\n" -- append the extra line
	end
	for _, state in ipairs(states) do -- for each state
		statename = statenames[state] or error (state) -- if a state isn't in the list for some reason, this will tell which one
		local link = overrides[state] or string.gsub(linktemplate, "STATE", statenames[state]) -- check for individual state override, if not, swap the state name into the link template
		imagemap = imagemap .. stateshapes[state] .. link -- add the link (which already contains the needed newline)
	end
	return imagemap
end

return p