モジュール:Release timeline

require('strict')
local getArgs = require('Module:Arguments').getArgs
local p = {}
local function items(args, year)
	local itemList = {}
	if argsyear or argsyear .. 'a' then
		table.insert(itemList, argsyear or argsyear .. 'a')
	end
	for asciiletter = 98, 106 do -- 98 > b, 106 > j
		if argsyear .. string.char(asciiletter) then
			table.insert(itemList, argsyear .. string.char(asciiletter))
		end
	end
	return table.maxn(itemList), itemList
end
local function color(args, year, itemNum)
	if argsyear .. '_color' then
		return argsyear .. '_color' 
	end
	for yearrange = 1, 10 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  0 and color1 or color2
			end
		end
	end
	return itemNum > 0 and '#0BDA51' or '#228B22'
end
local function left(builder, args, year, itemNum)
	builder = builder:tag('th')
		:attr('scope', 'row')
		:css('border-right', '1.4em solid ' .. color(args, year, itemNum))
		:addClass(itemNum > 0 and 'tlmain' or 'tlsub')
		:wikitext(year == 'TBA' and '未定' or year)
	if itemNum > 1 then
		builder = builder:attr('rowspan', itemNum)
	end
end
local function right(builder, args, year, itemNum, itemList)
	if itemNum == 0 then return end
	if itemNum == 1 then
		builder:tag('td')
			:wikitext(itemList1)
		return
	end
	-- if itemNum >= 2
	builder:tag('td')
		:addClass('rt_first')
		:wikitext(itemList1)
	for key = 2, itemNum - 1 do
		builder = builder:tag('tr')
			:tag('td')
			:addClass('rt_next')
			:wikitext(itemListkey)
	end
	builder = builder:tag('tr')
		:tag('td')
		:addClass('rt_last')
		:wikitext(itemListitemNum)
end
local function row(builder, args, year)
	local itemNum, itemList = items(args, year)
	builder = builder:tag('tr')
	left(builder, args, year, itemNum)
	right(builder, args, year, itemNum, itemList)
end
--------------------------------------------------------------------------------
function p.main(frame)
	local args = getArgs(frame)
	return frame:extensionTag{ name = 'templatestyles', args = { src = 'Timeline of release years/styles.css'} } .. tostring(p._main(args))
end
function p._main(args)
	-- Main module code goes here.
	local currentyear = os.date('%Y')
	local ret
	local firstyear, lastyear
	local TBA = items(args, 'TBA') > 0 and true or false
	ret = mw.html.create( 'table' )
		:addClass(args.align == 'left' and 'wikitable release_timeline rt_left' or 'wikitable release_timeline')
	ret:tag('caption')
		:addClass('rt_caption')
		:addClass('nowrap')
		:wikitext((args.title or '発売の年表')..(args.subtitle and (..args.subtitle..) or ))
	if tonumber(args.first) then
		firstyear = tonumber(args.first)
	else
		for i = 1800, currentyear do
			if items(args, i) > 0 then
				firstyear = i
				break
			end
		end
		firstyear = firstyear or (currentyear + 3)
	end
	if tonumber(args.last) then
		lastyear = tonumber(args.last)
	else
		for i = currentyear + 3, TBA and currentyear or firstyear, -1 do
			if items(args, i) > 0 then
				lastyear = i
				break
			end
		end
		lastyear = lastyear or (currentyear - 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