local function sterilizedec( decade )
if decade == nil or decade == '' then
return nil
end  
local dec = string.match(decade, '^[-%+]?(%d?%d?%d?0)$') or
string.match(decade, '^[-%+]?(%d?%d?%d?0)%D')
if dec then
return dec
else
--fix 2-4 digit decade
local decade_fixed234 = string.match(decade, '^[-%+]?(%d%d?%d?)%d$') or
string.match(decade, '^[-%+]?(%d%d?%d?)%d%D')
if decade_fixed234 then
return decade_fixed234..'0'
end  
--fix 1-digit decade
local decade_fixed1 = string.match(decade, '^[-%+]?(%d)$') or
string.match(decade, '^[-%+]?(%d)%D')
if decade_fixed1 then
return '0'
end  
--unfixable
return nil
end
end  
--Check for nav_hyphen default gap size + isolatedcat (not necessarily an error).
--Used by nav_hyphen only.
local function defaultgapcat( bool )
if bool and nexistingcats == 0 then
--using "nexistingcats > 0" isn't as useful, since the default gap size obviously worked
trackcat(17, 'Navseasoncats default season gap size')
end
end  
--12 -> 12th, etc.
--Used by nav_nordinal & nav_wordinal.
function p.addord( i )
if tonumber(i) then
local s = tostring(i)  
local tens = string.match(s, '1%d$')
if tens then return s..'th' end  
local ones = string.match(s, '%d$')
if ones == '1' then return s..'st'
elseif ones == '2' then return s..'nd'
elseif ones == '3' then return s..'rd' end  
return s..'th'
end
return i
end  
--Returns the properly formatted central nav element.
--Expects an integer i, and a catlinkfollowr table.
--Used by nav_decade & nav_ordinal only.
local function navcenter( i, catlink )
if i == 0 then -- nav element
if navborder == true then
return '*<b>'..catlink.displaytext..'</b>\n'
else
return '*<b>'..catlink.navelement..'</b>\n'
end
else
return '*'..catlink.navelement..'\n'
end
end  
--Return conditionally aligned stacked navs.
--Used by main only.
local function nav1nav2( nav1, nav2 )
if avoidself then
local force
return forcealign..'\n'..nav1..'\n'..nav2..'\n</div>'
else
return nav1..'\n'..nav2
end
end  
--==========================================================================
-- Formerly separated templates/modules
--==========================================================================  
--========================================================  
local function nav_hyphen( , start, hyph, finish, firstpart, lastpart, minseas, maxseas, testgap )
--Expects a PAGENAME of the form "Some sequential 2015–16 example cat", where
-- start = 2015
-- hyph = –
-- finish = 16 (sequential years can be abbreviated, but others should be full year, e.g. "2001–2005")
-- firstpart = Some sequential
-- lastpart = example cat
-- minseas = 1800 ('min' starting season shown; optional; defaults to -9999)
-- maxseas = 2000 ('max' starting season shown; optional; defaults to 9999; 2000 will show 2000-01)
-- testgap = 0 (testcasegap parameter for easier testing; optional)  
--sterilize start
if string.match(start or '', '^%d%d?%d?%d?$') == nil then --1-4 digits, AD only
local start_fixed = mw.ustring.match(start or '', '^%s*(%d%d?%d?%d?)%D')
if start_fixed then
start = start_fixed
else
errors = p.errorclass('Function nav_hyphen can\'t recognize the number "'..(start or '')..'" '..
'in the first part of the "season" that was passed to it. '..
'For e.g. "2015–16", "2015" is expected via "|2015|–|16|".')
return p.failedcat(errors, 'H')
end
end
local nstart = tonumber(start)  
--en dash check
if hyph ~= '–' then
trackcat(4, 'Navseasoncats range not using en dash') --nav still processable, but track
end  
--sterilize finish & check for weird parents
local tgaps = {} --table of gap sizes found b/w terms { [<gap size found>] = 1 }
local ttlens = {} --table of term lengths found w/i terms { [<term length found>] = 1 }
local tirregs = {} --table of ir/regular-term-length cats' "from"s & "to"s found
local regularparent = true
if (finish == -1) or --"Members of the Scottish Parliament 2021–present"
(finish == 0) --"Members of the Scottish Parliament 2021–"
then
regularparent = false
if maxseas == nil or maxseas == '' then
maxseas = start --hide subsequent ranges
end
if finish == -1 then trackcat(14, 'Navseasoncats range ends (present)')
else trackcat(15, 'Navseasoncats range ends (blank, MOS)') end
elseif (start == finish) and
(ttrackingcats[16] ~= '') --nav_year found isolated; check for surrounding hyphenated terms (e.g. UK MPs 1974)
then
trackcat(16, '') --reset for another check later
trackcat(13, 'Navseasoncats range irregular, 0-length')
ttlens[0] = 1 --calc ttlens for std cases below
regularparent = 'isolated'
end
if (string.match(finish or '', '^%d+$') == nil) and
(string.match(finish or '', '^%-%d+$') == nil)
then
local finish_fixed = mw.ustring.match(finish or '', '^%s*(%d%d?%d?%d?)%D')
if finish_fixed then
finish = finish_fixed
else
errors = p.errorclass('Function nav_hyphen can\'t recognize "'..(finish or '')..'" '..
'in the second part of the "season" that was passed to it. '..
'For e.g. "2015–16", "16" is expected via "|2015|–|16|".')
return p.failedcat(errors, 'I')
end
else
if string.len(finish) >= 5 then
errors = p.errorclass('The second part of the season passed to function nav_hyphen should only be four or fewer digits, not "'..(finish or '')..'". '..
'See MOS:DATERANGE for details.')
return p.failedcat(errors, 'J')
end
end
local nfinish = tonumber(finish)  
--save sterilized parent range for easier lookup later
tirregs['from0'] = nstart
tirregs['to0'] = nfinish  
--sterilize min/max
local nminseas_default = -9999
local nmaxseas_default = 9999
local nminseas = tonumber(minseas) or nminseas_default --same behavior as nav_year
local nmaxseas = tonumber(maxseas) or nmaxseas_default --same behavior as nav_year
if nminseas > nstart then nminseas = nstart end
if nmaxseas < nstart then nmaxseas = nstart end  
local lspace = ' ' --assume a leading space (most common)
local tspace = ' ' --assume a trailing space (most common)
if string.match(firstpart, '%($') then lspace = '' end --DNE for "Madrid city councillors (2007–2011)"-type cats
if string.match(lastpart, '^%)') then tspace = '' end --DNE for "Madrid city councillors (2007–2011)"-type cats  
--calculate term length/intRAseason size & finishing year
local term_limit = 10
local t = 1
while t <= term_limit and regularparent == true do
local nish = nstart + t --use switchADBC to flip this sign to work for years BC, if/when the time comes
if (nish == nfinish) or (string.match(nish, '%d?%d$') == finish) then
ttlens[t] = 1
break
end
if t == term_limit then
errors = p.errorclass('Function nav_hyphen can\'t determine a reasonable term length for "'..start..hyph..finish..'".')
return p.failedcat(errors, 'K')
end
t = t + 1
end  
--apply MOS:DATERANGE to parent
local lenstart = string.len(start)
local lenfinish = string.len(finish)
if lenstart == 4 and regularparent == true then --"2001–..."
if t == 1 then --"2001–02" & "2001–2002" both allowed
if lenfinish ~= 2 and lenfinish ~= 4 then
errors = p.errorclass('The second part of the season passed to function nav_hyphen should be two or four digits, not "'..finish..'".')
return p.failedcat(errors, 'L')
end
else --"2001–2005" is required for t > 1; track "2001–05"; anything else = error
if lenfinish == 2 then
trackcat(5, 'Navseasoncats range abbreviated (MOS)')
elseif lenfinish ~= 4 then
errors = p.errorclass('The second part of the season passed to function nav_hyphen should be four digits, not "'..finish..'".')
return p.failedcat(errors, 'M')
end
end
if finish == '00' then --full year required regardless of term length
trackcat(5, 'Navseasoncats range abbreviated (MOS)')
end
end  
--calculate intERseason gap size
local hgap_default = 0 --assume & start at the most common case: 2001–02 -> 2002–03, etc.
local hgap_limit_reg = 6 --less expensive per-increment (inc x 4)
local hgap_limit_irreg = 6 --more expensive per-increment (inc x 23: inc x (k_bwd + k_fwd) = inc x (12 + 11))
local hgap_success = false
local hgap = hgap_default
while hgap <= hgap_limit_reg and regularparent == true do --verify