Module:Region topic explained

local p =

local redirectTarget = require("Module:Redirect").getTarget

local function blankToNil(s) --Replaces string consisting of only whitespace with nil return s and string.find(s, '%S') and s or nilend

local function unlink(s) return s

"unlink"end

local yn_map = local function yn(s,map) --Converts a "yes"/"no" string s to a boolean. map is a --table that specifies what each type of input should be --interpreted as; its defaults are consistent with map = map or local fmap = --unspecified (nil) local num = s and fmap[yn_map[s] or "o"] or fmap.u return num ~= 0end

local function yn3(s,nrl) --Converts a "yes"/"no" string s to a number (1=yes, 0=no, 0.5=neither) local yn = yn_map[s or ""] return (yn

"y" and 1) or (nrl and unlink(s) and 1) or (yn

"n" and 0) or 0.5end

local function xor(a,b) --A logical XOR function return not a ~= not bend

local function fallthrough(t) local i = 1 local r = 0.5 while r

0.5 and t[i] do r = t[i] i = i + 1 end return (r and r~=0) and 1 or 0end

local function loadData(d) if type(d)

"table" then return d elseif type(d)

"string" and blankToNil(d) then return mw.loadData(d) else return error("No data page or table specified") endend

function p.luaMain(frame,args) --Produces the navbox (for other Lua scripts)

--Pass through navbox style parameters local navboxArgs =

--Load data page local data = loadData(args.data)

--Prefix/suffix parameters local prefix = blankToNil(args.prefix or args[1]) local suffix = blankToNil(args.suffix or args[2]) prefix = (prefix or "")..(not yn(args.noprefixspace) and prefix and " " or "") suffix = (not yn(args.nosuffixspace) and suffix and " " or "")..(suffix or "") --Switch to include the definite article "the" where necessary local article if args.article then article = yn(args.article) else article = (prefix~="" and suffix

"") end --Switch to omit nonexisting articles (0.5 if not specified) local noRedLinks = yn3(args.noredlinks,1) local unlinkRedLinks = unlink(args.noredlinks) --Switch to automatically follow redirects local noRedirs = yn3(args.noredirects)

--Create navbox title if args.title then navboxArgs.title = args.title else local linkName = data.region or error("No region parameter in data page") local linkArticle = (article and data.region_the) and (prefix

"" and "The " or "the ") or "" local fullLink = prefix..linkArticle..linkName..suffix if noRedLinks

0 or mw.title.new(fullLink).exists then navboxArgs.title = ""..fullLink.."" else navboxArgs.title = fullLink end end

--Loop over groups local nthGroup = 1 local nthShownGroup = 1 while data["group"..nthGroup] do --If group is not hidden or excluded if not data["group"..nthGroup].switch or not args[data["group"..nthGroup].switch] and not data["group"..nthGroup].hidden or args[data["group"..nthGroup].switch] and xor(yn(args[data["group"..nthGroup].switch],),data["group"..nthGroup].negate_switch) then --Create list & loop over entries local list = local listSortMap = for nthCountry,countryData in ipairs(data["group"..nthGroup].data) do local code = countryData[1] local countryName = blankToNil(args[code.."_name"]) or countryData[2] or countryData[1] local listItem --Determine if country should be included or not if yn(args[code],) and (args[code] or not (countryData.switch and args[countryData.switch]) and not countryData.hidden or countryData.switch and args[countryData.switch] and xor(yn(args[countryData.switch],),countryData.negate_switch)) then --Determine link target local linkName = countryData.link or countryData[2] or countryData[1] local linkArticle = (article and countryData.the) and "the " or "" local fullLink = not yn(args[code],) and args[code] or (prefix..linkArticle..linkName..suffix) --Create list item if not nonexisting local noRedLink = fallthrough if (args[code] or noRedLink~=1 or mw.title.new(fullLink).exists) and not unlink(args[code]) then local noRedir = fallthrough listItem = ""..countryName.."" elseif unlink(args[code]) or unlink(args[code.."_noredlink"]) or unlinkRedLinks or unlink(countryData.noredlink) then listItem = countryName end end --Create sub-list if present if countryData.subgroup then local subGroup = countryData.subgroup local subList = local subListSortMap = for nthSubCountry,subCountryData in ipairs(subGroup) do --Similar to main item code local subCode = subCountryData[1] local subCountryName = blankToNil(args[subCode.."_name"]) or subCountryData[2] or subCountryData[1] local subLinkName = subCountryData.link or subCountryData[2] or subCountryData[1] local subLinkArticle = (article and subCountryData.the) and "the " or "" local subFullLink = not yn(args[subCode],) and args[subCode] or (prefix..subLinkArticle..subLinkName..suffix) local noRedLink = fallthrough if yn(args[subCode],) and (args[subCode] or (not (subGroup.switch and args[subGroup.switch]) and not subGroup.hidden or subGroup.switch and args[subGroup.switch] and xor(yn(args[subGroup.switch],),subGroup.negate_switch)) and not (subCountryData.switch and args[subCountryData.switch]) and not subCountryData.hidden or subCountryData.switch and args[subCountryData.switch] and xor(yn(args[subCountryData.switch],),subCountryData.negate_switch)) then if (args[subCode] or noRedLink~=1 or mw.title.new(subFullLink).exists) and not unlink(args[subCode]) then local noRedir = fallthrough subList[#subList+1] = "

  • "..subCountryName.."
  • " subListSortMap[#subListSortMap+1] = elseif unlink(args[subCode]) or unlink(args[subCode.."_noredlink"]) or unlinkRedLinks or unlink(subCountryData.noredlink) then subList[#subList+1] = "
  • "..subCountryName.."
  • " subListSortMap[#subListSortMap+1] = end end end --If non-empty sub-list, add it to country item if #subList>0 then table.sort(subListSortMap, function(t1,t2) return t1[1]\n"..table.concat(subListSorted,"\n").."\n" end end if listItem then list[#list+1] = "
  • "..listItem.."
  • " listSortMap[#listSortMap+1] = end end --Add group name and data to navbox args if data["group"..nthGroup].name then if string.match(data["group"..nthGroup].name,"%