Module:Navbox timeline: Difference between revisions

No edit summary
Tag: Reverted
No edit summary
Tag: Manual revert
Line 5: Line 5:
local getArgs = require('Module:Arguments').getArgs
local getArgs = require('Module:Arguments').getArgs
local p = {}
local p = {}
-- Round a number to the nearest integer
local function round(num)
return math.floor(num + 0.5)
end
-- Convert date string to fractional year for timeline positioning
local function parseDate(dateStr)
if not dateStr then return nil end
-- Handle year-month format (YYYY-MM or YYYY/MM)
local year, month = dateStr:match('^(%d%d%d%d)[%-/](%d%d?)$')
if year and month then
year = tonumber(year)
month = tonumber(month)
if month < 1 or month > 12 then
error('Invalid month: ' .. month .. ' (must be 1-12)', 0)
end
-- Convert to fractional year (e.g., 2020.5 for June 2020)
return year + (month - 1) / 12
end
-- Handle year only
local yearOnly = dateStr:match('^(%d%d%d%d)$')
if yearOnly then
return tonumber(yearOnly)
end
return nil
end


-- Add blank table cells
-- Add blank table cells
Line 40: Line 10:
if row and prev < current then
if row and prev < current then
if yesno(args.decades) == false then
if yesno(args.decades) == false then
if yesno(args.months) ~= false then
row:tag('td')
-- Divide the cell up by months when showing months
:addClass('timeline-blank')
local date = prev
:cssText(args.blankstyle)
:attr('colspan', current - prev)
while date < current do
local dur = math.min(1/12, current - date) -- 1 month duration in fractional years
local colspanMonths = math.max(1, round(dur * 12))
row:tag('td')
:addClass('timeline-blank')
:cssText(args.blankstyle)
:attr('colspan', colspanMonths)
date = date + dur
end
else
local yearDiff = current - prev
row:tag('td')
:addClass('timeline-blank')
:cssText(args.blankstyle)
:attr('colspan', math.max(1, round(yearDiff)))
end
else
else
-- Divide the cell up every decade if showing decades at the top
-- Divide the cell up every decade if showing decades at the top
local year = math.floor(prev)
local year = prev
local endYear = math.ceil(current)
while year < endYear do
while year < current do
local dur = math.min(10 - year % 10, endYear - year)
local dur = math.min(10 - year % 10, current - year)
row:tag('td')
row:tag('td')
Line 117: Line 68:
then
then
local dates = mw.text.split(fullVal, '-', true)
local dates = mw.text.split(fullVal, '-', true)
local startYear = parseDate(dates[1])
local startYear = tonumber(dates[1])
local endYear = parseDate(dates[2]) or (tonumber(os.date('%Y')) + 1)
local endYear = tonumber(dates[2]) or tonumber(os.date('%Y')) + 1
if startYear == nil then
if startYear == nil then
Line 144: Line 95:
end
end
-- Add overrides from arguments (handle fractional years)
-- Add overrides from arguments
if args.startoffset then
if args.startoffset then
rows.minYear = rows.minYear - tonumber(args.startoffset)
rows.minYear = rows.minYear - tonumber(args.startoffset)
end
end
if args.startyear then
if args.startyear and tonumber(args.startyear) < rows.minYear then
local startOverride = parseDate(args.startyear) or tonumber(args.startyear)
rows.minYear = tonumber(args.startyear)
if startOverride and startOverride < rows.minYear then
rows.minYear = startOverride
end
end
end
Line 160: Line 108:
end
end
if args.endyear then
if args.endyear and tonumber(args.endyear) > rows.maxYear then
local endOverride = parseDate(args.endyear) or tonumber(args.endyear)
rows.maxYear = tonumber(args.endyear)
if endOverride and endOverride > rows.maxYear then
rows.maxYear = endOverride
end
end
end


return rows
return rows
end
-- Get month abbreviation
local function getMonthAbbr(month)
local months = {'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'}
return months[month] or ''
end
end


