Модуль для шаблонов {{перенаправление}} и {{redirect-multi}}.


--[[
-- Этот модуль реализует шаблоны {{перенаправление}} и {{redirect-multi}}
--]]
local mArguments --lazily initialize
local libraryUtil = require('libraryUtil')
local checkType = libraryUtil.checkType
local mHatnote = require('Module:Hatnote')
local mHatList = require('Module:Hatnote list')

local p = {}

function p.redirect(frame)
	mArguments = require('Module:Arguments')
	local args = mArguments.getArgs(frame, {parentOnly=true})
	--Get number of redirects
	local numRedirects = tonumber(frame.args[1]) or 1
	if not args[2] then args[2] = mHatList.disambiguate(args[1]) end
	return p._redirect(args, numRedirects)
end

function p._redirect(args, numRedirects, currentTitle, redirectTitle, targetTitle)
	-- Validate the input. Don't bother checking currentTitle, redirectTitle or
	-- targetTitle, as they are only used in testing.
	checkType('_redirect', 1, args, 'table')
	checkType('_redirect', 2, numRedirects, 'number', true)
	numRedirects = numRedirects or 1
	currentTitle = currentTitle or mw.title.getCurrentTitle()
	-- Get the table of redirects
	local redirect = {}
	for i = 1, numRedirects do
		-- Return an error if a redirect parameter is missing.
		if not args[i] then
			return error('missing redirect parameter')
		end
		redirect[i] = args[i]
	end
	-- Generate the text.
	local formattedRedirect = {}
	for k,v in pairs(redirect) do
		formattedRedirect[k] = '«' .. v .. '»'
	end
	local text = {}
	-- maxArg's gotten manually because getArgs() and table.maxn aren't friends
	local maxArg = 0
	for k, v in pairs(args) do
		if type(k) == 'number' and k > maxArg then maxArg = k end
	end
	if numRedirects == 1 then
		text = {
			'Запрос',
			formattedRedirect[1],
			'перенаправляется сюда;',
			mHatList.forSee(args[3]),
			mHatList.otherUse(args[3], args[2])
		}
		text = table.concat(text, ' ') .. '.'
	else
		text = {
		'Запросы',
		mHatList.conjList('', formattedRedirect),
		'перенаправляются сюда;'
		}
		i = 0
		text2 = {}
		repeat
			row = {}
			if args[numRedirects+i+1] then
				table.insert(row, ' ')
				table.insert(row, mHatList.forSee(args[numRedirects+i+2]))
				table.insert(row, mHatList.otherUse(args[numRedirects+i+2], args[numRedirects+i+1]))
				table.insert(text2, table.concat(row, ' '))
			end
			i = i + 2
		until i > maxArg - 2
		text = table.concat(text, ' ') .. table.concat(text2, ',') .. '.'
	end
	return mHatnote.hatnote({text, hide_disambig=1})
end
	
return p