Module:Flagg

local titleSelf = mw.title.new(... or mw.getCurrentFrame():getTitle())local titleCountryData = mw.title.new[[Module:CountryData]]if titleSelf.isSubpage thenlocal sandbox = titleSelf.subpageText:match[[^sandbox]]if sandbox thentitleCountryData = titleCountryData:subPageTitle(sandbox)endendlocal CountryData = require(tostring(titleCountryData))local p = {}function p.main(frame)--Get input argumentslocal args = require('Module:Arguments').getArgs(frame,{valueFunc =function(key,value)if value thenvalue = mw.text.trim(value)--Change empty string to nil for all args except 'image' and 'border'if key=="image" or key=="border" or value~="" thenreturn valueendendreturn nilend})--Call main functionreturn p.luaMain(frame,args)endfunction p.luaMain(frame,args)local function emp2nil(x)if x=="" then return nil else return x endendlocal function space2emp(x)if string.find(x,"^%s*$") then return "" else return x endendlocal function nopx(x)if x~=nil and (string.find(x,"^%d+$") or string.find(x,"^%d+px$")) then return string.gsub(x,"^(.*)px","%1") else return nil endend--Country & mode parameterslocal mode = string.lower(args[1] or "usc")local mi = string.sub(mode,1,1)local ms = string.sub(mode,2,2)local mn = string.sub(mode,3,3)local me = string.sub(mode,4,-1)local country = args[2] or ""local countryDataCountry = countryif string.find(me, "i") then    local titleIocCountryData = mw.title.new[[Module:Country alias/data]]    local IocCountryData = require(tostring(titleIocCountryData))    local iocCountryAlias = IocCountryData.countryAliases[country] or country    countryDataCountry = IocCountryData.countries[iocCountryAlias]["countrydata_name"] or IocCountryData.countries[iocCountryAlias]["name"]endlocal avar = args["avar"] or args["altvar"]local clink = args["clink"] or args["link"]--Get country data & altvar datalocal data, alink, amap, asufif avar thenlocal age = args["age"] or ""local aaliasamap, aalias = require("Module:Flagg/Altvar data").alttable(age)avar = string.gsub(string.lower(avar or ""),"[ -]","")avar = aalias[avar] or avarif not amap[avar] then error("Unknown avar") endlocal apar = {altvar=amap[avar].altvar;mw=amap[avar].mw;age=amap[avar].age;variant=args["variant"] or args[3]}data = CountryData.getcachedtable(frame, countryDataCountry, apar)asuf = amap[avar].altlinkalink = data["link alias-"..amap[avar].altvar] or (clink or data["shortname alias"] or data.alias or country).." "..asufelsedata = (args["nodata"] and {}) or CountryData.getcachedtable(frame, countryDataCountry, {variant=args["variant"] or args[3]})avar = ""amap = {[""]={altvar=""}}end--Name and link parametersclink = clink or data.alias or countrylocal pref = args["pref"]local suff = args["suff"] or asuflocal pref2 = args["pref2"]local suff2 = args["suff2"]if not pref and not suff then --Default prefixpref = "Flag of"endlocal yn_map = {[""]=0; ["0"]=0; ["no"]=0; ["n"]=0; ["1"]=1; ["yes"]=1; ["y"]=1}local fthe = (args["pthe"] and yn_map[args["pthe"]]~=0) or (args["the"] and yn_map[args["the"]]~=0)local nthe = (args["nthe"] and yn_map[args["nthe"]]~=0) or (args["the"] and yn_map[args["the"]]~=0)fthe = fthe and (pref and "t" or "T").."he " or ""nthe = nthe and (pref and "t" or "T").."he " or ""local flink = args["plink"] or args["flink"] or alink              or clink=="" and "" or space2emp((pref or "").." ")..fthe..clink..space2emp(" "..(suff or ""))local flink2if args["plink2"] or args["flink2"] or pref2 or suff2 thenflink2 = args["plink2"] or args["flink2"] or clink=="" and "" or space2emp((pref2 or "").." ")..fthe..clink..space2emp(" "..(suff2 or ""))elseflink2 = flinkendlocal fsec = args["psection"] or args["section"]local fsec2 = args["psection2"] or args["section"]local csec = args["csection"] or args["section"]fsec = fsec and "#"..fsec or ""fsec2 = fsec2 and "#"..fsec2 or ""csec = csec and "#"..csec or ""if string.find(me,"f") thenif mw.title.new( flink ).exists == false thenif flink2 ~= flink and mw.title.new ( flink2 ).exists == true thenflink = flink2fsec = fsec2elseflink = clinkendendendlocal name = args["name"]if not name thenlocal cname = string.find(me,"e") and (data["name alias-"..amap[avar].altvar] or data["shortname alias"] or data.alias) or countryif mn == "f" thenname = cname=="" and "" or space2emp((pref or "").." ")..nthe..cname..space2emp(" "..(suff or ""))elsename = cnameendend--Image parameterslocal pimage = args["image"]local placeholder = "Flag placeholder.svg"local variant = args["variant"] or args[3] or ""local image_map = {[""]=placeholder; ["none"]=placeholder; ["blank"]=placeholder}if pimage then --Remove namespacepimage = string.gsub(pimage,"^[Ff][Ii][Ll][Ee]:","")pimage = string.gsub(pimage,"^[Ii][Mm][Aa][Gg][Ee]:","")endlocal iname = image_map[pimage] or pimagelocal size = args["size"] or args["sz"]local size_map = {xs="12x8px"; s="17x11px"; m="23x15px"; l="32x21px"; xl="46x30px"}if size==nil or string.find(size,"^%d*x?%d+px$") then--valid EIS size (..px, x..px or ..x..px) or unsetelseif string.find(size,"^%d*x?%d+$") then --EIS size without "px" suffixsize=size.."px"else --size from map, or invalid valuesize = size_map[size] or nilendlocal border = args["border"]if iname thensize = size or "23x15px"if yn_map[border]==0 then border = "" else border = "|border" endelseiname = data["flag alias-"..amap[avar].altvar.."-"..variant] or data["flag alias-"..variant] or data["flag alias-"..amap[avar].altvar] or data["flag alias"] or placeholdersize = size or emp2nil(data.size) or "23x15px"if border thenif yn_map[border]==0 then border = "" else border = "|border" endelselocal autoborder = data["border-"..variant] or data["border-"..amap[avar].altvar] or data.borderif autoborder and autoborder~="border" then border = "" else border = "|border" endendendlocal am = ""if args["alt"] or string.find(me,"a") thenam = args["alt"] or args["name"] or countryam = am.."|"..amend--Build display namelocal text = args["text"]if not text thenif mn=="x" then --no texttext = ""elseif mn=="p" or mn=="f" or mn == "*" then --prefix/suffix linktext = flink=="" and name or "[["..flink..fsec.."|"..name.."]]"-- By the principle of least surprise, there should be some-- indication that the link [[X of Country|Country]] does not-- go to [[Country]].if mn == "*" and clink ~= flink thentext = text:sub(1, -3) .. "\226\128\175*]]" endelseif mn=="l" then --display link targettext = flink=="" and name or "[["..flink..fsec.."|"..(args["name"] or flink).."]]"elseif mn=="b" then --both prefix/suffix and normal country linklocal preflink = pref and (flink=="" and pref.." " or "[["..flink..fsec.."|"..pref.."]] ") or ""local sufflink = suff and (flink=="" and " "..suff or " [["..flink..fsec.."|"..suff.."]]") or ""local namelink = (name=="" and "" or nthe)..(clink=="" and name or "[["..clink..csec.."|"..name.."]]")text = preflink..namelink..sufflinkelseif mn=="d" then --data templatelocal title = mw.title.new("Template:Country data "..country)--check if redirectif title.isRedirect thentext = "<span class=\"plainlinks\">["..title:fullUrl("redirect=no").." "..name.."]</span>"elsetext = "[["..title.fullText.."|"..name.."]]"endelseif mn=="u" then --unlinkedtext = nameelseif mn=="a" then --abbrtext = name==country and (clink==country and country or "<abbr title='"..clink.."'>"..country.."</abbr>") or ("<abbr title='"..name.."'>"..country.."</abbr>")else --country link (default)text = clink=="" and name or "[["..clink..csec.."|"..name.."]]"endif string.find(me,"p") thentext = "("..text..")"endendif type(text) == 'function' thentext = text(data)end--Define separatorlocal separator = "&nbsp;" --non-breaking space (n)if ms=="x" then --no separatorseparator = ""elseif ms=="l" then --line breakseparator = "<br />"end--Add preftext, if usedlocal preftext = args["preftext"]if (preftext or "") ~= "" thentext = preftext..separator..textend--Build imagelocal ilink = args["ilink"]if not ilink thenif mi=="x" or (iname==placeholder and pimage~=placeholder) then --no image/invisible imageiname = placeholderborder = ""ilink = "|link="am = ""elseif mi=="i" then --image page linkilink = ""elseif mi=="c" then --country linkilink = "|link="..clink..(clink=="" and "" or csec)elseif mi=="p" or mi=="f" then --prefix/suffix linkilink = "|link="..flink..(flink=="" and "" or fsec)elseif mi=="d" then --data templatelocal title = mw.title.new("Template:Country data "..country)--check if redirectif title.isRedirect thenilink = "|link="..title:fullUrl("redirect=no")elseilink = "|link="..title.fullTextendelse --unlinked (default)ilink = "|link="endendif am == "" and string.find(me,"l") thenam = mw.ustring.sub(ilink,7,-1)endlocal image = "[[File:"..iname.."|"..size..border..ilink.."|alt="..am.."]]"if iname==placeholder thenif require('Module:yesno')(args["noredlink"]) == false or args["noredlink"] == "notext" thenif country ~= '' and data.alias == nil theniname = ''image = "[[:Template:Country data "..country.."]]"if args["noredlink"] == "notext" thentext = ''endendendif (args["missingcategory"] or '') ~= '' thenimage = image..args["missingcategory"]endif string.find(me,"b") thentext = ''endend--Combine image and name with separatorlocal align_map = {left="left", l="left", center="center", centre="center", c="center", middle="center", m="center", right="right", r="right"}local align = align_map[args["align"] or args["al"]]local nalign =  align_map[args["nalign"] or args["nal"]]local outif string.find(me,"r") then--image right of nameif (ms=="x" and mi=="x") or (string.find(me,"o") and iname==placeholder and pimage~=placeholder) then --name onlyout = textelseif ms=="x" or ms=="n" or ms=="l" then --no separator, non-breaking space, or line breakout = text.."<span class=\"flagicon\">"..separator..image.."</span>"elseif ms=="t" then --table cellout = "style=\"text-align:"..(nalign or "left").."\"|"..text.."||style=\"text-align:"..(align or "center").."\"|<span class=\"flagicon\">"..image.."</span>"else --fixed-width span box (default)local width = args["width"] or args["w"] or require("Module:Flaglist").luawidth(size)out = text.."&nbsp;<span class=\"flagicon\" style=\"display:inline-block;width:"..width.."px;text-align:"..(align or "right").."\">"..image.."</span>"endelse --image left of nameif (ms=="x" and mi=="x") or (string.find(me,"o") and iname==placeholder and pimage~=placeholder) then --name onlyout = textelseif ms=="x" or ms=="n" or ms=="l" then --no separator, non-breaking space, or line breakout = "<span class=\"flagicon\">"..image..separator.."</span>"..textelseif ms=="t" then --table cellout = "style=\"text-align:"..(align or "center")..";\"|<span class=\"flagicon\">"..image.."</span>||style=\"text-align:"..(nalign or "left").."\"|"..textelse --fixed-width span box (default)local width = nopx(args["width"] or args["w"]) or require("Module:Flaglist").luawidth(size)out = "<span class=\"flagicon\" style=\"display:inline-block;width:"..width.."px;text-align:"..(align or "left").."\">"..image.."</span>&nbsp;"..textendendif string.find(me,"w") then --avoid wrappingout = "<span class=\"nowrap\">"..out.."</span>"end-- align in tableif me:find("t") thenout = 'style="text-align:'..(align or 'left')..'"|'..outend--Tracking categorieslocal cat = ""if pimage and not image_map[pimage] and country~="" and data["flag alias"] and not args.demo thencat = "[[Category:Pages using Flagg with specified image instead of data template image]]"endreturn out..catendreturn p