Jump to content

Module:Timeline of release years/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by 2001:da8:4001:3:c84a:9cd3:cb80:950f (talk) at 12:34, 27 April 2016. 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 asciiletter = 97, 106 do -- 97 == a, 106 == j
		if args[year .. string.char(asciiletter)] then
			return true
		end
	end
end

local function color(args, year)
	
	if args[year .. '_color'] then
		return args[year .. '_color'] 
	end
	
	for yearrange = 1, 5 do
		if args['range' .. yearrange] and args['range' .. yearrange .. '_color'] then
			local _, _, beginyear, endyear = string.find( args['range' .. yearrange], '^(%d%d%d%d)%D+(%d%d%d%d)$' )
			
			local year = tonumber(year) or 9999 -- For year == 'TBA'
			beginyear = tonumber(beginyear) or 0
			endyear =  tonumber(endyear) or 9999
			
			if year >= beginyear and year <= endyear then
				local _, _, color1, color2 = string.find( args['range' .. yearrange .. '_color'], '^(%S*)%s*(%S*)$' )
				color2 = (string.find(color2, '^#?%w+$')) and color2 or color1
				return isYearReleased(args, year) and color1 or color2
			end
		end
	end
	
	return isYearReleased(args, year) and '#0BDA51' or '#228B22'
end

local function left(builder, args, year)
	builder:tag( 'td' )
		:attr('scope', 'row')
		:css('padding', '5px')
		:css('border-right', '20px solid ' .. color(args, year))
		:wikitext( year )
end

local function right(builder, args, year)
	if isYearReleased(args, year) == nil then return end
	
	local itemOne = args[year] or args[year .. 'a']
	for asciiletter = 98, 106 do
		if args[year .. string.char(asciiletter)] and itemOne then
			itemOne = '\127'
			break
		elseif itemOne then
			-- nothing to do
		elseif args[year .. string.char(asciiletter)] then
			itemOne  = args[year .. string.char(asciiletter)]
		end
	end
	
	if itemOne == '\127' then
			
		builder = builder:tag('td')
			:addClass('plainlist')
			:css('padding','5px')
			:tag('ol')
		
		if args[year] or args[year .. 'a'] then
			builder:tag('li'):wikitext(args[year] or args[year .. 'a'])
		end
	
		for asciiletter = 98, 106 do
			if args[year .. string.char(asciiletter)] then
				builder:tag('li'):wikitext(args[year .. string.char(asciiletter)])
			end
		end
	
	else
	
		builder = builder:tag('td')
			:css('padding','5px')
			:wikitext(itemOne)
	end
	

end

local function row(builder, args, year)
	builder = builder:tag('tr')
	left(builder, args, year)
	right(builder, 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, TBA
	TBA = isYearReleased(args, 'TBA')
	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%')
		:css('border-spacing', '0 1px')
	
	ret:tag('caption')
		:css('padding', '5px')
		:wikitext(args.title and ('<b>' .. args.title .. '</b>') or '<b>Timeline of release years</b>')
		:wikitext(args.subtitle and ('<br />' .. args.subtitle) )

	if tonumber(args.first) then
		firstyear = tonumber(args.first)
	else
		for i = 1940, 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, TBA and os.date('%Y') or firstyear, -1 do
			if isYearReleased(args, i) then
				lastyear = i
				break
			end
		end
		lastyear = lastyear or (os.date('%Y') - 1)
	end

	for year = firstyear, lastyear do
		row(ret, args, year)
	end
	
	if TBA then
		row(ret, args, 'TBA')
	end
	
	return ret
end

return p