Module:Flagg: Difference between revisions

Content deleted Content added
m Fix
Copy from sandbox
 
(39 intermediate revisions by 10 users not shown)
Line 1:
local titleSelf = mw.title.new(... or mw.getCurrentFrame():getTitle())
local titleCountryData = mw.title.new[[Module:CountryData]]
if titleSelf.isSubpage then
local sandbox = titleSelf.subpageText:match[[^sandbox]]
if sandbox then
titleCountryData = titleCountryData:subPageTitle(sandbox)
end
end
local CountryData = require(tostring(titleCountryData))
local p = {}
 
function p.main(frame)
 
--Get input arguments
function emp2nil(x)
local args = require('Module:Arguments').getArgs(frame,{valueFunc =
if x=="" then return nil else return x end
function(key,value)
end
if value then
function space2emp(x)
value = mw.text.trim(value)
if string.find(x,"^%s*$") then return "" else return x end
--Change empty string to nil for all args except 'image' and 'border'
end
if key=="image" or key=="border" or value~="" then
function nopx(x)
return value
if x~=nil and (string.find(x,"^%d+$") or string.find(x,"^%d+px$")) then return string.gsub(x,"^(.*)px","%1") else return nil end
end
end
return nil
end
})
 
--Call main function
return p.luaMain(frame,args)
 
end
 
function p.luaMain(frame,args)
--Get input arguments
local args = require('Module:Arguments').getArgs(frame)
 
local function emp2nil(x)
--Country & mode parameters
if x=="" then return nil else return x end
local mode = string.lower(args[1] or "usc")
end
local mi = string.sub(mode,1,1)
local msfunction = string.subspace2emp(mode,2,2x)
if string.find(x,"^%s*$") then return "" else return x end
local mn = string.sub(mode,3,3)
end
local me = string.sub(mode,4,-1)
local 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 end
end
 
--Country & mode parameters
local country = args[2] or ""
local avarmode = string.lower(args["avar"1] or args["altvarusc"])
local clinkmi = args["clink"] or args["link"]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)
 
--Get local country data= &args[2] altvaror data""
local countryDataCountry = country
local data, alink, amap, asuf
if avarstring.find(me, "i") then
local titleIocCountryData = mw.title.new[[Module:Country alias/data]]
local age = args["age"] or ""
local IocCountryData = require(tostring(titleIocCountryData))
local aalias
local iocCountryAlias = IocCountryData.countryAliases[country] or country
amap, aalias = require("Module:Flagg/Altvar data").alttable(age)
countryDataCountry = IocCountryData.countries[iocCountryAlias]["countrydata_name"] or IocCountryData.countries[iocCountryAlias]["name"]
avar = string.gsub(string.lower(avar or ""),"[ \-]","")
end
avar = aalias[avar] or avar
local avar if= not amapargs["avar"] thenor error(args["Unknown avaraltvar") end]
local aparclink = {altvar=amap[avar].altvar;mw=amap[avar].mw;age=amap[avar].age;variant=args["variantclink"] or args[3"link"]}
data = require("Module:CountryData").gettable(frame,country,apar)
asuf = amap[avar].altlink
alink = data["link alias-"..amap[avar].altvar] or (clink or data["shortname alias"] or data.alias or country).." "..asuf
else
data = require("Module:CountryData").gettable(frame,country,{variant=args["variant"] or args[3]})
avar = ""
amap = {[""]={altvar=""}}
end
 
--Get country data & altvar data
--Name and link parameters
local data, alink, amap, asuf
clink = clink or data.alias or country
if avar then
local age = args["age"] or ""
local aalias
amap, aalias = require("Module:Flagg/Altvar data").alttable(age)
avar = string.gsub(string.lower(avar or ""),"[ -]","")
avar = aalias[avar] or avar
if not amap[avar] then error("Unknown avar") end
local 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].altlink
alink = data["link alias-"..amap[avar].altvar] or (clink or data["shortname alias"] or data.alias or country).." "..asuf
else
data = (args["nodata"] and {}) or CountryData.getcachedtable(frame, countryDataCountry, {variant=args["variant"] or args[3]})
avar = ""
amap = {[""]={altvar=""}}
end
 
