Module:Flagg: Difference between revisions

From TSP Encyclopedia
Jump to navigation Jump to search
m (1 revision imported)
 
(update from wikipedia)
Line 1: 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 = {}
local p = {}


function p.main(frame)
function p.main(frame)


--Get input arguments
--Get input arguments
local args = require('Module:Arguments').getArgs(frame,{valueFunc =
local args = require('Module:Arguments').getArgs(frame,{valueFunc =
  function(key,value)
function(key,value)
    if value then
if value then
      value = mw.text.trim(value)
value = mw.text.trim(value)
      --Change empty string to nil for all args except 'image' and 'border'
--Change empty string to nil for all args except 'image' and 'border'
      if key=="image" or key=="border" or value~="" then
if key=="image" or key=="border" or value~="" then
        return value
return value
      end
end
    end
end
    return nil
return nil
  end
end
})
})


--Call main function
--Call main function
return p.luaMain(frame,args)
return p.luaMain(frame,args)


end
end
Line 24: Line 33:
function p.luaMain(frame,args)
function p.luaMain(frame,args)


function emp2nil(x)
local function emp2nil(x)
  if x=="" then return nil else return x end
if x=="" then return nil else return x end
end
end
function space2emp(x)
local function space2emp(x)
  if string.find(x,"^%s*$") then return "" else return x end
if string.find(x,"^%s*$") then return "" else return x end
end
end
function nopx(x)
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
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
 
--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 avar = args["avar"] or args["altvar"]
local clink = args["clink"] or args["link"]


--Country & mode parameters
--Get country data & altvar data
local mode = string.lower(args[1] or "usc")
local data, alink, amap, asuf
local mi = string.sub(mode,1,1)
if avar then
local ms = string.sub(mode,2,2)
local age = args["age"] or ""
local mn = string.sub(mode,3,3)
local aalias
local me = string.sub(mode,4,-1)
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, 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 = (args["nodata"] and {}) or CountryData.getcachedtable(frame, country, {variant=args["variant"] or args[3]})
avar = ""
amap = {[""]={altvar=""}}
end


local country = args[2] or ""
--Name and link parameters
local avar = args["avar"] or args["altvar"]
clink = clink or data.alias or country
local clink = args["clink"] or args["link"]


--Get country data & altvar data
local pref = args["pref"]
local data, alink, amap, asuf
local suff = args["suff"] or asuf
if avar then
local pref2 = args["pref2"]
  local age = args["age"] or ""
local suff2 = args["suff2"]
  local aalias
if not pref and not suff then --Default prefix
  amap, aalias = require("Module:Flagg/Altvar data").alttable(age)
pref = "Flag of"
  avar = string.gsub(string.lower(avar or ""),"[ \-]","")
end
  avar = aalias[avar] or avar
local yn_map = {[""]=0; ["0"]=0; ["no"]=0; ["n"]=0; ["1"]=1; ["yes"]=1; ["y"]=1}
  if not amap[avar] then error("Unknown avar") end
local fthe = (args["pthe"] and yn_map[args["pthe"]]~=0) or (args["the"] and yn_map[args["the"]]~=0)
  local apar = {altvar=amap[avar].altvar;mw=amap[avar].mw;age=amap[avar].age;variant=args["variant"] or args[3]}
local nthe = (args["nthe"] and yn_map[args["nthe"]]~=0) or (args["the"] and yn_map[args["the"]]~=0)
  data = require("Module:CountryData").gettable(frame,country,apar)
fthe = fthe and (pref and "t" or "T").."he " or ""
  asuf = amap[avar].altlink
nthe = nthe and (pref and "t" or "T").."he " or ""
  alink = data["link alias-"..amap[avar].altvar] or (clink or data["shortname alias"] or data.alias or country).." "..asuf
local flink = args["plink"] or args["flink"] or alink
else
              or clink=="" and "" or space2emp((pref or "").." ")..fthe..clink..space2emp(" "..(suff or ""))
  data = (args["nodata"] and {}) or require("Module:CountryData").gettable(frame,country,{variant=args["variant"] or args[3]})
local flink2
  avar = ""
