Module:Year in sports

local getArgs = require('Module:Arguments').getArgs
local sidebar = require('Module:Sidebar')
local list = require('Module:List')
local parser = mw.ext.ParserFunctions

local sports = {
	"air sports",
	"American football",
	"aquatic sports",
	"association football",
	"badminton",
	"baseball",
	"basketball",
	"bowling",
	"chess",
	"climbing",
	"combat sports",
	"cricket",
	"cycling",
	"equestrianism",
	"esports",
	"golf",
	"gymnastics",
	"ice sports",
	"modern pentathlon",
	"motorsport",
	"racquetball",
	"rugby union",
	"sailing",
	"skiing",
	"squash",
	"sumo",
	"table tennis",
	"tennis",
	"triathlon",
	"volleyball",
	"weightlifting"
}

local p = {}

local function link(a, b)
	if (not b) then
		return "[[" .. a .. "]]"
	end
	return "[[" .. a .. "|" .. b .. "]]"
end

function p.main(frame)
	local args = getArgs(frame)
	
	return p._main(frame, args)
end

function p._main(frame, args)
	local year = args[1] or args['year'] or os.date("%Y")
	local what = args[2] or args['sport'] or "sports"
	local inwhat = " in " .. what
	
	local pages = {}
	
	for i, sport in ipairs(sports) do
		local s = year .. " in " .. sport
		local page = mw.title.new(s, 0)
		
		if (page.exists) then
			table.insert(pages, link(s, sport:gsub("^%l", string.upper)))
		end
	end
	
	local l = list.horizontal(pages)
	
	local years = year
	local y = year - 1
	
	if (mw.title.new(y .. inwhat)) then
		years = link(y .. inwhat, y) .. " " .. years
	end
	y = y - 1
	if (mw.title.new(y .. inwhat)) then
		years = link(y .. inwhat, y) .. " " .. years
	end
	y = y - 1
	if (mw.title.new(y .. inwhat)) then
		years = link(y .. inwhat, y) .. " " .. years
	end
	y = y - 1
	if (mw.title.new(y .. inwhat)) then
		years = link(y .. inwhat, "←") .. " " .. years
	end
	y = year + 1
	if (mw.title.new(y .. inwhat)) then
		years = years .. " " .. link(y .. inwhat, y)
	end
	y = y + 1
	if (mw.title.new(y .. inwhat)) then
		years = years .. " " .. link(y .. inwhat, y)
	end
	y = y + 1
	if (mw.title.new(y .. inwhat)) then
		years = years .. " " .. link(y .. inwhat, y)
	end
	y = y + 1
	if (mw.title.new(y .. inwhat)) then
		years = years .. link(y .. inwhat, "→")
	end
	
	local v = "Years"
	
	if mw.title.new("List of years in " .. what) then
		v = link("List of years in " .. what, "Years")
	end
	
	local passing = {
		name = "Year in sports",
		title = v .. " in [[" .. what .. "]]",
		content1 = years,
		content2 = "----",
		heading3 = link(year .. " in sports"),
		content4 = l
	}
	
	return sidebar.sidebar(frame, passing)
end

return p