Jump to content

Module:Timeline of release years

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by 風中的刀劍 (talk | contribs) at 17:46, 16 July 2015 (Created page with 'local getArgs = require('Module:Arguments').getArgs local p = {} local function isYearReleased(args, year) if args[year] then return true else for i = 9...'). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

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

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

local function color(args, year)
	if isYearReleased(args, year) then
		return '#0BDA51'
	else
		return '#228B22'
	end
end

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

local function left(args, year, displeft)
	local ret
	ret = mw.html.create( 'td' )
	
	ret
		:wikitext( (displeft or year)  .. ' –' )
		
	return ret
end

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

local function right(args, year)
	if isYearReleased(args, year) == nil then return nil end
	
	local ret
	ret = mw.html.create( 'td' )

	ret
		: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, displeft)
	local ret
	ret = mw.html.create('tr')
	
	ret
		:node( left(args, year) )
		:node( center(args, year) )
		:node( right(args, year) )
		
	return ret
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' )
	
	ret
		: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 '')
	
	do
		local caption
		caption = mw.html.create('caption')
		caption
			:css('font-weight', 'bold')
			:wikitext(args.title or 'Timeline of release years')
		ret:node(caption)
	end

	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