Difference between revisions of "Module:Infobox road/locations/sandbox"

From blackwiki
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 countries(args, country)
+
local function ruralMunisLabel(province)
local data = {EUR = "no", ASIA = "no", default = "yes"}
 
local yesOrNo = data[country] or data.default
 
if yesOrNo == "no" then
 
return args.countries
 
else
 
return nil
 
end
 
end
 
 
 
local function regions(args, country)
 
local data = {EUR = "no", ESP = "no", ITA = "no", HRV = "no", CZE = "no", CAN = "no", CYP = "no", GRC = "no", POL = "no", HUN = "no",
 
SVN = "no", SVK = "no", AUT = "no", TUR = "no", UKR = "no", BIH = "no", SRB = "no", SGP = "no", AUS = "no", default = "yes"}
 
local yesOrNo = data[country] or data.default
 
if yesOrNo == "no" then
 
return args.regions
 
else
 
return nil
 
end
 
end
 
 
 
local function states(args, country)
 
local data = {USA = "no", IND = "no", DEU = "no", AUT = "no", MEX = "no", AUS = "no", default = "yes"}
 
local yesOrNo = data[country] or data.default
 
if yesOrNo == "no" then
 
return args.states
 
else
 
return nil
 
end
 
end
 
 
 
local function provinces(args, country)
 
local data = {TUR = "no", THA = "no", IRN = "no", NLD = "no", ESP = "no", default = "yes"}
 
local yesOrNo = data[country] or data.default
 
if yesOrNo == "no" then
 
return args.provinces
 
else
 
return nil
 
end
 
end
 
 
 
local function counties(args, country)
 
local counties = args.counties
 
local districts = args.districts
 
local municipalities = args.municipalities
 
local parishes = args.parishes
 
local boroughs = args.boroughs
 
if counties then
 
return "Counties:", counties
 
elseif districts then
 
return "Districts:", districts
 
elseif municipalities then
 
return "Municipalities:", municipalities
 
elseif parishes then
 
return "Parishes:", parishes
 
elseif boroughs then
 
return "Boroughs:", boroughs
 
else
 
return '', nil
 
end
 
end
 
 
 
local function ruralMunis(args, country)
 
local label
 
local province = country or ''
 
 
if province == "AB" then
 
if province == "AB" then
label = "Specialized<br>and rural<br>municipalities:"
+
return "Specialized<br>and rural<br>municipalities:"
else
 
label = "Rural<br>municipalities:"
 
end
 
local data = {MB = "no", SK = "no", AB = "no", default = "yes"}
 
local yesOrNo = data[province] or data.default
 
if yesOrNo == "no" then
 
return label, args.rural_municipalities
 
else
 
return '', nil
 
end
 
end
 
 
 
local function divisions(args, country)
 
local province = country or ''
 
local data = {ON = "no", default = "yes"}
 
local yesOrNo = data[province] or data.default
 
if yesOrNo == "no" then
 
return args.divisions
 
else
 
return nil
 
end
 
end
 
 
 
local function cities(args, country, parameter)
 
local data = {USA = "yes", default = "no"}
 
local yesOrNo = data[country] or data.default
 
if yesOrNo == "no" then
 
return args[parameter]
 
 
else
 
else
return nil
+
return "Rural<br>municipalities:"
 
end
 
end
 
end
 
end
  
local function destinations(args, country)
+
local function destinationsLabel(country)
local label
 
 
if country == "GBR" then
 
if country == "GBR" then
label = "[[Primary status|Primary<br>destinations]]:"
+
return "[[Primary status|Primary<br>destinations]]:"
else
 
label = "Primary<br>destinations:"
 
end
 
local data = {AUS = "no", NZL = "no", GBR = "no", IRL = "no", MYS = "no", IND = "no", NPL = "no", default = "yes"}
 
local yesOrNo = data[country] or data.default
 
if yesOrNo == "no" then
 
return label, args.destinations
 
 
else
 
else
return '', nil
+
return "Primary<br>destinations:"
 
end
 
end
 
end
 
end
  
local function lga(args, country)
+
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)"
 
}
 
}
local data = {AUS = "no", default = "yes"}
+
return labels[state] or "[[Local government in Australia|LGA(s)]]"
local yesOrNo = data[country] or data.default
 
if yesOrNo == "no" then
 
local state = args.state or ''
 
local label = labels[state] or "[[Local government in Australia|LGA(s)]]"
 
return label, args.lga
 
else
 
return '', nil
 
end
 
 
end
 
end
  
local function locations(args, country)
+
local function locationsLabel(type)
 
local labels = {
 
local labels = {
 
highway = "Major settlements:",
 
highway = "Major settlements:",
Line 150: Line 46:
 
street = "Suburb:"
 
street = "Suburb:"
 
}
 
}
local data = {AUS = "no", default = "yes"}
+
return labels[type] or "Primary<br>destinations:"
local yesOrNo = data[country] or data.default
+
end
if yesOrNo == "no" then
+
 
local type = args.type or ''
+
local argErrors = {}
local label = labels[type] or "Primary<br>destinations:"
+
 
return label, args.locations
+
local function filter(args, field, result)
else
+
local parserModule = require(parserModuleName)
return '', nil
+
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 emptyParam(param)
+
local function addIfExists(rows, label, data, labelArgs)
if param == '' then
+
if data then
return nil
+
if type(label) == "function" then
else
+
label = label(labelArgs)
return param
+
end
 +
insert(rows, {label = label, data = data})
 
end
 
end
 
end
 
end
  
