Модуль:Сцягафікацыя

Дакументацыю да гэтага модуля можна стварыць у Модуль:Сцягафікацыя/Дакументацыя

local HtmlBuilder = require( 'Module:HtmlBuilder' )
local countryData = require( 'Module:Сцягі краін' )
local errors = require( 'Module:Error' )

local p = {}

local function showError( message, category )
	return errors.error( { ['message'] = 'Памылка: ' .. tostring( message ) .. '.' .. ( ( category == nil or category == '' ) and '' or '[[Катэгорыя:' .. category .. ']]' ) } )
end

local function fallbackArgs( args, ... )
	for i = 1, arg.n do
		if args[arg[i]] ~= nil then return args[arg[i]] end
	end
	return nil
end

local function lcArgs( args )
	local newArgs = {}
	for k, v in pairs( args ) do
		if type( k ) == "string" then
			newArgs[mw.ustring.lower( k )] = type( v ) == 'string' and mw.text.trim( v ) or v
		else
			newArgs[k] = type( v ) == 'string' and mw.text.trim( v ) or v
		end
	end
	return newArgs
end

local function getCountryData( country )
	return countryData[country]
end

local function renderFlag( name, size, border, link, caption )
	if type( caption ) == 'nil' then
		caption = ''
	end

	local result = HtmlBuilder.create()
	result
		.tag( 'span' )
			.addClass( 'flagicon' )
			.css( 'white-space', 'nowrap' )
			.wikitext( '[[File:' .. name .. '|' .. border .. '|' .. size .. '|link=' .. link .. ']]' .. ( caption ~= '' and ' [[' .. link .. '|' .. caption .. ']]' or '' ) )

	return tostring( result )
end

function p.countryExists( frame )
	local country = mw.text.trim( frame.args[1] )
	if getCountryData( country ) ~= nil then return '1' end
	return ''
end

function p.getCaptionedFlag( frame )
	local args
	if frame == mw.getCurrentFrame() then
		args = frame:getParent().args
	else
		args = frame
	end
	args = lcArgs( args )
	local country, variant, size, link, caption = tostring( args[1] ), tostring( fallbackArgs( args, 'варыянт', 'variant', 2 ) ), fallbackArgs( args, 'памер', 3 ), fallbackArgs( args, 'спасылка', 4 ), fallbackArgs( args, 'подпіс', 5 )

	local isLoop = 0
	while type( getCountryData( country ) ) == 'string' do
		isLoop = isLoop + 1
		if isLoop > 6 then return showError( 'сцяг не знойдзены з-за множных перанакіраванняў', 'Старонкі з некарэктнымі параметрамі сцягафікацыі' ) end
		country = getCountryData( country ) -- following the redirect, incl. double redirects
	end
	if getCountryData( country ) == nil then return showError( 'краіна/тэрыторыя «' .. country .. '» не існуе альбо не дададзеная ў [[Модуль:Сцягі краін]]', 'Старонкі з некарэктнымі параметрамі сцягафікацыі' ) end
	if getCountryData( country )[variant] == nil then
		if getCountryData( country ).default == nil then return showError( 'краіна/тэрыторыя «' .. country .. '» не мае дапомнага сцяга', 'Старонкі з некарэктнымі параметрамі сцягафікацыі' ) end
		variant = 'default'
	end
	if size == '' or size == nil then size = '25px' end

	-- shortcut to the specified variant
	local getVariant = function()
		return getCountryData( country )[variant]
	end

	if not mw.title.makeTitle( 'File', getVariant().image ).fileExists then return showError( 'файл «[[:File:' .. getVariant().image .. ']]» не існуе', 'Старонкі з некарэктнымі параметрамі сцягафікацыі' ) end

	return renderFlag( getVariant().image, size, getVariant().border or 'border', link or getVariant().link or country, caption or getVariant().caption or getVariant().link or country )
end

function p.getFlag( frame )
	local args
	if frame == mw.getCurrentFrame() then
		args = frame:getParent().args
	else
		args = frame
	end
	args = lcArgs( args )
	local country, variant, size, link = tostring( args[1] ), tostring( fallbackArgs( args, 'варыянт', 'variant', 2 ) ), fallbackArgs( args, 'памер', 3 ), fallbackArgs( args, 'спасылка', 4 )

	local isLoop = 0
	while type( getCountryData( country ) ) == 'string' do
		isLoop = isLoop + 1
		if isLoop > 6 then return showError( 'сцяг не знойдзены з-за множных перанакіраванняў', 'Старонкі з некарэктнымі параметрамі сцягафікацыі' ) end
		country = getCountryData( country ) -- following the redirect, incl. double redirects
	end
	if getCountryData( country ) == nil then return showError( 'краіна/тэрыторыя «' .. country .. '» не існуе альбо не дададзеная ў [[Модуль:Сцягі краін]]', 'Старонкі з некарэктнымі параметрамі сцягафікацыі' ) end
	if getCountryData( country )[variant] == nil then
		if getCountryData( country ).default == nil then return showError( 'краіна/тэрыторыя «' .. country .. '» не мае дапомнага сцяга', 'Старонкі з некарэктнымі параметрамі сцягафікацыі' ) end
		variant = 'default'
	end
	if size == '' or size == nil then size = '25px' end

	-- shortcut to the specified variant
	local getVariant = function()
		return getCountryData( country )[variant]
	end

	if not mw.title.makeTitle( 'File', getVariant().image ).fileExists then return showError( 'файл «[[:File:' .. getVariant().image .. ']]» не існуе', 'Старонкі з некарэктнымі параметрамі сцягафікацыі' ) end

	return renderFlag( getVariant().image, size, getVariant().border or 'border', link or getVariant().link or country )
end

return p