Difference between revisions of "Module:Infobox road/locations/sandbox"
Jump to navigation
Jump to search
test>Chinissai (←Created page with 'local p = {} local insert = table.insert local function countries(args, country) local data = {EUR = "no", ASIA = "no", default = "yes"} local yesOrNo = data...') |
m (10 revisions imported) |
||
| (9 intermediate revisions by one other user not shown) | |||
| Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
| + | |||
| + | -- Change to live module upon deployment. | ||
| + | local parserModuleName = "Module:Road data/parser/sandbox" | ||
local insert = table.insert | local insert = table.insert | ||
| + | local format = mw.ustring.format | ||
| − | local function | + | local function ruralMunisLabel(province) |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
if province == "AB" then | if province == "AB" then | ||
| − | + | return "Specialized<br>and rural<br>municipalities:" | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
else | else | ||
| − | return | + | return "Rural<br>municipalities:" |
end | end | ||
end | end | ||
| − | local function | + | local function destinationsLabel(country) |
| − | |||
if country == "GBR" then | if country == "GBR" then | ||
| − | + | return "[[Primary status|Primary<br>destinations]]:" | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
else | else | ||
| − | return | + | return "Primary<br>destinations:" |
end | end | ||
end | end | ||
| − | local function | + | local function lgaLabel(state) |
local labels = { | local labels = { | ||
SA = "[[Local government areas of South Australia|LGA(s)]]", | SA = "[[Local government areas of South Australia|LGA(s)]]", | ||
| Line 130: | Line 34: | ||
ACT = "District(s)" | ACT = "District(s)" | ||
} | } | ||
| − | + | return labels[state] or "[[Local government in Australia|LGA(s)]]" | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
end | end | ||
| − | local function | + | local function locationsLabel(type) |
local labels = { | local labels = { | ||
highway = "Major settlements:", | highway = "Major settlements:", | ||
| Line 150: | Line 46: | ||
street = "Suburb:" | street = "Suburb:" | ||
} | } | ||
| − | local | + | return labels[type] or "Primary<br>destinations:" |
| − | local | + | end |
| − | + | ||
| − | + | local argErrors = {} | |
| − | + | ||
| − | + | local function filter(args, field, result) | |
| − | + | local parserModule = require(parserModuleName) | |
| − | + | local display = parserModule.parser(args, field, args.country, | |
| + | "Module:Infobox road/display") | ||
| + | if display == "yes" then return result end | ||
| + | if result then | ||
| + | insert(argErrors, field .. " parameter not permitted for the region") | ||
end | end | ||
| + | return nil | ||
end | end | ||
| − | local function | + | local function addIfExists(rows, label, data, labelArgs) |
| − | if | + | if data then |
| − | + | if type(label) == "function" then | |
| − | + | label = label(labelArgs) | |
| − | + | end | |
| + | insert(rows, {label = label, data = data}) | ||
end | end | ||
end | end | ||
| − | function | + | local function country(abbr) |
| − | local | + | if not abbr then return nil end |
| − | local | + | local parserModule = require(parserModuleName) |
| + | local spec = parserModule.parser({country = abbr}, "country", " common ") | ||
| + | if spec then | ||
| + | if type(spec) == "string" then | ||
| + | return format("[[%s]]", spec) | ||
| + | end | ||
| + | return format("[[%s|%s]]", spec.link, spec.name) | ||
| + | end | ||
| − | local | + | -- TODO transition |
| + | -- Issue warning? | ||
| + | local frame = mw.getCurrentFrame() | ||
| + | local result = frame:expandTemplate{ | ||
| + | title = "ConvertAbbrev", | ||
| + | args = {"ISO 3166-1", "alpha-3", abbr}} | ||
| + | return result ~= "" and result or nil | ||
| + | end | ||
| − | + | local function state(countryAbbr, abbr, name) | |
| − | local | + | if not abbr then return nil end |
| − | local | + | local parserModule = require(parserModuleName) |
| − | if | + | local spec = parserModule.parser({country = countryAbbr, state = abbr}, |
| − | + | name or "state", " common ") | |
| − | if | + | if spec then |
| − | + | if type(spec) == "string" then | |
| − | + | return format("[[%s]]", spec) | |
| − | |||
| − | |||
end | end | ||
| + | return format("[[%s|%s]]", spec.link, spec.name) | ||
end | end | ||
| − | local | + | |
| − | + | -- TODO transition | |
| − | + | -- Issue warning? | |
| − | + | local frame = mw.getCurrentFrame() | |
| − | + | local country = frame:expandTemplate{ | |
| − | + | title = "ConvertAbbrev", | |
| − | + | args = {"ISO 3166-1", "alpha-3", countryAbbr}} | |
| − | end | + | countryAbbr = frame:expandTemplate{ |
| − | + | title = "ConvertAbbrev", | |
| − | + | args = {"ISO 3166-1", "alpha-2", country}} | |
| − | + | local result = frame:expandTemplate{ | |
| − | + | title = "ConvertAbbrev", | |
| − | + | args = {"ISO 3166-2", countryAbbr, abbr}} | |
| − | + | return result ~= "" and result or nil | |
| − | + | end | |
| − | + | ||
| − | + | function p._locations(args) | |
| − | + | local rows = {} | |
| − | + | ||
| − | + | addIfExists(rows, "Country:", country(args.country)) | |
| − | + | addIfExists(rows, "State:", state(args.country, args.state)) | |
| − | + | addIfExists(rows, "Province:", state(args.country, args.province, "province")) | |
| − | + | addIfExists(rows, "Countries:", filter(args, "countries", args.countries)) | |
| − | + | addIfExists(rows, "Regions:", filter(args, "regions", args.regions)) | |
| − | + | addIfExists(rows, "States:", filter(args, "states", args.states)) | |
| − | + | addIfExists(rows, "Provinces:", filter(args, "provinces", args.provinces)) | |
| − | + | addIfExists(rows, "Counties:", args.counties) | |
| − | + | addIfExists(rows, "Parishes:", args.parishes) | |
| − | + | addIfExists(rows, "Boroughs:", args.boroughs) | |
| − | + | addIfExists(rows, "Municipalities:", args.municipalities) | |
| − | + | addIfExists(rows, "Districts:", filter(args, "districts", args.districts)) | |
| − | + | addIfExists(rows, ruralMunisLabel, | |
| − | + | filter(args, "ruralmuni", args.rural_municipalities), args.province) | |
| − | + | addIfExists(rows, "Divisions:", filter(args, "divisions", args.divisions)) | |
| − | + | addIfExists(rows, "Major cities:", filter(args, "cities", args.cities)) | |
| − | + | addIfExists(rows, "Towns:", filter(args, "cities", args.towns)) | |
| − | + | addIfExists(rows, "Villages:", filter(args, "cities", args.villages)) | |
| − | + | addIfExists(rows, destinationsLabel, filter(args, "destinations", args.destinations), args.country) | |
| − | + | addIfExists(rows, lgaLabel, filter(args, "lga", args.lga), args.state) | |
| − | + | addIfExists(rows, locationsLabel, filter(args, "locations", args.locations), args.type) | |
| − | + | return rows, #argErrors > 0 and table.concat(argErrors) or nil | |
| − | + | end | |
| − | + | ||
| − | + | function p.locations(frame) | |
| − | + | -- Import module function to work with passed arguments | |
| − | + | local getArgs = require('Module:Arguments').getArgs | |
| − | + | local args = getArgs(frame) | |
| − | + | local state = args.state or args.province | |
| − | local | + | if not args.country then |
| − | if | + | local countryMask = mw.loadData("Module:Road data/countrymask") |
| − | + | args.country = countryMask[state] | |
end | end | ||
| − | + | return p._locations(args) | |
| − | return | ||
end | end | ||
return p | return p | ||
Latest revision as of 12:54, 19 September 2020
Documentation for this module may be created at Module:Infobox road/locations/sandbox/doc
local p = {}
-- Change to live module upon deployment.
local parserModuleName = "Module:Road data/parser/sandbox"
local insert = table.insert
local format = mw.ustring.format
local function ruralMunisLabel(province)
if province == "AB" then
return "Specialized<br>and rural<br>municipalities:"
else
return "Rural<br>municipalities:"
end
end
local function destinationsLabel(country)
if country == "GBR" then
return "[[Primary status|Primary<br>destinations]]:"
else
return "Primary<br>destinations:"
end
end
local function lgaLabel(state)
local labels = {
SA = "[[Local government areas of South Australia|LGA(s)]]",
VIC = "[[Local government in Victoria|LGA(s)]]",
NSW = "[[Local government areas of New South Wales|LGA(s)]]",
QLD = "[[Local government areas of Queensland|LGA(s)]]",
NT = "[[Local government areas of the Northern Territory|LGA(s)]]",
WA = "[[Local government areas of Western Australia|LGA(s)]]",
TAS = "[[Local government areas of Tasmania|LGA(s)]]",
ACT = "District(s)"
}
return labels[state] or "[[Local government in Australia|LGA(s)]]"
end
local function locationsLabel(type)
local labels = {
highway = "Major settlements:",
["rural road"] = "Major settlements:",
freeway = "Major suburbs:",
["city highway"] = "Major suburbs:",
road = "Major suburbs:",
street = "Suburb:"
}
return labels[type] or "Primary<br>destinations:"
end
local argErrors = {}
local function filter(args, field, result)
local parserModule = require(parserModuleName)
local display = parserModule.parser(args, field, args.country,
"Module:Infobox road/display")
if display == "yes" then return result end
if result then
insert(argErrors, field .. " parameter not permitted for the region")
end
return nil
end
local function addIfExists(rows, label, data, labelArgs)
if data then
if type(label) == "function" then
label = label(labelArgs)
end
insert(rows, {label = label, data = data})
end
end
local function country(abbr)
if not abbr then return nil end
local parserModule = require(parserModuleName)
local spec = parserModule.parser({country = abbr}, "country", " common ")
if spec then
if type(spec) == "string" then
return format("[[%s]]", spec)
end
return format("[[%s|%s]]", spec.link, spec.name)
end
-- TODO transition
-- Issue warning?
local frame = mw.getCurrentFrame()
local result = frame:expandTemplate{
title = "ConvertAbbrev",
args = {"ISO 3166-1", "alpha-3", abbr}}
return result ~= "" and result or nil
end
local function state(countryAbbr, abbr, name)
if not abbr then return nil end
local parserModule = require(parserModuleName)
local spec = parserModule.parser({country = countryAbbr, state = abbr},
name or "state", " common ")
if spec then
if type(spec) == "string" then
return format("[[%s]]", spec)
end
return format("[[%s|%s]]", spec.link, spec.name)
end
-- TODO transition
-- Issue warning?
local frame = mw.getCurrentFrame()
local country = frame:expandTemplate{
title = "ConvertAbbrev",
args = {"ISO 3166-1", "alpha-3", countryAbbr}}
countryAbbr = frame:expandTemplate{
title = "ConvertAbbrev",
args = {"ISO 3166-1", "alpha-2", country}}
local result = frame:expandTemplate{
title = "ConvertAbbrev",
args = {"ISO 3166-2", countryAbbr, abbr}}
return result ~= "" and result or nil
end
function p._locations(args)
local rows = {}
addIfExists(rows, "Country:", country(args.country))
addIfExists(rows, "State:", state(args.country, args.state))
addIfExists(rows, "Province:", state(args.country, args.province, "province"))
addIfExists(rows, "Countries:", filter(args, "countries", args.countries))
addIfExists(rows, "Regions:", filter(args, "regions", args.regions))
addIfExists(rows, "States:", filter(args, "states", args.states))
addIfExists(rows, "Provinces:", filter(args, "provinces", args.provinces))
addIfExists(rows, "Counties:", args.counties)
addIfExists(rows, "Parishes:", args.parishes)
addIfExists(rows, "Boroughs:", args.boroughs)
addIfExists(rows, "Municipalities:", args.municipalities)
addIfExists(rows, "Districts:", filter(args, "districts", args.districts))
addIfExists(rows, ruralMunisLabel,
filter(args, "ruralmuni", args.rural_municipalities), args.province)
addIfExists(rows, "Divisions:", filter(args, "divisions", args.divisions))
addIfExists(rows, "Major cities:", filter(args, "cities", args.cities))
addIfExists(rows, "Towns:", filter(args, "cities", args.towns))
addIfExists(rows, "Villages:", filter(args, "cities", args.villages))
addIfExists(rows, destinationsLabel, filter(args, "destinations", args.destinations), args.country)
addIfExists(rows, lgaLabel, filter(args, "lga", args.lga), args.state)
addIfExists(rows, locationsLabel, filter(args, "locations", args.locations), args.type)
return rows, #argErrors > 0 and table.concat(argErrors) or nil
end
function p.locations(frame)
-- Import module function to work with passed arguments
local getArgs = require('Module:Arguments').getArgs
local args = getArgs(frame)
local state = args.state or args.province
if not args.country then
local countryMask = mw.loadData("Module:Road data/countrymask")
args.country = countryMask[state]
end
return p._locations(args)
end
return p