Line 180: Line 118:
local function renderDates(args, tbl, rows, invert)
local function renderDates(args, tbl, rows, invert)
local showDecades = yesno(args.decades)
local showDecades = yesno(args.decades)
local showMonths = yesno(args.months)
local yearRow = tbl:tag('tr')
local yearRow = tbl:tag('tr')
:addClass('timeline-row')
:addClass('timeline-row')
Line 186: Line 123:
-- Create label
-- Create label
if args.label or rows.hasLabels then
if args.label or rows.hasLabels then
local rowspan = 1
if showDecades ~= false then
rowspan = 2
elseif showDecades == false and showMonths ~= false then
rowspan = 2
end
local labelCell = mw.html.create('th')
local labelCell = mw.html.create('th')
:attr('scope', 'col')
:attr('scope', 'col')
:addClass('navbox-group timeline-label')
:addClass('navbox-group timeline-label')
:cssText(args.labelstyle)
:cssText(args.labelstyle)
:attr('rowspan', tostring(rowspan))
:attr('rowspan', showDecades ~= false and '2' or '1')
:wikitext(args.label or '')
:wikitext(args.label or '')
Line 207: Line 137:
local decadeRow = tbl:tag('tr')
local decadeRow = tbl:tag('tr')
:addClass('timeline-row')
:addClass('timeline-row')
local year = math.floor(rows.minYear)
local year = rows.minYear
local maxYear = math.ceil(rows.maxYear)
-- Move decade row  
-- Move decade row  
Line 215: Line 144:
end
end
while year < maxYear do
while year < rows.maxYear do
local dur = math.min(10 - year % 10, maxYear - year)
local dur = math.min(10 - year % 10, rows.maxYear - year)
decadeRow:tag('th')
decadeRow:tag('th')
Line 227: Line 156:
year = year + dur
year = year + dur
end
-- Create month row when decades are disabled but months are enabled
elseif showDecades == false and showMonths ~= false then
local monthRow = tbl:tag('tr')
:addClass('timeline-row')
-- Move month row
if not invert then
monthRow, yearRow = yearRow, monthRow
end
-- Calculate total months in range
local totalMonths = math.ceil((rows.maxYear - rows.minYear) * 12)
local monthWidth = 100 / totalMonths
local currentDate = rows.minYear
while currentDate < rows.maxYear do
local year = math.floor(currentDate)
local month = math.floor((currentDate - year) * 12) + 1
monthRow:tag('th')
:attr('scope', 'col')
:addClass('timeline-month')
:cssText(args.datestyle)
:cssText(args.monthstyle)
:cssText('width:' .. monthWidth .. '%')
:wikitext(getMonthAbbr(month))
currentDate = currentDate + 1/12
end
end
end
end
-- Populate year row element
-- Populate year row element
if showDecades == false and showMonths ~= false then
local width = 100 / (rows.maxYear - rows.minYear)
-- When showing months, each year spans 12 columns
local year = math.floor(rows.minYear)
for i = rows.minYear, rows.maxYear - 1 do
local maxYear = math.ceil(rows.maxYear)
yearRow:tag('th')
:attr('scope', 'col')
for currentYear = year, maxYear - 1 do
:addClass('timeline-year')
yearRow:tag('th')
:cssText(args.datestyle)
:attr('scope', 'col')
:cssText(args.yearstyle)
:addClass('timeline-year')
:cssText('width:' .. width .. '%')
:cssText(args.datestyle)
:wikitext(showDecades == false and i or i % 10)
:cssText(args.yearstyle)
:attr('colspan', '12')
:wikitext(tostring(currentYear))
end
else
-- Normal year display
local yearSpan = rows.maxYear - rows.minYear
local width = 100 / yearSpan
local year = math.floor(rows.minYear)
local maxYear = math.ceil(rows.maxYear)
for i = year, maxYear - 1 do
yearRow:tag('th')
:attr('scope', 'col')
:addClass('timeline-year')
:cssText(args.datestyle)
:cssText(args.yearstyle)
:cssText('width:' .. width .. '%')
:wikitext(showDecades == false and i or i % 10)
end
end
end
end
end
Line 327: Line 206:
-- Shrink previous item so new item can start at the start year
-- Shrink previous item so new item can start at the start year
if prevItem and prev > prevEndYear then
if prevItem and prev > prevEndYear then
local colspan = tonumber(prevItem:getAttr('colspan')) or 1
prevItem:attr('colspan', prevItem:getAttr('colspan') - prev + prevEndYear);
local adjustment = prev - prevEndYear
if yesno(args.decades) == false and yesno(args.months) ~= false then
-- Adjust for months
adjustment = round(adjustment * 12)
else
adjustment = round(adjustment)
end
prevItem:attr('colspan', math.max(1, colspan - adjustment))
end
end


Line 346: Line 215:
-- Add blanks before the cell
-- Add blanks before the cell
addBlank(args, rowElement, prevEndYear, cell.startYear)
addBlank(args, rowElement, prevEndYear, cell.startYear)
local colspan = cell.endYear - cell.startYear
if yesno(args.decades) == false and yesno(args.months) ~= false then
-- Each month is one column
colspan = math.max(1, round(colspan * 12))
else
colspan = math.max(1, round(colspan))
end
prevItem = rowElement:tag('td')
prevItem = rowElement:tag('td')
Line 359: Line 220:
:cssText(args.itemstyle)
:cssText(args.itemstyle)
:cssText(cell.style or '')
:cssText(cell.style or '')
:attr('colspan', colspan)
:attr('colspan', cell.endYear - cell.startYear)
:wikitext(cell.item)
:wikitext(cell.item)


Line 371: Line 232:
-- Remove any extra rowspan from the label
-- Remove any extra rowspan from the label
if prevLabel and labelSpan > 0 then
if prevLabel and labelSpan > 0 then
prevLabel:attr('rowspan', (tonumber(prevLabel:getAttr('rowspan')) or 1) - labelSpan);
prevLabel:attr('rowspan', prevLabel:getAttr('rowspan') - labelSpan);
end
end
end
end