Module:ISO 3166/sandbox: Difference between revisions

Content deleted Content added
Revert 1080380125: Hyphen fix only
Refactored; made output more structured
Line 54:
 
function p.strip(text)
local accents = {["À"]="A",["Á"]="A",["Â"]="A",["Ã"]="A", -- accent list (each is two bytes; first byte = 195)
["Ä"]="A",["Å"]="A",["Ç"]="C",["È"]="E",["É"]="E",
["Ê"]="E",["Ë"]="E",["Ì"]="I",["Í"]="I",["Î"]="I",
Line 61:
["Û"]="U",["Ü"]="U",["Ý"]="Y"
}
local remove = {"NATION OF","COUNTRY OF","TERRITORY OF", -- text to be removed list
"FLAG OF","FLAG","KINGDOM OF","STATE OF"," STATE ",
"PROVINCE OF","PROVINCE","TERRITORY"
}
local patterns = { -- patterns to follow (order may matter)
[".+:"]="",
["|.+"]="",
Line 75:
}
 
text = mw.ustring.upper(text) -- Casecase insensitivity
 
text = text:gsub('\195.', accents) -- Deaccent
 
for pattern,value in pairs(patterns) do -- Follow patterns
text = string.text:gsub(text,pattern,value)
end
 
for _,words in pairs(remove) do -- Remove unneeded words
text = string.text:gsub(text,words,"")
end
 
text = text:gsub("[^%w%-]","") --Only keep ASCII alphanumeric and hyphen
-- Hyphen-sensitive
text = string.gsub(text,"[^%w%-]","") -- Remove non alpha-numeric -- Remove non alpha-numeric
 
return text
Line 104 ⟶ 103:
 
--[[----------P . L U A C O D E---------]]-- -- Makes the ISO code of a country
-- Returns tuple (code,errorcat,errortext). code is empty string upon error because MW stops at first nil
 
function p.luacode(args)
if (args[1] ~= nil) then args[1] = p.strip(args[1]) end
if (args[2] ~= nil) then args[2] = p.strip(args[2]) end
 
localif a1,args[1] a2 =then args[1], = p.strip(args[21]) end
if (args[12] ~= nil) then args[12] = p.strip(args[12]) end
 
local a1,a2 = args[1],args[2]
 
local hyphenPos = string.find(a1 or '', '-', 1, true)
if not hyphenPos then --Nono hyphen
return p.luacodeimpl(args)
end
 
args[1] = string.a1:sub(a1, 1, hyphenPos-1) --Try splitting on hyphen
args[2] = string.a1:sub(a1, hyphenPos+1)
local hyphensplitresultresult = p.luacodeimpl(args)
if hyphensplitresult ~= nil and hyphensplitresultresult ~= '' then
return hyphensplitresultresult
end
 
args[1], args[2] = a1, a2 --Try again without splitting
return p.luacodeimpl(args) or ''
 
end
 
local function p.luacodeimpl(args)
 
local country, subdivision = args[1], args[2]
 
Line 142 ⟶ 143:
if not country then
if mw.title.getCurrentTitle().namespace ~= 0 then catnocountry = '' end
return '',catnocountry, '<span style="font-size:100%" class="error">"NoISO parameter3166: forname thenot country given"specified</span>'
end
 
if not subdivision then --3166-1 code
for alpha2,cdata in pairs(data) do
if findname(alpha2,cdata,country) then
Line 156 ⟶ 157:
end
if mw.title.getCurrentTitle().namespace ~= 0 then catnocountry = '' end
return '',catnocountry
else --3166-2 code
for alpha2,cdata in pairs(data) do
if findname(alpha2,cdata,country) then
if string.match(alpha2:sub(1,2) == "GB") then --Treat Forall England,UK countries as Walesthe etc.UK
alpha2 = "GB"
end
Line 166 ⟶ 167:
local empty = true
for scode,scdata in pairs(sdata) do
if type(scdata) == "table" then
empty = false
if findname(scode,scdata,subdivision) then
Line 174 ⟶ 175:
end
if mw.title.getCurrentTitle().namespace ~= 0 then catnosubdivision = '' end
return '',catnosubdivision
end
end
if mw.title.getCurrentTitle().namespace ~= 0 then catnocountry = '' end
return '',catnocountry
end
 
Line 197 ⟶ 198:
args["codetype"]="numeric"
 
return p.luacode(args) or ""
 
end
 
--[[----------P . L U A N A M E---------]]-- -- Makes the ISO/common name of a country
-- Returns tuple (name,errorcat,errortext). name is empty string upon error because MW stops at first nil
 
function p.luaname(args)
 
local code1 = p.luacode(args)
local code2 = ''
if string.len(code1) == 2'' then
 
return '','','<span class="error">No such country or subdivision</span>'
local hyphenPos = string.find(code1, "-", 1, true)
if hyphenPos then
code2 = string.sub(code1, hyphenPos+1)
code1 = string.sub(code1, 1, hyphenPos-1)
end
 
local hyphenPos = string.code1:find(code1, "-", 1, true)
if string.len(code1) == 2 then
if code2==""hyphenPos then --3166-1 alpha-2 code
local code2 = string.code1:sub(code1, hyphenPos+1)
if data[code1] then
code1 = string.code1:sub(code1, 1, hyphenPos-1)
return (args.isoname or args.lang) and isoname(data,code1,args.lang)
 
or (data[code1]["displayname"] or data[code1]["name"])
returnlocal catnodata = '[[Category:Wikipedia page with obscure country]]'
else
 
return '[[Category:Wikipedia page with obscure country]]'
local sdata
end
if data[code1] then
else --3166-2 code
sdata = mw.loadData("Module:ISO 3166/data/"..code1)
local sdata
else
if data[code1] then
return '',catnodata
sdata = mw.loadData("Module:ISO 3166/data/"..code1)
elseend
if sdata[code2] then
return '[[Category:Wikipedia page with obscure country]]'
return (args.isoname or args.lang) and isoname(sdata,code2,args.lang)
end
if or (sdata[code2]["displayname"] thenor sdata[code2]["name"])
else
return (args.isoname or args.lang) and isoname(sdata,code2,args.lang)
return '',catnodata
or (sdata[code2]["displayname"] or sdata[code2]["name"])
elseend
 
return '[[Category:Wikipedia page with obscure country]]'
else --3166-1 alpha-2 code
end
if data[code1] then
return (args.isoname or args.lang) and isoname(data,code1,args.lang)
or (data[code1]["displayname"] or data[code1]["name"])
else
return '[[Category:Wikipedia page with obscure country]]'
end
end
Line 244 ⟶ 247:
function p.name(frame)
 
return p.luaname(getArgs(frame)) or ""
 
end
Line 266 ⟶ 269:
if i == 'country' and not string.find(targs[1], 'region:', 1, true) then
local country = v
local k, region = 1, ''
-- look for a valid subdivision
while region == '' and k < 3 do
Line 279 ⟶ 282:
if region == '' then
region = p.luacode({country, nocat = 'true'})
if mw.title.getCurrentTitle().namespace ~= 0 then catnocountry, catnosubdivision = '', '' end
if region == '' then
tracking = tracking .. catnocountry