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...')
 
test>Chinissai
m (More refactoring.)
Line 3: Line 3:
 
local insert = table.insert
 
local insert = table.insert
  
local function countries(args, country)
+
local function flip(arr)
local data = {EUR = "no", ASIA = "no", default = "yes"}
+
local mask = {}
local yesOrNo = data[country] or data.default
+
for _,entry in ipairs(arr) do
if yesOrNo == "no" then
+
mask[entry] = true
return args.countries
 
else
 
return nil
 
 
end
 
end
 +
return mask
 
end
 
end
  
local function regions(args, country)
+
local showCountries = {"ASIA", "EUR"}
local data = {EUR = "no", ESP = "no", ITA = "no", HRV = "no", CZE = "no", CAN = "no", CYP = "no", GRC = "no", POL = "no", HUN = "no",
+
local countriesMask = flip(showCountries)
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 showRegions = {
local data = {USA = "no", IND = "no", DEU = "no", AUT = "no", MEX = "no", AUS = "no", default = "yes"}
+
"AUS", "AUT", "BIH", "CAN", "CYP", "CZE", "ESP", "EUR", "GRC", "HRV",
local yesOrNo = data[country] or data.default
+
"HUN", "ITA", "POL", "SGP", "SRC", "SVK", "SVN", "TUR", "UKR"
if yesOrNo == "no" then
+
}
return args.states
+
local regionsMask = flip(showRegions)
else
 
return nil
 
end
 
end
 
  
local function provinces(args, country)
+
local showStates = {"AUS", "AUT", "DEU", "IND", "MEX", "USA"}
local data = {TUR = "no", THA = "no", IRN = "no", NLD = "no", ESP = "no", default = "yes"}
+
local statesMask = flip(showStates)
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 showProvinces = {"ESP", "IRN", "NLD", "THA", "TUR"}
local counties = args.counties
+
local provincesMask = flip(showProvinces)
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 function ruralMunisLabel(province)
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
 
else
label = "Rural<br>municipalities:"
+
return "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
 
end
 
end
  
local function divisions(args, country)
+
local showRuralMunis = {"AB", "MB", "SK"}
local province = country or ''
+
local ruralMunisMask = flip(showRuralMunis)
local data = {ON = "no", default = "yes"}
+
 
local yesOrNo = data[province] or data.default
+
local showDivisions = {"ON"}
if yesOrNo == "no" then
+
local divisionsMask = flip(showDivisions)
return args.divisions
 
else
 
return nil
 
end
 
end
 
  
local function cities(args, country, parameter)
+
local noCities = {"USA"}
local data = {USA = "yes", default = "no"}
+
local noCitiesMask = flip(noCities)
local yesOrNo = data[country] or data.default
 
if yesOrNo == "no" then
 
return args[parameter]
 
else
 
return nil
 
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 showDestinations = {"AUS", "GBR", "IND", "IRL", "MYS", "NPL", "NZL"}
 +
local destinationsMask = flip(showDestinations)
 +
 
 +
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 65:
 
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 showLga = {"AUS"}
 +
local lgaMask = flip(showLga)
 +
 
 +
local function locationsLabel(type)
 
local labels = {
 
local labels = {
 
highway = "Major settlements:",
 
highway = "Major settlements:",
Line 150: Line 80:
 
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 showLocations = {"AUS"}
local label = labels[type] or "Primary<br>destinations:"
+
local locationsMask = flip(showLocations)
return label, args.locations
+
 
else
+
local function filter(mask, key, result, pattern)
return '', nil
+
if not pattern then pattern = true end
end
+
if mask[key] == pattern then return result 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)
+
function p._locations(args)
local pframe = frame:getParent()
+
local rows = {}
local args = pframe.args -- the arguments passed TO the template, in the wikitext that transcludes the template
 
 
 
local data = {}
 
  
local country = emptyParam(args.country)
+
local country = args.country
local state = emptyParam(args.state)
+
addIfExists(rows, "Countries:", filter(countriesMask, country, args.countries))
local province = emptyParam(args.province)
+
addIfExists(rows, "Regions:", filter(regionsMask, country, args.regions))
if not(country) then
+
addIfExists(rows, "States:", filter(statesMask, country, args.states))
local stateParam = args.state or args.province
+
addIfExists(rows, "Provinces:", filter(provincesMask, country, args.provinces))
if not(stateParam) then
+
addIfExists(rows, "Counties:", args.counties)
country = ''
+
addIfExists(rows, "Parishes:", args.parishes)
else
+
addIfExists(rows, "Boroughs:", args.boroughs)
local countryMask = require "Module:Infobox road/meta/mask/country"
+
addIfExists(rows, "Municipalities:", args.municipalities)
country = countryMask._country(stateParam, country)
+
addIfExists(rows, "Districts:", args.districts)
end
+
local province = args.province
end
+
if province then
local countries_ = countries(args, country)
+
addIfExists(rows, ruralMunisLabel,
if countries_ and countries_ ~= "" then
+
filter(ruralMunisMask, province, args.rural_municipalities), province)
insert(data, {label = "Countries:", data = countries_})
+
addIfExists(rows, "Divisions:", filter(divisionsMask, province, args.divisions))
end
 
local regions_ = regions(args, country)
 
if regions_ and regions_ ~= "" then
 
insert(data, {label = "Regions:", data = regions_})
 
end
 
local states_ = states(args, country)
 
if states_ and states_ ~= "" then
 
insert(data, {label = "States:", data = states_})
 
end
 
local provinces_ = provinces(args, country)
 
if provinces_ and provinces_ ~= "" then
 
insert(data, {label = "Provinces:", data = provinces_})
 
end
 
local countiesLabel, counties_ = counties(args, country)
 
if counties_ and counties_ ~= "" then
 
insert(data, {label = countiesLabel, data = counties_})
 
end
 
local ruralMunisLabel, ruralMunis__ = ruralMunis(args, country)
 
if ruralMunis__ and ruralMunis__ ~= "" then
 
insert(data, {label = ruralMunisLabel, data = ruralMunis__})
 
end
 
local divisions = divisions(args, country)
 
if divisions and divisions ~= "" then
 
insert(data, {label = "Divisions:", data = divisions})
 
end
 
local cities_ = cities(args, country, "cities")
 
if cities_ and cities_ ~= "" then
 
insert(data, {label = "Major cities:", data = cities_})
 
end
 
local towns_ = cities(args, country, "towns")
 
if towns_ and towns_ ~= "" then
 
insert(data, {label = "Towns:", data = towns_})
 
end
 
local villages_ = cities(args, country, "villages")
 
if villages_ and villages_ ~= "" then
 
insert(data, {label = "Villages:", data = villages_})
 
end
 
local destinationsLabel, destinations__ = destinations(args, country)
 
if destinations__ and destinations__ ~= "" then
 
insert(data, {label = destinationsLabel, data = destinations__})
 
end
 
local lgaLabel, lga_ = lga(args, country)
 
if lga_ and lga_ ~= "" then
 
insert(data, {label = lgaLabel, data = lga_})
 
end
 
local locationsLabel, locations_ = locations(args, country)
 
if locations_ and locations_ ~= "" then
 
insert(data, {label = locationsLabel, data = locations_})
 
 
end
 
end
 +
addIfExists(rows, "Major cities:", filter(noCitiesMask, country, args.cities, false))
 +
addIfExists(rows, "Towns:", filter(noCitiesMask, country, args.towns, false))
 +
addIfExists(rows, "Villages:", filter(noCitiesMask, country, args.villages, false))
 +
addIfExists(rows, destinationsLabel, filter(destinationsMask, country, args.destinations), country)
 +
addIfExists(rows, lgaLabel, filter(lgaMask, country, args.lga), args.state)
 +
addIfExists(rows, locationsLabel, filter(locationsMask, country, args.locations), args.type)
 +
return rows
 +
end
  
return data
+
function p.locations(frame)
 +
-- Import module function to work with passed arguments
 +
local getArgs = require('Module:Arguments').getArgs
 +
local args = getArgs(frame)
 +
return p._locations(args)
 
end
 
end
  
 
return p
 
return p

Revision as of 15:14, 1 May 2016

Documentation for this module may be created at Module:Infobox road/locations/sandbox/doc

local p = {}

local insert = table.insert

local function flip(arr)
	local mask = {}
	for _,entry in ipairs(arr) do
		mask[entry] = true
	end
	return mask
end

local showCountries = {"ASIA", "EUR"}
local countriesMask = flip(showCountries)

local showRegions = {
	"AUS", "AUT", "BIH", "CAN", "CYP", "CZE", "ESP", "EUR", "GRC", "HRV",
	"HUN", "ITA", "POL", "SGP", "SRC", "SVK", "SVN", "TUR", "UKR"
}
local regionsMask = flip(showRegions)

local showStates = {"AUS", "AUT", "DEU", "IND", "MEX", "USA"}
local statesMask = flip(showStates)

local showProvinces = {"ESP", "IRN", "NLD", "THA", "TUR"}
local provincesMask = flip(showProvinces)

local function ruralMunisLabel(province)
	if province == "AB" then
		return "Specialized<br>and rural<br>municipalities:"
	else
		return "Rural<br>municipalities:"
	end
end

local showRuralMunis = {"AB", "MB", "SK"}
local ruralMunisMask = flip(showRuralMunis)

local showDivisions = {"ON"}
local divisionsMask = flip(showDivisions)

local noCities = {"USA"}
local noCitiesMask = flip(noCities)

local function destinationsLabel(country)
	if country == "GBR" then
		return "[[Primary status|Primary<br>destinations]]:"
	else
		return "Primary<br>destinations:"
	end
end

local showDestinations = {"AUS", "GBR", "IND", "IRL", "MYS", "NPL", "NZL"}
local destinationsMask = flip(showDestinations)

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 showLga = {"AUS"}
local lgaMask = flip(showLga)

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 showLocations = {"AUS"}
local locationsMask = flip(showLocations)

local function filter(mask, key, result, pattern)
	if not pattern then pattern = true end
	if mask[key] == pattern then return result 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

function p._locations(args)
	local rows = {}

	local country = args.country
	addIfExists(rows, "Countries:", filter(countriesMask, country, args.countries))
	addIfExists(rows, "Regions:", filter(regionsMask, country, args.regions))
	addIfExists(rows, "States:", filter(statesMask, country, args.states))
	addIfExists(rows, "Provinces:", filter(provincesMask, country, 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:", args.districts)
	local province = args.province
	if province then
		addIfExists(rows, ruralMunisLabel,
			filter(ruralMunisMask, province, args.rural_municipalities), province)
		addIfExists(rows, "Divisions:", filter(divisionsMask, province, args.divisions))
	end
	addIfExists(rows, "Major cities:", filter(noCitiesMask, country, args.cities, false))
	addIfExists(rows, "Towns:", filter(noCitiesMask, country, args.towns, false))
	addIfExists(rows, "Villages:", filter(noCitiesMask, country, args.villages, false))
	addIfExists(rows, destinationsLabel, filter(destinationsMask, country, args.destinations), country)
	addIfExists(rows, lgaLabel, filter(lgaMask, country, args.lga), args.state)
	addIfExists(rows, locationsLabel, filter(locationsMask, country, args.locations), args.type)
	return rows
end

function p.locations(frame)
	-- Import module function to work with passed arguments
	local getArgs = require('Module:Arguments').getArgs
	local args = getArgs(frame)
	return p._locations(args)
end

return p