跳转到内容

模組:Release timeline

维基百科,自由的百科全书

这是本页的一个历史版本,由風中的刀劍留言 | 贡献2015年12月10日 (四) 12:23 ((Wikiplus))编辑。这可能和当前版本存在着巨大的差异。

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)$' )
			
			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 = (color2 == '') and color1 or color2
				return isYearReleased(args, year) and color1 or color2
			end
		end
	end
	
	return isYearReleased(args, year) and '#0BDA51' or '#228B22'
end

local function titleItem(builder, content)
	if content ~= nil then
		builder:tag('span'):wikitext(content .. '<br />')
	end
end

local function left(builder, year)
	year = (year == 'TBA') and '待定' or year
	
	builder:tag('td')
		:css('padding', '5px')
		:wikitext( year )
end

local function center(builder, args, year)
	builder:tag('td')
		:css('width', '10px')
		:css('border', '1px solid black')
		:css('background-color', color(args, year))
		:css('padding', '5px')
end

local function right(builder, args, year)
	if isYearReleased(args, year) == nil then return end
	
	builder = builder:tag('td')
		:css('padding','5px')
	titleItem(builder, args[year] or args[year .. 'a' ])

	for asciiletter = 98, 106 do -- 98 == b, 106 == j
		titleItem(builder, args[year .. string.char(asciiletter)])
	end
end

local function row(builder, args, year)
	builder = builder:tag('tr')
	left(builder, year)
	center(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', '86%')
		:css('line-height', '96%')
		:css('border-collapse', 'collapse')
		:css('border-spacing', '0')
	
	ret:tag('caption')
		:css('font-size', '110%')
		:css('padding', '5px')
		:wikitext(args.title and ('<b>' .. args.title .. '</b>') or '<b>发行年份时间轴</b>')
		:wikitext(args.subtitle and ('<br />' .. args.subtitle))

	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, 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