Module:Settlement short description: Difference between revisions

Jump to navigation Jump to search
m (1 revision imported)
m (1 revision imported)
 
(One intermediate revision by one other user not shown)
Line 9: Line 9:
init[1], init[3] = init[3], init[1]
init[1], init[3] = init[3], init[1]
return init
return init
end
function p.assign (args, argname, num)
local val
local var = {}
for i = 0,num do
--handle initial "subdivision_foo" without number
if i == 0 then
val = ""
else
val = tostring(i)
end
var[i+1] = p.validate(plain(args[argname..val]))
end
return var
end
end


Line 17: Line 32:


function p.category (cattype)
function p.category (cattype)
local category = string.format('[[Category:Infobox settlement pages with bad %s]]', cattype)
local category = string.format('[[Category:Pages using infobox settlement with bad %s]]', cattype)
if category then p.categories = p.categories..category end --categorize
if category then p.categories = p.categories..category end --categorize
end
end
Line 57: Line 72:
function p.main(frame)
function p.main(frame)
local categories = ""
local categories = ""
local subdivisions = {}
local subdivision_types = {}
local args = getArgs (frame, {frameOnly = true})
local subdivision_names = {}
local settlement_type = p.validate(plain(args[1]), "settlement type") or "Place"
local args = getArgs (frame, {parentOnly = true})
local short_description = plain(args[2])
local settlement_type = p.validate(plain(args.settlement_type or args.type), "settlement type") or "Place"
subdivisions[1] = p.validate(plain(args[3]))
local short_description = plain(args.short_description)
subdivisions[2] = p.validate(plain(args[4]))
subdivision_types = p.assign(args, "subdivision_type", 2)
subdivisions[3] = p.validate(plain(args[5]))
subdivision_names = p.assign(args, "subdivision_name", 2)
if short_description then
if short_description then
Line 73: Line 88:
end
end
if not(subdivisions[3] and
if not(subdivision_names[3] and
(string.find(settlement_type, '[nN]eighbo[u]?rhood') or string.find(settlement_type, '[sS]uburb'))) then
(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
subdivision_names[3] = nil --only display the third subdivision_type if suburb or neighborhood
end
--If say "Voivodeship" is found within the subdivision_type, then specially handle
--by adding Voivodeship to the end if not already so
for x, y in ipairs (subdivision_types) do
local special_types = {
"Voivodeship"
}
for i, j in ipairs(special_types) do
if subdivision_names[x] and string.find(y, j, 1, true)
and not string.find(subdivision_names[x], j, 1, true) then
subdivision_names[x] = subdivision_names[x].." "..j
end
end
end
end
for x, y in ipairs (subdivisions) do
for x, y in ipairs (subdivision_names) do
if y then
if y then
if string.find(settlement_type, y, 1, true) then --if the subdivision is found within the settlement type
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
subdivision_names[x] = nil --don't display redundancy
p.category ("settlement type")
p.category ("settlement type")
end
end
if y == mw.title.getCurrentTitle().text then --if the title is the same as one of the subdivisions
if y == mw.title.getCurrentTitle().text then --if the title is the same as one of the subdivision_names
subdivisions[x] = nil --don't display redundancy
subdivision_names[x] = nil --don't display redundancy
end
end
end
end
end
end


local location = table.concat(tableTools.compressSparseArray(p.reverseTable(subdivisions)), ', ')
local location = table.concat(tableTools.compressSparseArray(p.reverseTable(subdivision_names)), ', ')
location = p.cleanupLoc (location)
location = p.cleanupLoc (location)

Latest revision as of 21:25, 29 January 2020

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
end

function p.assign (args, argname, num)
	local val
	local var = {}
	for i = 0,num do
		--handle initial "subdivision_foo" without number
		if i == 0 then
			val = ""
		else
			val = tostring(i)
		end
		var[i+1] = p.validate(plain(args[argname..val]))
	end
	return var
end

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

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

--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
	end
	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",
		["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
	end
	return location
end

function p.main(frame)
	local categories = ""
	local subdivision_types = {}
	local subdivision_names = {}
	local args = getArgs (frame, {parentOnly = true})
	local settlement_type = p.validate(plain(args.settlement_type or args.type), "settlement type") or "Place"
	local short_description = plain(args.short_description)
	subdivision_types = p.assign(args, "subdivision_type", 2)
	subdivision_names = p.assign(args, "subdivision_name", 2)
	
	if short_description then
		if (short_description == 'no') then
			return
		else
			return p.shortdesc(short_description, frame)
		end
	end
	
	if not(subdivision_names[3] and
		(string.find(settlement_type, '[nN]eighbo[u]?rhood') or string.find(settlement_type, '[sS]uburb'))) then
		subdivision_names[3] = nil --only display the third subdivision_type if suburb or neighborhood
	end
	
	--If say "Voivodeship" is found within the subdivision_type, then specially handle
	--by adding Voivodeship to the end if not already so
	for x, y in ipairs (subdivision_types) do
		local special_types = {
			"Voivodeship"
		}
		for i, j in ipairs(special_types) do
			if subdivision_names[x] and string.find(y, j, 1, true)
				and not string.find(subdivision_names[x], j, 1, true) then
				subdivision_names[x] = subdivision_names[x].." "..j
			end
		end
	end
	
	for x, y in ipairs (subdivision_names) do
		if y then
			if string.find(settlement_type, y, 1, true) then --if the subdivision is found within the settlement type
				subdivision_names[x] = nil --don't display redundancy
				p.category ("settlement type")
			end
			if y == mw.title.getCurrentTitle().text then --if the title is the same as one of the subdivision_names
				subdivision_names[x] = nil --don't display redundancy
			end
		end
	end

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

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

return p