Module:Flags: Difference between revisions

Content deleted Content added
Found it. Doh.
Taking changes from Moroboshi at Module:Flags/Sandbox
 
(10 intermediate revisions by the same user not shown)
Line 1:
local p = {}
 
-- Converts "Flag of" in a variable in order to accept images that don't follow this name schema
flagOf = "Flag_of_"
 
-- Loading the flag translations module --
local translations = mw.loadData("Module:SandboxFlags/QuimGil/FlagTranslationsLocaleData")
local master = mw.loadData("Module:SandboxFlags/QuimGil/FlagMasterMasterData")
 
-- check if name is an original name in translation.fullname and
-- return its value, otherwise return nil
function check_translation(name)
local link
for translation, commonsName in pairs(translations.fullName) do
if commonsName == name then
link = translation
break --if found break out from the loop
end
end
return link
end
 
-- Size of flag --
-- Function to define the default size for the flag if needed
function defaultSize()
--todo: move exception to Module:Flags/MasterData
local sizeExceptions = { "Nepal", "Switzerland", "the Vatican City", }
local size = "20x22px" --initialize with default value
for some,exceptions in pairs(sizeExceptions) do
if commonsName == exceptions then
size = "20x17px"
break --if found break out from loop
end
end
return size
end
 
-- Assigning the parameter to a flag and a link
function p.flag(territory)
--always declare local variable, they are more efficient and dont pollute global namespace
local commonsName
local flagOf = "Flag_of_" -- Converts "Flag of" in a variable in order to accept images that don't follow this name schema
local link = ""
-- more efficient to access
local flag_code = territory.args[1] or ""
-- Searching in the master table only.
-- 2 letter code search
if #territory.args[1]flag_code == 2 then
-- try to assign a value to commonsName and check for nil value
for english,locale in pairs(master.twoLetter) do
commonsName = master.twoLetter[flag_code]
if territory.args[1] == locale then
--if check_translation return nil then it will execute the or part and assign commonsName to link
commonsName = english
if commonsName then link = check_translation(commonsName) or commonsName; end
link = locale
elseif #flag_code == 3 then -- 3 letter code search
end -- FIXME this link is problematic, especially in non-English
commonsName = master.threeLetter[flag_code]
end
if commonsName then link = check_translation(commonsName) or commonsName; end
end
-- check if commonsName is still nil
 
if commonsName == nil then
-- 3 letter code search
-- check master.fullName table
if #territory.args[1] == 3 then
forcommonsName english,locale= in pairs(master.threeLetter) do fullName[flag_code]
if territory.args[1] == localecommonsName then
link = check_translation(commonsName) or commonsName = english ;
else -- Searching in link = locale FlagTranslations
commonsName = translations.fullName[flag_code]
end -- FIXME this link is problematic, especially in non-English
end if commonsName then
link = flag_code
end
else -- Fallback to Commons when the parameter doesn't have an entry in the tables
commonsName = flag_code
-- Searching in both tables
link = flag_code
-- Full name search
flagTables = { master.fullName, translations.fullName, }
for k,v in ipairs(flagTables) do
for english,locale in pairs(v) do
if territory.args[1] == locale or territory.args[1] == english then
commonsName = english
link = locale
end
end
end
 
-- Variant check for historical flags --
-- In the absence of translation for an existing entry, links to the english/original name --
local variant = territory.args[3]
if commonsName ~= nil and link == '' then
if variant and variant ~= "" then
link = commonsName
commonsName = master.variant[commonsName .. "|" .. variant]
end
flagOf=""
end
-- Fallback to Commons when the parameter doesn't have an entry in the table.
 
if commonsName == nil then
-- Label check --
commonsName = territory.args[1]
linkvariant = territory.args[12]
if variant and variant ~="{{{2}}}" then
end
commonsName = master.variant[commonsName .. "|" .. variant]
flagOf=""
-- Border for everybody except Nepal and Ohio
if commonsName == "Nepal" or commonsName == "Ohio" then
border = ""
else
border = "border|"
end
 
-- Digesting Commons flag files not following the format "Flag of "
-- Variant check for historical flags --
-- These filenamess must be preceded by "File:" in the table values.
if territory.args[3] ~= "" then
 
variant = territory.args[3]
if commonsName ~= nil commonsNameand =string.find( master.variant[commonsName .., "|File:", ..1 ) == variant]1 then
commonsName = string.sub( commonsName, 6)
flagOf=""
end
 
-- Label check --
if territory.args[2] ~= "{{{2}}}" then
variant = territory.args[2]
commonsName = master.variant[commonsName .. "|" .. variant]
flagOf = ""
end
 
-- Fallback for non-identified variant/label flags --
if commonsName == nil then commonsName = "Flag of None" end
 
commonsName = "Flag of None"
-- Border for everybody except Nepal and Ohio
-- todo: move exception to Module:Flags/MasterData
local border = "border|"
if commonsName == "Nepal" or commonsName == "Ohio" then
border = ""
end
 
-- Checking whether a size parameter has been introduced, otherwise set default
-- Size of flag --
if territory.args[4]:find("px", -2) ~= nil then
-- Function to define the default size for the flag if needed
size = territory.args[4]
function defaultSize()
else
sizeExceptions = { "Nepal", "Switzerland", "the Vatican City", }
size = defaultSize(commonsName)
for some,exceptions in pairs(sizeExceptions) do
if commonsName == exceptions then
size = "20x17px"
end
end
if size == nil then
size = "20x22px"
end
return size
end
 
-- Checking whether a size parameter has been introduced, otherwise set default
-- Customizing the link
if territory.args[4]:find("px", -2) ~= nil then
sizeopenBrackets = territory.args"[4] ["
elsecloseBrackets = "]]"
if territory.args[5] == "" then
size = defaultSize(commonsName)
flagLink = ""
textLink = ""
openBrackets = ""
closeBrackets = ""
elseif territory.args[5] ~= "{{{link}}}" then
flagLink = territory.args[5]
textLink = territory.args[5] .. "|"
else flagLink = link
textLink = link .. "|"
end
 
-- Text in addition to flag
if territory.args[6] == "" then
text = " " .. openBrackets .. link .. closeBrackets
elseif territory.args[6] ~= "{{{text}}}" then
text = " " .. openBrackets .. textLink .. territory.args[6] .. closeBrackets
else text = ""
end
 
return '[[File:' .. flagOf .. commonsName .. '.svg|' .. border .. 'link=' .. linkflagLink .. '|'.. size .. ']]' .. text
end
return p