Module:Settlement short description
Appearance
![]() | This Lua module is used on approximately 711,000 pages, or roughly 1% of all pages. To avoid major disruption and server load, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Consider discussing changes on the talk page before implementing them. |
![]() | This module depends on the following other modules: |
Usage
Used in Template:Infobox settlement to generate short descriptions.
--generates auto short description for use in infobox settlement
local p = {}
local args
local categories = ""
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 using {{short description}}
function p.shortdesc(text, frame)
return frame:expandTemplate{title = 'Short description', args = {text}}
end
function p.category (cattype)
local category = categoryHandler{string.format('[[Category:Infobox settlement pages with bad %s]]', cattype), nocat = args.nocat}
if category then categories = categories..category end --categorize
end
--sanity and other checks
function p.validate (parameter, cat)
if not parameter then return nil 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
end
parameter = parameter
:gsub('%b()', '') --remove things in brackets as generally not relevant
:gsub("%s+", " ") --remove any possible redundant spaces from the above
if (parameter == "") then return nil end
return parameter
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)
if location == "" then return nil end
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)
args = getArgs (frame, {frameOnly = true})
local categories = ""
local subdivisions = {}
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
else
return p.shortdesc(short_description, frame)
end
end
for x, y in ipairs (subdivisions) do
if (y and 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")
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)
if location then location = " in " .. location else location = "" end
return p.shortdesc(settlement_type..location, frame)..categories
end
return p