Module:Flagg: Difference between revisions

Content deleted Content added
From /sandbox; unlink if no country name or custom link is given to prevent prefix-only links (e.g. Flag of) and broken links (e.g. [[|Name]])
Copy from sandbox
 
(26 intermediate revisions by 8 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
local args = require('Module:Arguments').getArgs(frame,{valueFunc =
function(key,value)
if value then
value = mw.text.trim(value)
--Change empty string to nil for all args except 'image' and 'border'
if key=="image" or key=="border" or value~="" then
return value
end
end
return nil
end
})
 
--Call main function
return p.luaMain(frame,args)
 
end
Line 24 ⟶ 33:
function p.luaMain(frame,args)
 
local function emp2nil(x)
if x=="" then return nil else return x end
end
local function space2emp(x)
if string.find(x,"^%s*$") then return "" else return x end
end
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 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 = country
local avar = args["avar"] or args["altvar"]
if string.find(me, "i") then
local clink = args["clink"] or args["link"]
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"]
end
local avar = args["avar"] or args["altvar"]
local clink = args["clink"] or args["link"]
 
--Get country data & altvar data
local data, alink, amap, asuf
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 = require("Module:CountryData").gettablegetcachedtable(frame,country 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 = require(args["Module:CountryDatanodata"] and {}) or CountryData.gettablegetcachedtable(frame,country countryDataCountry, {variant=args["variant"] or args[3]})
avar = ""
amap = {[""]={altvar=""}}
end
 
--Name and link parameters
clink = clink or data.alias or country
 
local pref = args["pref"]
local suff = args["suff"] or asuf
local pref2 = args["pref2"]
if not pref and not suff then --Default prefix
local suff2 = args["suff2"]
pref = "Flag of"
if not pref and not suff then --Default prefix
end
pref = "Flag of"
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)
local ntheyn_map = (args{["nthe"]=0; and["0"]=0; yn_map[args"no"]=0; ["nthen"]]~=0); or["1"]=1; (args["theyes"]=1; and yn_map[args["they"]]~=0)1}
local fthe = fthe and (prefargs["pthe"] and yn_map[args["tpthe"]]~=0) or (args["Tthe").."he] " orand yn_map[args["the"]]~=0)
local nthe = (args["nthe"] and (pref and yn_map[args["tnthe"]]~=0) or (args["Tthe").."he] " orand yn_map[args["the"]]~=0)
local flink fthe = args[fthe and (pref and "plinkt"] or args["flinkT").."he "] or alink""
nthe = nthe and or clink==""(pref and "t" or space2emp((pref or "T").."he ")..fthe..clink..space2emp(" "..(suff or ""))
local fsecflink = args["psectionplink"] or args["sectionflink"] or alink
or clink=="" and "" or space2emp((pref or "").." ")..fthe..clink..space2emp(" "..(suff or ""))
local csec = args["csection"] or args["section"]
local flink2
fsec = fsec and "#"..fsec or ""
if args["plink2"] or args["flink2"] or pref2 or suff2 then
csec = csec and "#"..csec or ""
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
local name = args["name"]
if mw.title.new( flink ).exists == false then
if not name then
if flink2 ~= flink and mw.title.new ( flink2 ).exists == true then
local cname = string.find(me,"e") and (data["name alias-"..amap[avar].altvar] or data["shortname alias"] or data.alias) or country
flink = flink2
if mn == "f" then
fsec = fsec2
name = cname=="" and "" or space2emp((pref or "").." ")..nthe..cname..space2emp(" "..(suff or ""))
else
flink = clink
name = cname
end
end
end
 
local name = args["name"]
--Image parameters
if not name then
local pimage = args["image"]
local cname = string.find(me,"e") and (data["name alias-"..amap[avar].altvar] or data["shortname alias"] or data.alias) or country
local placeholder = "Flag placeholder.svg"
if mn == "f" then
local variant = args["variant"] or args[3] or ""
name = cname=="" and "" or space2emp((pref or "").." ")..nthe..cname..space2emp(" "..(suff or ""))
local image_map = {[""]=placeholder; ["none"]=placeholder; ["blank"]=placeholder}
else
if pimage then --Remove namespace
name = cname
pimage = string.gsub(pimage,"^[Ff][Ii][Ll][Ee]:","")
end
pimage = string.gsub(pimage,"^[Ii][Mm][Aa][Gg][Ee]:","")
end
local iname = image_map[pimage] or pimage
 
--Image parameters
local size = args["size"] or args["sz"]
local pimage = args["image"]
local size_map = {xs="12x8px"; s="17x11px"; m="23x15px"; l="32x21px"; xl="46x30px"}
local placeholder = "Flag placeholder.svg"
if size==nil or string.find(size,"^%d*x?%d+px$") then
local variant = args["variant"] or args[3] or ""
--valid EIS size (..px, x..px or ..x..px) or unset
local image_map = {[""]=placeholder; ["none"]=placeholder; ["blank"]=placeholder}
elseif string.find(size,"^%d*x?%d+$") then --EIS size without "px" suffix
if pimage then --Remove namespace
size=size.."px"
pimage = string.gsub(pimage,"^[Ff][Ii][Ll][Ee]:","")
else --size from map, or invalid value
pimage = string.gsub(pimage,"^[Ii][Mm][Aa][Gg][Ee]:","")
size = size_map[size] or nil
end
local borderiname = argsimage_map["border"pimage] or pimage
 
local size = args["size"] or args["sz"]
if iname then
local size_map = {xs="12x8px"; s="17x11px"; m="23x15px"; l="32x21px"; xl="46x30px"}
size = size or "23x15px"
if size==nil or string.find(size,"^%d*x?%d+px$") then
if yn_map[border]==0 then border = "" else border = "|border" end
--valid EIS size (..px, x..px or ..x..px) or unset
else
elseif string.find(size,"^%d*x?%d+$") then --EIS size without "px" suffix
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
size = size or emp2nil(data.size) or ."23x15pxpx"
else --size from map, or invalid value
if border then
size = size_map[size] or nil
if yn_map[border]==0 then border = "" else border = "|border" end
end
else
local border = args["border"]
local autoborder = data["border-"..variant] or data["border-"..amap[avar].altvar] or data.border
if autoborder and autoborder~="border" then border = "" else border = "|border" end
end
end
 
if iname then
local am = ""
size = size or "23x15px"
if args["alt"] or string.find(me,"a") then
if yn_map[border]==0 then border = "" else border = "|border" end
am = args["alt"] or args["name"] or country
else
am = am.."|"..am
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
end
size = size or emp2nil(data.size) or "23x15px"
if border then
if yn_map[border]==0 then border = "" else border = "|border" end
else
local autoborder = data["border-"..variant] or data["border-"..amap[avar].altvar] or data.border
if autoborder and autoborder~="border" then border = "" else border = "|border" end
end
end
 
local am = ""
--Build display name
if args["alt"] or string.find(me,"a") then
local text = args["text"]
am = args["alt"] or args["name"] or country
if not text then
am = am.."|"..am
if mn=="x" then --no text
end
text = ""
elseif mn=="p" or mn=="f" then --prefix/suffix link
--Build display name
text = flink=="" and name or "[["..flink..fsec.."|"..name.."]]"
local text = args["text"]
elseif mn=="b" then --both prefix/suffix and normal country link
if not text then
local preflink = pref and (flink=="" and pref.." " or "[["..flink..fsec.."|"..pref.."]] ") or ""
if mn=="x" then --no text
local sufflink = suff and (flink=="" and " "..suff or " [["..flink..fsec.."|"..suff.."]]") or ""
text = ""
local namelink = (name=="" and "" or nthe)..(clink=="" and name or "[["..clink..csec.."|"..name.."]]")
elseif mn=="p" or mn=="f" or mn == "*" then --prefix/suffix link
text = preflink..namelink..sufflink
text = flink=="" and name or "[["..flink..fsec.."|"..name.."]]"
elseif mn=="d" then --data template
-- By the principle of least surprise, there should be some
--check if redirect
-- indication that the link [[X of Country|Country]] does not
if require('Module:Redirect').luaIsRedirect("Template:Country data "..country) then
-- go to [[Country]].
text = "<span class=\"plainlinks\">["..mw.title.new("Template:Country data "..country):fullUrl("redirect=no").." "..name.."]</span>"
if mn == "*" and clink ~= flink then
else
text = text:sub(1, -3) .. "\226\128\175*]]" end
text = "[[Template:Country data "..country.."|"..name.."]]"
elseif mn=="l" then --display link target
end
text = flink=="" and name or "[["..flink..fsec.."|"..(args["name"] or flink).."]]"
elseif mn=="u" then --unlinked
elseif mn=="b" then --both prefix/suffix and normal country link
text = name
local preflink = pref and (flink=="" and pref.." " or "[["..flink..fsec.."|"..pref.."]] ") or ""
else --country link (default)
local sufflink = suff textand = clink(flink=="" and name" "..suff or " [["..clinkflink..csecfsec.."|"..namesuff.."]]") or ""
local namelink = (name=="" and "" or nthe)..(clink=="" and name or "[["..clink..csec.."|"..name.."]]")
end
text = preflink..namelink..sufflink
end
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
 
--Define separator
--Build image
local ilinkseparator = args["ilink&nbsp;"]
if not ilinkms=="x" then --no separator
separator = ""
if mi=="x" or (iname==placeholder and pimage~=placeholder) then --no image/invisible image
elseif ms=="l" then --line break
iname = placeholder
border separator = "<br />"
end
ilink = "|link="
am = ""
--Add preftext, if used
elseif mi=="i" then --image page link
local preftext = args["preftext"]
ilink = ""
if (preftext or "") ~= "" then
elseif mi=="c" then --country link
text = preftext..separator..text
ilink = "|link="..clink..(clink=="" and "" or csec)
end
elseif mi=="p" or mi=="f" then --prefix/suffix link
ilink = "|link="..flink..(flink=="" and "" or fsec)
--Build image
elseif mi=="d" then --data template
local ilink = args["ilink"]
--check if redirect
if not ilink then
if require('Module:Redirect').luaIsRedirect("Template:Country data "..country) then
if mi=="x" or (iname==placeholder and pimage~=placeholder) then --no image/invisible image
ilink = "|link="..mw.title.new("Template:Country data "..country):fullUrl("redirect=no")
iname = placeholder
else
border = ""
ilink = "|link=Template:Country data "..country
ilink = "|link="
end
am = ""
else --unlinked (default)
elseif mi=="i" then --image page link
ilink = "|link="
ilink = ""
end
elseif mi=="c" then --country link
end
ilink = "|link="..clink..(clink=="" and "" or csec)
local image = "[[File:"..iname.."|"..size..border..ilink.."|alt="..am.."]]"
elseif mi=="p" or mi=="f" then --prefix/suffix link
ilink = "|link="..flink..(flink=="" and "" or fsec)
elseif mi=="d" then --data template
local title = mw.title.new("Template:Country data "..country)
--check if redirect
if title.isRedirect then
ilink = "|link="..title:fullUrl("redirect=no")
else
ilink = "|link="..title.fullText
end
else --unlinked (default)
ilink = "|link="
end
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
--Combine image and name with separator
local align = if require('Module:yesno')(args["alignnoredlink"]) == false or args["alnoredlink"] == "notext" then
if country ~= '' and data.alias == nil then
local nalign = args["nalign"] or args["nal"]
iname = ''
local align_map = {left="left", l="left", center="center", centre="center", c="center", middle="center", m="center", right="right", r="right"}
image = "[[:Template:Country data "..country.."]]"
local out
if args["noredlink"] == "notext" then
if string.find(me,"r") then
text = ''
--image right of name
end
if (ms=="x" and mi=="x") or (string.find(me,"o") and iname==placeholder and pimage~=placeholder) then --name only
end
out = text
end
elseif ms=="x" then --no separator
if (args["missingcategory"] or '') ~= '' then
out = text.."<span class=\"flagicon\">"..image.."</span>"
image = image..args["missingcategory"]
elseif ms=="n" then --non-breaking space
end
out = text.."<span class=\"flagicon\">&nbsp;"..image.."</span>"
if string.find(me,"b") then
elseif ms=="l" then --line break
text = ''
out = text.."<span class=\"flagicon\"><br/>"..image.."</span>"
end
elseif ms=="t" then --table cell
end
out = "style=\"text-align:"..(align_map[nalign] or "left").."\"|"..text.."||style=\"text-align:"..(align_map[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_map[align] or "right")..";\">"..image.."</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" then --no separator
out = "<span class=\"flagicon\">"..image.."</span>"..text
elseif ms=="n" then --non-breaking space
out = "<span class=\"flagicon\">"..image.."&nbsp;</span>"..text
elseif ms=="l" then --line break
out = "<span class=\"flagicon\">"..image.."<br/></span>"..text
elseif ms=="t" then --table cell
out = "style=\"text-align:"..(align_map[align] or "center")..";\"|<span class=\"flagicon\">"..image.."</span>||style=\"text-align:"..(align_map[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=\"flagicon\" style=\"display:inline-block;width:"..width.."px;text-align:"..(align_map[align] or "left")..";\">"..image.."</span>&nbsp;"..text
end
end
if string.find(me,"w") then --avoid wrapping
out = "<span class=\"nowrap\">"..out.."</span>"
end
 
--Combine image and name with separator
--Tracking categories
local align_map = {left="left", l="left", center="center", centre="center", c="center", middle="center", m="center", right="right", r="right"}
local cat = ""
local align = align_map[args["align"] or args["al"]]
if pimage and not image_map[pimage] and country~="" and data["flag alias"] and not args.demo then
local nalign = align_map[args["nalign"] or args["nal"]]
cat = "[[Category:Pages using Flagg with specified image instead of data template image]]"
local out
end
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..cat
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