Jump to content

Module:ISO 3166/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by BrandonXLF (talk | contribs) at 03:10, 13 February 2019 (string.gsub). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = {}
local data = mw.loadData("Module:ISO 3166/data/National")
local catnocountry = '[[Category:Wikipedia page with obscure country]]'
local catnosubdivision = '[[Category:Wikipedia page with obscure subdivision]]'
--[[----------S T R I P----------]]--	
-- Remove unnecessary text
local function strip(text)
	local accents = {
		["À"]="A", ["Á"]="A", ["Â"]="A", ["Ã"]="A", ["Ä"]="A", ["Å"]="A",
		["Ç"]="C",
		["È"]="E", ["É"]="E", ["Ê"]="E", ["Ë"]="E",
		["Ì"]="I", ["Í"]="I", ["Î"]="I", ["Ï"]="I",
		["Ñ"]="N",
		["Ò"]="O", ["Ó"]="O", ["Ô"]="O", ["Õ"]="O", ["Ö"]="O", ["Ø"]="O",
		["Ù"]="U", ["Ú"]="U", ["Û"]="U", ["Ü"]="U",
		["Ý"]="Y"
	}
	local patterns = {															
		["%_"]=" ",
		["%-"]=" "
	}
	local remove = {		
		"%d+PX",
		"<.*>",
		".+:",
		"|.+",
		"%(.-%)",
		"%..*",
		"^THE",
		",.*$",
		"%W",
		"^[0]+",
		"KINGDOMOF",
		"OF",
		"^STATE",
		"STATE$",
		"REPUBLIC$",
		"NATION",
		"COUNTRY",
		"TERRITORY",
		"FLAG",
		"PROVINCE"
	}
	text = mw.ustring.upper(text)	
	if mw.ustring.match(text,"[À-Ý]") then
		mw.ustring.gsub(text,"[À-Ý]",accents)
	end
	text = mw.text.unstrip(text)												
	for find, replace in pairs(patterns) do										
		text = text:gsub(find,replace) 
	end
	for _, find in ipairs(remove) do											
		text = text:gsub(find,"") 
	end
	return text
end
--[[----------F I N D C O D E----------]]--	
-- Check to find the code or name
local function findcode(code,cdata,qry)
	local sqry = strip(qry)
	if     sqry == code
		or sqry == cdata["alpha3"]
		or cdata["numeric"] and sqry == string.gsub(cdata["numeric"], '^[0]+','')
		or sqry == strip(cdata["isoname"] or '')
		or sqry == strip(cdata["name"] or '')
	then
		return true
	end
	for _,tname in pairs(cdata["isonames"] or {}) do
		if sqry == strip(tname) then
			return true
		end
	end
	for _,tname in pairs(cdata["altnames"] or {}) do
		if sqry == strip(tname) then
			return true
		end
	end
	return false
end
--[[----------N A M E O R C O D E---------]]--
--Determine what name or alpha2 code to return
local function nameorcode(args,data,ldata,alpha2,dash2)
	if args.type == "name" then
		if args.isoname or args.lang then
			local lang = args.lang or ldata["defaultlang"] or data["defaultlang"] or "en"
			return ldata["isonames"] and ldata["isonames"][lang] or ldata["isoname"] or ''
		end
		return ldata["name"]
	elseif dash2 then
		return alpha2.."-"..dash2
	else
		return alpha2
	end
end
--[[----------G E T C O D E---------]]--
-- Return the code or name
local function getcode(args)
	catnocountry = args.nocat == 'true' and '' or catnocountry
	catnosubdivision = args.nocat == 'true' and '' or catnosubdivision
	if not args[1] then return catnocountry end
	if string.find(args[1], "^%s*(%w%w?%w?)%-(%w%w?%w?)") then
		args[1], args[2] = string.match(args[1],"^%s*(%w%w?%w?)%-(%w%w?%w?)")
	end
	if args[1] then args[1] = strip(args[1]) end
	if args[2] then args[2] = strip(args[2]) end
	if args["codetype"] == "3" then args["codetype"] = "alpha3" end
	for alpha2,cdata in pairs(data) do
		if findcode(alpha2,cdata,args[1]) then
			if args.type == "numeric" or args.type == "alpha3" then
				return cdata[args.type]
			else 
				local scode = nil
				if args[2] then
					local scdata = mw.loadData("Module:ISO 3166/data/"..alpha2)
					for scode,sdata in pairs(scdata) do
						if findcode(scode,sdata,args[2]) then
							return nameorcode(args,scdata,sdata,alpha2,scode)
						end
					end
					return catnosubdivision
				else
					return nameorcode(args,data,cdata,alpha2)
				end
			end
		end
	end
	return catnocountry
end
--[[----------M A K E F U N C---------]]--	
-- Make a function for the p table
local function makefunc(params)
	params = params or {}
	return function(frame)
		local args = {}
		if type(frame.args) == 'table' then
			args = frame.args
		elseif type(frame) == 'table' then
			args = frame
		end
		if params.type then
			args.type = params.type
		end
		if params.func == 'strip' then
			return strip(args[1] or args.text) or ''
		else
			return getcode(args) or ''
		end
	end
end
--[[----------E X P O R T E D   F U N C T I O N S---------]]--
-- Functions in the p table
p.strip = makefunc({func='strip'})
p.code = makefunc()
p['alpha2'] = makefunc()
p['alpha3'] = makefunc({type='alpha3'})
p.numeric = makefunc({type='numeric'})
p.name = makefunc({type='name'})
--[[----------P . G E O C O O R D I N S E R T---------]]--						
-- Wrapper for Module:Coordinates.coordinsert
function p.geocoordinsert(frame)
	-- {{#invoke:ISO 3166|geocoordinsert|{{coord|...}}
	-- |country=..|subdivision1=...|subdivision2=...
	-- |type=...|scale=...|dim=...|source=...|globe=...
	-- }}
	local args = frame.args
	local subdivisionqueried = false
	catnocountry = args.nocat == 'true' and '' or catnocountry
	catnosubdivision = args.nocat == 'true' and '' or catnosubdivision
	local tracking = ''
	local targs = {}
	targs[1] = args[1] or ''
	for i, v in pairs(args) do
		if i == 'country' and not mw.ustring.find(targs[1], 'region:') then
			local country = v
			local k, region = 1, ''
			-- look for a valid subdivision
			while region == '' and k < 3 do
				local subdivision = args['subdivision' .. k] or ''
				if subdivision ~= '' then
					region = p.luacode({country, subdivision, nocat = 'true'})
					subdivisionqueried = true
				end
				k = k + 1
			end
			-- subdivision lookup failed or never attempted, try country only
			if region == '' then
				region = p.luacode({country, nocat = 'true'})
				if region == '' then
					tracking = tracking .. catnocountry
				elseif subdivisionqueried == true then
					tracking = tracking .. catnosubdivision
				end
			end
			-- something worked, add it to the targs
			if region ~= '' then
				targs[#targs + 1] = 'region:' .. region
			end
		elseif i == 'type' or i == 'scale' or i == 'dim' 
				or i == 'source' or i == 'globe' then
			targs[#targs + 1] = i .. ':' .. v
		end
	end
	-- call Module:Coordinates.coordinsert if there is something to insert
	if #targs > 1 then
		local coordinsert = require('Module:Coordinates').coordinsert
		return coordinsert({args = targs}) .. tracking
	end
	-- otherwise, just return the coordinates
	return targs[1] .. tracking
end
return p