Module:Geography

From stencil.wiki
Revision as of 01:04, 12 January 2025 by Robertbaxter (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:Geography/doc

local p = {}

local CLASS_INFO = {
	place = {
		standard_category = "Places in @@@@"
	}, event = {
		standard_category = "Events in @@@@",
		multi_category = {
			country = "International events",
			state_province = "Multi-state/province events",
			city = "Multi-city events"
		}
	}
}

local REGION_MAP = {
	["afghanistan"] = "Asia",
	["albania"] = "Europe",
	["algeria"] = "Africa",
	["andorra"] = "Europe",
	["angola"] = "Africa",
	["antigua and barbuda"] = "North America",
	["argentina"] = "South America",
	["armenia"] = "Asia",
	["australia"] = "Oceania",
	["austria"] = "Europe",
	["azerbaijan"] = "Asia",
	["bahamas"] = "North America",
	["bahrain"] = "Asia",
	["bangladesh"] = "Asia",
	["barbados"] = "North America",
	["belarus"] = "Europe",
	["belgium"] = "Europe",
	["belize"] = "North America",
	["benin"] = "Africa",
	["bhutan"] = "Asia",
	["bolivia"] = "South America",
	["bosnia and herzegovina"] = "Europe",
	["botswana"] = "Africa",
	["brazil"] = "South America",
	["brunei"] = "Asia",
	["bulgaria"] = "Europe",
	["burkina faso"] = "Africa",
	["burundi"] = "Africa",
	["cambodia"] = "Asia",
	["cameroon"] = "Africa",
	["canada"] = "North America",
	["cape verde"] = "Africa",
	["central african republic"] = "Africa",
	["chad"] = "Africa",
	["chile"] = "South America",
	["china"] = "Asia",
	["colombia"] = "South America",
	["comoros"] = "Africa",
	["congo"] = "Africa",
	["costa rica"] = "North America",
	["croatia"] = "Europe",
	["cuba"] = "North America",
	["cyprus"] = "Europe",
	["czech republic"] = "Europe",
	["denmark"] = "Europe",
	["djibouti"] = "Africa",
	["dominica"] = "North America",
	["dominican republic"] = "North America",
	["ecuador"] = "South America",
	["egypt"] = "Africa",
	["el salvador"] = "North America",
	["equatorial guinea"] = "Africa",
	["eritrea"] = "Africa",
	["estonia"] = "Europe",
	["eswatini"] = "Africa",
	["ethiopia"] = "Africa",
	["fiji"] = "Oceania",
	["finland"] = "Europe",
	["france"] = "Europe",
	["gabon"] = "Africa",
	["gambia"] = "Africa",
	["georgia"] = "Asia",
	["germany"] = "Europe",
	["ghana"] = "Africa",
	["greece"] = "Europe",
	["grenada"] = "North America",
	["guatemala"] = "North America",
	["guinea"] = "Africa",
	["guinea-bissau"] = "Africa",
	["guyana"] = "South America",
	["haiti"] = "North America",
	["honduras"] = "North America",
	["hungary"] = "Europe",
	["iceland"] = "Europe",
	["india"] = "Asia",
	["indonesia"] = "Asia",
	["iran"] = "Asia",
	["iraq"] = "Asia",
	["ireland"] = "Europe",
	["israel"] = "Asia",
	["italy"] = "Europe",
	["jamaica"] = "North America",
	["japan"] = "Asia",
	["jordan"] = "Asia",
	["kazakhstan"] = "Asia",
	["kenya"] = "Africa",
	["kiribati"] = "Oceania",
	["kuwait"] = "Asia",
	["kyrgyzstan"] = "Asia",
	["laos"] = "Asia",
	["latvia"] = "Europe",
	["lebanon"] = "Asia",
	["lesotho"] = "Africa",
	["liberia"] = "Africa",
	["libya"] = "Africa",
	["liechtenstein"] = "Europe",
	["lithuania"] = "Europe",
	["luxembourg"] = "Europe",
	["madagascar"] = "Africa",
	["malawi"] = "Africa",
	["malaysia"] = "Asia",
	["maldives"] = "Asia",
	["mali"] = "Africa",
	["malta"] = "Europe",
	["marshall islands"] = "Oceania",
	["mauritania"] = "Africa",
	["mauritius"] = "Africa",
	["mexico"] = "North America",
	["micronesia"] = "Oceania",
	["moldova"] = "Europe",
	["monaco"] = "Europe",
	["mongolia"] = "Asia",
	["montenegro"] = "Europe",
	["morocco"] = "Africa",
	["mozambique"] = "Africa",
	["myanmar"] = "Asia",
	["namibia"] = "Africa",
	["nauru"] = "Oceania",
	["nepal"] = "Asia",
	["netherlands"] = "Europe",
	["new zealand"] = "Oceania",
	["nicaragua"] = "North America",
	["niger"] = "Africa",
	["nigeria"] = "Africa",
	["north korea"] = "Asia",
	["north macedonia"] = "Europe",
	["norway"] = "Europe",
	["oman"] = "Asia",
	["pakistan"] = "Asia",
	["palau"] = "Oceania",
	["palestine"] = "Asia",
	["panama"] = "North America",
	["papua new guinea"] = "Oceania",
	["paraguay"] = "South America",
	["peru"] = "South America",
	["philippines"] = "Asia",
	["poland"] = "Europe",
	["portugal"] = "Europe",
	["qatar"] = "Asia",
	["romania"] = "Europe",
	["russia"] = "Europe",
	["rwanda"] = "Africa",
	["saint kitts and nevis"] = "North America",
	["saint lucia"] = "North America",
	["saint vincent and the grenadines"] = "North America",
	["samoa"] = "Oceania",
	["san marino"] = "Europe",
	["sao tome and principe"] = "Africa",
	["saudi arabia"] = "Asia",
	["senegal"] = "Africa",
	["serbia"] = "Europe",
	["seychelles"] = "Africa",
	["sierra leone"] = "Africa",
	["singapore"] = "Asia",
	["slovakia"] = "Europe",
	["slovenia"] = "Europe",
	["solomon islands"] = "Oceania",
	["somalia"] = "Africa",
	["south africa"] = "Africa",
	["south korea"] = "Asia",
	["spain"] = "Europe",
	["sri lanka"] = "Asia",
	["sudan"] = "Africa",
	["suriname"] = "South America",
	["sweden"] = "Europe",
	["switzerland"] = "Europe",
	["syria"] = "Asia",
	["taiwan"] = "Asia",
	["tajikistan"] = "Asia",
	["tanzania"] = "Africa",
	["thailand"] = "Asia",
	["timor-leste"] = "Asia",
	["togo"] = "Africa",
	["tonga"] = "Oceania",
	["trinidad and tobago"] = "North America",
	["tunisia"] = "Africa",
	["turkey"] = "Asia",
	["turkmenistan"] = "Asia",
	["tuvalu"] = "Oceania",
	["uganda"] = "Africa",
	["ukraine"] = "Europe",
	["united arab emirates"] = "Asia",
	["united kingdom"] = "Europe",
	["united states"] = "North America",
	["uruguay"] = "South America",
	["uzbekistan"] = "Asia",
	["vanuatu"] = "Oceania",
	["vatican city"] = "Europe",
	["venezuela"] = "South America",
	["vietnam"] = "Asia",
	["yemen"] = "Asia",
	["zambia"] = "Africa",
	["zimbabwe"] = "Africa"
}

local ARTICLE_MAP = {
	["afghanistan"] = false,
	["albania"] = false,
	["algeria"] = false,
	["andorra"] = false,
	["angola"] = false,
	["antigua and barbuda"] = false,
	["argentina"] = false,
	["armenia"] = false,
	["australia"] = false,
	["austria"] = false,
	["azerbaijan"] = false,
	["bahamas"] = true,
	["bahrain"] = false,
	["bangladesh"] = false,
	["barbados"] = false,
	["belarus"] = false,
	["belgium"] = false,
	["belize"] = false,
	["benin"] = false,
	["bhutan"] = false,
	["bolivia"] = false,
	["bosnia and herzegovina"] = false,
	["botswana"] = false,
	["brazil"] = false,
	["brunei"] = false,
	["bulgaria"] = false,
	["burkina faso"] = false,
	["burundi"] = false,
	["cambodia"] = false,
	["cameroon"] = false,
	["canada"] = false,
	["cape verde"] = false,
	["central african republic"] = true,
	["chad"] = false,
	["chile"] = false,
	["china"] = false,
	["colombia"] = false,
	["comoros"] = false,
	["congo"] = false,
	["costa rica"] = false,
	["croatia"] = false,
	["cuba"] = false,
	["cyprus"] = false,
	["czech republic"] = true,
	["denmark"] = false,
	["djibouti"] = false,
	["dominica"] = false,
	["dominican republic"] = false,
	["ecuador"] = false,
	["egypt"] = false,
	["el salvador"] = false,
	["equatorial guinea"] = false,
	["eritrea"] = false,
	["estonia"] = false,
	["eswatini"] = false,
	["ethiopia"] = false,
	["fiji"] = false,
	["finland"] = false,
	["france"] = false,
	["gabon"] = false,
	["gambia"] = true,
	["georgia"] = false,
	["germany"] = false,
	["ghana"] = false,
	["greece"] = false,
	["grenada"] = false,
	["guatemala"] = false,
	["guinea"] = false,
	["guinea-bissau"] = false,
	["guyana"] = false,
	["haiti"] = false,
	["honduras"] = false,
	["hungary"] = false,
	["iceland"] = false,
	["india"] = false,
	["indonesia"] = false,
	["iran"] = false,
	["iraq"] = false,
	["ireland"] = false,
	["israel"] = false,
	["italy"] = false,
	["jamaica"] = false,
	["japan"] = false,
	["jordan"] = false,
	["kazakhstan"] = false,
	["kenya"] = false,
	["kiribati"] = false,
	["kuwait"] = false,
	["kyrgyzstan"] = false,
	["laos"] = false,
	["latvia"] = false,
	["lebanon"] = false,
	["lesotho"] = false,
	["liberia"] = false,
	["libya"] = false,
	["liechtenstein"] = false,
	["lithuania"] = false,
	["luxembourg"] = false,
	["madagascar"] = false,
	["malawi"] = false,
	["malaysia"] = false,
	["maldives"] = true,
	["mali"] = false,
	["malta"] = false,
	["marshall islands"] = true,
	["mauritania"] = false,
	["mauritius"] = false,
	["mexico"] = false,
	["micronesia"] = false,
	["moldova"] = false,
	["monaco"] = false,
	["mongolia"] = false,
	["montenegro"] = false,
	["morocco"] = false,
	["mozambique"] = false,
	["myanmar"] = false,
	["namibia"] = false,
	["nauru"] = false,
	["nepal"] = false,
	["netherlands"] = true,
	["new zealand"] = false,
	["nicaragua"] = false,
	["niger"] = false,
	["nigeria"] = false,
	["north korea"] = false,
	["north macedonia"] = false,
	["norway"] = false,
	["oman"] = false,
	["pakistan"] = false,
	["palau"] = false,
	["palestine"] = false,
	["panama"] = false,
	["papua new guinea"] = false,
	["paraguay"] = false,
	["peru"] = false,
	["philippines"] = true,
	["poland"] = false,
	["portugal"] = false,
	["qatar"] = false,
	["romania"] = false,
	["russia"] = false,
	["rwanda"] = false,
	["saint kitts and nevis"] = false,
	["saint lucia"] = false,
	["saint vincent and the grenadines"] = false,
	["samoa"] = false,
	["san marino"] = false,
	["sao tome and principe"] = false,
	["saudi arabia"] = false,
	["senegal"] = false,
	["serbia"] = false,
	["seychelles"] = false,
	["sierra leone"] = false,
	["singapore"] = false,
	["slovakia"] = false,
	["slovenia"] = false,
	["solomon islands"] = true,
	["somalia"] = false,
	["south africa"] = false,
	["south korea"] = false,
	["spain"] = false,
	["sri lanka"] = false,
	["sudan"] = false,
	["suriname"] = false,
	["sweden"] = false,
	["switzerland"] = false,
	["syria"] = false,
	["taiwan"] = false,
	["tajikistan"] = false,
	["tanzania"] = false,
	["thailand"] = false,
	["timor-leste"] = false,
	["togo"] = false,
	["tonga"] = false,
	["trinidad and tobago"] = false,
	["tunisia"] = false,
	["turkey"] = false,
	["turkmenistan"] = false,
	["tuvalu"] = false,
	["uganda"] = false,
	["ukraine"] = false,
	["united arab emirates"] = true,
	["united kingdom"] = true,
	["united states"] = true,
	["uruguay"] = false,
	["uzbekistan"] = false,
	["vanuatu"] = false,
	["vatican city"] = true,
	["venezuela"] = false,
	["vietnam"] = false,
	["yemen"] = false,
	["zambia"] = false,
	["zimbabwe"] = false
}

-- If all strings in the list are identical, returns the string, otherwise
-- returns nil (ignores blank string items, if all strings are blank returns
-- the blank string).
local function sharedPlace(list)
	local result = ""

   	for name in string.gmatch(list, "[^,]+") do
    	name = name:gsub("^%s*(.-)%s*$", "%1")
    	if name ~= "" then
    		if result == "" then
				result = name
			elseif result ~= name then
				return false
			end	
    	end
   	end

	return result
end

-- Switch on the provided country and return the region string. Allows
-- processing and returning a CSV list.
local function getRegion(data)
    if data == "" then return "" end
    
    local result = ""
	if data:match(',') then
		local first = true
		for name in string.gmatch(data, "[^,]+") do
			name = name:gsub("^%s*(.-)%s*$", "%1")
			if not first then
				result = result .. ", "
			else
				first = false
			end
			result = result .. getRegion(name)
		end
		return result
	else
		return REGION_MAP[string.lower(data)]
	end
	return result
end

-- Adds the article "the" to country names that use it (e.g. "the Netherlands").
-- Allows processing and returning a CSV list.
function countryWithArticle(data)
	if data == "" then return "" end
	
	if data:match(',') then
		local result = ""
		local first = true
		for name in string.gmatch(data, "[^,]+") do
			name = name:gsub("^%s*(.-)%s*$", "%1")
			if not first then
				result = result .. ", "
			else
				first = false
			end
			result = result .. countryWithArticle(name)
		end
		return result
	else
		if ARTICLE_MAP[string.lower(data)] then
			return "the " .. data
		else
			return data
		end
	end
end

-- Base function to generate a category for a geographic division given a
-- data string (the name of that division or a CSV list of multiple names
-- (primarily for events taking place in multiple areas).
local function geoCat(data, class, division)
	if data:match(',') then
		-- Query is a list, determine if the items in the list are identical.
		local shared = sharedPlace(data)
		if shared == false then
			-- If they are different, need to render a multi-category (if there
			-- is one for the specified class + division) or each unique
			-- category (if not).
			if CLASS_INFO[class]["multi_category"] ~= nil
					and CLASS_INFO[class]["multi_category"][division] ~= nil then
				return "[[Category:" .. CLASS_INFO[class]["multi_category"][division] .. "]]"
			else
				local result = ""
				local history = {}
				for name in string.gmatch(data, "[^,]+") do
					name = name:gsub("^%s*(.-)%s*$", "%1")
			    	if name ~= "" and not history[name] then
			    		history[name] = true
			    		result = result .. geoCat(name, class, division)
			    	end
				end
				return result
			end
		else
			-- Items in the list match, just render the category for the
			-- shared value.
			data = shared
		end
	end

	if data ~= "" then
		return "[[Category:" .. CLASS_INFO[class]["standard_category"]:gsub("@@@@", data) .. "]]"
	else
		return ""
	end
end

function p.geoCategories(frame)
	local args = frame.args or frame:getParent().args
	local class = string.lower(args["class"])
	local country = args["country"]
	local state_province = args["state_province"]
	local city = args["city"]
	
	local result = ""
	
	if country ~= nil and country ~= "" then
		result = result .. geoCat(countryWithArticle(country), class, "country")
		result = result .. geoCat(getRegion(country), class, "region")
	end
	if state_province ~= nil and state_province ~= "" then
		result = result .. geoCat(state_province, class, "state_province")
	end
	if city ~= nil and city ~= "" then
		result = result .. geoCat(city, class, "city")
	end
	
	return result
end

function p.countryWithArticle(frame)
	local args = frame.args or frame:getParent().args
	return countryWithArticle(args[1])
end

function p.getRegion(frame)
	local args = frame.args or frame:getParent().args
	return getRegion(args[1])
end

return p