Difference between revisions of "Module:Jct/city"

From blackwiki
Jump to navigation Jump to search
blackwiki>Rschen7754
m (Protected Module:Jct/city: High-risk Lua module ([Edit=Allow only template editors and admins] (indefinite) [Move=Allow only template editors and admins] (indefinite)))
blackwiki>Happy5214
(Sync with sandbox: Allow unlimited locations and calling from Module:Jct)
Line 53: Line 53:
 
end
 
end
  
function p.city(frame)
+
function p.city(args)
local pframe = frame:getParent()
 
local config = frame.args -- the arguments passed BY the template, in the wikitext of the template itself
 
local args = pframe.args -- the arguments passed TO the template, in the wikitext that transcludes the template
 
 
local city1 = args.city1 or args.location1
 
if not city1 then
 
return ''
 
end
 
 
 
state = stateName(args)
 
state = stateName(args)
 
local cities = {}
 
local cities = {}
for i = 1, 4 do
+
local locationCount = 1
cities[i] = location(args, i)
+
repeat
end
+
local location = location(args, locationCount)
 +
table.insert(cities, location)
 +
local empty = (location == '')
 +
locationCount = locationCount + 1
 +
until empty
 
return table.concat(cities)
 
return table.concat(cities)
 
end
 
end
  
 
return p
 
return p

Revision as of 02:15, 5 June 2014

Documentation for this module may be created at Module:Jct/city/doc

local p = {}

local state

local function stateName(args)
	local dualabbrs = {NT = "Northern Territory", WA = "Western Australia"}
	
	local data = mw.loadData("Module:Jct/statename/data")
	local abbr = args.state or args.province
	local country = args.country
	if country == 'AUS' then
		return dualabbrs[abbr] or data.statenames[abbr]
	else
		return data.statenames[abbr]
	end
end

local function location(args, num)
	local city = args["city" .. num]
	local location = args["location" .. num]
	local areadab = args["areadab" .. num]
	local countydab = args["countydab" .. num]
	
	if not(city or location) then
		return ''
	end
	
	local parts
	if num == 1 then
		parts = {" – "}
	else
		parts = {", "}
	end
	
	if location then
		table.insert(parts, location)
		return table.concat(parts)
	end
	
	table.insert(parts, "[[" .. city)
	if areadab then
		table.insert(parts, " (" .. areadab .. ")")
	end
	if countydab then
		table.insert(parts, ", " .. countydab .. " County")
	end
	if state then
		table.insert(parts, ", " .. state)
	end
	
	table.insert(parts, "|" .. city .. "]]")
	return table.concat(parts)
end

function p.city(args)
	state = stateName(args)
	local cities = {}
	local locationCount = 1
	repeat
		local location = location(args, locationCount)
		table.insert(cities, location)
		local empty = (location == '')
		locationCount = locationCount + 1
	until empty
	return table.concat(cities)
end

return p