if args["plink2"] or args["flink2"] or pref2 or suff2 then
  amap = {[""]={altvar=""}}
flink2 = args["plink2"] or args["flink2"] or clink=="" and "" or space2emp((pref2 or "").." ")..fthe..clink..space2emp(" "..(suff2 or ""))
end
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 ""


--Name and link parameters
if string.find(me,"f") then
clink = clink or data.alias or country
if mw.title.new( flink ).exists == false then
if flink2 ~= flink and mw.title.new ( flink2 ).exists == true then
flink = flink2
fsec = fsec2
else
flink = clink
end
end
end


local pref = args["pref"]
local name = args["name"]
local suff = args["suff"] or asuf
if not name then
if not pref and not suff then --Default prefix
local cname = string.find(me,"e") and (data["name alias-"..amap[avar].altvar] or data["shortname alias"] or data.alias) or country
  pref = "Flag of"
if mn == "f" then
end
name = cname=="" and "" or space2emp((pref or "").." ")..nthe..cname..space2emp(" "..(suff or ""))
local yn_map = {[""]=0; ["0"]=0; ["no"]=0; ["n"]=0; ["1"]=1; ["yes"]=1; ["y"]=1}
else
local fthe = (args["pthe"] and yn_map[args["pthe"]]~=0) or (args["the"] and yn_map[args["the"]]~=0)
name = cname
local nthe = (args["nthe"] and yn_map[args["nthe"]]~=0) or (args["the"] and yn_map[args["the"]]~=0)
end
fthe = fthe and (pref and "t" or "T").."he " or ""
end
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 fsec = args["psection"] or args["section"]
local csec = args["csection"] or args["section"]
fsec = fsec and "#"..fsec or ""
csec = csec and "#"..csec or ""


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
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
end
local iname = image_map[pimage] or pimage
local border = args["border"]


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.."px"
size = size or emp2nil(data.size) or "23x15px"
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" 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 mn=="b" then --both prefix/suffix and normal country link
local 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..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
else --country link (default)
text = clink=="" and name or "[["..clink..csec.."|"..name.."]]"
end
end


--Build display name
--Build image
local text = args["text"]
local ilink = args["ilink"]
if not text then
if not ilink then
  if mn=="x" then --no text
if mi=="x" or (iname==placeholder and pimage~=placeholder) then --no image/invisible image
    text = ""
iname = placeholder
  elseif mn=="p" or mn=="f" then --prefix/suffix link
border = ""
    text = flink=="" and name or "[["..flink..fsec.."|"..name.."]]"
ilink = "|link="
  elseif mn=="b" then --both prefix/suffix and normal country link
am = ""
  local preflink = pref and (flink=="" and pref.." " or "[["..flink..fsec.."|"..pref.."]] ") or ""
elseif mi=="i" then --image page link
  local sufflink = suff and (flink=="" and " "..suff or " [["..flink..fsec.."|"..suff.."]]") or ""
ilink = ""
  local namelink = (name=="" and "" or nthe)..(clink=="" and name or "[["..clink..csec.."|"..name.."]]")
elseif mi=="c" then --country link
    text = preflink..namelink..sufflink
ilink = "|link="..clink..(clink=="" and "" or csec)
  elseif mn=="d" then --data template
elseif mi=="p" or mi=="f" then --prefix/suffix link
    --check if redirect
ilink = "|link="..flink..(flink=="" and "" or fsec)
    if require('Module:Redirect').luaIsRedirect("Template:Country data "..country) then
elseif mi=="d" then --data template
      text = "<span class=\"plainlinks\">["..mw.title.new("Template:Country data "..country):fullUrl("redirect=no").." "..name.."]</span>"
local title = mw.title.new("Template:Country data "..country)
    else
--check if redirect
      text = "[[Template:Country data "..country.."|"..name.."]]"
if title.isRedirect then
    end
ilink = "|link="..title:fullUrl("redirect=no")
  elseif mn=="u" then --unlinked
else
    text = name
ilink = "|link="..title.fullText
  else --country link (default)
end
    text = clink=="" and name or "[["..clink..csec.."|"..name.."]]"
