Module:Settlement short description

From MicroWiki, the micronational encyclopædia
This is an old revision of this page, as edited by KingdomOfBaustralia (talk | contribs) at 21:46, 25 March 2019. It may differ significantly from the current revision.
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Documentation for this module may be created at Module:Settlement short description/doc

--generates auto short description for use in infobox settlement
local p = {}
p.categories = ""
local plain = require('Module:Plain text')._main
local getArgs = require('Module:Arguments').getArgs
local tableTools = require ('Module:TableTools')

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

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

function p.category (cattype)
	local category = string.format('[[Category:Infobox settlement pages with bad %s]]', cattype)
	if category then p.categories = p.categories..category end --categorize

--sanity and other checks
function p.validate (parameter, cat)
	if not parameter then return nil end
	parameter = parameter:gsub('%b()', '') --remove things in brackets as etxtraneous information
			   :gsub('%s+', ' ') --fix possible extra spaces from previous cleanup
			   :gsub('^%s+', '') --trim spaces from beginning
			   :gsub('%s+$', '') --trim spaces from end
	if parameter:match("[,;]") or not parameter:match("%a") then --must have some letters, ignore if multiple types/subdivions
		if cat then p.category (cat) end
		return nil
	if (parameter == "") then return nil end
	return parameter

--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)
	if location == "" then return nil end
	local replacements = {
		["England, United Kingdom"] =  "England",
		["Scotland, United Kingdom"] =  "Scotland",
		["Wales, United Kingdom"] =  "Wales",
		["New York City, New York, United States"] =  "New York City",
		["^United States$"] = "the United States",
		["London, United Kingdom"] = "London",
		["London, England"] = "London"
	for i, v in pairs(replacements) do 
		location = location:gsub(i, v) --series of replacements
	return location

function p.main(frame)
	local categories = ""
	local subdivisions = {}
	local args = getArgs (frame, {frameOnly = true})
	local settlement_type = p.validate(plain(args[1]), "settlement type") or "Place"
	local short_description = plain(args[2])
	subdivisions[1] = p.validate(plain(args[3]))
	subdivisions[2] = p.validate(plain(args[4]))
	subdivisions[3] = p.validate(plain(args[5]))
	if short_description then
		if (short_description == 'no') then
			return p.shortdesc(short_description, frame)
	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
	for x, y in ipairs (subdivisions) do
		if y then
			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
				p.category ("settlement type")
			if y == mw.title.getCurrentTitle().text then --if the title is the same as one of the subdivisions
				subdivisions[x] = nil --don't display redundancy

	local location = table.concat(tableTools.compressSparseArray(p.reverseTable(subdivisions)), ', ')
	location = p.cleanupLoc (location)
	if location then location =  " in " .. location else location = "" end

	return p.shortdesc(settlement_type..location, frame)..p.categories

return p