Module:Infobox road/color: Difference between revisions

From MicrasWiki
Jump to navigationJump to search
No pretend countries, please
No edit summary
 
Line 52: Line 52:
do -- Multi-color countries
do -- Multi-color countries
do -- Phinbella
do -- Phinbella
local PHI = Country:new{default = "background:#0079C1; color:#fff;"}
local PHI = Country:new{default = "background:#006A4D; color:#fff;"}
PHI:addTypesAsColor({"E"}, "background:#006A4D; color:#fff;")
PHI:addTypesAsColor({"E"}, "background:#006A4D; color:#fff;")
colors.PHI = PHI
colors.PHI = PHI

Latest revision as of 18:17, 25 July 2021

Documentation for this module may be created at Module:Infobox road/color/doc

local p = {}

Country = {}

function Country:new(obj)
	obj = obj or {}
	setmetatable(obj, self)
	self.__index = self
	return obj
end

function Country:color(args)
	local type = args.type
	return self[type] or self.default
end

function Country:addTypesAsColor(types, color)
	for i,type in pairs(types) do
		self[type] = color
	end
end

local colors = {}
do
	function colors:color(args)
		local headerType = args.headerType
		local countryArg = args.country
		local headerTypeColor = self[headerType]
		if headerTypeColor then return headerTypeColor end
		local success, country = pcall(self.country, self, countryArg)
		if not(success) then
			return 'background:#cedff2;'
		else
			return country:color(args)
		end
	end
	function colors:country(name)
		local color = self[name]
		if color then return color end
		if name == 'CAN' then
			local module = require(string.format("Module:Infobox road/color/%s", name))
			return module.colors
		else
			error("Country not included", 0)
		end
	end
	
	do -- Single-color countries
		
	end -- Single-color countries
	
	do -- Multi-color countries
		do -- Phinbella
			local PHI = Country:new{default = "background:#006A4D; color:#fff;"}
			PHI:addTypesAsColor({"E"}, "background:#006A4D; color:#fff;")
			colors.PHI = PHI
		end -- Phinbella
	end -- Multi-color countries
	
    do -- Built-in header types
		colors["under construction"] = "background:#fc6;"
		colors["const"] = "background:#fc6;"
		colors["uc"] = "background:#fc6;"
		colors["historic"] = "background:#704214; color:white;"
		colors["historical"] = "background:#704214; color:white;"
		colors["hist"] = "background:#704214; color:white;"
		colors["scenic"] = "background:#704214; color:white;"
		colors["decommissioned"] = "background:#AAA;"
		colors["former"] = "background:#AAA;"
		colors["motorway"] = "background:#003DA5; color:white;" --for Norway only
	end -- Built-in header types
end

function p._color(args)
	return colors:color(args)
end

function p.color(frame)
	local pframe = frame:getParent()
	local config = frame.args -- the arguments passed BY the template, in the wikitext of the template itself
	local args = pframe.args -- the arguments passed TO the template, in the wikitext that transcludes the template
	
	local function emptyParam(param)
		local empty = {[''] = true, ['¬'] = true}
		if not(param) or empty[param] then
			return nil
		else
			return param
		end
	end
	
	local state = config.state or args.state
	state = emptyParam(state)
	local province = config.province or args.province
	province = emptyParam(province)
	local type = config.type or args.type
	local subtype = config.subtype or args.subtype or nil
	
	local headerType = args.header_type or config.header_type
	headerType = emptyParam(headerType)
	if headerType then
		headerType = string.lower(headerType)
	end
	
	local countryArg = config.country or args.country
	countryArg = emptyParam(countryArg)
	local country = countryArg
	if not(country) then
		if colors[headerType] then
			country = ''
		else
			local stateParam = state or province
			if not(stateParam) then
				country = ''
			else
				local countryMask = require "Module:Infobox road/meta/mask/country"
				country = countryMask._country(stateParam, countryArg)
			end
		end
	end
	
	return p._color{country=country, state=state, province=province, type=type, subtype=subtype, headerType=headerType}
end

return p