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

From blackwiki
Jump to navigation Jump to search
test>Chinissai
(Corrected filter.)
test>Chinissai
(Use Module:Infobox road/display for display filters.)
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 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)
 
local function ruralMunisLabel(province)
Line 33: Line 13:
 
end
 
end
 
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)
 
local function destinationsLabel(country)
Line 50: Line 21:
 
end
 
end
 
end
 
end
 
local showDestinations = {"AUS", "GBR", "IND", "IRL", "MYS", "NPL", "NZL"}
 
local destinationsMask = flip(showDestinations)
 
  
 
local function lgaLabel(state)
 
local function lgaLabel(state)
Line 67: Line 35:
 
return labels[state] or "[[Local government in Australia|LGA(s)]]"
 
return labels[state] or "[[Local government in Australia|LGA(s)]]"
 
end
 
end
 
local showLga = {"AUS"}
 
local lgaMask = flip(showLga)
 
  
 
local function locationsLabel(type)
 
local function locationsLabel(type)
Line 83: Line 48:
 
end
 
end
  
local showLocations = {"AUS"}
+
local function filter(args, field, result)
local locationsMask = flip(showLocations)
+
local parserModule = require(parserModuleName)
 
+
local display = parserModule.parser(args, field, args.country,
local function filter(mask, key, result, pattern)
+
"Module:Infobox road/display")
if pattern == nil then pattern = true end
+
return display == "yes" and result or nil
if (mask[key] or false) == pattern then return result end
 
return nil
 
 
end
 
end
  
Line 105: Line 68:
  
 
local country = args.country
 
local country = args.country
addIfExists(rows, "Countries:", filter(countriesMask, country, args.countries))
+
addIfExists(rows, "Countries:", filter(args, "countries", args.countries))
addIfExists(rows, "Regions:", filter(regionsMask, country, args.regions))
+
addIfExists(rows, "Regions:", filter(args, "regions", args.regions))
addIfExists(rows, "States:", filter(statesMask, country, args.states))
+
addIfExists(rows, "States:", filter(args, "states", args.states))
addIfExists(rows, "Provinces:", filter(provincesMask, country, args.provinces))
+
addIfExists(rows, "Provinces:", filter(args, "provinces", args.provinces))
 
addIfExists(rows, "Counties:", args.counties)
 
addIfExists(rows, "Counties:", args.counties)
 
addIfExists(rows, "Parishes:", args.parishes)
 
addIfExists(rows, "Parishes:", args.parishes)
 
addIfExists(rows, "Boroughs:", args.boroughs)
 
addIfExists(rows, "Boroughs:", args.boroughs)
 
addIfExists(rows, "Municipalities:", args.municipalities)
 
addIfExists(rows, "Municipalities:", args.municipalities)
addIfExists(rows, "Districts:", args.districts)
+
addIfExists(rows, "Districts:", filter(args, "districts", args.districts))
local province = args.province
+
addIfExists(rows, ruralMunisLabel,
if province then
+
filter(args, "ruralmuni", args.rural_municipalities), args.province)
addIfExists(rows, ruralMunisLabel,
+
addIfExists(rows, "Divisions:", filter(args, "divisions", args.divisions))
filter(ruralMunisMask, province, args.rural_municipalities), province)
+
addIfExists(rows, "Major cities:", filter(args, "cities", args.cities))
addIfExists(rows, "Divisions:", filter(divisionsMask, province, args.divisions))
+
addIfExists(rows, "Towns:", filter(args, "cities", args.towns))
end
+
addIfExists(rows, "Villages:", filter(args, "cities", args.villages))
addIfExists(rows, "Major cities:", filter(noCitiesMask, country, args.cities, false))
+
addIfExists(rows, destinationsLabel, filter(args, "destinations", args.destinations), country)
addIfExists(rows, "Towns:", filter(noCitiesMask, country, args.towns, false))
+
addIfExists(rows, lgaLabel, filter(args, "lga", args.lga), args.state)
addIfExists(rows, "Villages:", filter(noCitiesMask, country, args.villages, false))
+
addIfExists(rows, locationsLabel, filter(args, "locations", args.locations), args.type)
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
 
return rows
 
end
 
end

Revision as of 15:16, 5 May 2016

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 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 function filter(args, field, result)
	local parserModule = require(parserModuleName)
	local display = parserModule.parser(args, field, args.country,
		"Module:Infobox road/display")
	return display == "yes" and result or 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(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), country)
	addIfExists(rows, lgaLabel, filter(args, "lga", args.lga), args.state)
	addIfExists(rows, locationsLabel, filter(args, "locations", 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)
	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