Module:ISO 3166/sandbox: Difference between revisions

Content deleted Content added
Make luacodeimpl() a subfunction (past or present tense summaries??)
Handle multiple hyphens
Line 105:
-- Returns tuple (code,errorcat,errortext). code is empty string upon error because MW stops at first nil
function p.luacode(args)
local catnocountry = (args.nocat and args.nocat == 'true') and ''
or '[[Category:Wikipedia page with obscure country]]'
local catnosubdivision = (args.nocat and args.nocat == 'true') and ''
or '[[Category:Wikipedia page with obscure subdivision]]'
local function luacodeimpl(args)
Line 111 ⟶ 116:
if args["codetype"]=="3" then
args["codetype"]="alpha3"
end
local catnocountry = (args.nocat and args.nocat == 'true') and ''
or '[[Category:Wikipedia page with obscure country]]'
local catnosubdivision = (args.nocat and args.nocat == 'true') and ''
or '[[Category:Wikipedia page with obscure subdivision]]'
if not country then
if mw.title.getCurrentTitle().namespace ~= 0 then catnocountry = '' end
return '',catnocountry,'<span class="error">ISO 3166: name not specified</span>'
end
Line 166 ⟶ 161:
local a1,a2 = args[1],args[2]
 
localif hyphenPosnot = string.find(a1 or '',a1 == '-', 1, true)then
if mw.title.getCurrentTitle().namespace ~= 0 then catnocountry = '' end
return '',catnocountry,'<span class="error">ISO 3166: name not specified</span>'
end
 
if a2 ~= nil and a2 ~= '' then --explicit subdivision, no splitting
return luacodeimpl(args)
end
 
-- Which hyphen to split on, for cases in order of likelihood:
-- none | <country>
-- first | <country>-<subdivision>
-- first | <country>-<hyphenated-subdivision>
-- none | <hyphenated-country>
-- later | <hyphenated-country>-<subdivision>
-- later | <hyphenated-country>-<hyphenated-subdivision>
-- Therefore, first try splitting on first, then on none, then other left-to-right.
 
local hyphenPos = a1:find('-',1,true)
 
if not hyphenPos then --no hyphen
return luacodeimpl(args)
end
 
args[1] = a1:sub(1, hyphenPos-1) --Try splittingSplit on hyphenfirst
args[2] = a1:sub(hyphenPos+1)
local result = luacodeimpl(args)
Line 178 ⟶ 192:
end
 
-- (a2 is known to be blank by this point)
args[1],args[2] = a1,a2 --TryDon't again without splittingsplit
return luacodeimpl(args)
local result = luacodeimpl(args)
if result ~= '' then
return result
end
 
while true do --Split on rest
hyphenPos = a1:find('-', hyphenPos+1, true)
if not hyphenPos then break end
 
args[1] = a1:sub(1, hyphenPos-1)
args[2] = a1:sub(hyphenPos+1)
result = luacodeimpl(args)
if notresult country~= '' then
return result
end
end
 
if mw.title.getCurrentTitle().namespace ~= 0 then catnocountry = '' end
return '', catnocountry..catnosubdivision
 
end