Ҷаҳиш ба мӯҳтаво

Модул: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>', '')         -- шаблони нп3-ро хориҷ мекунем
		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 = mw.ustring.gsub(text, '[–—]', '-')      -- навъҳои гуногуни тиреро ба аломати тарҳ иваз мекунем
		text = string.gsub(text, '%d+%-%d+', '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