Module:Region: Difference between revisions
Jump to navigation
Jump to search
no edit summary
PhantomCaleb (talk | contribs) No edit summary |
PhantomCaleb (talk | contribs) No edit summary |
||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
local h = {} | |||
local Data = mw.loadData("Module:Region/Data") | local Data = mw.loadData("Module:Region/Data") | ||
Line 6: | Line 7: | ||
local CLASS_TOOLTIP = require("Module:Constants/class/tooltip") | local CLASS_TOOLTIP = require("Module:Constants/class/tooltip") | ||
function p. | function p.getRegion(code, options) | ||
options = options or {} | |||
options.flagSize = options.flagSize or DEFAULT_FLAG_SIZE | |||
local region = Data.regions[code] | local region = Data.regions[code] | ||
if not region then | |||
local utilsError = require("Module:UtilsError") | |||
utilsError.warn(string.format("Invalid region <code>%s</code>. See [[Module:Region/Data]] for a list of supported regions.", code)) | |||
return nil, CATEGORY_INVALID_ARGS | |||
else | |||
return { | |||
name = region.name, | |||
flag = h.flag(region, options), | |||
} | |||
end | |||
end | |||
function h.flag(region, options) | |||
if not region then | if not region then | ||
return nil, CATEGORY_INVALID_ARGS | return nil, CATEGORY_INVALID_ARGS | ||
Line 14: | Line 30: | ||
local name = region.official or region.name | local name = region.official or region.name | ||
local flagFile = region.flag | local flagFile = region.flag | ||
local size = options.flagSize | |||
local flag = string.format('<span class="%s" title="%s">[[%s|%s|%s]]</span>', CLASS_TOOLTIP, name, flagFile, size, name) | local flag = string.format('<span class="%s" title="%s">[[%s|%s|%s]]</span>', CLASS_TOOLTIP, name, flagFile, size, name) | ||
return flag, nil | return flag, nil | ||
Line 19: | Line 36: | ||
local flags = "" | local flags = "" | ||
for i, country in ipairs(region.countries) do | for i, country in ipairs(region.countries) do | ||
flags = flags..p. | flags = flags..p.getRegion(country).flag | ||
end | end | ||
return flags, nil | return flags, nil | ||
Line 25: | Line 42: | ||
return "", nil | return "", nil | ||
end | end | ||
end | |||
function p.Documentation(frame) | |||
return { | |||
getRegion = { | |||
params = {"code", "options"}, | |||
returns = { | |||
"An object containing the region's name and flag(s), or <code>nil</code> if no region exists for the given code.", | |||
"An error category if no region exists for the given code.", | |||
}, | |||
cases = { | |||
outputOnly = true, | |||
{ | |||
args = {"ca"}, | |||
expect = { | |||
{ | |||
name = "Canada", | |||
flag = '<span class="tooltip" title="Canada">[[File:Canada Flag.png|20px|Canada]]</span>', | |||
}, | |||
nil, | |||
}, | |||
}, | |||
{ | |||
args = {"cn", { flagSize = "15px" } }, | |||
expect = { | |||
{ | |||
name = "China", | |||
flag = "<span class=\"tooltip\" title=\"The People's Republic of China\">[[File:China Flag.png|15px|The People's Republic of China]]</span>", | |||
}, | |||
nil, | |||
}, | |||
}, | |||
{ | |||
args = {"eur"}, | |||
expect = { | |||
{ | |||
name = "Europe", | |||
flag = '<span class="tooltip" title="Europe">[[File:European Union Flag.png|20px|Europe]]</span>', | |||
}, | |||
nil | |||
}, | |||
}, | |||
{ | |||
args = {"thm"}, | |||
expect = { | |||
{ | |||
name = "Taiwan, Hong Kong, Macao", | |||
flag = '<span class="tooltip" title="The Republic of China">[[File:Taiwan Flag.svg|20px|The Republic of China]]</span><span class="tooltip" title="The Hong Kong Special Administrative Region of China">[[File:Hong Kong Flag.svg|20px|The Hong Kong Special Administrative Region of China]]</span><span class="tooltip" title="The Macao Special Administrative Region of China">[[File:Macao Flag.svg|20px|The Macao Special Administrative Region of China]]</span>', | |||
}, | |||
nil, | |||
}, | |||
}, | |||
{ | |||
args = {"narnia"}, | |||
expect = { | |||
nil, | |||
CATEGORY_INVALID_ARGS, | |||
}, | |||
}, | |||
}, | |||
}, | |||
} | |||
end | end | ||
function p.Schemas(frame) | function p.Schemas(frame) | ||
return { | return { | ||
getRegion = { | |||
code = { | |||
required = true, | |||
type = "string", | |||
desc = "A region code from [[Module:Region/Data]]." | |||
}, | |||
options = { | |||
type = "record", | |||
properties = { | |||
{ | |||
name = "flagSize", | |||
type = "string", | |||
default = DEFAULT_FLAG_SIZE, | |||
desc = "The image size to use for the region's flag(s).", | |||
}, | |||
}, | |||
}, | |||
}, | |||
Data = { | Data = { | ||
type = "record", | type = "record", | ||
Line 83: | Line 180: | ||
local multiCountryRegions = {} | local multiCountryRegions = {} | ||
for code | for code in pairs(Data.regions) do | ||
local region = p.getRegion(code) | |||
local row = { | local row = { | ||
id = code, | id = code, | ||
cells = { | cells = { | ||
"<code>"..code.."</code>", | "<code>"..code.."</code>", | ||
region.flag, | |||
region.name, | region.name, | ||
} | } |