Module:Flagg

From Zoophilia Wiki
Revision as of 11:15, 21 August 2015 by meta>SiBr4 (Removed ialtvar param)
Jump to navigationJump to search

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

local p = {}

function p.main(frame)

function emp2nil(x)
 if x=="" then return nil else return x end
end
function space2emp(x)
 if string.find(x,"^%s*$") then return "" else return x end
end
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

--Get input arguments
local args = require('Module:Arguments').getArgs(frame)

--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"]

--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").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

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

local pref = args["pref"]
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 name = args["name"]
if string.find(me,"e") then
  local cname = data["name alias-"..amap[avar].altvar] or data["shortname alias"] or data.alias or country
  if mn == "f" then
    name = name or space2emp((pref or "").." ")..the..cname..space2emp(" "..(suff or ""))
  else
    name = name or cname
  end
else
  if mn == "f" then
    name = name or space2emp((pref or "").." ")..the..country..space2emp(" "..(suff or ""))
  else
    name = name or country
  end
end

--Image parameters
local image = frame:getParent().args["image"] or frame.args["image"]
local placeholder = "Flag placeholder.svg"
local variant = args["variant"] or args[3] or ""
local image_map = {[""]=placeholder; ["none"]=placeholder; ["blank"]=placeholder}
if image then --Remove namespace
 image = string.gsub(image,"^[Ff][Ii][Ll][Ee]:(.*)$","%1")
 image = string.gsub(image,"^[Ii][Mm][Aa][Gg][Ee]:(.*)$","%1")
end
local iname = image_map[image] or image

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 size=size.."px" --EIS size without "px" suffix
 else size = size_map[size] or nil
end
local border = frame:getParent().args["border"] or frame.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
    if data["border-"..variant] or data["border-"..amap[avar].altvar] or data.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" --no text
   then text = ""
  elseif mn=="p" or mn=="f" --prefix/suffix link
   then text = "[["..flink.."|"..name.."]]"
  elseif mn=="b" --both prefix/suffix and normal country link
   then
    if pref and suff
     then text = "[["..flink.."|"..pref.."]] "..the.."[["..clink.."|"..name.."]] [["..flink.."|"..suff.."]]"
    elseif pref
     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

--Build image
local ilink = args["ilink"]
if not ilink then
  if mi=="x" or (iname==placeholder and image~=placeholder) --no image/invisible image
   then iname = placeholder border = "" ilink = "|link=" am = ""
  elseif mi=="i" --image page link
   then ilink = ""
  elseif mi=="c" --country link
   then ilink = "|link="..clink
  elseif mi=="p" or mi=="f" --prefix/suffix link
   then ilink = "|link="..flink
  elseif mi=="d" --data template
   then --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
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 out
if string.find(me,"r")
 then --image right of name
  if ms=="x" and mi=="x" --name only
   then out = text
  elseif ms=="x" --no separator
   then out = text.."<span class=\"flagicon\">"..image.."</span>"
  elseif ms=="n" --non-breaking space
   then out = text.."<span class=\"flagicon\">&nbsp;"..image.."</span>"
  elseif ms=="l" --line break
   then out = text.."<span class=\"flagicon\"><br/>"..image.."</span>"
  elseif ms=="t" --table cell
   then 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/size").luawidth(size)
   out = text.."<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" --name only
   then out = text
  elseif ms=="x" --no separator
   then out = "<span class=\"flagicon\">"..image.."</span>"..text
  elseif ms=="n" --non-breaking space
   then out = "<span class=\"flagicon\">"..image.."&nbsp;</span>"..text
  elseif ms=="l" --line break
   then out = "<span class=\"flagicon\">"..image.."<br/></span>"..text
  elseif ms=="t" --table cell
   then 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/size").luawidth(size)
   out = "<span class=\"flagicon\" style=\"display:inline-block;width:"..width.."px;text-align:"..(align_map[align] or "left")..";\">"..image.."</span>"..text
  end
end
if string.find(me,"w") --avoid wrapping
 then out = "<span class=\"nowrap\">"..out.."</span>"
end

return out

end

return p