Перайсці да зместу

Модуль:Plural switch

З Вікіпедыі, свабоднай энцыклапедыі

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

local p = {}

function p.plural(frame)
	local text          = frame.args[1]
	local text_singular = frame.args[2]
	local text_plural   = frame.args[3]

	local is_ident = false
	local is_plural = false

	-- прымусовая ўстаноўка
	if not is_ident then
		if string.find(text, 'data%-plural="0"') then is_plural = false; is_ident = true; end
		if string.find(text, 'data%-plural="1"') then is_plural = true;  is_ident = true; end
	end

	-- праверка па тэгах
	if not is_ident and string.find(text, "<br") then is_plural = true; is_ident = true; end
	if not is_ident then
		_, count = string.gsub(text, "<li", "<li")
		if count > 1 then is_plural = true; is_ident = true; end
	end

	-- анализ текста
	if not is_ident then
		text = string.gsub(text, '%[%[.-%]%] <span lang=.-</span>', '') -- прыбіраем расшыфроўкі з іншых моў
		text = string.gsub(text, '%]<span.-</span></span>', '')         -- выдаліць шаблон np3
		text = string.gsub(text, '<sup.-</sup>', '')   -- прыбіраем верхнія індэксы
		text = string.gsub(text, '<.->', '')           -- прыбіраем усе тэгі
		text = string.gsub(text, '&[^&]-;', ' ')       -- мяняем спец. сімвалы прабеламі
		text = string.gsub(text, '%[%[Category.-%]%]', '') -- выразаем спасылкі на катэгорыі
		text = string.gsub(text, '%[%[File.-%]%]', 'img')  -- замяняем шаблоны-карцінкі (сцягі)
		text = string.gsub(text, '%[%[Файл.-%]%]', 'img')
		text = string.gsub(text, '%]%][а-я-]+%[%[', '')   -- склейваем элементы побач
		text = string.gsub(text, '%]%] +%[%[', '')
		text = string.gsub(text, '%[%[.-%]%]', 'x ')   -- лічым [[...]] адным элементам
		text = string.gsub(text, '%[.-%]', 'x ')       -- лічым [...] адным элементам
		text = string.gsub(text, '.."`UNIQ.-QINU`"..', '')     -- прыбіраем спецыяльныя коды вікіразметкі + магчымыя замены [[...]]
		text = string.gsub(text, '[a-z]x', 'a')        -- скарачаем англійскія словы
		text = string.gsub(text, '\n%*', ',')          -- зорачка ў пачатку - элемент спісу
		text = string.gsub(text, 'x,%-x', 'x')         -- элементы-словы праз злучок лічым адным
		text = string.gsub(text, '^ *,', '')           -- прыбіраем першую коску
		text = string.gsub(text, 'x.-x', 'x,x')        -- дадаем раздзяляльнік паміж x: x .. , .. x
		text = string.gsub(text, '[ \n]+$', '')        -- trim

		delimiters = {",", ";", "·", "•", "/", " и ", "\n"}
		for i,d in pairs(delimiters) do
			if not is_plural and string.find(text, d) then is_plural = true; is_ident = true; end
		end
	end

	-- фармуем вынік
	local result = text_singular
	if is_plural then result = text_plural end

	return result
end

return p