Module:CountryData: Difference between revisions
// via Wikitext Extension for VSCode Tag: Reverted |
// via Wikitext Extension for VSCode |
||
| (4 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
-- | --[[ | ||
* Name: CountryData | |||
* Author: Mark W. Datysgeld | |||
* Description: Unified module for country data management with JSON loading, normalization, region mapping, and Semantic MediaWiki integration | |||
* Notes: Loads from Data:CountryDataset.json; normalizes country names to canonical forms; maps countries to ICANN regions; provides extensible property access; formats country lists with region-specific emoji styling; processes countries for category assignment | |||
]] | |||
-- Dependencies | -- Dependencies | ||
| Line 15: | Line 10: | ||
local NormalizationText = require('Module:NormalizationText') | local NormalizationText = require('Module:NormalizationText') | ||
local loader = require('Module:DatasetLoader') | local loader = require('Module:DatasetLoader') | ||
-- Module-level cache tables for improved performance | -- Module-level cache tables for improved performance | ||
| Line 278: | Line 272: | ||
end | end | ||
function CountryData.getRegionByCountry(name | function CountryData.getRegionByCountry(name) | ||
if not name or name == "" then | if not name or name == "" then | ||
return "(Unrecognized)" | return "(Unrecognized)" | ||
| Line 293: | Line 287: | ||
if country and country.icann_region then | if country and country.icann_region then | ||
result = country.icann_region | result = country.icann_region | ||
else | else | ||
result = "(Unrecognized)" | result = "(Unrecognized)" | ||
| Line 458: | Line 444: | ||
functionCache[cacheKey] = properties | functionCache[cacheKey] = properties | ||
return properties | return properties | ||
end | end | ||
| Line 478: | Line 452: | ||
end | end | ||
local ConfigRepository = require('Module:ConfigRepository') | |||
local | |||
local countryPropertyName = ConfigRepository.getSemanticPropertyName("Has country") | local countryPropertyName = ConfigRepository.getSemanticPropertyName("Has country") | ||
local regionPropertyName = ConfigRepository.getSemanticPropertyName("Has ICANN region") | local regionPropertyName = ConfigRepository.getSemanticPropertyName("Has ICANN region") | ||
| Line 550: | Line 518: | ||
function CountryData.formatCountryList(value) | function CountryData.formatCountryList(value) | ||
if not value or value == "" then return "" end | if not value or value == "" then return "" end | ||
local | local ListGeneration = require('Module:ListGeneration') | ||
for | local itemsToProcess = {} | ||
-- First, check if the entire string is a single, valid country. | |||
-- This correctly handles names like "Trinidad and Tobago". | |||
local singleCountryName = CountryData.normalizeCountryName(value) | |||
if singleCountryName ~= "(Unrecognized)" then | |||
-- If it's a valid country, treat it as a single item. | |||
table.insert(itemsToProcess, value) | |||
else | |||
-- If not a single country, assume it's a list and split ONLY by semicolon. | |||
-- This is safer than letting ListGeneration guess the delimiter. | |||
for item in string.gmatch(value, "[^;]+") do | |||
local trimmed = item:match("^%s*(.-)%s*$") | |||
if trimmed and trimmed ~= "" then | |||
table.insert(itemsToProcess, trimmed) | |||
end | |||
end | end | ||
end | end | ||
local | -- Define the item hook for country-specific formatting | ||
local function countryItemHook(countryName) | |||
local normalized = CountryData.normalizeCountryName(countryName) | local normalized = CountryData.normalizeCountryName(countryName) | ||
if normalized ~= "(Unrecognized)" then | if normalized ~= "(Unrecognized)" then | ||
local countryRegion = CountryData.getRegionByCountry(normalized) | local countryRegion = CountryData.getRegionByCountry(normalized) | ||
-- Return a table with content and class for the li element | |||
return { | |||
content = normalized, | |||
class = getRegionClass(countryRegion) | |||
} | |||
end | end | ||
return nil -- Exclude unrecognized countries from the list | |||
end | end | ||
-- Set the options for the list generation | |||
local options = { | |||
mode = 'bullet', | |||
listClass = 'template-list-country', | |||
return | itemHook = countryItemHook | ||
} | |||
-- Pass the pre-processed table of items to the list generator. | |||
return ListGeneration.createList(itemsToProcess, options) | |||
end | end | ||