function p.locations(frame)
+
local function country(abbr)
local pframe = frame:getParent()
+
if not abbr then return nil end
local args = pframe.args -- the arguments passed TO the template, in the wikitext that transcludes the template
+
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 data = {}
+
-- 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 country = emptyParam(args.country)
+
local function state(countryAbbr, abbr, name)
local state = emptyParam(args.state)
+
if not abbr then return nil end
local province = emptyParam(args.province)
+
local parserModule = require(parserModuleName)
if not(country) then
+
local spec = parserModule.parser({country = countryAbbr, state = abbr},
local stateParam = args.state or args.province
+
name or "state", " common ")
if not(stateParam) then
+
if spec then
country = ''
+
if type(spec) == "string" then
else
+
return format("[[%s]]", spec)
local countryMask = require "Module:Infobox road/meta/mask/country"
 
country = countryMask._country(stateParam, country)
 
 
end
 
end
 +
return format("[[%s|%s]]", spec.link, spec.name)
 
end
 
end
local countries_ = countries(args, country)
+
 
if countries_ and countries_ ~= "" then
+
-- TODO transition
insert(data, {label = "Countries:", data = countries_})
+
-- Issue warning?
end
+
local frame = mw.getCurrentFrame()
local regions_ = regions(args, country)
+
local country = frame:expandTemplate{
if regions_ and regions_ ~= "" then
+
title = "ConvertAbbrev",
insert(data, {label = "Regions:", data = regions_})
+
args = {"ISO 3166-1", "alpha-3", countryAbbr}}
end
+
countryAbbr = frame:expandTemplate{
local states_ = states(args, country)
+
title = "ConvertAbbrev",
if states_ and states_ ~= "" then
+
args = {"ISO 3166-1", "alpha-2", country}}
insert(data, {label = "States:", data = states_})
+
local result = frame:expandTemplate{
end
+
title = "ConvertAbbrev",
local provinces_ = provinces(args, country)
+
args = {"ISO 3166-2", countryAbbr, abbr}}
if provinces_ and provinces_ ~= "" then
+
return result ~= "" and result or nil
insert(data, {label = "Provinces:", data = provinces_})
+
end
end
+
 
local countiesLabel, counties_ = counties(args, country)
+
function p._locations(args)
if counties_ and counties_ ~= "" then
+
local rows = {}
insert(data, {label = countiesLabel, data = counties_})
+
 
end
+
addIfExists(rows, "Country:", country(args.country))
local ruralMunisLabel, ruralMunis__ = ruralMunis(args, country)
+
addIfExists(rows, "State:", state(args.country, args.state))
if ruralMunis__ and ruralMunis__ ~= "" then
+
addIfExists(rows, "Province:", state(args.country, args.province, "province"))
insert(data, {label = ruralMunisLabel, data = ruralMunis__})
+
addIfExists(rows, "Countries:", filter(args, "countries", args.countries))
end
+
addIfExists(rows, "Regions:", filter(args, "regions", args.regions))
local divisions = divisions(args, country)
+
addIfExists(rows, "States:", filter(args, "states", args.states))
if divisions and divisions ~= "" then
+
addIfExists(rows, "Provinces:", filter(args, "provinces", args.provinces))
insert(data, {label = "Divisions:", data = divisions})
+
addIfExists(rows, "Counties:", args.counties)
end
+
addIfExists(rows, "Parishes:", args.parishes)
local cities_ = cities(args, country, "cities")
+
addIfExists(rows, "Boroughs:", args.boroughs)
if cities_ and cities_ ~= "" then
+
addIfExists(rows, "Municipalities:", args.municipalities)
insert(data, {label = "Major cities:", data = cities_})
+
addIfExists(rows, "Districts:", filter(args, "districts", args.districts))
end
+
addIfExists(rows, ruralMunisLabel,
local towns_ = cities(args, country, "towns")
+
filter(args, "ruralmuni", args.rural_municipalities), args.province)
if towns_ and towns_ ~= "" then
+
addIfExists(rows, "Divisions:", filter(args, "divisions", args.divisions))
insert(data, {label = "Towns:", data = towns_})
+
addIfExists(rows, "Major cities:", filter(args, "cities", args.cities))
end
+
addIfExists(rows, "Towns:", filter(args, "cities", args.towns))
local villages_ = cities(args, country, "villages")
+
addIfExists(rows, "Villages:", filter(args, "cities", args.villages))
if villages_ and villages_ ~= "" then
+
addIfExists(rows, destinationsLabel, filter(args, "destinations", args.destinations), args.country)
insert(data, {label = "Villages:", data = villages_})
+
addIfExists(rows, lgaLabel, filter(args, "lga", args.lga), args.state)
end
+
addIfExists(rows, locationsLabel, filter(args, "locations", args.locations), args.type)
local destinationsLabel, destinations__ = destinations(args, country)
+
return rows, #argErrors > 0 and table.concat(argErrors) or nil
if destinations__ and destinations__ ~= "" then
+
end
insert(data, {label = destinationsLabel, data = destinations__})
+
 
end
+
function p.locations(frame)
local lgaLabel, lga_ = lga(args, country)
+
-- Import module function to work with passed arguments
if lga_ and lga_ ~= "" then
+
local getArgs = require('Module:Arguments').getArgs
insert(data, {label = lgaLabel, data = lga_})
+
local args = getArgs(frame)
end
+
local state = args.state or args.province
local locationsLabel, locations_ = locations(args, country)
+
if not args.country then
if locations_ and locations_ ~= "" then
+
local countryMask = mw.loadData("Module:Road data/countrymask")
insert(data, {label = locationsLabel, data = locations_})
+
args.country = countryMask[state]
 
end
 
end
 
+
return p._locations(args)
return data
 
 
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