Mô đun:Navseasoncats/navhyphen

Bách khoa toàn thư mở Wikipedia
Tài liệu mô đun[tạo]
local p = {}

function errorclass(msg)
	return mw.text.tag( 'span', {class='error mw-ext-cite-error'}, '<b>Lỗi!</b> '..msg )
end

function p.navhyphen(frame)
	--Expects a PAGENAME of the form "Some sequential 2015–16 cat", where 
	--	{{{1}}}=2015
	--	{{{2}}}=–
	--	{{{3}}}=16
	--	{{{4}}}=Some sequential
	--	{{{5}}}=cat
	-- For "1999-2000", "2000" must be truncated so {{{3}}} is "00".
	local arg1 = frame.args[1]
	local arg2 = frame.args[2]
	local arg3 = frame.args[3]
	local arg4 = frame.args[4]
	local arg5 = frame.args[5]
	local narg1 = tonumber(arg1)
	local narg3 = tonumber(arg3)
	
	local errors = '' --TODO: use local regex for these checks instead of template-expands
	if string.len(arg3) > 2 then --expecting 16
		errors = errorclass('Phần thứ hai của mùa giải được chuyển cho Navseasoncats/navhyphen chỉ nên có hai ký tự, không phải là '..arg3..'. '..
							'Ngoài ra, ví dụ 1999-2000 nên được cắt giảm đến 1999-00.')
	else
		local strnumber = frame:expandTemplate{ title = 'Str number/trim', args = { arg1 } }
		if string.len(strnumber) < string.len(arg1) then --if 4-digit but non-numeric, e.g. 201t
			errors = errorclass('Navseasoncats/navhyphen không thể nhận ra số '..arg1..' trong phần thứ nhất của "mùa giải" đã được chuyển cho nó. '..
								'Cho ví dụ 2015–16, 16 được mong đợi thông qua |2015|–|16|.')
			if narg1 == nil then narg1 = tonumber(strnumber) end
		else
			local strnumber = frame:expandTemplate{ title = 'Str number/trim', args = { arg3 } }
			if string.len(strnumber) < string.len(arg3) then --if 2-digit but non-numeric, e.g. 1O
				errors = errorclass('Navseasoncats/navhyphen không thể nhận ra số '..arg3..' trong phần thứ hai của "mùa giải" đã được chuyển cho nó. '..
									'Cho ví dụ 2015–16, 16 được mong đợi thông qua |2015|–|16|.')
			end
		end
	end
	if narg1 == nil then narg1 = tonumber( frame:expandTemplate{ title = 'Str number/trim', args = { arg1 } } ) end
	if narg3 == nil then narg3 = tonumber( frame:expandTemplate{ title = 'Str number/trim', args = { arg3 } } ) end
	if errors ~= '' then errors = errors..'\n' end
	
	local nav = '{| class="toccolours" style="text-align: center; margin: auto;"\n'
	
	local i = 0
	while i <= 2 do
		local zero1 = ''
		local cent1 = ''
		if narg3 > (3-i) and narg3 < (13-i) then zero1 = '0' end
		if narg3 > (3-i) then cent1 = tostring(narg3 - (3-i))
		elseif narg3 == (3-i) then cent1 = mw.ustring.match(arg1, '^%s*(%d%d)')..'00'
		else cent1 = tostring(narg3 + (97+i))
		end
		
		local zero2 = ''
		local cent2 = ''
		if narg3 > (2-i) and narg3 < (13-i) then zero2 = '0' end
		if narg3 > (2-i) then cent2 = tostring(narg3 - (3-i))
		else cent2 = tostring(narg3 + (97+i))
		end
		
		nav = nav..'|'..
				frame:expandTemplate{
					title = 'LinkCatIfExists2',
					args = { 
						arg4..' '..(narg1 - (3-i))..arg2..zero1..cent1..' '..arg5,
						(narg1 - (3-i))..arg2..zero2..cent2
					}
				}..' &bull;'
		nav = nav..'\n\n'
		i = i + 1
	end
	
	nav = nav..'|<b>'..arg1..arg2..arg3..'</b> &bull; \n\n'
	
	local j = 0
	while j <= 2 do
		local zero1 = ''
		local cent1 = ''
		if narg3 < (9-j) or narg3 > (99-j) then zero1 = '0' end
		if narg3 < (99-j) then cent1 = tostring(narg3 + (1+j))
		elseif narg3 == (99-j) then
			cent1 = tostring(tonumber(mw.ustring.match(arg1, '^%s*(%d%d)')) + 1)..'00'
		else cent1 = tostring(narg3 - (99-j))
		end
		
		local zero2 = ''
		local cent2 = ''
		if narg3 < (9-j) or narg3 > (98-j) then zero2 = '0' end
		if narg3 < (99-j) then cent2 = tostring(narg3 + (1+j))
		else cent2 = tostring(narg3 - (99-j))
		end
		
		local bull = ''
		if j < 2 then bull = ' &bull;' end
		nav = nav..'|'..
				frame:expandTemplate{
					title = 'LinkCatIfExists2',
					args = { 
						arg4..' '..(narg1 + (1+j))..arg2..zero1..cent1..' '..arg5,
						(narg1 + (1+j))..arg2..zero2..cent2
					}
				}..bull
		nav = nav..'\n\n'
		j = j + 1
	end
	
	return errors..nav..'|}'
end

return p