Jump to content

Module:Jct/city/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Philroc (talk | contribs) at 15:49, 25 August 2021 (fix). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = {}

local concat = table.concat
local insert = table.insert

local state

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

local function location(args, num, page, linkCheck)
	local city = args["city" .. num]
	local location = args["location" .. num]
	local areadab = args["areadab" .. num]
	local countydab = args["countydab" .. num]
	local place = city or location
	
	local pageContainsLink
	local jctString = "{{jct|" .. ".+" .. place .. ".+" .. "}}"
	local linkString = location(place, num, page, true)
	
	local firstPosition = strfind(linkString, page)
	if firstPosition then
		local secondPosition = strfind(linkString, page, firstPosition)
	end
	
	if firstPosition then
		if secondPosition or strfind(jctString, page) then pageContainsLink = true
		else pageContainsLink = false
		end
	else pageContainsLink = false
	end
	
	if not(city or location) then
		return ''
	end
	
	local parts
	if num == 1 then
		parts = {"– "}
	else
		parts = {", "}
	end
	
	if location then
		insert(parts, location)
		return concat(parts)
	end
	
	if not pageContainsLink then
	    insert(parts, "[[" .. city)
	end
	if areadab then
		insert(parts, " (" .. areadab .. ")")
	end
	if countydab then
		insert(parts, ", " .. countydab .. " County")
	end
	if state then
		insert(parts, ", " .. state)
	end
	if not pageContainsLink then
		insert(parts, "|" .. city .. "]]")
	end
	
	return concat(parts)
end

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

return p