Module:CountryData: Difference between revisions

// via Wikitext Extension for VSCode
// via Wikitext Extension for VSCode
Line 824: Line 824:
     -- Fallback to simple string if JSON encoding fails
     -- Fallback to simple string if JSON encoding fails
     return '{}'
     return '{}'
end
--------------------------------------------------------------------------------
-- Country Display Functions (Migrated from MultiCountryDisplay)
--------------------------------------------------------------------------------
-- Get region-specific CSS class for country display
local function getRegionClass(region)
    if not region or region == "(Unrecognized)" then
        return "region-default"
    end
   
    if region == "NA" or region == "LAC" then
        return "region-americas"
    elseif region == "AP" then
        return "region-asia-pacific"
    else
        return "region-europe-africa"
    end
end
-- Format a list of countries from a semicolon-separated string
-- Returns either plain text (single country) or bullet points (multiple countries)
-- Each country gets its own region-specific class for styling
function CountryData.formatCountryList(value)
    if not value or value == "" then return "" end
   
    -- Split and normalize countries
    local countries = {}
    for country in string.gmatch(value, "[^;]+") do
        local trimmed = country:match("^%s*(.-)%s*$")
        if trimmed and trimmed ~= "" then
            table.insert(countries, trimmed)
        end
    end
   
    local normalizedCountries = {}
    local validCountriesCount = 0
   
    for _, country in ipairs(countries) do
        local normalized = CountryData.normalizeCountryName(country)
        -- Only include recognized countries
        if normalized ~= "(Unrecognized)" then
            validCountriesCount = validCountriesCount + 1
            normalizedCountries[validCountriesCount] = normalized
        end
    end
   
    -- Generate output based on number of countries
    if validCountriesCount > 1 then
        local listItems = {}
       
        for _, country in ipairs(normalizedCountries) do
            -- Get the region for this specific country
            local countryRegion = CountryData.getRegionByCountry(country)
            local regionClass = getRegionClass(countryRegion)
           
            -- Create a list item with region-specific class
            table.insert(listItems, string.format("<li class=\"%s\">%s</li>", regionClass, country))
        end
       
        return string.format("<ul class=\"template-list template-list-country\">%s</ul>",
                            table.concat(listItems, ""))
    elseif validCountriesCount == 1 then
        -- For a single country, create a similar list with just one item
        local countryRegion = CountryData.getRegionByCountry(normalizedCountries[1])
        local regionClass = getRegionClass(countryRegion)
       
        -- Single item list with the same styling
        return string.format("<ul class=\"template-list template-list-country\"><li class=\"%s\">%s</li></ul>",
                            regionClass, normalizedCountries[1])
    end
   
    return ""
end
-- Alias for backward compatibility
function CountryData.formatCountries(value)
    return CountryData.formatCountryList(value)
end
-- Get a list of normalized countries for category assignment
function CountryData.getCountriesForCategories(value)
    if not value or value == "" then return {} end
   
    local countries = {}
    for country in string.gmatch(value, "[^;]+") do
        local trimmed = country:match("^%s*(.-)%s*$")
        if trimmed and trimmed ~= "" then
            table.insert(countries, trimmed)
        end
    end
   
    local normalizedCountries = {}
    local validCount = 0
   
    for _, country in ipairs(countries) do
        local normalized = CountryData.normalizeCountryName(country)
        -- Only include recognized countries
        if normalized ~= "(Unrecognized)" then
            validCount = validCount + 1
            normalizedCountries[validCount] = normalized
        end
    end
   
    return normalizedCountries
end
end


-- Return the module for use
-- Return the module for use
return CountryData
return CountryData