Difference between revisions of "Module:ISO 3166/sandbox"
Jump to navigation
Jump to search
blackwiki>BrandonXLF |
blackwiki>BrandonXLF |
||
| Line 3: | Line 3: | ||
local catnocountry = '[[Category:Wikipedia page with obscure country]]' | local catnocountry = '[[Category:Wikipedia page with obscure country]]' | ||
local catnosubdivision = '[[Category:Wikipedia page with obscure subdivision]]' | local catnosubdivision = '[[Category:Wikipedia page with obscure subdivision]]' | ||
| − | --[[----------S T R I P----------]]-- | + | --[[----------S T R I P----------]]-- |
| + | -- Remove unnecessary text | ||
local function strip(text) | local function strip(text) | ||
local accents = { | local accents = { | ||
| Line 14: | Line 15: | ||
["Ù"]="U", ["Ú"]="U", ["Û"]="U", ["Ü"]="U", | ["Ù"]="U", ["Ú"]="U", ["Û"]="U", ["Ü"]="U", | ||
["Ý"]="Y" | ["Ý"]="Y" | ||
| + | } | ||
| + | local patterns = { | ||
| + | ["%_"]=" ", | ||
| + | ["%-"]=" " | ||
} | } | ||
local remove = { | local remove = { | ||
| − | |||
"%d+PX", | "%d+PX", | ||
"<.*>", | "<.*>", | ||
| Line 24: | Line 28: | ||
"%..*", | "%..*", | ||
"^THE", | "^THE", | ||
| − | " | + | ",.*$", |
"%W", | "%W", | ||
| − | " | + | "^[0]+", |
| − | " | + | "KINGDOMOF", |
| − | " | + | "OF", |
| − | " | + | "^STATE", |
| + | "STATE$", | ||
| + | "REPUBLIC$", | ||
| + | "NATION", | ||
| + | "COUNTRY", | ||
| + | "TERRITORY", | ||
"FLAG", | "FLAG", | ||
| − | + | "PROVINCE" | |
| − | |||
| − | |||
| − | "PROVINCE | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
} | } | ||
text = mw.ustring.upper(text) | text = mw.ustring.upper(text) | ||
| Line 54: | Line 55: | ||
return text | return text | ||
end | end | ||
| − | --[[----------F I N D | + | --[[----------F I N D C O D E----------]]-- |
| − | -- | + | -- Check to find the code or name |
| − | local function | + | local function findcode(code,cdata,qry) |
local sqry = strip(qry) | local sqry = strip(qry) | ||
| − | if sqry == code | + | if sqry == code |
or sqry == cdata["alpha3"] | or sqry == cdata["alpha3"] | ||
| − | or sqry == cdata["numeric"] | + | or sqry == cdata["numeric"]:gsub('^[0]+','') |
or sqry == strip(cdata["isoname"] or '') | or sqry == strip(cdata["isoname"] or '') | ||
or sqry == strip(cdata["name"] or '') | or sqry == strip(cdata["name"] or '') | ||
| Line 79: | Line 80: | ||
end | end | ||
--[[----------N A M E O R C O D E---------]]-- | --[[----------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) | local function nameorcode(args,data,ldata,alpha2,dash2) | ||
if args.type == "name" then | if args.type == "name" then | ||
| Line 92: | Line 94: | ||
end | end | ||
end | end | ||
| − | --[[----------G E T C O D E---------]]-- | + | --[[----------G E T C O D E---------]]-- |
| − | -- | + | -- Return the code or name |
local function getcode(args) | local function getcode(args) | ||
catnocountry = args.nocat == 'true' and '' or catnocountry | catnocountry = args.nocat == 'true' and '' or catnocountry | ||
| Line 105: | Line 107: | ||
if args["codetype"] == "3" then args["codetype"] = "alpha3" end | if args["codetype"] == "3" then args["codetype"] = "alpha3" end | ||
for alpha2,cdata in pairs(data) do | for alpha2,cdata in pairs(data) do | ||
| − | if | + | if findcode(alpha2,cdata,args[1]) then |
if args.type == "numeric" or args.type == "alpha3" then | if args.type == "numeric" or args.type == "alpha3" then | ||
return cdata[args.type] | return cdata[args.type] | ||
| Line 113: | Line 115: | ||
local scdata = mw.loadData("Module:ISO 3166/data/"..alpha2) | local scdata = mw.loadData("Module:ISO 3166/data/"..alpha2) | ||
for scode,sdata in pairs(scdata) do | for scode,sdata in pairs(scdata) do | ||
| − | if | + | if findcode(scode,sdata,args[2]) then |
return nameorcode(args,scdata,sdata,alpha2,scode) | return nameorcode(args,scdata,sdata,alpha2,scode) | ||
end | end | ||
| Line 127: | Line 129: | ||
end | end | ||
--[[----------M A K E F U N C---------]]-- | --[[----------M A K E F U N C---------]]-- | ||
| + | -- Make a function for the p table | ||
local function makefunc(params) | local function makefunc(params) | ||
params = params or {} | params = params or {} | ||
| Line 140: | Line 143: | ||
end | end | ||
if params.func == 'strip' then | if params.func == 'strip' then | ||
| − | return strip(args[1] or args.text) | + | return strip(args[1] or args.text) or '' |
else | else | ||
| − | return getcode(args) | + | return getcode(args) or '' |
end | end | ||
end | end | ||
end | end | ||
--[[----------E X P O R T E D F U N C T I O N S---------]]-- | --[[----------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.strip = makefunc({func='strip'}) | ||
p.code = makefunc() | p.code = makefunc() | ||
Revision as of 03:06, 13 February 2019
Documentation for this module may be created at Module:ISO 3166/sandbox/doc
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 sqry == cdata["numeric"]:gsub('^[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