Module:Geography

From stencil.wiki
Revision as of 03:34, 4 January 2025 by Robertbaxter (talk | contribs)

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

local p = {}

function p.countryCategory(frame)
	-- Generate country categories (and optional regional category) based on
	-- the passed class and country. Can provide a single country name or a
	-- list (in which case if they are identical, their shared value is used,
	-- otherwise "International _____"). If a blank string is sent no 
	-- categories are rendered.
	local args = frame.args or frame:getParent().args
	local class = args[1]
	local country = args[2]:gsub("^%s*(.-)%s*$", "%1")
	local regional = args[region]
	
	local result = ""
	local international = false
	
	if country:match(',') then
		local list = {}
		for name in string.gmatch(country, "[^,]+") do
	    	name = name:gsub("^%s*(.-)%s*$", "%1")
			insert(list, name)
		end
		country = sharedPlace(list)
		if country == nil then international = true end
	end
	
	if country == "" then
		return ""
	elseif international then
		return "[[Category:" .. glossary[class]["international"] .. "]]"
	else
		result = result .. "[[Category:"
			.. glossary[class]["category"] .. " " .. country .. "]]"
		if regional then
			local region = getRegion(country)
			if region ~= nil and region ~= "" then
				result = result .. "[[Category:"
					.. glossary[class]["category"] .. " " .. region .. "]]"
			end
		end
		return result
	end
end

function p.stateProvinceCategory(frame) end
function p.cityCategory(frame) end

-- DEPRECATED
function p.sharedPlace(frame)
    local args = frame.args or frame:getParent().args
    local a = ""
    local b = true
    for name in string.gmatch(args[1], "[^,]+") do
    	name = name:gsub("^%s*(.-)%s*$", "%1")
		if a == "" then
			a = name
		elseif a ~= name then
			b = false
		end
	end
	if b then return a else return "" end
end

-- 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 = nil
    local flag = true
    for name in list do
    	-- name = name:gsub("^%s*(.-)%s*$", "%1")
		if result == nil then
			result = name
		elseif result ~= name and name ~= "" then
			flag = false
		end
	end
	if flag then return result else return nil end
end

function getRegion(country)
    -- Switch on the provided country and return the region string.
end

return p