Module:Navbox timeline: Difference between revisions
Appearance
AZMindroma (talk | contribs) Trying out months change (AI) Tag: Reverted |
AZMindroma (talk | contribs) No edit summary Tag: Reverted |
||
| Line 5: | Line 5: | ||
local getArgs = require('Module:Arguments').getArgs | local getArgs = require('Module:Arguments').getArgs | ||
local p = {} | local p = {} | ||
-- 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 15: | Line 40: | ||
while date < current do | while date < current do | ||
local dur = math.min(1, current - date) -- 1 month duration | local dur = math.min(1/12, current - date) -- 1 month duration in fractional years | ||
local colspanMonths = math.max(1, math.round(dur * 12)) | |||
row:tag('td') | row:tag('td') | ||
:addClass('timeline-blank') | :addClass('timeline-blank') | ||
:cssText(args.blankstyle) | :cssText(args.blankstyle) | ||
:attr('colspan', | :attr('colspan', colspanMonths) | ||
date = date + dur | date = date + dur | ||
end | end | ||
else | else | ||
local yearDiff = current - prev | |||
row:tag('td') | row:tag('td') | ||
:addClass('timeline-blank') | :addClass('timeline-blank') | ||
:cssText(args.blankstyle) | :cssText(args.blankstyle) | ||
:attr('colspan', | :attr('colspan', math.max(1, math.round(yearDiff))) | ||
end | 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 = prev | local year = math.floor(prev) | ||
local endYear = math.ceil(current) | |||
while year < | while year < endYear do | ||
local dur = math.min(10 - year % 10, | local dur = math.min(10 - year % 10, endYear - year) | ||
row:tag('td') | row:tag('td') | ||
| Line 84: | Line 112: | ||
then | then | ||
local dates = mw.text.split(fullVal, '-', true) | local dates = mw.text.split(fullVal, '-', true) | ||
local startYear = | local startYear = parseDate(dates[1]) | ||
local endYear = | local endYear = parseDate(dates[2]) or (tonumber(os.date('%Y')) + 1) | ||
if startYear == nil then | if startYear == nil then | ||
| Line 111: | Line 139: | ||
end | end | ||
-- Add overrides from arguments | -- Add overrides from arguments (handle fractional years) | ||
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 | if args.startyear then | ||
local startOverride = parseDate(args.startyear) or tonumber(args.startyear) | |||
if startOverride and startOverride < rows.minYear then | |||
rows.minYear = startOverride | |||
end | |||
end | end | ||
| Line 124: | Line 155: | ||
end | end | ||
if args.endyear | if args.endyear then | ||
local endOverride = parseDate(args.endyear) or tonumber(args.endyear) | |||
if endOverride and endOverride > rows.maxYear then | |||
rows.maxYear = endOverride | |||
end | |||
end | end | ||
| Line 168: | Line 202: | ||
local decadeRow = tbl:tag('tr') | local decadeRow = tbl:tag('tr') | ||
:addClass('timeline-row') | :addClass('timeline-row') | ||
local year = rows.minYear | local year = math.floor(rows.minYear) | ||
local maxYear = math.ceil(rows.maxYear) | |||
-- Move decade row | -- Move decade row | ||
| Line 175: | Line 210: | ||
end | end | ||
while year < | while year < maxYear do | ||
local dur = math.min(10 - year % 10, | local dur = math.min(10 - year % 10, maxYear - year) | ||
decadeRow:tag('th') | decadeRow:tag('th') | ||
| Line 199: | Line 234: | ||
-- Calculate total months in range | -- Calculate total months in range | ||
local totalMonths = (rows.maxYear - rows.minYear) * 12 | local totalMonths = math.ceil((rows.maxYear - rows.minYear) * 12) | ||
local monthWidth = 100 / totalMonths | 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 | ||
| Line 218: | Line 257: | ||
if showDecades == false and showMonths ~= false then | if showDecades == false and showMonths ~= false then | ||
-- When showing months, each year spans 12 columns | -- When showing months, each year spans 12 columns | ||
local year = math.floor(rows.minYear) | |||
local maxYear = math.ceil(rows.maxYear) | |||
for currentYear = year, maxYear - 1 do | |||
yearRow:tag('th') | yearRow:tag('th') | ||
:attr('scope', 'col') | :attr('scope', 'col') | ||
| Line 225: | Line 267: | ||
:cssText(args.yearstyle) | :cssText(args.yearstyle) | ||
:attr('colspan', '12') | :attr('colspan', '12') | ||
:wikitext(tostring( | :wikitext(tostring(currentYear)) | ||
end | end | ||
else | else | ||
-- Normal year display | -- Normal year display | ||
local | 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') | yearRow:tag('th') | ||
:attr('scope', 'col') | :attr('scope', 'col') | ||
| Line 276: | Line 322: | ||
-- 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 = prevItem:getAttr('colspan') | local colspan = tonumber(prevItem:getAttr('colspan')) or 1 | ||
local adjustment = prev - prevEndYear | |||
if yesno(args.decades) == false and yesno(args.months) ~= false then | if yesno(args.decades) == false and yesno(args.months) ~= false then | ||
-- Adjust for months | -- Adjust for months | ||
adjustment = math.round(adjustment * 12) | |||
else | else | ||
adjustment = math.round(adjustment) | |||
end | end | ||
prevItem:attr('colspan', math.max(1, colspan - adjustment)) | |||
end | end | ||
| Line 294: | Line 344: | ||
local colspan = cell.endYear - cell.startYear | local colspan = cell.endYear - cell.startYear | ||
if yesno(args.decades) == false and yesno(args.months) ~= false then | if yesno(args.decades) == false and yesno(args.months) ~= false then | ||
-- Each | -- Each month is one column | ||
colspan = colspan * 12 | colspan = math.max(1, math.round(colspan * 12)) | ||
else | |||
colspan = math.max(1, math.round(colspan)) | |||
end | end | ||
| Line 314: | Line 366: | ||
-- 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', prevLabel:getAttr('rowspan') - labelSpan); | prevLabel:attr('rowspan', (tonumber(prevLabel:getAttr('rowspan')) or 1) - labelSpan); | ||
end | end | ||
end | end | ||
Revision as of 09:25, 5 June 2025
Documentation for this module may be created at Module:Navbox timeline/doc
require('strict')
local yesno = require('Module:Yesno')
local navbox = require('Module:Navbox')._navbox
local getArgs = require('Module:Arguments').getArgs
local p = {}
-- 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
local function addBlank(args, row, prev, current)
if row and prev < current then
if yesno(args.decades) == false then
if yesno(args.months) ~= false then
-- Divide the cell up by months when showing months
local date = prev
while date < current do
local dur = math.min(1/12, current - date) -- 1 month duration in fractional years
local colspanMonths = math.max(1, math.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, math.round(yearDiff)))
end
else
-- Divide the cell up every decade if showing decades at the top
local year = math.floor(prev)
local endYear = math.ceil(current)
while year < endYear do
local dur = math.min(10 - year % 10, endYear - year)
row:tag('td')
:addClass('timeline-blank')
:cssText(args.blankstyle)
:attr('colspan', dur)
year = year + dur
end
end
end
end
-- Get timeline data
local function timelineInfo(args)
local rows = {
[1] = {},
minYear = math.huge,
maxYear = -math.huge,
hasLabels = false
}
for num, fullVal in ipairs(args) do
local key, val = fullVal:match('^([a-z]+): *(.*)$')
local cellIndex = #rows[#rows]
-- Row data key value pairs
if cellIndex == 0 and key then
rows[#rows][key] = val
-- Record that there are labels
if key == 'label' then
rows.hasLabels = true
end
-- Data cell key value pairs
elseif key then
-- Data cell key value pairs
rows[#rows][cellIndex][key] = val
-- New row
elseif fullVal == '' then
if next(rows[#rows]) then
table.insert(rows, {})
end
-- Add date to cell with item already and no date
elseif cellIndex > 0
and rows[#rows][cellIndex].item
and not rows[#rows][cellIndex].startYear
then
local dates = mw.text.split(fullVal, '-', true)
local startYear = parseDate(dates[1])
local endYear = parseDate(dates[2]) or (tonumber(os.date('%Y')) + 1)
if startYear == nil then
error('Argument ' .. num .. ' is an invalid time range', 0)
end
if endYear < startYear then
error('Argument ' .. num .. '\'s end year is less than the start year', 0)
end
rows[#rows][cellIndex].startYear = startYear
rows[#rows][cellIndex].endYear = endYear
if startYear < rows.minYear then
rows.minYear = startYear
end
if endYear > rows.maxYear then
rows.maxYear = endYear
end
-- New item
else
table.insert(rows[#rows], { item = fullVal })
end
end
-- Add overrides from arguments (handle fractional years)
if args.startoffset then
rows.minYear = rows.minYear - tonumber(args.startoffset)
end
if args.startyear then
local startOverride = parseDate(args.startyear) or tonumber(args.startyear)
if startOverride and startOverride < rows.minYear then
rows.minYear = startOverride
end
end
if args.endoffset then
rows.maxYear = rows.maxYear + tonumber(args.endoffset)
end
if args.endyear then
local endOverride = parseDate(args.endyear) or tonumber(args.endyear)
if endOverride and endOverride > rows.maxYear then
rows.maxYear = endOverride
end
end
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
-- Render the date rows
local function renderDates(args, tbl, rows, invert)
local showDecades = yesno(args.decades)
local showMonths = yesno(args.months)
local yearRow = tbl:tag('tr')
:addClass('timeline-row')
-- Create label
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')
:attr('scope', 'col')
:addClass('navbox-group timeline-label')
:cssText(args.labelstyle)
:attr('rowspan', tostring(rowspan))
:wikitext(args.label or '')
yearRow:node(labelCell)
end
-- Create decade row
if showDecades ~= false then
local decadeRow = tbl:tag('tr')
:addClass('timeline-row')
local year = math.floor(rows.minYear)
local maxYear = math.ceil(rows.maxYear)
-- Move decade row
if not invert then
decadeRow, yearRow = yearRow, decadeRow
end
while year < maxYear do
local dur = math.min(10 - year % 10, maxYear - year)
decadeRow:tag('th')
:attr('scope', 'col')
:addClass('timeline-decade')
:cssText(args.datestyle)
:cssText(args.decadestyle)
:attr('colspan', dur)
:wikitext(math.floor(year / 10) .. '0s')
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
-- Populate year row element
if showDecades == false and showMonths ~= false then
-- When showing months, each year spans 12 columns
local year = math.floor(rows.minYear)
local maxYear = math.ceil(rows.maxYear)
for currentYear = year, maxYear - 1 do
yearRow:tag('th')
:attr('scope', 'col')
:addClass('timeline-year')
:cssText(args.datestyle)
: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
-- Render the timeline itself
local function renderTimeline(args, tbl, rows)
local rowElement = nil
local rowSuffix = nil
local prev = rows.minYear
local prevItem = nil
local prevLabel = nil
local labelSpan = 0
for rowNum, row in ipairs(rows) do
local rowElement = tbl:tag('tr')
:addClass('timeline-row')
if labelSpan <= 0 and (rows.hasLabels or args.label) then
labelSpan = tonumber(row.span) or 1
prevLabel = rowElement:tag('th')
:attr('scope', 'row')
:attr('rowspan', labelSpan)
:addClass('navbox-group timeline-label')
:cssText(args.labelstyle)
:cssText(row.labelstyle)
:wikitext(row.label)
end
labelSpan = labelSpan - 1
local prevEndYear = rows.minYear
local prevItem = nil
for cellNum, cell in ipairs(row) do
-- Shrink previous item so new item can start at the start year
if prevItem and prev > prevEndYear then
local colspan = tonumber(prevItem:getAttr('colspan')) or 1
local adjustment = prev - prevEndYear
if yesno(args.decades) == false and yesno(args.months) ~= false then
-- Adjust for months
adjustment = math.round(adjustment * 12)
else
adjustment = math.round(adjustment)
end
prevItem:attr('colspan', math.max(1, colspan - adjustment))
end
if cell.startYear == nil then
error('Missing timerange for row ' .. rowNum .. ' cell ' .. cellNum, 0)
end
-- Add blanks before the cell
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, math.round(colspan * 12))
else
colspan = math.max(1, math.round(colspan))
end
prevItem = rowElement:tag('td')
:addClass('timeline-item')
:cssText(args.itemstyle)
:cssText(cell.style or '')
:attr('colspan', colspan)
:wikitext(cell.item)
prevEndYear = cell.endYear
end
-- Add blanks to the end of the row
addBlank(args, rowElement, prevEndYear, rows.maxYear)
end
-- Remove any extra rowspan from the label
if prevLabel and labelSpan > 0 then
prevLabel:attr('rowspan', (tonumber(prevLabel:getAttr('rowspan')) or 1) - labelSpan);
end
end
function p.main(frame)
local args = getArgs(frame, {
removeBlanks = false,
wrappers = 'Template:Navbox timeline'
})
local targs = {
listpadding = '0'
}
-- Arguments to passthrough to navbox
local passthrough = {
'name', 'title', 'above', 'below', 'state', 'navbar', 'border', 'image',
'imageleft', 'style', 'bodystyle', 'style', 'bodystyle', 'basestyle',
'titlestyle', 'abovestyle', 'belowstyle', 'imagestyle',
'imageleftstyle', 'titleclass', 'aboveclass', 'bodyclass', 'belowclass',
'imageclass'
}
local rows = timelineInfo(args)
local wrapper = mw.html.create('table')
:addClass('timeline-wrapper notheme')
local tbl = wrapper:tag('tr')
:tag('td')
:addClass('timeline-wrapper-cell')
:tag('table')
:addClass('timeline-table')
renderDates(args, tbl, rows)
renderTimeline(args, tbl, rows)
if yesno(args.footer) then
renderDates(args, tbl, rows, true)
end
for _, name in ipairs(passthrough) do
targs[name] = args[name]
end
targs.templatestyles = 'Module:Navbox timeline/styles.css'
targs.list1 = tostring(wrapper)
return navbox(targs)
end
return p