Difference between revisions of "Module:Navseasoncats/navyear"

From blackwiki
Jump to navigation Jump to search
blackwiki>Tom.Reding
m (ws fmt, --ce + example years for clarity)
blackwiki>Tom.Reding
(Simplify, ce)
Line 1: Line 1:
 
local p = {}
 
local p = {}
  
local greyLinkColor = "#888"
+
local grey = "#888"
  
 
-- Make a piped link to a category, if it exists
 
-- Make a piped link to a category, if it exists
-- If it doesn't exist, just display the greyed the link title without linking
+
-- If it doesn't exist, just display the greyed unlinked title
 
function makeCatLink(catname, disp)
 
function makeCatLink(catname, disp)
local displaytext
+
local displaytext = catname
if (disp ~= "") and (disp ~= nil) then
+
if disp and disp ~= '' then
 
-- use 'disp' parameter, but strip any trailing disambiguator
 
-- use 'disp' parameter, but strip any trailing disambiguator
displaytext = mw.ustring.gsub(disp, "%s+%(.+$", "");
+
displaytext = mw.ustring.gsub(disp, '%s+%(.+$', '');
else
 
displaytext = catname
 
 
end
 
end
local fmtlink
+
local catPage = mw.title.new( catname, 'Category' )
local catPage = mw.title.new( catname, "Category" )
 
 
if (catPage.exists) then
 
if (catPage.exists) then
fmtlink = "[[:Category:" .. catname .. "|" .. displaytext .. "]]"
+
return '[[:Category:'..catname..'|'..displaytext..']]'
 
else
 
else
fmtlink = '<span style="color:' .. greyLinkColor .. '">' .. displaytext .. "</span>"
+
return '<span style="color:'..grey..'">'..displaytext.."</span>"
 
end
 
end
 
return fmtlink
 
 
end
 
end
  
Line 31: Line 26:
 
-- {{{4}}}=1758 ('min' year parameter; optional)
 
-- {{{4}}}=1758 ('min' year parameter; optional)
 
-- {{{5}}}=1800 ('max' year parameter; optional)
 
-- {{{5}}}=1800 ('max' year parameter; optional)
local arg1 = frame.args[1]
+
local firsthalf = frame.args[1]
local arg2 = tonumber(frame.args[2])
+
local year      = tonumber(frame.args[2])
local arg3 = frame.args[3]
+
local lasthalf  = frame.args[3]
local arg4 = tonumber(frame.args[4])
+
local minyear  = tonumber(frame.args[4]) or -9999
local arg5 = tonumber(frame.args[5])
+
local maxyear  = tonumber(frame.args[5]) or 9999
if arg4 == nil then arg4 = -9999 end
 
if arg5 == nil then arg5 = 9999 end
 
 
 
 
local navyear = '{| class="toccolours hlist" style="text-align: center; margin: auto;"\n'..'|\n'
 
local navyear = '{| class="toccolours hlist" style="text-align: center; margin: auto;"\n'..'|\n'
Line 43: Line 36:
 
local i = -5
 
local i = -5
 
while i <= 5 do
 
while i <= 5 do
local year = arg2 + i
+
local y = year + i
 
if i ~= 0 then
 
if i ~= 0 then
if (year >= arg4) and (year <= arg5) then -- ex: 1758, 1759, 1761, 1762, 1763, 1764, 1765
+
if (y >= minyear) and (y <= maxyear) then -- ex: 1758, 1759, 1761, 1762, 1763, 1764, 1765
navyear = navyear..'*'..makeCatLink( arg1..' '..year..' '..arg3, year )..'\n'
+
navyear = navyear..'*'..makeCatLink( firsthalf..' '..y..' '..lasthalf, y )..'\n'
 
else -- ex: 1755, 1756, 1757
 
else -- ex: 1755, 1756, 1757
navyear = navyear..'*<span style="visibility:hidden">'..year..'</span>\n'
+
navyear = navyear..'*<span style="visibility:hidden">'..y..'</span>\n'
 
end
 
end
 
else -- ex: 1760
 
else -- ex: 1760
navyear = navyear..'*<b>'..arg2..'</b>\n'
+
navyear = navyear..'*<b>'..year..'</b>\n'
 
end
 
end
 
i = i + 1
 
i = i + 1

Revision as of 12:47, 25 April 2019

About

This is a former submodule to {{Navseasoncats}}. Due to minimal error checking and limited scope, it should not be used on its own except in controlled situations. It should not be used in category sequences which extend below AD 6, to simplify maintenance for that sequence and to limit the potential for error.



local p = {}

local grey = "#888"

-- Make a piped link to a category, if it exists
-- If it doesn't exist, just display the greyed unlinked title
function makeCatLink(catname, disp)
	local displaytext = catname
	if disp and disp ~= '' then
		-- use 'disp' parameter, but strip any trailing disambiguator
		displaytext = mw.ustring.gsub(disp, '%s+%(.+$', '');
	end
	local catPage = mw.title.new( catname, 'Category' )
	if (catPage.exists) then
		return '[[:Category:'..catname..'|'..displaytext..']]'
	else
		return '<span style="color:'..grey..'">'..displaytext.."</span>"
	end
end

function p.navyear(frame)
	--Expects a PAGENAME of the form "Some sequential 1760 example cat", where 
	--	{{{1}}}=Some sequential
	--	{{{2}}}=1760
	--	{{{3}}}=example cat
	--	{{{4}}}=1758 ('min' year parameter; optional)
	--	{{{5}}}=1800 ('max' year parameter; optional)
	local firsthalf = frame.args[1]
	local year      = tonumber(frame.args[2])
	local lasthalf  = frame.args[3]
	local minyear   = tonumber(frame.args[4]) or -9999
	local maxyear   = tonumber(frame.args[5]) or 9999
	
	local navyear = '{| class="toccolours hlist" style="text-align: center; margin: auto;"\n'..'|\n'
	
	local i = -5
	while i <= 5 do
		local y = year + i
		if i ~= 0 then
			if (y >= minyear) and (y <= maxyear) then -- ex: 1758, 1759, 1761, 1762, 1763, 1764, 1765
				navyear = navyear..'*'..makeCatLink( firsthalf..' '..y..' '..lasthalf, y )..'\n'
			else -- ex: 1755, 1756, 1757
				navyear = navyear..'*<span style="visibility:hidden">'..y..'</span>\n'
			end
		else -- ex: 1760
			navyear = navyear..'*<b>'..year..'</b>\n'
		end
		i = i + 1
	end
	return navyear..'|}'
end

return p