Module:Timeline: Difference between revisions
Jump to navigation
Jump to search
mNo edit summary |
mNo edit summary |
||
| Line 1: | Line 1: | ||
local Calendar = require("Module:Calendar") | |||
local p = {} | local p = {} | ||
local SEPARATOR = ";" | local SEPARATOR = ";" | ||
local SPLIT_CHARACTERS = true | local SPLIT_CHARACTERS = true | ||
local LEGACY_MAX_EVENTS = 9 | local LEGACY_MAX_EVENTS = 9 | ||
local EMPTY_ROW_TEXT = "" | |||
local | local CARGO_TABLE = "NewsPosts" | ||
local CARGO_FIELDS = "Page_Name,Title,Author,Publisher,RealDate,DayNum" | |||
local CARGO_LIMIT = 1000 | |||
local DAY_LABELS = { | local DAY_LABELS = {} | ||
for i, period in ipairs(Calendar.periods) do | |||
DAY_LABELS[i] = Calendar.pretty(period) | |||
end | |||
local DAYS_PER_YEAR = Calendar.daysPerYear | |||
local DAYS_PER_YEAR = | |||
local SECTIONS = { | local SECTIONS = { | ||
| Line 33: | Line 22: | ||
Domestic = "Domestic News", | Domestic = "Domestic News", | ||
Character = "Character News", | Character = "Character News", | ||
} | |||
local TYPE_ALIASES = { | |||
["global"] = "Global News", | |||
["global news"] = "Global News", | |||
["domestic"] = "Domestic News", | |||
["domestic news"] = "Domestic News", | |||
["character"] = "Character News", | |||
["character news"] = "Character News", | |||
} | } | ||
| Line 50: | Line 48: | ||
local function errorText(message) | local function errorText(message) | ||
return '<strong class="error">Timeline error: ' .. message .. "</strong>" | return '<strong class="error">Timeline error: ' .. message .. "</strong>" | ||
end | end | ||
| Line 108: | Line 71: | ||
end | end | ||
return table.concat(parts, ", ") | return table.concat(parts, ", ") | ||
end | |||
local function normalizeTitle(name) | |||
name = trim((tostring(name or ""):gsub("_", " "))) | |||
return name:sub(1, 1):upper() .. name:sub(2) | |||
end | |||
local function linkTarget(wikitext) | |||
local target = tostring(wikitext or ""):match("^%[%[%s*:?([^%]|]+)") | |||
if target then | |||
return normalizeTitle(target) | |||
end | |||
end | end | ||
| Line 138: | Line 113: | ||
end | end | ||
local function | local function collectManualEvents(getArg, day) | ||
local events = {} | local events = {} | ||
| Line 162: | Line 137: | ||
return events | return events | ||
end | |||
local function cargoEvents(typeName, year) | |||
local byDay, names = {}, {} | |||
for day = 1, DAYS_PER_YEAR do byDay[day] = {} end | |||
if not (mw.ext and mw.ext.cargo) then | |||
return byDay, names, "the Cargo extension is not available to Lua" | |||
end | |||
local ok, rows = pcall(mw.ext.cargo.query, CARGO_TABLE, CARGO_FIELDS, { | |||
where = 'Type="' .. typeName .. '" AND YearNum=' .. string.format("%d", year), | |||
orderBy = "RealDate,_pageID", | |||
limit = CARGO_LIMIT, | |||
}) | |||
if not ok then | |||
return byDay, names, tostring(rows) | |||
end | |||
for _, row in ipairs(rows) do | |||
local info = Calendar.info(row.RealDate) | |||
local day = tonumber(row.DayNum) or (info and info.day) | |||
local page = trim(row.Page_Name) | |||
if page ~= "" and day and day >= 1 and day <= DAYS_PER_YEAR then | |||
local title = trim(row.Title) | |||
if title == "" then | |||
title = page:match("[^/]+$") or page | |||
end | |||
local post | |||
if title == page then | |||
post = "[[" .. page .. "]]" | |||
else | |||
post = "[[" .. page .. "|" .. title .. "]]" | |||
end | |||
local char | |||
if trim(row.Author) ~= "" then | |||
char = linkify(row.Author) | |||
else | |||
char = linkifyCharacters(row.Publisher) | |||
end | |||
names[normalizeTitle(page)] = true | |||
local list = byDay[day] | |||
list[#list + 1] = { | |||
post = post, | |||
char = char, | |||
date = info and info.iso or trim(row.RealDate), | |||
} | |||
end | |||
end | |||
return byDay, names | |||
end | end | ||
| Line 197: | Line 227: | ||
if count > 0 then | if count > 0 then | ||
local default = realDate(year, day) | local default = Calendar.realDate(year, day) | ||
local dates, span = {}, {} | local dates, span = {}, {} | ||
for i, ev in ipairs(events) do | for i, ev in ipairs(events) do | ||
| Line 236: | Line 266: | ||
local last = day - 1 | local last = day - 1 | ||
local startDate, endDate = realDate(year, first), realDate(year, last) | local startDate, endDate = Calendar.realDate(year, first), Calendar.realDate(year, last) | ||
local dateText = (first == last) and startDate or (startDate .. " to " .. endDate) | local dateText = (first == last) and startDate or (startDate .. " to " .. endDate) | ||
| Line 250: | Line 280: | ||
out[#out + 1] = "</div>" | out[#out + 1] = "</div>" | ||
return table.concat(out, "\n") | return table.concat(out, "\n") | ||
end | |||
function p.newsType(frame) | |||
local key = trim(frame.args[1]):lower() | |||
return TYPE_ALIASES[key] or SECTIONS.Character | |||
end | end | ||
| Line 272: | Line 307: | ||
if not year or year < 1 or year ~= math.floor(year) then | if not year or year < 1 or year ~= math.floor(year) then | ||
return errorText("set <code>Year</code> to a whole number, 1 or higher.") | return errorText("set <code>Year</code> to a whole number, 1 or higher.") | ||
end | |||
local auto, names, cargoError = {}, {}, nil | |||
if trim(getArg("Auto")):lower() ~= "no" then | |||
auto, names, cargoError = cargoEvents(title, year) | |||
end | end | ||
local schedule = {} | local schedule = {} | ||
for day = 1, DAYS_PER_YEAR do | for day = 1, DAYS_PER_YEAR do | ||
local events = {} | |||
for _, ev in ipairs(collectManualEvents(getArg, day)) do | |||
local target = linkTarget(ev.post) | |||
if not (target and names[target]) then | |||
events[#events + 1] = ev | |||
end | |||
end | |||
for _, ev in ipairs(auto[day] or {}) do | |||
events[#events + 1] = ev | |||
end | |||
schedule[day] = events | |||
end | end | ||
local html = renderSection(title, year, schedule) | |||
if cargoError then | |||
html = html .. '\n<div class="error">Could not read the Cargo table “' .. CARGO_TABLE | |||
.. "”: " .. cargoError .. "</div>" | |||
end | |||
return html | |||
end | end | ||
return p | return p | ||
Revision as of 09:36, 24 October 2026
Documentation for this module may be created at Module:Timeline/doc
local Calendar = require("Module:Calendar")
local p = {}
local SEPARATOR = ";"
local SPLIT_CHARACTERS = true
local LEGACY_MAX_EVENTS = 9
local EMPTY_ROW_TEXT = ""
local CARGO_TABLE = "NewsPosts"
local CARGO_FIELDS = "Page_Name,Title,Author,Publisher,RealDate,DayNum"
local CARGO_LIMIT = 1000
local DAY_LABELS = {}
for i, period in ipairs(Calendar.periods) do
DAY_LABELS[i] = Calendar.pretty(period)
end
local DAYS_PER_YEAR = Calendar.daysPerYear
local SECTIONS = {
Global = "Global News",
Domestic = "Domestic News",
Character = "Character News",
}
local TYPE_ALIASES = {
["global"] = "Global News",
["global news"] = "Global News",
["domestic"] = "Domestic News",
["domestic news"] = "Domestic News",
["character"] = "Character News",
["character news"] = "Character News",
}
local COLUMNS = {
{ "In-universe date", "24%" },
{ "Post", "34%" },
{ "Character / nation", "24%" },
{ "Real-world date", "18%" },
}
local SEP_PATTERN = (SEPARATOR:gsub("%p", "%%%0"))
local function trim(s)
return (tostring(s or ""):match("^%s*(.-)%s*$"))
end
local function errorText(message)
return '<strong class="error">Timeline error: ' .. message .. "</strong>"
end
local function linkify(text)
text = trim(text)
if text == "" then return "" end
if text:find("[[", 1, true) or text:find("^[%[<']") then
return text
end
return "[[" .. text .. "]]"
end
local function linkifyCharacters(text)
text = trim(text)
if text == "" then return "" end
if not SPLIT_CHARACTERS or text:find("[[", 1, true) or text:find("^[<']") then
return linkify(text)
end
local parts = {}
for part in (text .. ","):gmatch("(.-),") do
part = trim(part)
if part ~= "" then parts[#parts + 1] = linkify(part) end
end
return table.concat(parts, ", ")
end
local function normalizeTitle(name)
name = trim((tostring(name or ""):gsub("_", " ")))
return name:sub(1, 1):upper() .. name:sub(2)
end
local function linkTarget(wikitext)
local target = tostring(wikitext or ""):match("^%[%[%s*:?([^%]|]+)")
if target then
return normalizeTitle(target)
end
end
local function splitFields(line)
line = line:gsub("(&#?%w+);", "%1\1")
local fields = {}
for field in (line .. SEPARATOR):gmatch("(.-)" .. SEP_PATTERN) do
fields[#fields + 1] = trim((field:gsub("\1", ";")))
end
return fields
end
local function parseLine(line)
line = trim(line)
if line == "" then return nil end
local f = splitFields(line)
local post, char, date = f[1] or "", f[2] or "", f[3] or ""
if post == "" and char == "" and date == "" then return nil end
if post == "" then
post = '<span class="error">(missing post title)</span>'
else
post = linkify(post)
end
if #f > 3 then
post = post .. ' <span class="error">(too many “' .. SEPARATOR .. '” on this line)</span>'
end
return { post = post, char = linkifyCharacters(char), date = date }
end
local function collectManualEvents(getArg, day)
local events = {}
local text = getArg("Day" .. day)
if text and trim(text) ~= "" then
for line in (text .. "\n"):gmatch("(.-)\r?\n") do
local ev = parseLine(line)
if ev then events[#events + 1] = ev end
end
end
for n = 1, LEGACY_MAX_EVENTS do
local prefix = "Day" .. day .. "_" .. n .. "_"
local post = trim(getArg(prefix .. "Post"))
if post ~= "" then
events[#events + 1] = {
post = linkify(post),
char = linkifyCharacters(getArg(prefix .. "Char")),
date = trim(getArg(prefix .. "Date")),
}
end
end
return events
end
local function cargoEvents(typeName, year)
local byDay, names = {}, {}
for day = 1, DAYS_PER_YEAR do byDay[day] = {} end
if not (mw.ext and mw.ext.cargo) then
return byDay, names, "the Cargo extension is not available to Lua"
end
local ok, rows = pcall(mw.ext.cargo.query, CARGO_TABLE, CARGO_FIELDS, {
where = 'Type="' .. typeName .. '" AND YearNum=' .. string.format("%d", year),
orderBy = "RealDate,_pageID",
limit = CARGO_LIMIT,
})
if not ok then
return byDay, names, tostring(rows)
end
for _, row in ipairs(rows) do
local info = Calendar.info(row.RealDate)
local day = tonumber(row.DayNum) or (info and info.day)
local page = trim(row.Page_Name)
if page ~= "" and day and day >= 1 and day <= DAYS_PER_YEAR then
local title = trim(row.Title)
if title == "" then
title = page:match("[^/]+$") or page
end
local post
if title == page then
post = "[[" .. page .. "]]"
else
post = "[[" .. page .. "|" .. title .. "]]"
end
local char
if trim(row.Author) ~= "" then
char = linkify(row.Author)
else
char = linkifyCharacters(row.Publisher)
end
names[normalizeTitle(page)] = true
local list = byDay[day]
list[#list + 1] = {
post = post,
char = char,
date = info and info.iso or trim(row.RealDate),
}
end
end
return byDay, names
end
local function cell(content, attrs)
return "<td" .. (attrs or "") .. ">" .. content .. "</td>"
end
local function rangeLabel(first, last, year)
if first == last then
return DAY_LABELS[first] .. ", Year " .. year
end
return DAY_LABELS[first] .. " to " .. DAY_LABELS[last] .. ", Year " .. year
end
local function renderSection(title, year, schedule)
local out = {}
out[#out + 1] = '<div style="overflow-x:auto;">'
out[#out + 1] = '<table class="wikitable aquila-timeline" style="width:100%;">'
out[#out + 1] = '<tr><th colspan="4" style="background-color:#f1f5f9; color:#0b132b; '
.. 'text-align:center; font-weight:bold; padding:6px; text-transform:uppercase;">'
.. title .. "</th></tr>"
local head = {}
for _, col in ipairs(COLUMNS) do
head[#head + 1] = '<th style="width:' .. col[2] .. ';">' .. col[1] .. "</th>"
end
out[#out + 1] = "<tr>" .. table.concat(head) .. "</tr>"
local day = 1
while day <= DAYS_PER_YEAR do
local events = schedule[day]
local count = #events
if count > 0 then
local default = Calendar.realDate(year, day)
local dates, span = {}, {}
for i, ev in ipairs(events) do
dates[i] = (ev.date ~= "") and ev.date or default
end
local i = 1
while i <= count do
local j = i
while j < count and dates[j + 1] == dates[i] do j = j + 1 end
span[i] = j - i + 1
i = j + 1
end
for idx, ev in ipairs(events) do
local row = {}
if idx == 1 then
row[#row + 1] = cell(
DAY_LABELS[day] .. ", Year " .. year,
count > 1 and (' rowspan="' .. count .. '"') or ""
)
end
row[#row + 1] = cell(ev.post)
row[#row + 1] = cell(ev.char)
if span[idx] then
row[#row + 1] = cell(
dates[idx],
span[idx] > 1 and (' rowspan="' .. span[idx] .. '"') or ""
)
end
out[#out + 1] = "<tr>" .. table.concat(row) .. "</tr>"
end
day = day + 1
else
local first = day
while day <= DAYS_PER_YEAR and #schedule[day] == 0 do
day = day + 1
end
local last = day - 1
local startDate, endDate = Calendar.realDate(year, first), Calendar.realDate(year, last)
local dateText = (first == last) and startDate or (startDate .. " to " .. endDate)
out[#out + 1] = "<tr>"
.. cell(rangeLabel(first, last, year))
.. cell(EMPTY_ROW_TEXT, ' colspan="2"')
.. cell(dateText)
.. "</tr>"
end
end
out[#out + 1] = "</table>"
out[#out + 1] = "</div>"
return table.concat(out, "\n")
end
function p.newsType(frame)
local key = trim(frame.args[1]):lower()
return TYPE_ALIASES[key] or SECTIONS.Character
end
function p.render(frame)
local own = frame.args
local parent = frame:getParent()
local inherited = parent and parent.args or {}
local function getArg(name)
local v = inherited[name]
if v == nil or trim(v) == "" then v = own[name] end
return v
end
local key = trim(getArg("Category"))
local title = SECTIONS[key]
if not title then
return errorText('unknown Category "' .. key .. '" (use Global, Domestic or Character).')
end
local year = tonumber(trim(getArg("Year")))
if not year or year < 1 or year ~= math.floor(year) then
return errorText("set <code>Year</code> to a whole number, 1 or higher.")
end
local auto, names, cargoError = {}, {}, nil
if trim(getArg("Auto")):lower() ~= "no" then
auto, names, cargoError = cargoEvents(title, year)
end
local schedule = {}
for day = 1, DAYS_PER_YEAR do
local events = {}
for _, ev in ipairs(collectManualEvents(getArg, day)) do
local target = linkTarget(ev.post)
if not (target and names[target]) then
events[#events + 1] = ev
end
end
for _, ev in ipairs(auto[day] or {}) do
events[#events + 1] = ev
end
schedule[day] = events
end
local html = renderSection(title, year, schedule)
if cargoError then
html = html .. '\n<div class="error">Could not read the Cargo table “' .. CARGO_TABLE
.. "”: " .. cargoError .. "</div>"
end
return html
end
return p