Module:Flagg
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==&quot;image&quot; or key==&quot;border&quot; or value~=&quot;&quot; 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==&quot;&quot; then return nil else return x end
 	end
 	local function space2emp(x)
 		if string.find(x,&quot;^%s*$&quot;) then return &quot;&quot; else return x end
 	end
 	local function nopx(x)
 		if x~=nil and (string.find(x,&quot;^%d+$&quot;) or string.find(x,&quot;^%d+px$&quot;)) then return string.gsub(x,&quot;^(.*)px&quot;,&quot;%1&quot;) else return nil end
 	end
 
 	--Country &amp; mode parameters
 	local mode = string.lower(args[1] or &quot;usc&quot;)
 	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 &quot;&quot;
 	local avar = args[&quot;avar&quot;] or args[&quot;altvar&quot;]
 	local clink = args[&quot;clink&quot;] or args[&quot;link&quot;]
 
 	--Get country data &amp; altvar data
 	local data, alink, amap, asuf
 	if avar then
 		local age = args[&quot;age&quot;] or &quot;&quot;
 		local aalias
 		amap, aalias = require(&quot;Module:Flagg/Altvar data&quot;).alttable(age)
 		avar = string.gsub(string.lower(avar or &quot;&quot;),&quot;[ \-]&quot;,&quot;&quot;)
 		avar = aalias[avar] or avar
 		if not amap[avar] then error(&quot;Unknown avar&quot;) end
 		local apar = {altvar=amap[avar].altvar;mw=amap[avar].mw;age=amap[avar].age;variant=args[&quot;variant&quot;] or args[3]}
 		data = require(&quot;Module:CountryData&quot;).gettable(frame,country,apar)
 		asuf = amap[avar].altlink
 		alink = data[&quot;link alias-&quot;..amap[avar].altvar] or (clink or data[&quot;shortname alias&quot;] or data.alias or country)..&quot; &quot;..asuf
 	else
 		data = (args[&quot;nodata&quot;] and {}) or require(&quot;Module:CountryData&quot;).gettable(frame,country,{variant=args[&quot;variant&quot;] or args[3]})
 		avar = &quot;&quot;
 		amap = {[&quot;&quot;]={altvar=&quot;&quot;}}
 	end
 
 	--Name and link parameters
 	clink = clink or data.alias or country
 
 	local pref = args[&quot;pref&quot;]
 	local suff = args[&quot;suff&quot;] or asuf
 	if not pref and not suff then --Default prefix
 		pref = &quot;Flag of&quot;
 	end
 	local yn_map = {[&quot;&quot;]=0; [&quot;0&quot;]=0; [&quot;no&quot;]=0; [&quot;n&quot;]=0; [&quot;1&quot;]=1; [&quot;yes&quot;]=1; [&quot;y&quot;]=1}
 	local fthe = (args[&quot;pthe&quot;] and yn_map[args[&quot;pthe&quot;]]~=0) or (args[&quot;the&quot;] and yn_map[args[&quot;the&quot;]]~=0)
 	local nthe = (args[&quot;nthe&quot;] and yn_map[args[&quot;nthe&quot;]]~=0) or (args[&quot;the&quot;] and yn_map[args[&quot;the&quot;]]~=0)
 	fthe = fthe and (pref and &quot;t&quot; or &quot;T&quot;)..&quot;he &quot; or &quot;&quot;
 	nthe = nthe and (pref and &quot;t&quot; or &quot;T&quot;)..&quot;he &quot; or &quot;&quot;
 	local flink = args[&quot;plink&quot;] or args[&quot;flink&quot;] or alink
 	              or clink==&quot;&quot; and &quot;&quot; or space2emp((pref or &quot;&quot;)..&quot; &quot;)..fthe..clink..space2emp(&quot; &quot;..(suff or &quot;&quot;))
 	local fsec = args[&quot;psection&quot;] or args[&quot;section&quot;]
 	local csec = args[&quot;csection&quot;] or args[&quot;section&quot;]
 	fsec = fsec and &quot;#&quot;..fsec or &quot;&quot;
 	csec = csec and &quot;#&quot;..csec or &quot;&quot;
 
 	if string.find(me,&quot;f&quot;) then
 		if mw.title.new( flink ).exists == false then
 			flink = clink
 		end
 	end
 
 	local name = args[&quot;name&quot;]
 	if not name then
 		local cname = string.find(me,&quot;e&quot;) and (data[&quot;name alias-&quot;..amap[avar].altvar] or data[&quot;shortname alias&quot;] or data.alias) or country
 		if mn == &quot;f&quot; then
 			name = cname==&quot;&quot; and &quot;&quot; or space2emp((pref or &quot;&quot;)..&quot; &quot;)..nthe..cname..space2emp(&quot; &quot;..(suff or &quot;&quot;))
 		else
 			name = cname
 		end
 	end
 
 	--Image parameters
 	local pimage = args[&quot;image&quot;]
 	local placeholder = &quot;Flag placeholder.svg&quot;
 	local variant = args[&quot;variant&quot;] or args[3] or &quot;&quot;
 	local image_map = {[&quot;&quot;]=placeholder; [&quot;none&quot;]=placeholder; [&quot;blank&quot;]=placeholder}
 	if pimage then --Remove namespace
 		pimage = string.gsub(pimage,&quot;^[Ff][Ii][Ll][Ee]:&quot;,&quot;&quot;)
 		pimage = string.gsub(pimage,&quot;^[Ii][Mm][Aa][Gg][Ee]:&quot;,&quot;&quot;)
 	end
 	local iname = image_map[pimage] or pimage
 
 	local size = args[&quot;size&quot;] or args[&quot;sz&quot;]
 	local size_map = {xs=&quot;12x8px&quot;; s=&quot;17x11px&quot;; m=&quot;23x15px&quot;; l=&quot;32x21px&quot;; xl=&quot;46x30px&quot;}
 	if size==nil or string.find(size,&quot;^%d*x?%d+px$&quot;) then
 		--valid EIS size (..px, x..px or ..x..px) or unset
 	elseif string.find(size,&quot;^%d*x?%d+$&quot;) then --EIS size without &quot;px&quot; suffix
 		size=size..&quot;px&quot;
 	else --size from map, or invalid value
 		size = size_map[size] or nil
 	end
 	local border = args[&quot;border&quot;]
 
 	if iname then
 		size = size or &quot;23x15px&quot;
 		if yn_map[border]==0 then border = &quot;&quot; else border = &quot;|border&quot; end
 	else
 		iname = data[&quot;flag alias-&quot;..amap[avar].altvar..&quot;-&quot;..variant] or data[&quot;flag alias-&quot;..variant] or data[&quot;flag alias-&quot;..amap[avar].altvar] or data[&quot;flag alias&quot;] or placeholder
 		size = size or emp2nil(data.size) or &quot;23x15px&quot;
 		if border then
 			if yn_map[border]==0 then border = &quot;&quot; else border = &quot;|border&quot; end
 		else
 			local autoborder = data[&quot;border-&quot;..variant] or data[&quot;border-&quot;..amap[avar].altvar] or data.border
 			if autoborder and autoborder~=&quot;border&quot; then border = &quot;&quot; else border = &quot;|border&quot; end
 		end
 	end
 
 	local am = &quot;&quot;
 	if args[&quot;alt&quot;] or string.find(me,&quot;a&quot;) then
 		am = args[&quot;alt&quot;] or args[&quot;name&quot;] or country
 		am = am..&quot;|&quot;..am
 	end
 
 	--Build display name
 	local text = args[&quot;text&quot;]
 	if not text then
 		if mn==&quot;x&quot; then --no text
 			text = &quot;&quot;
 		elseif mn==&quot;p&quot; or mn==&quot;f&quot; then --prefix/suffix link
 			text = flink==&quot;&quot; and name or &quot;[[&quot;..flink..fsec..&quot;|&quot;..name..&quot;]]&quot;
 		elseif mn==&quot;b&quot; then --both prefix/suffix and normal country link
 			local preflink = pref and (flink==&quot;&quot; and pref..&quot; &quot; or &quot;[[&quot;..flink..fsec..&quot;|&quot;..pref..&quot;]] &quot;) or &quot;&quot;
 			local sufflink = suff and (flink==&quot;&quot; and &quot; &quot;..suff or &quot; [[&quot;..flink..fsec..&quot;|&quot;..suff..&quot;]]&quot;) or &quot;&quot;
 			local namelink = (name==&quot;&quot; and &quot;&quot; or nthe)..(clink==&quot;&quot; and name or &quot;[[&quot;..clink..csec..&quot;|&quot;..name..&quot;]]&quot;)
 			text = preflink..namelink..sufflink
 		elseif mn==&quot;d&quot; then --data template
 			local title = mw.title.new(&quot;Template:Country data &quot;..country)
 			--check if redirect
 			if title.isRedirect then
 				text = &quot;&lt;span class=\&quot;plainlinks\&quot;&gt;[&quot;..title:fullUrl(&quot;redirect=no&quot;)..&quot; &quot;..name..&quot;]&lt;/span&gt;&quot;
 			else
 				text = &quot;[[&quot;..title.fullText..&quot;|&quot;..name..&quot;]]&quot;
 			end
 		elseif mn==&quot;u&quot; then --unlinked
 			text = name
 		else --country link (default)
 			text = clink==&quot;&quot; and name or &quot;[[&quot;..clink..csec..&quot;|&quot;..name..&quot;]]&quot;
 		end
 	end
 
 	--Build image
 	local ilink = args[&quot;ilink&quot;]
 	if not ilink then
 		if mi==&quot;x&quot; or (iname==placeholder and pimage~=placeholder) then --no image/invisible image
 			iname = placeholder
 			border = &quot;&quot;
 			ilink = &quot;|link=&quot;
 			am = &quot;&quot;
 		elseif mi==&quot;i&quot; then --image page link
 			ilink = &quot;&quot;
 		elseif mi==&quot;c&quot; then --country link
 			ilink = &quot;|link=&quot;..clink..(clink==&quot;&quot; and &quot;&quot; or csec)
 		elseif mi==&quot;p&quot; or mi==&quot;f&quot; then --prefix/suffix link
 			ilink = &quot;|link=&quot;..flink..(flink==&quot;&quot; and &quot;&quot; or fsec)
 		elseif mi==&quot;d&quot; then --data template
 			local title = mw.title.new(&quot;Template:Country data &quot;..country)
 			--check if redirect
 			if title.isRedirect then
 				ilink = &quot;|link=&quot;..title:fullUrl(&quot;redirect=no&quot;)
 			else
 				ilink = &quot;|link=&quot;..title.fullText
 			end
 		else --unlinked (default)
 			ilink = &quot;|link=&quot;
 		end
 	end
 	if am == &quot;&quot; and string.find(me,&quot;l&quot;) then
 		am = mw.ustring.sub(ilink,7,-1)
 	end
 	local image = &quot;[[File:&quot;..iname..&quot;|&quot;..size..border..ilink..&quot;|alt=&quot;..am..&quot;]]&quot;
 
 	if iname==placeholder then
 		if require('Module:yesno')(args[&quot;noredlink&quot;]) == false then
 			iname = ''
 			image = &quot;[[:Template:Country data &quot;..country..&quot;]]&quot;
 		end
 		if (args[&quot;missingcategory&quot;] or '') ~= '' then
 			image = image..args[&quot;missingcategory&quot;]
 		end
 		if string.find(me,&quot;b&quot;) then
 			text = ''
 		end
 	end
 
 	--Combine image and name with separator
 	local align = args[&quot;align&quot;] or args[&quot;al&quot;]
 	local nalign = args[&quot;nalign&quot;] or args[&quot;nal&quot;]
 	local align_map = {left=&quot;left&quot;, l=&quot;left&quot;, center=&quot;center&quot;, centre=&quot;center&quot;, c=&quot;center&quot;, middle=&quot;center&quot;, m=&quot;center&quot;, right=&quot;right&quot;, r=&quot;right&quot;}
 	local out
 	if string.find(me,&quot;r&quot;) then
 		--image right of name
 		if (ms==&quot;x&quot; and mi==&quot;x&quot;) or (string.find(me,&quot;o&quot;) and iname==placeholder and pimage~=placeholder) then --name only
 			out = text
 		elseif ms==&quot;x&quot; then --no separator
 			out = text..&quot;&lt;span class=\&quot;flagicon\&quot;&gt;&quot;..image..&quot;&lt;/span&gt;&quot;
 		elseif ms==&quot;n&quot; then --non-breaking space
 			out = text..&quot;&lt;span class=\&quot;flagicon\&quot;&gt;&amp;nbsp;&quot;..image..&quot;&lt;/span&gt;&quot;
 		elseif ms==&quot;l&quot; then --line break
 			out = text..&quot;&lt;span class=\&quot;flagicon\&quot;&gt;&lt;br/&gt;&quot;..image..&quot;&lt;/span&gt;&quot;
 		elseif ms==&quot;t&quot; then --table cell
 			out = &quot;style=\&quot;text-align:&quot;..(align_map[nalign] or &quot;left&quot;)..&quot;\&quot;|&quot;..text..&quot;||style=\&quot;text-align:&quot;..(align_map[align] or &quot;center&quot;)..&quot;;\&quot;|&lt;span class=\&quot;flagicon\&quot;&gt;&quot;..image..&quot;&lt;/span&gt;&quot;
 		else --fixed-width span box (default)
 			local width = args[&quot;width&quot;] or args[&quot;w&quot;] or require(&quot;Module:Flaglist&quot;).luawidth(size)
 			out = text..&quot;&amp;nbsp;&lt;span class=\&quot;flagicon\&quot; style=\&quot;display:inline-block;width:&quot;..width..&quot;px;text-align:&quot;..(align_map[align] or &quot;right&quot;)..&quot;;\&quot;&gt;&quot;..image..&quot;&lt;/span&gt;&quot;
 		end
 	else --image left of name
 		if (ms==&quot;x&quot; and mi==&quot;x&quot;) or (string.find(me,&quot;o&quot;) and iname==placeholder and pimage~=placeholder) then --name only
 			out = text
 		elseif ms==&quot;x&quot; then --no separator
 			out = &quot;&lt;span class=\&quot;flagicon\&quot;&gt;&quot;..image..&quot;&lt;/span&gt;&quot;..text
 		elseif ms==&quot;n&quot; then --non-breaking space
 			out = &quot;&lt;span class=\&quot;flagicon\&quot;&gt;&quot;..image..&quot;&amp;nbsp;&lt;/span&gt;&quot;..text
 		elseif ms==&quot;l&quot; then --line break
 			out = &quot;&lt;span class=\&quot;flagicon\&quot;&gt;&quot;..image..&quot;&lt;br/&gt;&lt;/span&gt;&quot;..text
 		elseif ms==&quot;t&quot; then --table cell
 			out = &quot;style=\&quot;text-align:&quot;..(align_map[align] or &quot;center&quot;)..&quot;;\&quot;|&lt;span class=\&quot;flagicon\&quot;&gt;&quot;..image..&quot;&lt;/span&gt;||style=\&quot;text-align:&quot;..(align_map[nalign] or &quot;left&quot;)..&quot;\&quot;|&quot;..text
 		else --fixed-width span box (default)
 			local width = nopx(args[&quot;width&quot;] or args[&quot;w&quot;]) or require(&quot;Module:Flaglist&quot;).luawidth(size)
 			out = &quot;&lt;span class=\&quot;flagicon\&quot; style=\&quot;display:inline-block;width:&quot;..width..&quot;px;text-align:&quot;..(align_map[align] or &quot;left&quot;)..&quot;;\&quot;&gt;&quot;..image..&quot;&lt;/span&gt;&amp;nbsp;&quot;..text
 		end
 	end
 	if string.find(me,&quot;w&quot;) then --avoid wrapping
 		out = &quot;&lt;span class=\&quot;nowrap\&quot;&gt;&quot;..out..&quot;&lt;/span&gt;&quot;
 	end
 
 	--Tracking categories
 	local cat = &quot;&quot;
 	if pimage and not image_map[pimage] and country~=&quot;&quot; and data[&quot;flag alias&quot;] and not args.demo then
 		cat = &quot;[[Category:Pages using Flagg with specified image instead of data template image]]&quot;
 	end
 
 	return out..cat
 
 end