Module:Timeline of release years

This is an old revision of this page, as edited by Jackmcbarn (talk | contribs) at 19:20, 16 July 2015 (rm unused parameters). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

require('Module:No globals')
local getArgs = require('Module:Arguments').getArgs
local p = {}

local function isYearReleased(args, year)
	if args[year] then
		return true
	end
	for i = 96, 105 do
		if args[year .. string.char(i)] then
			return true
		end
	end
end

local function color(args, year)
	return isYearReleased(args, year) and '#0BDA51' or '#228B22'
end

local function titleItem(content)
	if content ~= nil then
		return mw.html.create( 'span' ):wikitext( '– ' .. content .. '<br />' )
	end
end

local function left(year)
	return mw.html.create( 'td' ):wikitext( year  .. ' –' )
end

local function center(args, year)
	return mw.html.create( 'td' )
		:css( 'width', '10px' )
		:css( 'border', '1px solid black' )
		:css( 'background-color', color(args, year) )
end

local function right(args, year)
	if isYearReleased(args, year) == nil then return nil end
	
	local ret
	ret = mw.html.create( 'td' )
		:node( titleItem(args[year]) or titleItem(args[year .. 'a' ]) )

	for i = 98, 106 do
		ret:node( titleItem(args[year .. string.char(i)]) )
	end

	return ret
end

local function row(args, year)
	return mw.html.create('tr')
		:node( left(year) )
		:node( center(args, year) )
		:node( right(args, year) )
end

--------------------------------------------------------------------------------

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

function p._main(args)
	-- Main module code goes here.
	local ret
	local firstyear, lastyear
	ret = mw.html.create( 'table' )
		:css('float', args.align or 'right')
		:css('clear', args.align or 'right')
		:css('margin', '0 0 0.5ex 1em')
		:css('font-size', '80%')
		:css('line-height', '90%')
		:attr('cellspacing', '0')
		:attr('cellpadding', '4')
		:attr('summary', args.summary or '')
	
	ret:tag('caption')
		:css('font-weight', 'bold')
		:wikitext(args.title or 'Timeline of release years')

	if tonumber(args.first) then
		firstyear = tonumber(args.first)
	else
		for i = 1980, os.date('%Y') do
			if isYearReleased(args, i) then
				firstyear = i
				break
			end
		end
	end
	
	if tonumber(args.last) then
		lastyear = tonumber(args.last)
	else
		for i = os.date('%Y') + 3, firstyear, -1 do
			if isYearReleased(args, i) then
				lastyear = i
				break
			end
		end
	end

	for year = firstyear, lastyear do
		ret:node( row(args, year) )
	end
	
	return ret
end

return p