Module:Timeline of release years: Difference between revisions

Content deleted Content added
Jackmcbarn (talk | contribs)
no globals
Ncfavier (talk | contribs)
Add abbr tag on 'TBA' rows
 
(71 intermediate revisions by 15 users not shown)
Line 1:
require('Module:No globalsstrict')
local getArgs = require('Module:Arguments').getArgs
local p = {}
 
local function isYearReleaseditems(args, year, oldrange)
local itemList = {}
-- First loop through is to find the lowest year range, if any. If oldrange is supplied, the year range must also be greater than it.
if args[year] then
local range = 0;
return true
if args[year .. '_to'] or args[year .. 'a_to'] then
else
local newrange = tonumber(args[year .. '_to'] or args[year .. 'a_to'])
for i = 96, 105 do
if newrange and (oldrange == nil or newrange > oldrange) then
if args[year .. string.char(i)] then
range = newrange;
return true
end
end
for asciiletter = 98, 106 do -- 98 > b, 106 > j
if args[year .. string.char(asciiletter) .. '_to'] then
local newrange = tonumber(args[year .. string.char(asciiletter) .. '_to'])
if newrange and (oldrange == nil or newrange > oldrange) and (range == 0 or newrange < range) then
range = newrange;
end
end
end
end
 
-- Find items, filtered by range if available.
local function color(args, year)
if isYearReleased(args,[year] or args[year) .. 'a'] then
local thisrange = tonumber(args[year .. '_to'] or args[year .. 'a_to'])
return '#0BDA51'
if (range == 0 and thisrange == nil) or (thisrange and thisrange == range) then
else
table.insert(itemList, args[year] or args[year .. 'a'])
return '#228B22'
end
end
for asciiletter = 98, 106 do -- 98 > b, 106 > j
if args[year .. string.char(asciiletter)] then
local thisrange = tonumber(args[year .. string.char(asciiletter) .. '_to'])
if (range == 0 and thisrange == nil) or (thisrange and thisrange == range) then
table.insert(itemList, args[year .. string.char(asciiletter)])
end
end
end
return table.maxn(itemList), itemList, range
end
 
local function titleItemcolor(contentargs, year, itemNum, to_range)
 
local ret
if args[year .. '_color'] then
return args[year .. '_color']
end
if to_range and args[year .. '_to_' .. to_range .. '_color'] then
if content ~= nil then
return args[year .. '_to_' .. to_range .. '_color']
ret = mw.html.create( 'span' )
ret
:wikitext( '– ' .. content .. '<br />' )
end
return ret
end
 
for yearrange = 1, 10 do
local function left(args, year, displeft)
if args['range' .. yearrange] and args['range' .. yearrange .. '_color'] then
local ret
local _, _, beginyear, endyear = string.find( args['range' .. yearrange], '^(%d%d%d%d)%D+(%d%d%d%d)$' )
ret = mw.html.create( 'td' )
 
local year = tonumber(year) or 9999 -- For year == 'TBA'
ret
beginyear = tonumber(beginyear) or 0
:wikitext( (displeft or year) .. ' –' )
endyear = tonumber(endyear) or 9999
 
return ret
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 itemNum > 0 and color1 or color2
end
end
end
 
return itemNum > 0 and '#0BDA51' or '#228B22'
end
 
local function centerleft(builder, args, year, itemNum, range)
builder = builder:tag('th')
local ret
:attr('scope', 'row')
ret = mw.html.create( 'td' )
:css('border-right', '1.4em solid ' .. color(args, year, itemNum, range))
if itemNum > 1 then
ret
builder = builder:attr('rowspan', itemNum)
:css( 'width', '10px' )
end
:css( 'border', '1px solid black' )
if year == 'TBA' then
:css( 'background-color', color(args, year) )
builder:tag('abbr'):attr('title', 'To be announced'):wikitext('TBA')
else
return ret
builder:wikitext(range ~= 0 and year .. '–' .. range or year)
end
end
 
local function right(argsbuilder, yearitemNum, itemList)
if isYearReleased(args, year)itemNum == nil0 then return nil end
local ret
ret = mw.html.create( 'td' )
 
