Module:Flagg: Difference between revisions

Content deleted Content added
convert unnecessary global variables to local variables
Copy from sandbox
 
(19 intermediate revisions by 7 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 = {}
 
Line 42 ⟶ 51:
 
local country = args[2] or ""
local countryDataCountry = country
if string.find(me, "wi") then --avoid wrapping
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"]
Line 51 ⟶ 67:
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 = (args["nodata"] and {}) or require("Module:CountryData").gettablegetcachedtable(frame,country countryDataCountry, {variant=args["variant"] or args[3]})
avar = ""
amap = {[""]={altvar=""}}
Line 69 ⟶ 85:
local pref = args["pref"]
local suff = args["suff"] or asuf
local alignpref2 = args["align"] or args["alpref2"]
local suff2 = args["suff2"]
if not pref and not suff then --Default prefix
pref = "Flag of"
Line 79 ⟶ 97:
local flink = args["plink"] or args["flink"] or alink
or clink=="" and "" or space2emp((pref or "").." ")..fthe..clink..space2emp(" "..(suff or ""))
local flink2
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 nalignfsec2 = args["nalignpsection2"] or args["nalsection"]
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
if mw.title.new( flink ).exists == false then
if flink2 ~= flink and mw.title.new ( flink2 ).exists == true then
flink = clink
flink = flink2
fsec = fsec2
else
flink = clink
end
end
end
Line 141 ⟶ 172:
am = am.."|"..am
end
 
--Build display name
local text = args["text"]
Line 147 ⟶ 178:
if mn=="x" then --no text
text = ""
elseif mn=="p" or mn=="f" or mn == "*" then --prefix/suffix link
text = 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 then
text = text:sub(1, -3) .. "\226\128\175*]]" end
elseif msmn=="l" then --linedisplay breaklink target
text = flink=="" and name or "[["..flink..fsec.."|"..(args["name"] or flink).."]]"
elseif mn=="b" then --both prefix/suffix and normal country link
local preflink = pref and (flink=="" and pref.." " or "[["..flink..fsec.."|"..pref.."]] ") or ""
Line 164 ⟶ 202:
elseif mn=="u" then --unlinked
text = name
elseif msmn=="la" then --line breakabbr
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
local separator = "&nbsp;"
if ms=="x" then --no separator
separator = ""
elseif ms=="nl" then --non-breakingline spacebreak
separator = "<br />"
end
--Add preftext, if used
local preftext = args["preftext"]
if (preftext or "") ~= "" then
text = preftext..separator..text
end
--Build image
local ilink = args["ilink"]
Line 201 ⟶ 262:
 
if iname==placeholder then
if require('Module:yesno')(args["noredlink"]) == false or args["noredlink"] == "notext" then
if country ~= '' and data.alias == nil then
iname = ''
iname = ''
image = "[[:Template:Country data "..country.."]]"
if args["noredlink"] == "notext" then
iname text = ''
end
end
end
if (args["missingcategory"] or '') ~= '' then
Line 214 ⟶ 280:
 
--Combine image and name with separator
local align = args["align"] or args["al"]
local nalign = args["nalign"] or args["nal"]
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
Line 222 ⟶ 288:
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=="n" then --non-breaking space
out = text.."<span class=\"flagicon\">&nbsp;"..image.."</span>"
elseif ms=="l" then --line break
out = text.."<span class=\"flagicon\"><br/>"..image.."</span>"
elseif ms=="t" then --table cell
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.."<span class=\"nowrap\">&nbsp;<span class=\"flagicon\" style=\"display:inline-block;width:"..width.."px;text-align:"..(align_map[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=="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=\"nowrap\"><span class=\"flagicon\" style=\"display:inline-block;width:"..width.."px;text-align:"..(align_map[align] or "left")..";\">"..image.."</span>&nbsp;</span>"..text
end
end
if string.find(me,"w") then --avoid wrapping
-- align in table
out = "<span class=\"nowrap\">"..out.."</span>"
if me:find("t") then
out = 'style="text-align:'..(align or 'left')..'"|'..out
end
 
Line 263 ⟶ 323:
 
end
 
p[''] = p.main
 
return p