Jump to content

Module:Year in sports

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by SWinxy (talk | contribs) at 20:13, 17 August 2022 (Add link() function). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

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

local sports = {
	"American football",
	"aquatic sports",
	"association football",
	"badminton",
	"baseball",
	"basketball",
	"chess",
	"climbing",
	"combat sports",
	"sumo",
	"cycling",
	"equestrianism",
	"esports",
	"golf",
	"gymnastics",
	"ice sports",
	"modern pentathlon",
	"motorsport",
	"racquetball",
	"sailing",
	"skiing",
	"squash",
	"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 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 below = year
	local y = year - 1
	
	if (mw.title.new(y .. " in sports")) then
		below = link(y .. " in sports", y) .. below
	end
	y = y - 1
	if (mw.title.new(y .. " in sports")) then
		below = link(y .. " in sports", y) .. below
	end
	y = y - 1
	if (mw.title.new(y .. " in sports")) then
		below = link(y .. " in sports", y) .. below
	end
	y = y - 1
	if (mw.title.new(y .. " in sports")) then
		below = link(y .. " in sports", "←") .. below
	end
	y = year + 1
	if (mw.title.new(y .. " in sports")) then
		below = link(y .. " in sports", y) .. below
	end
	y = year + 1
	if (mw.title.new(y .. " in sports")) then
		below = link(y .. " in sports", y) .. below
	end
	y = year + 1
	if (mw.title.new(y .. " in sports")) then
		below = link(y .. " in sports", y) .. below
	end
	y = year + 1
	if (mw.title.new(y .. " in sports")) then
		below = below .. link(y .. " in sports", "→")
	end
	
	local passing = {
		name = "Year in sports",
		title = link(year .. " in sports"),
		content1 = l,
		below = below
	}
	
	return sidebar.sidebar(frame, passing)
end

return p