Module:Region topic

local p = {}local redirectTarget = require("Module:Redirect").getTargetlocal function blankToNil(s)--Replaces string consisting of only whitespace with nilreturn s and string.find(s, '%S') and s or nilendlocal function unlink(s)return s=="unlink"endlocal yn_map = {yes="y", y="y", ["true"]="y", ["1"]="y", no="n", n="n", ["false"]="n", ["0"]="n", [""]="e"}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 {{yesno}}map = map or {}local fmap = {y = map.y or 1, --yes, y, true, 1              n = map.n or 0, --no, n, false, 0              o = map.o or map.y or 1, --other              e = map.e or map.n or 0, --empty string              u = map.u or map.n or 0} --unspecified (nil)local num = s and fmap[yn_map[s] or "o"] or fmap.ureturn num ~= 0endlocal 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.5endlocal function xor(a,b)--A logical XOR functionreturn not a ~= not bendlocal function fallthrough(t)local i = 1local r = 0.5while r==0.5 and t[i] dor = t[i]i = i + 1endreturn (r and r~=0) and 1 or 0endlocal function loadData(d)if type(d)=="table" thenreturn delseif type(d)=="string" and blankToNil(d) thenreturn mw.loadData(d)elsereturn error("No data page or table specified")endendfunction p.luaMain(frame,args)--Produces the navbox (for other Lua scripts)--Pass through navbox style parameterslocal navboxArgs = {name = args.name or error("No name parameter"),state = args.state or "autocollapse",titlestyle = args.titlestyle,bodystyle = args.bodystyle,abovestyle = args.abovestyle,belowstyle = args.belowstyle,groupstyle = args.groupstyle,liststyle = args.liststyle,listclass = "hlist",image = args.image,above = args.above,border = args.border,navbar = args.navbar}--Load data pagelocal data = loadData(args.data)--Prefix/suffix parameterslocal 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 necessarylocal articleif args.article thenarticle = yn(args.article)elsearticle = (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 redirectslocal noRedirs = yn3(args.noredirects)--Create navbox titleif args.title thennavboxArgs.title = args.titleelselocal 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..suffixif noRedLinks==0 or mw.title.new(fullLink).exists thennavboxArgs.title = "[["..fullLink.."]]"elsenavboxArgs.title = fullLinkendend--Loop over groupslocal nthGroup = 1local nthShownGroup = 1while data["group"..nthGroup] do--If group is not hidden or excludedif 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],{o=0}),data["group"..nthGroup].negate_switch)then--Create list & loop over entrieslocal list = {}local listSortMap = {}for nthCountry,countryData in ipairs(data["group"..nthGroup].data) dolocal code = countryData[1]local countryName = blankToNil(args[code.."_name"]) or countryData[2] or countryData[1]local listItem--Determine if country should be included or notif yn(args[code],{u=1})  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],{o=0}),countryData.negate_switch))then--Determine link targetlocal linkName = countryData.link or countryData[2] or countryData[1]local linkArticle = (article and countryData.the) and "the " or ""local fullLink = not yn(args[code],{o=0}) and args[code] or (prefix..linkArticle..linkName..suffix)--Create list item if not nonexistinglocal noRedLink = fallthrough({yn3(args[code.."_noredlink"],1),noRedLinks,countryData.noredlink or 0})if (args[code] or noRedLink~=1 or mw.title.new(fullLink).exists) and not unlink(args[code]) thenlocal noRedir = fallthrough({yn3(args[code.."_noredirect"]),noRedirs,countryData.noredirect or 0})listItem = "[["..(noRedir==1 and redirectTarget(fullLink) or fullLink).."|"..countryName.."]]"elseif unlink(args[code]) or unlink(args[code.."_noredlink"])       or unlinkRedLinks or unlink(countryData.noredlink)thenlistItem = countryNameendend--Create sub-list if presentif countryData.subgroup thenlocal subGroup = countryData.subgrouplocal subList = {}local subListSortMap = {}for nthSubCountry,subCountryData in ipairs(subGroup) do--Similar to main item codelocal 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],{o=0}) and args[subCode]                    or (prefix..subLinkArticle..subLinkName..suffix)local noRedLink = fallthrough({yn3(args[subCode.."_noredlink"],1),                               noRedLinks,subCountryData.noredlink or 0})if yn(args[subCode],{u=1})   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],{o=0}),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],{o=0}),subCountryData.negate_switch))thenif (args[subCode] or noRedLink~=1 or mw.title.new(subFullLink).exists) and not unlink(args[subCode]) thenlocal noRedir = fallthrough({yn3(args[subCode.."_noredirect"]),                             noRedirs,subCountryData.noredirect or 0})subList[#subList+1] = "<li>[["..(noRedir==1 and redirectTarget(subFullLink) or subFullLink)                      .."|"..subCountryName.."]]</li>"subListSortMap[#subListSortMap+1] = {args[subCode.."_sort"] or args[subCode.."_name"]                                     or subCountryData[2] or subCountryData[1],#subListSortMap+1}elseif unlink(args[subCode]) or unlink(args[subCode.."_noredlink"])       or unlinkRedLinks or unlink(subCountryData.noredlink)thensubList[#subList+1] = "<li>"..subCountryName.."</li>"subListSortMap[#subListSortMap+1] = {args[subCode.."_sort"] or args[subCode.."_name"]                                     or subCountryData[2] or subCountryData[1],#subListSortMap+1}endendend--If non-empty sub-list, add it to country itemif #subList>0 thentable.sort(subListSortMap, function(t1,t2) return t1[1]<t2[1] end)local subListSorted = {}for sortListPosition,sortListEntry in ipairs(subListSortMap) dosubListSorted[sortListPosition] = subList[sortListEntry[2]]endlistItem = (listItem or countryName).."\n<ul>\n"..table.concat(subListSorted,"\n").."\n</ul>"endendif listItem thenlist[#list+1] = "<li>"..listItem.."</li>"listSortMap[#listSortMap+1] = {args[code.."_sort"] or countryName, #listSortMap+1}endend--Add group name and data to navbox argsif data["group"..nthGroup].name thenif string.match(data["group"..nthGroup].name,"%{%{") thennavboxArgs["group"..nthShownGroup] = frame:preprocess(data["group"..nthGroup].name)elsenavboxArgs["group"..nthShownGroup] = data["group"..nthGroup].nameendend--Sort list and move to navbox parameters if not emptyif #list>0 or yn(args.showemptygroups) thentable.sort(listSortMap, function(t1,t2) return t1[1]<t2[1] end)local listSorted = {}for sortListPosition,sortListEntry in ipairs(listSortMap) dolistSorted[sortListPosition] = list[sortListEntry[2]]endnavboxArgs["list"..nthShownGroup] = "<ul>\n"..table.concat(listSorted,"\n").."\n</ul>"nthShownGroup = nthShownGroup + 1endendnthGroup = nthGroup + 1end--Invoke navbox modulereturn require("Module:Navbox")._navbox(navboxArgs)endfunction p.main(frame)--Produces the navbox (for wikitext usage)local args = require("Module:Arguments").getArgs(frame, {removeBlanks = false})return p.luaMain(frame,args)endfunction p.luaList(frame,dataPage)--Produces a list of entities and associated parameters, for--use in template documentation (for other Lua scripts)--Load data pagelocal data = loadData(dataPage)--Create table and header rowlocal table = mw.html.create("table"):addClass("wikitable collapsible"):css("color","#000")local tableHead = table:tag("tr"):css("font-weight","bold")tableHead:tag("th"):css("background-color","#e8e8e8"):wikitext("Code")tableHead:tag("th"):css("background-color","#e8e8e8"):wikitext("Display name [link name]")tableHead:tag("th"):css("background-color","#e8e8e8"):wikitext("Switch")tableHead:tag("th"):css("background-color","#e8e8e8"):wikitext("Hidden?")--Loop over groupslocal nthGroup = 1while data["group"..nthGroup] do--Add group datalocal groupHead = table:tag("tr"):css("background-color","#eaf1fe")groupHead:tag("td")if data["group"..nthGroup].name and string.match(data["group"..nthGroup].name,"%{%{") thengroupHead:tag("td"):css("font-weight","bold"):wikitext(frame:preprocess(data["group"..nthGroup].name))elsegroupHead:tag("td"):css("font-weight","bold"):wikitext(data["group"..nthGroup].name or "<i>Unnamed group</i>")endgroupHead:tag("td"):cssText(data["group"..nthGroup].negate_switch and "text-decoration:overline;" or "")         :wikitext(data["group"..nthGroup].switch or "")groupHead:tag("td"):wikitext(data["group"..nthGroup].hidden and "Yes" or "")--Loop over group entriesfor nthCountry,countryData in ipairs(data["group"..nthGroup].data) do--Add single entry datalocal countryRow = table:tag("tr"):css("background-color","#f8f8f8")countryRow:tag("td"):wikitext(countryData[1])local countryName = countryRow:tag("td"):css("padding-left","1em"):wikitext(countryData[2] or countryData[1])if countryData.the or countryData.link thencountryName:wikitext(" ["..(countryData.the and "the" or "")                         ..(countryData.the and countryData.link and " " or "")                         ..(countryData.link or "").."]")endcountryRow:tag("td"):cssText(countryData.negate_switch and "text-decoration:overline;" or ""):wikitext(countryData.switch or "")countryRow:tag("td"):wikitext(countryData.hidden and "Yes" or (countryData.noredlink and "Depends on existence" or ""))--Add subgroup data if existsif countryData.subgroup thenlocal subListHead = table:tag("tr"):css("background-color","#fefce2")subListHead:tag("td")subListHead:tag("td"):css("padding-left","2em"):css("font-weight","bold"):wikitext("Subgroup")subListHead:tag("td"):cssText(countryData.subgroup.negate_switch and "text-decoration:overline;" or "")                     :wikitext(countryData.subgroup.switch or "")subListHead:tag("td"):wikitext(countryData.subgroup.hidden and "Yes" or "")for nthSubCountry,subCountryData in ipairs(countryData.subgroup) dolocal subCountryRow = table:tag("tr"):css("background-color","#fdfcf4")subCountryRow:tag("td"):wikitext(subCountryData[1])local subCountryName = subCountryRow:tag("td"):css("padding-left","2em"):css("font-style","italic")                                              :wikitext(subCountryData[2] or subCountryData[1])if subCountryData.the or subCountryData.link thensubCountryName:wikitext(" ["..(subCountryData.the and "the" or "")                            ..(subCountryData.the and subCountryData.link and " " or "")                            ..(subCountryData.link or "").."]")endsubCountryRow:tag("td"):cssText(subCountryData.negate_switch and "text-decoration:overline;" or "")                       :wikitext(subCountryData.switch or "")subCountryRow:tag("td"):wikitext(subCountryData.hidden                                 and "Yes" or (subCountryData.noredlink and "Depends on existence" or ""))endendendnthGroup = nthGroup + 1endreturn tostring(table)endfunction p.list(frame)--Produces a list of entities and associated parameters, for--use in template documentation (for wikitext usage)local args = require("Module:Arguments").getArgs(frame)return p.luaList(frame,args.data)endreturn p