Module:ISO 3166: Difference between revisions
Appearance
m 1 revision imported |
m 1 revision imported |
||
| (One intermediate revision by one other user not shown) | |||
| Line 54: | Line 54: | ||
function p.strip(text) | function p.strip(text) | ||
if not text then return nil end | |||
local accents = {["À"]="A",["Á"]="A",["Â"]="A",["Ã"]="A", -- accent list | local accents = {["À"]="A",["Á"]="A",["Â"]="A",["Ã"]="A", -- accent list | ||
["Ä"]="A",["Å"]="A",["Ç"]="C",["È"]="E",["É"]="E", | ["Ä"]="A",["Å"]="A",["Ç"]="C",["È"]="E",["É"]="E", | ||
| Line 70: | Line 72: | ||
} | } | ||
text = mw.text.decode(text) -- Decode HTML entities (passed from wikidata?) | |||
text = mw.text.unstripNoWiki(text) -- Remove <nowiki> tags | |||
text = mw.ustring.upper(text) -- Case insensitivity | text = mw.ustring.upper(text) -- Case insensitivity | ||
text = mw.ustring.gsub(text,"[À-Ý]",accents) -- Deaccent | text = mw.ustring.gsub(text,"[À-Ý]",accents) -- Deaccent | ||
| Line 101: | Line 105: | ||
function p.luacode(args) | function p.luacode(args) | ||
local code1 = p.strip(args[1]) | |||
local code2 = p.strip(args[2]) | |||
if args["codetype"]=="3" then args["codetype"]="alpha3" end | |||
if args["codetype"]=="3" then | |||
local eot = args.error or "" | local eot = args.error or "" | ||
| Line 119: | Line 117: | ||
or '[[Category:Wikipedia page with obscure subdivision]]' | or '[[Category:Wikipedia page with obscure subdivision]]' | ||
if | if (code1 or "") == "" then | ||
if mw.title.getCurrentTitle().namespace ~= 0 then catnocountry = '' end | if mw.title.getCurrentTitle().namespace ~= 0 then catnocountry = '' end | ||
return catnocountry, '<span style="font-size:100%" class="error">"No parameter for the country given"</span>' | return catnocountry, '<span style="font-size:100%" class="error">"No parameter for the country given"</span>' | ||
end | end | ||
if | if (code2 or "") == "" then --3166-1 code | ||
for alpha2,cdata in pairs(data) do | for alpha2,cdata in pairs(data) do | ||
if findname(alpha2,cdata, | if findname(alpha2,cdata,code1) then | ||
if args["codetype"]=="numeric" or args["codetype"]=="alpha3" then | if args["codetype"]=="numeric" or args["codetype"]=="alpha3" then | ||
return cdata[args["codetype"]] | return cdata[args["codetype"]] | ||
| Line 133: | Line 131: | ||
end | end | ||
end | end | ||
end | |||
if string.find(args[1] or '',"%-") then | |||
args[1], args[2] = string.match(args[1] or '',"^([^%-]*)%-(.*)$") | |||
if args[2] then return p.luacode(args) end | |||
end | end | ||
if mw.title.getCurrentTitle().namespace ~= 0 then catnocountry = '' end | if mw.title.getCurrentTitle().namespace ~= 0 then catnocountry = '' end | ||
| Line 138: | Line 140: | ||
else --3166-2 code | else --3166-2 code | ||
for alpha2,cdata in pairs(data) do | for alpha2,cdata in pairs(data) do | ||
if findname(alpha2,cdata, | if findname(alpha2,cdata,code1) then | ||
if mw.ustring.match(alpha2,"GB") then -- For England, Wales etc. | if mw.ustring.match(alpha2,"GB") then -- For England, Wales etc. | ||
alpha2 = "GB" | alpha2 = "GB" | ||
| Line 147: | Line 149: | ||
if type(scdata)=="table" then | if type(scdata)=="table" then | ||
empty = false | empty = false | ||
if findname(scode,scdata, | if findname(scode,scdata,code2) then | ||
return alpha2.."-"..scode | return alpha2.."-"..scode | ||
end | end | ||
end | end | ||
end | |||
if string.find(args[2] or '',"%-") then | |||
code1, args[2] = string.match(args[2] or '',"^([^%-]*)%-(.*)$") | |||
args[1] = args[1].."-"..code1 | |||
return p.luacode(args) | |||
end | end | ||
if mw.title.getCurrentTitle().namespace ~= 0 then catnosubdivision = '' end | if mw.title.getCurrentTitle().namespace ~= 0 then catnosubdivision = '' end | ||
return catnosubdivision | return catnosubdivision | ||
end | end | ||
end | |||
if string.find(args[1] or '',"%-") then | |||
args[1] = string.match(args[1] or '',"^([^%-]*)%-(.*)$") | |||
return p.luacode(args) | |||
end | end | ||
if mw.title.getCurrentTitle().namespace ~= 0 then catnocountry = '' end | if mw.title.getCurrentTitle().namespace ~= 0 then catnocountry = '' end | ||