Module:Timeline of release years/sandbox: Difference between revisions

Content deleted Content added
Return silver for compressed ranges by default, allow specification though.
Simplifications
 
(29 intermediate revisions by 5 users not shown)
Line 1:
require('Module:No globalsstrict')
local getArgs = require('Module:Arguments').getArgs
local p = {}
 
local function items(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.
local range = 0;
if args[year .. '_to'] or args[year .. 'a_to'] then
local newrange = tonumber(args[year .. '_to'] or args[year .. 'a_to'])
if newrange and (oldrange == nil or newrange > oldrange) then
range = newrange;
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
 
-- Find items, filtered by range if available.
if args[year] or args[year .. 'a'] then
table.insert(itemList,local thisrange = tonumber(args[year .. '_to'] or args[year .. 'aa_to'])
if (range == 0 and thisrange == nil) or (thisrange and thisrange == range) then
table.insert(itemList, args[year] or args[year .. 'a'])
end
end
for asciiletter = 98, 106 do -- 98 > b, 106 > j
if args[year .. string.char(asciiletter)] then
table.insert(itemList,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 color(args, year, itemNum, to_range)
if string.find(year, "~") then
if args['compress_color'] then
return args['compress_color']
else
return '#C0C0C0'
end
end
 
if args[year .. '_color'] then
return args[year .. '_color']
end
if to_range and args[year .. '_to_' .. to_range .. '_color'] then
return args[year .. '_to_' .. to_range .. '_color']
end
 
Line 50 ⟶ 69:
end
 
local function left(builder, args, year, itemNum, range)
builder = builder:tag('th')
:attr('scope', 'row')
:css('border-right', '1.4em solid ' .. color(args, year, itemNum, range))
:wikitext(year)
if itemNum > 1 then
builder = builder:attr('rowspan', itemNum)
end
if year == 'TBA' then
builder:tag('abbr'):attr('title', 'To be announced'):wikitext('TBA')
else
builder:wikitext(range ~= 0 and year .. '–' .. range or year)
end
end
 
local function right(builder, args, year, itemNum, itemList)
if itemNum == 0 then return end
 
Line 71 ⟶ 94:
-- if itemNum >= 2
builder:tag('td')
:addClass('rt_firstrt-first')
:wikitext(itemList[1])
 
Line 77 ⟶ 100:
builder = builder:tag('tr')
:tag('td')
:addClass('rt_nextrt-next')
:wikitext(itemList[key])
end
Line 83 ⟶ 106:
builder = builder:tag('tr')
:tag('td')
:addClass('rt_lastrt-last')
:wikitext(itemList[itemNum])
 
end
 
local function row(builder, args, year, emptyyear, lastyear, highrange)
local itemNum, itemListoldrange = items(args, year)nil
repeat
-- If excludeempty is set and no items are returned, don't build a row.
local itemNum, itemList, range = items(args, year, oldrange)
if args.excludeempty and #itemList < 1 then
return nil
-- Now check for a new high range and catch it. We need to know what highrange was prior to update though.
end
local oldhighrange = nil
if(range > 0 and (highrange == nil or range > highrange)) then
-- If compressempty is set, check for empty items, track empty years, and put out a compressed range when next year is found.
oldhighrange = (highrange or range)
if args.compressempty then
highrange = range
-- If we're compressing and there's no items, return this year for tracking.
if #itemList < 1 then
return year
end
oldhighrange = (oldhighrange or highrange)
 
-- If we have items but are tracking an empty year, output compressed range row.
-- If compressempty is set, check for empty items, track empty years and high ranges, and
if emptyyear ~= nil then
-- put out a compressed range when next year is found.
builder = builder:tag('tr')
if yearargs.compressempty and oldrange == 'TBA'nil then
-- If we're compressing and there's no items, return this year for tracking.
left(builder, args, emptyyear .. ' ~ ' .. lastyear, 0)
if #itemList < 1 then
right(builder, args, emptyyear .. ' ~ ' .. lastyear, 0, {})
elseif return year, ~= emptyyear thenhighrange
left(builder, args, emptyyear .. ' ~ ' .. (year-1), 0)
right(builder, args, emptyyear .. ' ~ ' .. (year-1), 0, {})
else
left(builder, args, (year-1), 0)
right(builder, args, (year-1), 0, {})
end
end
end
 
-- If emptyyear is below or equal the highrange, we need to make adjustments.
builder = builder:tag('tr')
if(emptyyear and oldhighrange and emptyyear <= oldhighrange) then
left(builder, args, year, itemNum)
-- If the current year is highrange or highrange +1, suppress empty row output entirely.
right(builder, args, year, itemNum, itemList)
-- If the current year is highrange+2 or more, adjust the emptyyear to be above highrange)
if(year <= (oldhighrange+1)) then
return nil
emptyyear = nil
end
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)
elseif year-1 == emptyyear then
left(builder, args, emptyyear, 0, 0)
else
left(builder, args, emptyyear, 0, year-1)
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
function p.main(frame)
oldrange = range
local args = getArgs(frame)
end
return frame:extensionTag{ name = 'templatestyles', args = { src = 'Timeline of release years/styles.css'} } .. tostring(p._main(args))
until range == 0
return nil, highrange
end
 
Line 140 ⟶ 180:
local ret
local firstyear, lastyear
local TBA = items(args, 'TBA') > 0 and true or false
 
ret = mw.html.create( 'table' )
:addClass(args.align == 'left'release-timeline and 'wikitable release_timeline rt_left' or 'wikitable release_timeline')
:addClass(args.align == 'left' and 'rt-left' or nil)
 
ret:tag('caption')
:wikitext((args.title or 'Release timeline') ..
:addClass('rt_caption')
(args.subtitle and ('<div class="rt-subtitle">'..args.subtitle..'</div>') or ''))
:addClass('nowrap')
:wikitext((args.title or 'Release timeline')..(args.subtitle and ('<div class="rt_subtitle">'..args.subtitle..'</div>') or ''))
 
if tonumber(args.first) then
Line 175 ⟶ 215:
 
local emptyyear = nil
local highrange = nil
for year = firstyear, lastyear do
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
 
Line 186 ⟶ 228:
end
 
return mw.getCurrentFrame():extensionTag{
return ret
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