else --unlinked (default)
  end
ilink = "|link="
end
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.."]]"


--Build image
if iname==placeholder then
local ilink = args["ilink"]
if require('Module:yesno')(args["noredlink"]) == false then
if not ilink then
iname = ''
  if mi=="x" or (iname==placeholder and pimage~=placeholder) then --no image/invisible image
image = "[[:Template:Country data "..country.."]]"
    iname = placeholder
end
    border = ""
if (args["missingcategory"] or '') ~= '' then
    ilink = "|link="
image = image..args["missingcategory"]
    am = ""
end
  elseif mi=="i" then --image page link
if string.find(me,"b") then
    ilink = ""
text = ''
  elseif mi=="c" then --country link
end
    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)
  elseif mi=="d" then --data template
    --check if redirect
    if require('Module:Redirect').luaIsRedirect("Template:Country data "..country) then
      ilink = "|link="..mw.title.new("Template:Country data "..country):fullUrl("redirect=no")
    else
      ilink = "|link=Template:Country data "..country
    end
  else --unlinked (default)
    ilink = "|link="
  end
end
local image = "[[File:"..iname.."|"..size..border..ilink.."|alt="..am.."]]"


--Combine image and name with separator
--Combine image and name with separator
local align = args["align"] or args["al"]
local align_map = {left="left", l="left", center="center", centre="center", c="center", middle="center", m="center", right="right", r="right"}
local nalign = args["nalign"] or args["nal"]
local align = align_map[args["align"] or args["al"]]
local align_map = {left="left", l="left", center="center", centre="center", c="center", middle="center", m="center", right="right", r="right"}
local nalign =  align_map[args["nalign"] or args["nal"]]
local out
local out
if string.find(me,"r") then
if string.find(me,"r") then
  --image right of name
--image right of name
  if (ms=="x" and mi=="x") or (string.find(me,"o") and iname==placeholder and pimage~=placeholder) then --name only
if (ms=="x" and mi=="x") or (string.find(me,"o") and iname==placeholder and pimage~=placeholder) then --name only
    out = text
out = text
  elseif ms=="x" then --no separator
elseif ms=="x" then --no separator
    out = text.."<span class=\"flagicon\">"..image.."</span>"
out = text.."<span class=\"flagicon\">"..image.."</span>"
  elseif ms=="n" then --non-breaking space
elseif ms=="n" then --non-breaking space
    out = text.."<span class=\"flagicon\">&nbsp;"..image.."</span>"
out = text.."<span class=\"flagicon\">&nbsp;"..image.."</span>"
  elseif ms=="l" then --line break
elseif ms=="l" then --line break
    out = text.."<span class=\"flagicon\"><br/>"..image.."</span>"
out = text.."<span class=\"flagicon\"><br/>"..image.."</span>"
  elseif ms=="t" then --table cell
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>"
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)
else --fixed-width span box (default)
    local width = args["width"] or args["w"] or require("Module:Flaglist").luawidth(size)
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>"
out = text.."&nbsp;<span class=\"flagicon\" style=\"display:inline-block;width:"..width.."px;text-align:"..(align or "right")..";\">"..image.."</span>"
  end
end
else --image left of name
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
if (ms=="x" and mi=="x") or (string.find(me,"o") and iname==placeholder and pimage~=placeholder) then --name only
    out = text
out = text
  elseif ms=="x" then --no separator
elseif ms=="x" then --no separator
    out = "<span class=\"flagicon\">"..image.."</span>"..text
out = "<span class=\"flagicon\">"..image.."</span>"..text
  elseif ms=="n" then --non-breaking space
elseif ms=="n" then --non-breaking space
    out = "<span class=\"flagicon\">"..image.."&nbsp;</span>"..text
out = "<span class=\"flagicon\">"..image.."&nbsp;</span>"..text
  elseif ms=="l" then --line break
elseif ms=="l" then --line break
    out = "<span class=\"flagicon\">"..image.."<br/></span>"..text