--Name and link parameters
local pref = args["pref"]
clink = clink or data.alias or country
local suff = args["suff"] or asuf
if not pref and not suff --Default prefix
then pref = "Flag of"
end
local yn_map = {[""]=0; ["0"]=0; ["no"]=0; ["n"]=0; ["1"]=1; ["yes"]=1; ["y"]=1}
local the = args["the"] and yn_map[args["the"]]~=0
if the and pref
then the = "the "
elseif the
then the = "The "
else the = ""
end
local flink = args["flink"] or alink or space2emp((pref or "").." ")..the..clink..space2emp(" "..(suff or ""))
 
local namepref = args["namepref"]
local suff = args["suff"] or asuf
if string.find(me,"e") then
local pref2 = args["pref2"]
local cname = data["name alias-"..amap[avar].altvar] or data["shortname alias"] or data.alias or country
local suff2 = args["suff2"]
if mn == "f" then
if not pref and not suff then --Default prefix
name = name or space2emp((pref or "").." ")..the..cname..space2emp(" "..(suff or ""))
pref = "Flag of"
else
end
name = name or cname
local yn_map = {[""]=0; ["0"]=0; ["no"]=0; ["n"]=0; ["1"]=1; ["yes"]=1; ["y"]=1}
end
local fthe = (args["pthe"] and yn_map[args["pthe"]]~=0) or (args["the"] and yn_map[args["the"]]~=0)
else
local nthe = (args["nthe"] and yn_map[args["nthe"]]~=0) or (args["the"] and yn_map[args["the"]]~=0)
if mn == "f" then
name fthe = namefthe orand space2emp((pref orand "t").." or "T")..the..country..space2emp("he "..(suff or ""))
nthe = nthe and (pref and "t" or "T").."he " or ""
else
local flink = args["plink"] or args["flink"] or alink
name = name or country
or clink=="" and "" or space2emp((pref or "").." ")..fthe..clink..space2emp(" "..(suff or ""))
end
local flink2
end
if args["plink2"] or args["flink2"] or pref2 or suff2 then
flink2 = args["plink2"] or args["flink2"] or clink=="" and "" or space2emp((pref2 or "").." ")..fthe..clink..space2emp(" "..(suff2 or ""))
else
flink2 = flink
end
local 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") then
--Image parameters
if mw.title.new( flink ).exists == false then
local image = frame:getParent().args["image"] or frame.args["image"]
if flink2 ~= flink and mw.title.new ( flink2 ).exists == true then
local placeholder = "Flag placeholder.svg"
flink = flink2
local variant = args["variant"] or args[3] or ""
fsec = fsec2
local image_map = {[""]=placeholder; ["none"]=placeholder; ["blank"]=placeholder}
else
if image then --Remove namespace
flink = clink
image = string.gsub(image,"^[Ff][Ii][Ll][Ee]:(.*)$","%1")
end
image = string.gsub(image,"^[Ii][Mm][Aa][Gg][Ee]:(.*)$","%1")
end
end
local iname = image_map[image] or image
 
local sizename = args["size"] or args["szname"]
if not name then
local size_map = {xs="12x8px"; s="17x11px"; m="23x15px"; l="32x21px"; xl="46x30px"}
local cname = string.find(me,"e") and (data["name alias-"..amap[avar].altvar] or data["shortname alias"] or data.alias) or country
if size==nil or string.find(size,"^%d*x?%d+px$")
if mn == "f" then
then --valid EIS size (..px, x..px or ..x..px), or unset
name = cname=="" and "" or space2emp((pref or "").." ")..nthe..cname..space2emp(" "..(suff or ""))
elseif string.find(size,"^%d*x?%d+$")
else
then size=size.."px" --EIS size without "px" suffix
name = cname
else size = size_map[size] or nil
end
end
local border = frame:getParent().args["border"] or frame.args["border"]
 
--Image parameters
if iname then
local pimage = args["image"]
size = size or "23x15px"
local placeholder = "Flag placeholder.svg"
if yn_map[border]==0 then border = "" else border = "|border" end
local variant = args["variant"] or args[3] or ""
else
local image_map = {[""]=placeholder; ["none"]=placeholder; ["blank"]=placeholder}
iname = data["flag alias-"..amap[avar].altvar.."-"..variant] or data["flag alias-"..variant] or data["flag alias-"..amap[avar].altvar] or data["flag alias"] or placeholder
if pimage then --Remove namespace
size = size or emp2nil(data.size) or "23x15px"
pimage = string.gsub(pimage,"^[Ff][Ii][Ll][Ee]:","")
if border then
pimage = string.gsub(pimage,"^[Ii][Mm][Aa][Gg][Ee]:","")
if yn_map[border]==0 then border = "" else border = "|border" end
end
else
local iname = image_map[pimage] or pimage
if data["border-"..variant] or data["border-"..amap[avar].altvar] or data.border then border = "" else border = "|border" end
end
end
 
local amsize = args["size"] or args["sz"]
local size_map = {xs="12x8px"; s="17x11px"; m="23x15px"; l="32x21px"; xl="46x30px"}
if args["alt"] or string.find(me,"a") then
if size==nil or string.find(size,"^%d*x?%d+px$") then
am = args["alt"] or args["name"] or country
--valid EIS size (..px, x..px or ..x..px) or unset
am = am.."|"..am
elseif string.find(size,"^%d*x?%d+$") then --EIS size without "px" suffix
end
size=size.."px"
else --size from map, or invalid value
size = size_map[size] or nil
end
local border = args["border"]
 
if iname then
--Build display name
size = size or "23x15px"
local text = args["text"]
if yn_map[border]==0 then border = "" else border = "|border" end
if not text then
else
if mn=="x" --no text
iname = data["flag alias-"..amap[avar].altvar.."-"..variant] or data["flag alias-"..variant] or data["flag alias-"..amap[avar].altvar] or data["flag alias"] or placeholder
then text = ""
size = size or emp2nil(data.size) or "23x15px"
elseif mn=="p" or mn=="f" --prefix/suffix link
if border then
then text = "[["..flink.."|"..name.."]]"
if yn_map[border]==0 then border = "" else border = "|border" end
elseif mn=="b" --both prefix/suffix and normal country link
else
then
local autoborder = data["border-"..variant] or data["border-"..amap[avar].altvar] or data.border
if pref and suff
if autoborder and autoborder~="border" then border = "" else border = "|border" end
then text = "[["..flink.."|"..pref.."]] "..the.."[["..clink.."|"..name.."]] [["..flink.."|"..suff.."]]"
end
elseif pref
end
then text = "[["..flink.."|"..pref.."]] "..the.."[["..clink.."|"..name.."]]"
elseif suff
then text = the.."[["..clink.."|"..name.."]] [["..flink.."|"..suff.."]]"
else
text = the.."[["..clink.."|"..name.."]]"
end
elseif mn=="d" --data template
then --check if redirect
if require('Module:Redirect').luaIsRedirect("Template:Country data "..country)
then text = "<span class=\"plainlinks\">["..mw.title.new("Template:Country data "..country):fullUrl("redirect=no").." "..name.."]</span>"
else text = "[[Template:Country data "..country.."|"..name.."]]"
end
elseif mn=="u" --unlinked
then text = name
else --country link (default)
text = "[["..clink.."|"..name.."]]"
end
end
 
local am = ""
--Build image
if args["alt"] or string.find(me,"a") then
local ilink = args["ilink"]
am = args["alt"] or args["name"] or country
if not ilink then
am = am.."|"..am
if mi=="x" or (iname==placeholder and image~=placeholder) --no image/invisible image
end
then iname = placeholder border = "" ilink = "|link=" am = ""
elseif mi=="i" --image page link
--Build display name
then ilink = ""
local text = args["text"]
elseif mi=="c" --country link
if not text then
then ilink = "|link="..clink
if mn=="x" then --no text
elseif mi=="p" or mi=="f" --prefix/suffix link
text = ""
then ilink = "|link="..flink
elseif mn=="p" or mn=="f" or mn == "*" then --prefix/suffix link
elseif mi=="d" --data template
text = flink=="" and name or "[["..flink..fsec.."|"..name.."]]"
then --check if redirect
-- By the principle of least surprise, there should be some
if require('Module:Redirect').luaIsRedirect("Template:Country data "..country)
-- indication that the link [[X of Country|Country]] does not
then ilink = "|link="..mw.title.new("Template:Country data "..country):fullUrl("redirect=no")
-- go to [[Country]].
else ilink = "|link=Template:Country data "..country
if mn == "*" and clink ~= flink then
end
text = text:sub(1, -3) .. "\226\128\175*]]" end
else --unlinked (default)
elseif mn=="l" then --display link target
ilink = "|link="
text = flink=="" and name or "[["..flink..fsec.."|"..(args["name"] or flink).."]]"
end
elseif mn=="b" then --both prefix/suffix and normal country link
end
local imagepreflink = pref and (flink=="[[File:"..iname and pref.."| " or "[["..sizeflink..border..ilinkfsec.."|alt="..ampref.."]] ") 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..sufflink
elseif mn=="d" then --data template
local title = mw.title.new("Template:Country data "..country)
--check if redirect
if title.isRedirect then
text = "<span class=\"plainlinks\">["..title:fullUrl("redirect=no").." "..name.."]</span>"
else
text = "[["..title.fullText.."|"..name.."]]"
end
elseif mn=="u" then --unlinked
text = name
elseif mn=="a" then --abbr
text = 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.."]]"
end
if string.find(me,"p") then
text = "("..text..")"
end
end
if type(text) == 'function' then
text = text(data)
end
 
--Combine image and name withDefine separator
local alignseparator = args["align&nbsp;"] or args["al"]
if ms=="x" then --no separator
local nalign = args["nalign"] or args["nal"]
separator = ""
local align_map = {left="left", l="left", center="center", centre="center", c="center", middle="center", m="center", right="right", r="right"}
elseif ms=="l" then --line break
local out
separator = "<br />"
if string.find(me,"r")
end
then --image right of name
if ms=="x" and mi=="x" --name only
--Add preftext, if used
then out = text
local preftext = args["preftext"]
elseif ms=="x" --no separator
if (preftext or "") ~= "" then
then out = text.."<span class=\"flagicon\">"..image.."</span>"
text = preftext..separator..text
elseif ms=="n" --non-breaking space
end
then out = text.."<span class=\"flagicon\">&nbsp;"..image.."</span>"
elseif ms=="l" --line break
--Build image
then out = text.."<span class=\"flagicon\"><br/>"..image.."</span>"
local ilink = args["ilink"]
elseif ms=="t" --table cell
if not ilink then
then out = "style=\"text-align:"..(align_map[nalign] or "left").."\"|"..text.."||style=\"text-align:"..(align_map[align] or "center")..";\"|<span class=\"flagicon\">"..image.."</span>"
if mi=="x" or (iname==placeholder and pimage~=placeholder) then --no image/invisible image
else --fixed-width span box (default)
iname = placeholder
local width = args["width"] or args["w"] or require("Module:Flaglist/size").luawidth(size)
border = ""
out = text.."<span class=\"flagicon\" style=\"display:inline-block;width:"..width.."px;text-align:"..(align_map[align] or "right")..";\">"..image.."</span>"
ilink = "|link="
end
am = ""
else --image left of name
elseif mi=="i" then --image page link
if ms=="x" and mi=="x" --name only
ilink = ""
then out = text
elseif msmi=="xc" then --nocountry separatorlink
ilink = "|link="..clink..(clink=="" and "" or csec)
then out = "<span class=\"flagicon\">"..image.."</span>"..text
elseif mi=="p" or mi=="f" then --prefix/suffix link
elseif ms=="n" --non-breaking space
ilink = "|link="..flink..(flink=="" and "" or fsec)
then out = "<span class=\"flagicon\">"..image.."&nbsp;</span>"..text
elseif msmi=="ld" then --linedata breaktemplate
local title = mw.title.new("Template:Country data "..country)
then out = "<span class=\"flagicon\">"..image.."<br/></span>"..text
--check if redirect
elseif ms=="t" --table cell
if title.isRedirect then
then out = "style=\"text-align:"..(align_map[align] or "center")..";\"|<span class=\"flagicon\">"..image.."</span>||style=\"text-align:"..(align_map[nalign] or "left").."\"|"..text
ilink = "|link="..title:fullUrl("redirect=no")
else --fixed-width span box (default)
else
local width = nopx(args["width"] or args["w"]) or require("Module:Flaglist/size").luawidth(size)
ilink = "|link="..title.fullText
out = "<span class=\"flagicon\" style=\"display:inline-block;width:"..width.."px;text-align:"..(align_map[align] or "left")..";\">"..image.."</span>"..text
end
else --unlinked (default)
end
ilink = "|link="
if string.find(me,"w") --avoid wrapping
end
then out = "<span class=\"nowrap\">"..out.."</span>"
end
if am == "" and string.find(me,"l") then
am = mw.ustring.sub(ilink,7,-1)
end
local image = "[[File:"..iname.."|"..size..border..ilink.."|alt="..am.."]]"
 
if iname==placeholder then
if require('Module:yesno')(args["noredlink"]) == false or args["noredlink"] == "notext" then
if country ~= '' and data.alias == nil then
iname = ''
image = "[[:Template:Country data "..country.."]]"
if args["noredlink"] == "notext" then
text = ''
end
end
end
if (args["missingcategory"] or '') ~= '' then
image = image..args["missingcategory"]
end
if string.find(me,"b") then
text = ''
end
end
 
--Combine image and name with separator
local 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 out
if string.find(me,"r") then
--image right of name
if (ms=="x" and mi=="x") or (string.find(me,"o") and iname==placeholder and pimage~=placeholder) then --name only
out = text
elseif ms=="x" or ms=="n" or ms=="l" then --no separator, non-breaking space, or line break
out = text.."<span class=\"flagicon nowrap\">"..separator..image.."</span>"
elseif ms=="t" then --table cell
out = "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.."<span class=\"nowrap\">&nbsp;<span class=\"flagicon\" style=\"display:inline-block;width:"..width.."px;text-align:"..(align or "right").."\">"..image.."</span></span>"
end
else --image left of name
if (ms=="x" and mi=="x") or (string.find(me,"o") and iname==placeholder and pimage~=placeholder) then --name only
out = text
elseif ms=="x" or ms=="n" or ms=="l" then --no separator, non-breaking space, or line break
out = "<span class=\"flagicon\">"..image..separator.."</span>"..text
elseif ms=="t" then --table cell
out = "style=\"text-align:"..(align or "center")..";\"|<span class=\"flagicon\">"..image.."</span>||style=\"text-align:"..(nalign or "left").."\"|"..text
else --fixed-width span box (default)
local width = nopx(args["width"] or args["w"]) or require("Module:Flaglist").luawidth(size)
out = "<span class=\"nowrap\"><span class=\"flagicon\" style=\"display:inline-block;width:"..width.."px;text-align:"..(align or "left").."\">"..image.."</span>&nbsp;</span>"..text
end
end
-- align in table
if me:find("t") then
out = 'style="text-align:'..(align or 'left')..'"|'..out
end
 
--Tracking categories
return out
local cat = ""
if pimage and not image_map[pimage] and country~="" and data["flag alias"] and not args.demo then
cat = "[[Category:Pages using Flagg with specified image instead of data template image]]"
end
 
return out..cat
 
end
 
p[''] = p.main
 
return p