Module:Navbox timeline: Difference between revisions
AZMindroma (talk | contribs) No edit summary Tag: Reverted |
AZMindroma (talk | contribs) Replaced month text with numbers |
||
| (4 intermediate revisions by the same user not shown) | |||
| Line 6: | Line 6: | ||
local p = {} | local p = {} | ||
-- Convert date string | -- Convert year-month to numeric value for comparison | ||
local function dateToNumber(year, month) | |||
return year * 12 + (month or 1) - 1 | |||
end | |||
-- Convert numeric value back to year and month | |||
local function numberToDate(num) | |||
local year = math.floor(num / 12) | |||
local month = (num % 12) + 1 | |||
return year, month | |||
end | |||
-- Get month abbreviation | |||
local function getMonthNum(month) | |||
return tostring(month) | |||
end | |||
-- Parse date string (supports YYYY, YYYY-MM, or YYYY-YYYY formats) | |||
local function parseDate(dateStr) | local function parseDate(dateStr) | ||
if not dateStr then return nil end | if not dateStr then return nil, nil end | ||
-- | -- Match YYYY-MM format | ||
local year, month = dateStr:match('^(%d%d%d%d) | local year, month = dateStr:match('^(%d%d%d%d)%-(%d%d?)$') | ||
if year and month then | if year and month then | ||
return tonumber(year), tonumber(month) | |||
end | end | ||
-- | -- Match YYYY format | ||
year = dateStr:match('^(%d%d%d%d)$') | |||
if | if year then | ||
return tonumber( | return tonumber(year), 1 | ||
end | end | ||
return nil | return nil, nil | ||
end | end | ||
| Line 34: | Line 46: | ||
local function addBlank(args, row, prev, current) | local function addBlank(args, row, prev, current) | ||
if row and prev < current then | if row and prev < current then | ||
local duration = current - prev | |||
if duration > 0 then | |||
row:tag('td') | |||
:addClass('timeline-blank') | |||
:cssText(args.blankstyle) | |||
:attr('colspan', duration) | |||
end | end | ||
end | end | ||
| Line 80: | Line 60: | ||
local rows = { | local rows = { | ||
[1] = {}, | [1] = {}, | ||
minDate = math.huge, | |||
maxDate = -math.huge, | |||
hasLabels = false | hasLabels = false, | ||
useMonths = yesno(args.months) ~= false | |||
} | } | ||
| Line 99: | Line 80: | ||
-- Data cell key value pairs | -- Data cell key value pairs | ||
elseif key then | elseif key then | ||
rows[#rows][cellIndex][key] = val | rows[#rows][cellIndex][key] = val | ||
-- New row | -- New row | ||
| Line 109: | Line 89: | ||
elseif cellIndex > 0 | elseif cellIndex > 0 | ||
and rows[#rows][cellIndex].item | and rows[#rows][cellIndex].item | ||
and not rows[#rows][cellIndex]. | and not rows[#rows][cellIndex].startDate | ||
then | then | ||
local dates = mw.text.split(fullVal, '-', true) | local dates = mw.text.split(fullVal, '-', true) | ||
local startYear = parseDate(dates[1]) | local startYear, startMonth, endYear, endMonth | ||
if #dates == 2 then | |||
-- Handle YYYY-YYYY format | |||
startYear, startMonth = parseDate(dates[1]) | |||
endYear, endMonth = parseDate(dates[2]) | |||
elseif #dates == 3 then | |||
-- Handle YYYY-MM-YYYY format | |||
startYear, startMonth = tonumber(dates[1]), tonumber(dates[2]) | |||
endYear, endMonth = parseDate(dates[3]) | |||
elseif #dates == 4 then | |||
-- Handle YYYY-MM-YYYY-MM format | |||
startYear, startMonth = tonumber(dates[1]), tonumber(dates[2]) | |||
endYear, endMonth = tonumber(dates[3]), tonumber(dates[4]) | |||
else | |||
-- Single date | |||
startYear, startMonth = parseDate(fullVal) | |||
endYear, endMonth = startYear, (startMonth == 12) and 1 or (startMonth + 1) | |||
if startMonth == 12 then endYear = endYear + 1 end | |||
end | |||
if startYear | if not startYear then | ||
error('Argument ' .. num .. ' is an invalid time range', 0) | error('Argument ' .. num .. ' is an invalid time range', 0) | ||
end | end | ||
-- Set defaults | |||
startMonth = startMonth or 1 | |||
endMonth = endMonth or 1 | |||
endYear = endYear or (tonumber(os.date('%Y')) + 1) | |||
local startDate = dateToNumber(startYear, startMonth) | |||
local endDate = dateToNumber(endYear, endMonth) | |||
if endDate <= startDate then | |||
endDate = startDate + 1 -- Minimum duration of 1 month | |||
end | end | ||
rows[#rows][cellIndex].startDate = startDate | |||
rows[#rows][cellIndex].endDate = endDate | |||
rows[#rows][cellIndex].startYear = startYear | rows[#rows][cellIndex].startYear = startYear | ||
rows[#rows][cellIndex].startMonth = startMonth | |||
rows[#rows][cellIndex].endYear = endYear | rows[#rows][cellIndex].endYear = endYear | ||
rows[#rows][cellIndex].endMonth = endMonth | |||
if | if startDate < rows.minDate then | ||
rows. | rows.minDate = startDate | ||
end | end | ||
if | if endDate > rows.maxDate then | ||
rows. | rows.maxDate = endDate | ||
end | end | ||
-- New item | -- New item | ||
| Line 139: | Line 149: | ||
end | end | ||
-- Add overrides from arguments | -- Add overrides from arguments | ||
if args.startoffset then | if args.startoffset then | ||
rows. | rows.minDate = rows.minDate - tonumber(args.startoffset) | ||
end | end | ||
if args.startyear then | if args.startyear then | ||
local | local startMonth = tonumber(args.startmonth) or 1 | ||
if | local customStart = dateToNumber(tonumber(args.startyear), startMonth) | ||
rows. | if customStart < rows.minDate then | ||
rows.minDate = customStart | |||
end | end | ||
end | end | ||
if args.endoffset then | if args.endoffset then | ||
rows. | rows.maxDate = rows.maxDate + tonumber(args.endoffset) | ||
end | end | ||
if args.endyear then | if args.endyear then | ||
local | local endMonth = tonumber(args.endmonth) or 12 | ||
if | local customEnd = dateToNumber(tonumber(args.endyear), endMonth) | ||
rows. | if customEnd > rows.maxDate then | ||
rows.maxDate = customEnd | |||
end | end | ||
end | end | ||
return rows | return rows | ||
end | end | ||
| Line 175: | Line 180: | ||
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 | local useMonths = rows.useMonths | ||
local yearRow = tbl:tag('tr') | local yearRow = tbl:tag('tr'):addClass('timeline-row') | ||
-- Create label | -- Create label | ||
if args.label or rows.hasLabels then | if args.label or rows.hasLabels then | ||
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', | :attr('rowspan', (showDecades ~= false and useMonths) and '3' or | ||
(showDecades ~= false or useMonths) and '2' or '1') | |||
:wikitext(args.label or '') | :wikitext(args.label or '') | ||
| Line 200: | Line 198: | ||
-- Create decade row | -- Create decade row | ||
if showDecades ~= false then | if showDecades ~= false then | ||
local decadeRow = tbl:tag('tr') | local decadeRow = tbl:tag('tr'):addClass('timeline-row') | ||
local currentDate = rows.minDate | |||
local | |||
if not invert then | if not invert then | ||
decadeRow, yearRow = yearRow, decadeRow | decadeRow, yearRow = yearRow, decadeRow | ||
end | end | ||
while | while currentDate < rows.maxDate do | ||
local dur = math.min( | local year, month = numberToDate(currentDate) | ||
local nextDecade = math.ceil(year / 10) * 10 | |||
local decadeEnd = dateToNumber(nextDecade, 1) | |||
local dur = math.min(decadeEnd - currentDate, rows.maxDate - currentDate) | |||
decadeRow:tag('th') | decadeRow:tag('th') | ||
| Line 221: | Line 219: | ||
:wikitext(math.floor(year / 10) .. '0s') | :wikitext(math.floor(year / 10) .. '0s') | ||
currentDate = currentDate + dur | |||
end | end | ||
-- Create month row | end | ||
-- Create month row if using months | |||
local monthRow = nil | |||
if useMonths then | |||
monthRow = tbl:tag('tr'):addClass('timeline-row') | |||
if invert and showDecades ~= false then | |||
monthRow, yearRow = | -- Reorder for footer | ||
monthRow, yearRow, decadeRow = decadeRow, monthRow, yearRow | |||
end | end | ||
end | end | ||
-- Populate year row | -- Populate year row | ||
local totalDuration = rows.maxDate - rows.minDate | |||
local currentDate = rows.minDate | |||
local | |||
local | while currentDate < rows.maxDate do | ||
local year, month = numberToDate(currentDate) | |||
local nextYear = dateToNumber(year + 1, 1) | |||
local yearDuration = math.min(nextYear - currentDate, rows.maxDate - currentDate) | |||
local width = (yearDuration / totalDuration) * 100 | |||
yearRow:tag('th') | |||
:attr('scope', 'col') | |||
:addClass('timeline-year') | |||
:cssText(args.datestyle) | |||
:cssText(args.yearstyle) | |||
:cssText('width:' .. width .. '%') | |||
:attr('colspan', yearDuration) | |||
:wikitext(tostring(year)) | |||
for | -- Add months for this year | ||
if useMonths and monthRow then | |||
local yearStart = currentDate | |||
while yearStart < nextYear and yearStart < rows.maxDate do | |||
local monthYear, monthNum = numberToDate(yearStart) | |||
local monthWidth = (1 / totalDuration) * 100 | |||
monthRow:tag('th') | |||
:attr('scope', 'col') | |||
:addClass('timeline-month') | |||
:cssText(args.datestyle) | |||
:cssText(args.monthstyle) | |||
:cssText('width:' .. monthWidth .. '%') | |||
:wikitext(getMonthNum(monthNum)) | |||
yearStart = yearStart + 1 | |||
end | |||
end | end | ||
currentDate = nextYear | |||
end | end | ||
end | end | ||
| Line 291: | Line 277: | ||
-- Render the timeline itself | -- Render the timeline itself | ||
local function renderTimeline(args, tbl, rows) | local function renderTimeline(args, tbl, rows) | ||
for rowNum, row in ipairs(rows) do | for rowNum, row in ipairs(rows) do | ||
local rowElement = tbl:tag('tr') | local rowElement = tbl:tag('tr'):addClass('timeline-row') | ||
if | if rows.hasLabels or args.label then | ||
rowElement:tag('th') | |||
:attr('scope', 'row') | :attr('scope', 'row') | ||
:addClass('navbox-group timeline-label') | :addClass('navbox-group timeline-label') | ||
:cssText(args.labelstyle) | :cssText(args.labelstyle) | ||
:cssText(row.labelstyle) | :cssText(row.labelstyle) | ||
:wikitext(row.label) | :wikitext(row.label or '') | ||
end | end | ||
local prevEndDate = rows.minDate | |||
local | |||
for cellNum, cell in ipairs(row) do | for cellNum, cell in ipairs(row) do | ||
if cell.startDate == nil then | |||
if cell. | |||
error('Missing timerange for row ' .. rowNum .. ' cell ' .. cellNum, 0) | error('Missing timerange for row ' .. rowNum .. ' cell ' .. cellNum, 0) | ||
end | end | ||
-- Add blanks before the cell | -- Add blanks before the cell | ||
addBlank(args, rowElement, | addBlank(args, rowElement, prevEndDate, cell.startDate) | ||
rowElement:tag('td') | |||
:addClass('timeline-item') | :addClass('timeline-item') | ||
:cssText(args.itemstyle) | :cssText(args.itemstyle) | ||
:cssText(cell.style or '') | :cssText(cell.style or '') | ||
:attr('colspan', | :attr('colspan', cell.endDate - cell.startDate) | ||
:wikitext(cell.item) | :wikitext(cell.item) | ||
prevEndDate = cell.endDate | |||
end | end | ||
-- Add blanks to the end of the row | -- Add blanks to the end of the row | ||
addBlank(args, rowElement, | addBlank(args, rowElement, prevEndDate, rows.maxDate) | ||
end | end | ||
end | end | ||
| Line 378: | Line 322: | ||
listpadding = '0' | listpadding = '0' | ||
} | } | ||
local passthrough = { | local passthrough = { | ||
'name', 'title', 'above', 'below', 'state', 'navbar', 'border', 'image', | 'name', 'title', 'above', 'below', 'state', 'navbar', 'border', 'image', | ||
'imageleft | 'imageleft', 'style', 'bodystyle', 'basestyle', | ||
'titlestyle', 'abovestyle', 'belowstyle', 'imagestyle', | 'titlestyle', 'abovestyle', 'belowstyle', 'imagestyle', | ||
'imageleftstyle', 'titleclass', 'aboveclass', 'bodyclass', 'belowclass', | 'imageleftstyle', 'titleclass', 'aboveclass', 'bodyclass', 'belowclass', | ||
'imageclass' | 'imageclass' | ||
} | } | ||
local rows = timelineInfo(args) | local rows = timelineInfo(args) | ||
local wrapper = mw.html.create('table') | local wrapper = mw.html.create('table') | ||
| Line 408: | Line 353: | ||
targs.templatestyles = 'Module:Navbox timeline/styles.css' | targs.templatestyles = 'Module:Navbox timeline/styles.css' | ||
targs.list1 = tostring(wrapper) | targs.list1 = tostring(wrapper) | ||
targs.bodyclass = (targs.bodyclass or '') .. ' timeline-container' -- Add this line | |||
return navbox(targs) | return navbox(targs) | ||