out = "<span class=\"flagicon\">"..image.."<br/></span>"..text
  elseif ms=="t" then --table cell
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
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)
else --fixed-width span box (default)
    local width = nopx(args["width"] or args["w"]) or require("Module:Flaglist").luawidth(size)
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
out = "<span class=\"flagicon\" style=\"display:inline-block;width:"..width.."px;text-align:"..(align or "left")..";\">"..image.."</span>&nbsp;"..text
  end
end
end
end
if string.find(me,"w") then --avoid wrapping
if string.find(me,"w") then --avoid wrapping
  out = "<span class=\"nowrap\">"..out.."</span>"
out = "<span class=\"nowrap\">"..out.."</span>"
end
end
-- align in table
if me:find("t") then
out = 'style="text-align:'..(align or 'left')..'"|'..out
end


--Tracking categories
--Tracking categories
local cat = ""
local cat = ""
if pimage and not image_map[pimage] and country~="" and data["flag alias"] and not args.demo then
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]]"
cat = "[[Category:Pages using Flagg with specified image instead of data template image]]"
end
end


return out..cat
return out..cat


end
end


return p
return p

Revision as of 01:39, 28 December 2021

Documentation for this module may be created at Module:Flagg/doc

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

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 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 = CountryData.getcachedtable(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 = (args["nodata"] and {}) or CountryData.getcachedtable(frame, country, {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"]
	local suff2 = args["suff2"]
	if not pref and not suff then --Default prefix
		pref = "Flag of"
	end
	local 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 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 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
		if mw.title.new( flink ).exists == false then
			if flink2 ~= flink and mw.title.new ( flink2 ).exists == true then
				flink = flink2
				fsec = fsec2
			else
				flink = clink
			end
		end
	end

	local name = args["name"]
	if not name then
		local cname = string.find(me,"e") and (data["name alias-"..amap[avar].altvar] or data["shortname alias"] or data.alias) or country
		if mn == "f" then
			name = cname=="" and "" or space2emp((pref or "").." ")..nthe..cname..space2emp(" "..(suff or ""))
		else
			name = cname
		end
	end

	--Image parameters
	local 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 namespace
		pimage = string.gsub(pimage,"^[Ff][Ii][Ll][Ee]:","")
		pimage = string.gsub(pimage,"^[Ii][Mm][Aa][Gg][Ee]:","")
	end
	local iname = image_map[pimage] or pimage

	local 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 unset
	elseif string.find(size,"^%d*x?%d+$") then --EIS size without "px" suffix
		size=size.."px"
	else --size from map, or invalid value
		size = size_map[size] or nil
	end
	local border = args["border"]

	if iname then
		size = size or "23x15px"
		if yn_map[border]==0 then border = "" else border = "|border" end
	else
		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 "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 = ""
	if args["alt"] or string.find(me,"a") then
		am = args["alt"] or args["name"] or country
		am = am.."|"..am
	end

	--Build display name
	local text = args["text"]
	if not text then
		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 mn=="b" then --both prefix/suffix and normal country link
			local 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..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
		else --country link (default)
			text = clink=="" and name or "[["..clink..csec.."|"..name.."]]"
		end
	end

	--Build image
	local ilink = args["ilink"]
	if not ilink then
		if mi=="x" or (iname==placeholder and pimage~=placeholder) then --no image/invisible image
			iname = placeholder
			border = ""
			ilink = "|link="
			am = ""
		elseif mi=="i" then --image page link
			ilink = ""
		elseif mi=="c" then --country link
			ilink = "|link="..clink..(clink=="" and "" or csec)
		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
		if require('Module:yesno')(args["noredlink"]) == false then
			iname = ''
			image = "[[:Template:Country data "..country.."]]"
		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" then --no separator
			out = text.."<span class=\"flagicon\">"..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:"..(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>"
		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 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=\"flagicon\" style=\"display:inline-block;width:"..width.."px;text-align:"..(align or "left")..";\">"..image.."</span>&nbsp;"..text
		end
	end
	if string.find(me,"w") then --avoid wrapping
		out = "<span class=\"nowrap\">"..out.."</span>"
	end
	
	-- align in table
	if me:find("t") then
		out = 'style="text-align:'..(align or 'left')..'"|'..out
	end

	--Tracking categories
	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

return p