Module:Timeline

From aquila
Revision as of 19:19, 23 October 2026 by Baseplate (talk | contribs)
Jump to navigation Jump to search

Documentation for this module may be created at Module:Timeline/doc

local p = {}

local dayLabels = {
    "Early January", "January--February", "February--March", "March--April", 
    "April--May", "May--June", "June", "June--July", "July--August", 
    "August--September", "September--October", "October--November", 
    "November--December", "Late December"
}

function p.renderTable(frame)
    local year = tonumber(frame.args["Year"])
    local category = frame.args["Category"]
    
    local events = {}
    for i = 1, 14 do events[i] = {} end

    -- Dynamically capture all events passed to the template
    for k, v in pairs(frame.args) do
        local day, index, field = string.match(k, "^Day(%d+)_(%d+)_(%w+)$")
        if day and index and field then
            day = tonumber(day)
            index = tonumber(index)
            if events[day][index] == nil then
                events[day][index] = {}
            end
            events[day][index][field] = v
        end
    end

    local html = {}
    table.insert(html, '{| class="wikitable mw-collapsible" style="width: 100%; margin-top: 10px; font-size: 85%; line-height: 1.4; border-collapse: collapse;"')
    table.insert(html, '|+ Era Nova, Year ' .. year .. ' — ' .. category .. ' Timeline')
    table.insert(html, '! In-Universe Date')
    table.insert(html, '! Post')
    table.insert(html, '! Character / Nation')
    table.insert(html, '! Real-World Date')
    table.insert(html, '|-')
    table.insert(html, '! colspan="4" style="background-color: #f1f5f9; color: #0b132b; text-align: center; font-weight: bold; padding: 6px;" | ' .. string.upper(category) .. ' NEWS')
    table.insert(html, '|-')

    local skipRows = 0
    for i = 1, 14 do
        if skipRows > 0 then
            skipRows = skipRows - 1
        else
            local eventCount = #events[i]
            
            if eventCount == 0 then
                local emptySpan = 1
                for j = i + 1, 14 do
                    if #events[j] == 0 then
                        emptySpan = emptySpan + 1
                    else
                        break
                    end
                end
                
                local dateLabel = dayLabels[i]
                if emptySpan > 1 then
                    dateLabel = dayLabels[i] .. " to<br>" .. dayLabels[i + emptySpan - 1]
                end
                dateLabel = dateLabel .. ", Year " .. year
                
                table.insert(html, '| ' .. dateLabel)
                table.insert(html, '| ')
                table.insert(html, '| ')
                table.insert(html, '| ')
                table.insert(html, '|-')
                
                skipRows = emptySpan - 1
            else
                local rowspanAttr = ''
                if eventCount > 1 then
                    rowspanAttr = ' rowspan="' .. eventCount .. '" '
                end
                
                table.insert(html, '|' .. rowspanAttr .. '| ' .. dayLabels[i] .. ', Year ' .. year)
                
                for idx, event in ipairs(events[i]) do
                    if idx > 1 then
                        table.insert(html, '|-')
                    end
                    
                    local postText = event.Post or ""
                    if postText ~= "" and not string.find(postText, "%[%[") then
                        postText = "[[" .. postText .. "]]"
                    end
                    
                    local charText = event.Char or ""
                    if charText ~= "" and not string.find(charText, "%[%[") then
                        charText = "[[" .. charText .. "]]"
                    end

                    local dateText = event.Date or ""
                    if dateText == "" then
                        local daysOffset = ((year - 1) * 14) + (i - 1)
                        local timestamp = os.time({year=2026, month=07, day=28, hour=12}) + (daysOffset * 86400)
                        dateText = os.date("%Y-%m-%d", timestamp)
                    end

                    table.insert(html, '| ' .. postText)
                    table.insert(html, '| ' .. charText)
                    table.insert(html, '| ' .. dateText)
                    table.insert(html, '|-')
                end
            end
        end
    end

    table.insert(html, '|}')
    return table.concat(html, "\n")
end

return p