Module:Flag

Jump to navigation Jump to search

Lua-native alternative to popular flag templates. Can be used to reduce post-expand include size of pages with lots of templates, since it avoids calling multilevel templates.

Usage

Examples

Template Module
Code Output PEIS Code Output PEIS
{{flag|China}}  China 443b Template:Mlx Lua error in Module:Flagg at line 70: attempt to call field 'getcachedtable' (a nil value). 128b
{{flagcountry|China}}  Wikipedia:China 577b Template:Mlx Lua error in Module:Flagg at line 70: attempt to call field 'getcachedtable' (a nil value). 128b
{{flagdeco|China}} 452b Template:Mlx Lua error in Module:Flagg at line 70: attempt to call field 'getcachedtable' (a nil value). 107b
{{flagicon|China}} Wikipedia:China 548b Template:Mlx Lua error in Module:Flagg at line 70: attempt to call field 'getcachedtable' (a nil value). 117b
{{flag+link|History of|China}} Lua error in Module:Flagg at line 70: attempt to call field 'getcachedtable' (a nil value). Wikipedia:China 663b Template:Mlx Lua error in Module:Flagg at line 70: attempt to call field 'getcachedtable' (a nil value). 139b
{{flagathlete|Andy Murray|GBR}}  Andy Murray (GBR) 617b Template:Mlx Lua error in Module:Flagg at line 70: attempt to call field 'getcachedtable' (a nil value). 210b

local p = {}

function p._main(frame, name, mode, check)
	local categories = {
		Flagicon = '[[Category:Flagicons with missing country data templates]]',
		Flagdeco = '[[Category:Flagdeco with missing country data templates]]',
	}

	local args = {}
    local yesno = require('Module:Yesno')

	if yesno(frame.args.getParent) then
		for k,v in pairs(frame:getParent().args) do
			if (v or '') ~= '' then
				args[k] = v
			end
		end
	end
	for k,v in pairs(frame.args) do
		if (v or '') ~= '' then
			args[k] = v
		end
	end

	if name == 'Flag+link' then
		args.pref = args[1]
	elseif name == 'Flagathlete' then
		local athlete = args[1]
		local code = args.code or args[2] or ''
        args.text = function(data)
            local tooltip = ((data['shortname alias'] or '') == '') and (data.alias or '') or data['shortname alias']
            return athlete .. '&nbsp;<span style="font-size:90%">(<abbr title="' .. tooltip .. '">' .. code .. '</abbr>)</span>';
        end
        args.variant = args.variant or args[3]
        args.placeholder = true
    else
		args.variant = args[2] or args.variant
		args[2] = args[1]
	end

	args[1] = mode .. (args.opts or '')
	args.missingcategory = categories[name]
	args.noredlink = args.noredlink or 'no'

	if yesno(args.placeholder) ~= true then
		args[1] = args[1] .. 'o'
	end

	if mw.title.getCurrentTitle().namespace == 0 then
		local unknown = '[[Category:Pages using ' .. mw.ustring.lower(name) .. ' template with unknown parameters|_VALUE_' ..
				frame:getParent():getTitle() .. ']]'
	else
		local unknown = ''
	end

	if check then
		local opts = {
            unknown=unknown,
            preview='Page using [[Template:' .. name .. ']] with unknown parameter "_VALUE_"',
            ignoreblank='y',
            '1',
            '2',
            'variant',
            'image',
            'size',
            'sz',
            'border',
            'align',
            'al',
            'width',
            'w',
            'alt',
            'ilink',
            'noredlink',
            'missingcategory',
            'name',
            'clink',
            'link',
            'pref',
            'suff',
            'plink',
            'the',
            'section',
            'altvar',
            'avar',
            'age',
            'nalign',
            'nal',
            'text',
            'nodata',
            'opts',
            'placeholder',
            'getParent'
		}

		check = require('Module:Check for unknown parameters')._check(opts,args)
	else
		check = ''
	end

	return require('Module:Flagg').luaMain(frame,args) .. check
end

function p.main(frame) return p._main(frame,     'Flag',        'uncb',  false) end
p.flag = p.main
function p.deco(frame) return p._main(frame,     'Flagdeco',    'uxx',   false) end
p.flagdeco = p.deco
function p.icon(frame) return p._main(frame,     'Flagicon',    'cxxl',  true) end
p.flagicon = p.icon
function p.pluslink(frame) return p._main(frame, 'Flag+link',   'unpof', false) end
p['+link'] = p.pluslink
p['flag+link'] = p.pluslink
function p.country(frame) return p._main(frame,  'Flagcountry', 'unce',  false) end
p.flagcountry = p.country
function p.athlete(frame) return p._main(frame,  'Flagathlete', 'unx',   false) end
p.flagathlete = p.athlete

return p