Module:Navseasoncats/navhyphen

local p = {}

function errorclass(msg) return mw.text.tag( 'span', {class='error mw-ext-cite-error'}, 'Error! '..msg ) end

function p.navhyphen(frame) --Expects a PAGENAME of the form "Some sequential 2015–16 cat", where --	=2015	--	=–	--	=16	--	=Some sequential --	=cat -- For "1999-2000", "2000" must be truncated so 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('Second part of season passed to Navseasoncats/navhyphen should only be two digits, not '..arg3..'. '..							'Also, e.g. 1999-2000 should be cut down to 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 can\'t recognise the number '..arg1..' in the first part of the "season" that has been passed to it. '..								'For e.g. 2015–16, 16 is expected via |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 can\'t recognise the number '..arg3..' in the second part of the "season" that has been passed to it. '..									'For e.g. 2015–16, 16 is expected via |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="border:1px solid #A2A9B1; background-color:#F8F9FA; padding:0.375em; 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..'|'..arg1..arg2..arg3..' &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