Jump to content

Module:Settlement short description

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Galobtter (talk | contribs) at 10:26, 20 January 2019 (+). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

--generates auto short description for use in infobox settlement
local p = {}
local plain = require('Module:Plain text/sandbox')._main
local getArgs = require('Module:Arguments').getArgs
local categoryHandler = require( 'Module:Category handler' ).main
local tableTools = require ('Module:TableTools')

function p.reverseTable (init)
	init[1], init[3] = init[3], init[1]
	return init
end

--Display short description usin {{short description}}
function p.shortdesc(text, frame)
	return frame:expandTemplate{title = 'Short description', args = {text}}
end

--removes redundancy like "England, United Kingdom" and fixes issues like "Foo in United States" (to "Foo in the United States")
--also used in Module:Type in location
function p.cleanupLoc (location)
	local replacements = {
		["England, United Kingdom"] =  "England",
		["Scotland, United Kingdom"] =  "Scotland",
		["Wales, United Kingdom"] =  "Wales",
		["^United States$"] = "the United States"
	}
	for i, v in pairs(replacements) do 
		location = location:gsub(i, v) --series of replacements
	end
	return location
end

function p.main(frame)
	local categories = ""
	local subdivisions = {}
	local args = getArgs (frame, {frameOnly = true})
	local settlement_type = plain(args[1]) or "Place"
	local short_description = plain(args[2])
	subdivisions[1] = plain(args[3])
	subdivisions[2] = plain(args[4])
	subdivisions[3] = plain(args[5])
	
	if short_description then
		if (short_description == 'no') then
			return
		else
			return p.shortdesc(short_description, frame)
		end
	end
	
	for x, y in ipairs (subdivisions) do
		if string.find(settlement_type, y, 1, true) then --if the subdivision is found within the settlement type
			subdivisions[x] = nil --don't display redundancy
			categories = categoryHandler{'[[Category:Infobox settlement pages with bad settlement type]]', nocat = args.nocat} --categorize
		end
	end

	if not(subdivisions[3] and
		(string.find(settlement_type, '[nN]eighbo[u]?rhood') or string.find(settlement_type, '[sS]uburb'))) then
		subdivisions[3] = nil --only display the third subdivision_type if suburb or neighborhood
	end
	
	local location = table.concat(tableTools.compressSparseArray(p.reverseTable(subdivisions)), ', ')
	
	location = p.cleanupLoc (location)
	
	return p.shortdesc(settlement_type..(location and " in " .. location), frame)..categories
end

return p