Mô đun:Navseasoncats/navdecade

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.navdecade(frame)
	--Expects a PAGENAME of the form "Some sequential 2015 cat", where 
	--	{{{1}}}=Some sequential
	--	{{{2}}}=2015
	--	{{{3}}}=cat
	--and
	--	{{{4}}}=2010
	--the penultimate decade to be shown, normally the current 4-digit decade.
	local arg1 = frame.args[1]
	local arg2 = frame.args[2]
	local arg3 = frame.args[3]
	local arg4 = frame.args[4]
	local narg2 = tonumber(arg2)
	local narg4 = tonumber(arg4)
	local errors = '' --TODO: use local regex for these checks instead of template-expands
	if string.len(arg2) ~= 4 then --expecting e.g. yyyy
		errors = errorclass('Decade module at Navseasoncats/navdecade has been sent "'..arg2..'" but is expecting a 4-digit year ending in zero, of the form yyy0 - there should be 4 digits only. '..
							'So e.g. the 2010s should be cut down to 2010.')
	elseif string.len(arg4) ~= 4 then --expecting e.g. yyyy
		errors = errorclass('Decade module at Navseasoncats/navdecade can\'t recognise a number in the "year" '..arg4..' that has been passed to it as the penultimate decade to be shown. '..
							'It expects to be sent e.g. 2010.')
	else
		local strnumber = frame:expandTemplate{ title = 'Str number/trim', args = { arg2 } }
		if string.len(strnumber) < string.len(arg2) then --if 4-digit but non-numeric, e.g. 201t
			errors = errorclass('Decade module at Navseasoncats/navdecade can\'t recognise a number in the "year" '..arg2..' that has been passed to it. '..
								'It expects to be sent e.g. 2010.')
			if narg2 == nil then narg2 = tonumber(strnumber) end
		else
			local strrightc = frame:expandTemplate{ title = 'Str rightc', args = { arg4, 1 } }
			if strrightc ~= '0' then --expecting 2010
				errors = errorclass('Decade module at Navseasoncats/navdecade has been sent "'..arg4..'" but is expecting a 4-digit year ending in zero, of the form yyy0 - there should be no trailing s. '..
									'So e.g. the 1990s should be cut down to 1990.')
			end
			if narg4 == nil then narg4 = tonumber( frame:expandTemplate{ title = 'Str number/trim', args = { arg4 } } ) end
		end
	end
	if errors ~= '' then errors = errors..'\n' end
	
	local nav = '{| class="toccolours" style="text-align: center; margin: auto;"\n'
	
	local i = 0
	while i <= 20 do
		nav = nav..'|'
		if narg2 > (narg4 - (1+i)) then
			nav = nav..frame:expandTemplate{ title = 'LinkCatIfExists2', args = { arg1..' '..(narg2 - (70-i))..' '..arg3, (narg2 - (70-i))..'' } }..' &bull;'
		end
		nav = nav..'\n'
		i = i + 10
	end
	
	local j = 0
	while j <= 30 do
		nav = nav..'|'..frame:expandTemplate{ title = 'LinkCatIfExists2', args = { arg1..' '..(narg2 - (40-j))..' '..arg3, (narg2 - (40-j))..'' } }..' &bull;\n'
		j = j + 10
	end
	
	nav = nav..'|<b>'..arg2..'</b>\n'..
		  '\n'..
		  '| &bull; '..frame:expandTemplate{ title = 'LinkCatIfExists2', args = { arg1..' '..(narg2 + 10)..' '..arg3, (narg2 + 10)..'' } }..'\n'
		  
	local k = 0
	while k <= 20 do
		nav = nav..'|'
		if narg2 < (narg4 - (1+k)) then
			nav = nav..'&bull; '..frame:expandTemplate{ title = 'LinkCatIfExists2', args = { arg1..' '..(narg2 + (20+k))..' '..arg3, (narg2 + (20+k))..'' } }
		end
		nav = nav..'\n'
		k = k + 10
	end
	
	return errors..nav..'|}'
end

return p