Module:Geography: Difference between revisions

From stencil.wiki
No edit summary
No edit summary
Line 1: Line 1:
local p = {}
local p = {}


function p.countryCategory(frame)
local CLASS_INFO = {
-- Generate country categories (and optional regional category) based on
place = {
-- the passed class and country. Can provide a single country name or a
standard_category = "Places in @@@@"
-- list (in which case if they are identical, their shared value is used,
}, event = {
-- otherwise "International _____"). If a blank string is sent no
standard_category = "Events in @@@@",
-- categories are rendered.
multi_category = {
country = "International events",
state_province = "Multi-state/province events",
city = "Multi-city events"
}
}
}
 
function p.geoCategories(frame)
local args = frame.args or frame:getParent().args
local args = frame.args or frame:getParent().args
local class = args[1]
local class = args["class"]
local country = args[2]:gsub("^%s*(.-)%s*$", "%1")
local country = args["country"]
local regional = args[region]
local state_province = args["state_province"]
local city = args["city"]
local regional = args["region"]
local result = ""
local result = ""
local international = false
if country ~= nil then
result = result .. geographicCategory(countryWithArticle(country), class, "country")
if country:match(',') then
end
local list = {}
if state_province ~= nil then
for name in string.gmatch(country, "[^,]+") do
result = result .. geographicCategory(state_province, class, "state_province")
    name = name:gsub("^%s*(.-)%s*$", "%1")
end
insert(list, name)
if city ~= nil then
end
result = result .. geographicCategory(city, class, "city")
country = sharedPlace(list)
end
if country == nil then international = true end
if regional and country ~= nil then
result = result .. geographicCategory(getRegion(country), class, "region")
end
end
if country == "" then
return result
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
end
function p.stateProvinceCategory(frame) end
function p.cityCategory(frame) end


-- DEPRECATED
-- DEPRECATED
Line 66: Line 59:
-- the blank string).
-- the blank string).
local function sharedPlace(list)
local function sharedPlace(list)
local result = nil
local result = ""
     local flag = true
     if type(list) == string then
    for name in list do
    for name in string.gmatch(list, "[^,]+") do
    -- name = name:gsub("^%s*(.-)%s*$", "%1")
    name = name:gsub("^%s*(.-)%s*$", "%1")
if result == nil then
    if name ~= "" then
result = name
    if result == "" then
elseif result ~= name and name ~= "" then
result = name
flag = false
elseif result ~= name then
return false
end
    end
end
end
    elseif type(list) == table then
    for name in list do
if name ~= "" then
    if result == "" then
result = name
elseif result ~= name then
return false
end
    end
    end
end
end
if flag then return result else return nil end
return result
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).
function geographicCategory(data, class, division)
if data == "" then return "" end
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 differnt, 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(list, "[^,]+") do
name = name:gsub("^%s*(.-)%s*$", "%1")
    if name ~= "" and not history[name] then
    history[name] = true
    result = result .. geographicCategory(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
 
return "[[Category:" .. CLASS_INFO[class]["standard_category"].gsub("@@@@", data) .. "]]"
end
end


function getRegion(country)
function getRegion(country)
     -- Switch on the provided country and return the region string.
     -- Switch on the provided country and return the region string.
end
function countryWithArticle(country)
-- Adds the article "the" to country names that use it
-- (e.g. "the Netherlands").
end
end


return p
return p

Revision as of 03:09, 11 January 2025

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"
		}
	}
}

function p.geoCategories(frame)
	local args = frame.args or frame:getParent().args
	local class = args["class"]
	local country = args["country"]
	local state_province = args["state_province"]
	local city = args["city"]
	local regional = args["region"]
	
	local result = ""
	if country ~= nil then
		result = result .. geographicCategory(countryWithArticle(country), class, "country")
	end
	if state_province ~= nil then
		result = result .. geographicCategory(state_province, class, "state_province")
	end
	if city ~= nil then
		result = result .. geographicCategory(city, class, "city")
	end
	if regional and country ~= nil then
		result = result .. geographicCategory(getRegion(country), class, "region")
	end
	
	return result
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 = ""
    if type(list) == string then
    	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
    elseif type(list) == table then
	    for name in list do
			if name ~= "" then
	    		if result == "" then
					result = name
				elseif result ~= name then
					return false
				end	
	    	end
	    end
	end
	return result
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).
function geographicCategory(data, class, division)
	if data == "" then return "" end
	
	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 differnt, 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(list, "[^,]+") do
					name = name:gsub("^%s*(.-)%s*$", "%1")
			    	if name ~= "" and not history[name] then
			    		history[name] = true
			    		result = result .. geographicCategory(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

	return "[[Category:" .. CLASS_INFO[class]["standard_category"].gsub("@@@@", data) .. "]]"
end

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

function countryWithArticle(country)
	-- Adds the article "the" to country names that use it
	-- (e.g. "the Netherlands").
end

return p