Module:Region: Difference between revisions

From Zelda Wiki, the Zelda encyclopedia
Jump to navigation Jump to search
(Created page with "local p = {} local Data = mw.loadData("Module:Region/Data") local CATEGORY_INVALID_ARGS = "Category:"..require("Module:Constants/category/invalidArgs").."" local CLASS_TOOLTIP = require("Module:Constants/class/tooltip") function p.flag(code, size) size = size or Data.defaultFlagSize local region = Data.regions[code] if not region then return nil, CATEGORY_INVALID_ARGS elseif region.flag then local name = region.official or region.name local flagFile = reg...")
 
No edit summary
Line 2: Line 2:
local Data = mw.loadData("Module:Region/Data")
local Data = mw.loadData("Module:Region/Data")


local DEFAULT_FLAG_SIZE = "20px"
local CATEGORY_INVALID_ARGS = "[[Category:"..require("Module:Constants/category/invalidArgs").."]]"
local CATEGORY_INVALID_ARGS = "[[Category:"..require("Module:Constants/category/invalidArgs").."]]"
local CLASS_TOOLTIP = require("Module:Constants/class/tooltip")
local CLASS_TOOLTIP = require("Module:Constants/class/tooltip")


function p.flag(code, size)
function p.flag(code, size)
size = size or Data.defaultFlagSize
size = size or DEFAULT_FLAG_SIZE
local region = Data.regions[code]
local region = Data.regions[code]
if not region then
if not region then
Line 22: Line 23:
return flags, nil
return flags, nil
else
else
error(string.format("No flags associated to region <code>%s</code>", code))
return "", nil
end
end
end
function p.Schemas(frame)
return {
Data = {
type = "record",
required = true,
properties = {
{
name = "regions",
required = true,
type = "map",
desc = "A list of regions that Nintendo (or other Zelda-related companies) markets to.",
keyPlaceholder = "code",
keys = {
type = "string",
desc = "An {{Wp|ISO 3166-1 alpha-2}} country code, or a 3+ character code denoting a multi-country region forming a single market segment.",
},
values = {
type = "record",
properties = {
{
name = "name",
required = true,
type = "string",
desc = "The name of the country or region.",
},
{
name = "official",
type = "string",
desc = "The official state name of the country. Defaults to <code>name</code>.",
},
{
name = "flag",
type = "string",
desc = "The file name of the region's flag",
},
{
name = "countries",
type = "array",
items = { type = "string" },
desc = "For multi-country regions, a list of country codes that comprise the region.",
},
},
},
},
},
}
}
end
end



Revision as of 18:55, 30 October 2022

The source of truth for world region data relevant to The Legend of Zelda series.


local p = {}
local Data = mw.loadData("Module:Region/Data")

local DEFAULT_FLAG_SIZE = "20px"
local CATEGORY_INVALID_ARGS = "[[Category:"..require("Module:Constants/category/invalidArgs").."]]"
local CLASS_TOOLTIP = require("Module:Constants/class/tooltip")

function p.flag(code, size)
	size = size or DEFAULT_FLAG_SIZE
	local region = Data.regions[code]
	if not region then
		return nil, CATEGORY_INVALID_ARGS
	elseif region.flag then
		local name = region.official or region.name
		local flagFile = region.flag
		local flag = string.format('<span class="%s" title="%s">[[%s|%s|%s]]</span>', CLASS_TOOLTIP, name, flagFile, size, name)
		return flag, nil
	elseif region.countries then
		local flags = "" 
		for i, country in ipairs(region.countries) do
			flags = flags..p.flag(country, size)
		end
		return flags, nil
	else
		return "", nil
	end
end

function p.Schemas(frame)
	return {
		Data = {
			type = "record",
			required = true,
			properties = {
				{
					name = "regions",
					required = true,
					type = "map",
					desc = "A list of regions that Nintendo (or other Zelda-related companies) markets to.",
					keyPlaceholder = "code",
					keys = {
						type = "string",
						desc = "An {{Wp|ISO 3166-1 alpha-2}} country code, or a 3+ character code denoting a multi-country region forming a single market segment.",
					},
					values = {
						type = "record",
						properties = {
							{
								name = "name",
								required = true,
								type = "string",
								desc = "The name of the country or region.",
							},
							{
								name = "official",
								type = "string",
								desc = "The official state name of the country. Defaults to <code>name</code>.",
							},
							{
								name = "flag",
								type = "string",
								desc = "The file name of the region's flag",
							},
							{
								name = "countries",
								type = "array",
								items = { type = "string" },
								desc = "For multi-country regions, a list of country codes that comprise the region.",
							},
						},
					},
				},
			},
		}
	}
end

function p.Data(frame)
	local utilsLayout = require("Module:UtilsLayout")
	local utilsTable = require("Module:UtilsTable")

	local countries = {}
	local multiCountryRegions = {}
	
	for code, region in pairs(Data.regions) do
		local row = {
			id = code,
			cells = {
				"<code>"..code.."</code>",
				p.flag(code),
				region.name,
			}
		}
		if code:len() == 2 then
			table.insert(countries, row)
		else
			table.insert(multiCountryRegions, row)
		end
	end
	countries = utilsTable.sortBy(countries, "id")
	multiCountryRegions = utilsTable.sortBy(multiCountryRegions, "id")
	
	
	local countriesTable = utilsLayout.table({
		caption = "Countries",
		sortable = {1, 3},
		headers = {"{{Wp|ISO 3166-1 alpha-2|Code}}", "Flag", "Name"},
		rows = countries,
	})
	local multiCountryRegionsTable = utilsLayout.table({
		caption = "Multi-Country Regions",
		sortable = {1, 3},
		headers = {"Code", "Flags", "Name"},
		rows = multiCountryRegions,
	})

	local html = mw.html.create("div")
		:css({
			["display"] = "flex",
			["flex-wrap"] = "wrap",
			["gap"] = "1rem",
		})
		:wikitext(countriesTable)
		:wikitext(multiCountryRegionsTable)

	return tostring(html)
end

return p