Module:Flags

local p = {}

local master = mw.loadData("Module:Flags/MasterData")

-- Template functions function p.Flag(frame) return p._Flag(frame.args[1]) end function p.FlagNoLink(frame) return p._FlagNoLink(frame.args[1]) end

-- Returns a flag with a link to the category page function p._Flag(name) if (not name) or name == '' then return '' end

local flagData = getFlag(name)

if flagData then return ' ' .. flagData.name .. '|link=Category:' .. flagData.name .. ' ' else mw.log('Unknown flag: ', name) return safeExpand(mw.getCurrentFrame, 'Flag/' .. name) .. ''	end end

-- Returns a flag with no link function p._FlagNoLink(name) if (not name) or name == '' then return '' end

local flagData = getFlag(name)

if flagData then return ' ' .. flagData.name .. '|link= ' else mw.log('Unknown flag: ', name) return safeExpand(mw.getCurrentFrame, 'FlagNoLink/' .. name) .. ''	end end

-- Checks the Flags/MasterData tables function getFlag(name) local inputName = name or '' local flagData

-- Convert input to lowercase and remove spaces inputName = string.lower(string.gsub(inputName, ' ', ''))

if #inputName == 2 then -- Check for a 2-letter code local index = master.twoLetter[inputName] if index then flagData = master.data[index] end elseif #inputName == 3 then -- Check for a 3-letter code local index = master.threeLetter[inputName] if index then flagData = master.data[index] end else -- Check for an alias local index = master.aliases[inputName] flagData = master.data[index] end

-- Lookup the full name anyways if there is no match if not flagData then flagData = master.data[inputName] end

return flagData end

-- Fallback to templates when no flag is found function safeExpand(frame, templateTitle, templateArgs) local result, value = pcall(frame.expandTemplate, frame, {title = templateTitle, args = templateArgs}) if result then return value else return 'Template:' .. templateTitle .. '' end end

return p