if itemNum == 1 then
ret
builder:tag('td')
:node( titleItem(args[year]) or titleItem(args[year .. 'a' ]) )
:wikitext(itemList[1])
return
end
 
-- if itemNum >= 2
for i = 98, 106 do
builder:tag('td')
ret:node( titleItem(args[year .. string.char(i)]) )
:addClass('rt-first')
:wikitext(itemList[1])
 
for key = 2, itemNum - 1 do
builder = builder:tag('tr')
:tag('td')
:addClass('rt-next')
:wikitext(itemList[key])
end
 
builder = builder:tag('tr')
return ret
:tag('td')
:addClass('rt-last')
:wikitext(itemList[itemNum])
 
end
 
local function row(builder, args, year, displeftemptyyear, lastyear, highrange)
local retoldrange = nil
ret = mw.html.create('tr')
repeat
ret
:node(local leftitemNum, itemList, range = items(args, year), oldrange)
:node( center(args, year) )
:node( right(args, year) )
-- Now check for a new high range and catch it. We need to know what highrange was prior to update though.
return ret
local oldhighrange = nil
end
if(range > 0 and (highrange == nil or range > highrange)) then
oldhighrange = (highrange or range)
highrange = range
end
oldhighrange = (oldhighrange or highrange)
 
-- If compressempty is set, check for empty items, track empty years and high ranges, and
--------------------------------------------------------------------------------
-- put out a compressed range when next year is found.
if args.compressempty and oldrange == nil then
-- If we're compressing and there's no items, return this year for tracking.
if #itemList < 1 then
return year, highrange
end
 
-- If emptyyear is below or equal the highrange, we need to make adjustments.
function p.main(frame)
if(emptyyear and oldhighrange and emptyyear <= oldhighrange) then
local args = getArgs(frame)
-- If the current year is highrange or highrange +1, suppress empty row output entirely.
return p._main(args)
-- If the current year is highrange+2 or more, adjust the emptyyear to be above highrange)
if(year <= (oldhighrange+1)) then
emptyyear = nil
elseif(year > (oldhighrange+1)) then
emptyyear = oldhighrange+1
end
end
 
-- If we have items but are tracking an empty year, output compressed range row.
if emptyyear ~= nil then
builder = builder:tag('tr')
if year == 'TBA' then
left(builder, args, emptyyear, 0, lastyear)
else
if year-1 == emptyyear then
left(builder, args, emptyyear, 0, 0)
else
left(builder, args, emptyyear, 0, year-1)
end
end
end
end
-- We can break out if this is the case. This means we have looped through more than once, but there were no more items remaining.
if range == 0 and oldrange and #itemList < 1 then
break
end
 
builder = builder:tag('tr')
left(builder, args, year, itemNum, range)
right(builder, itemNum, itemList)
 
if range ~= 0 then
oldrange = range
end
until range == 0
return nil, highrange
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('release-timeline wikitable')
:addClass(args.align == 'left' and 'rt-left' or nil)
ret
 
:css('float', args.align or 'right')
ret:tag('caption')
:css('clear', args.align or 'right')
:wikitext((args.title or 'Release timeline') ..
:css('margin', '0 0 0.5ex 1em')
(args.subtitle and ('<div class="rt-subtitle">'..args.subtitle..'</div>') or ''))
: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 = 19801, os.date('%Y')currentyear do
if isYearReleaseditems(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 = os.date('%Y')currentyear + 3, TBA and currentyear or firstyear, -1 do
if isYearReleaseditems(args, i) > 0 then
lastyear = i
break
end
end
lastyear = lastyear or (currentyear - 1)
end
 
local emptyyear = nil
local highrange = nil
for year = firstyear, lastyear do
ret:node(local yearcheck, newhighrange = row(ret, args, year), emptyyear, lastyear, highrange)
if (emptyyear == nil and yearcheck ~= nil) or (emptyyear ~= nil and yearcheck == nil) then
emptyyear = yearcheck
end
highrange = newhighrange
end
 
if TBA then
return ret
row(ret, args, 'TBA')
end
 
return mw.getCurrentFrame():extensionTag{
name = 'templatestyles', args = { src = 'Module:Timeline of release years/styles.css'}
} .. tostring(ret)
end
 
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame)
return p._main(args)
end