Module:BCIChapterList: Difference between revisions
adding headers! |
m Another bugfix |
||
| (One intermediate revision by the same user not shown) | |||
| Line 25: | Line 25: | ||
function p.renderTable(frame) | function p.renderTable(frame) | ||
-- grab raw wikitext of | -- grab raw wikitext of your template | ||
local tpl = mw.title.new("Template: | local tpl = mw.title.new("Template:BCIChapterList") | ||
local raw = tpl and tpl:getContent() or "" | local raw = tpl and tpl:getContent() or "" | ||
-- | -- split on newlines into lines | ||
local lines = mw.text.split(raw, "\n", true) | |||
local rows = {} | local rows = {} | ||
for line in | |||
for _, line in ipairs(lines) do | |||
local trimmed = mw.text.trim(line) | local trimmed = mw.text.trim(line) | ||
if trimmed:find("^{{BCIChapter") then | if trimmed:find("^{{BCIChapter") then | ||
-- extract the inside of {{BCIChapter ... }} | |||
local inner = trimmed:match("^{{BCIChapter%s*(.+)}}$") | local inner = trimmed:match("^{{BCIChapter%s*(.+)}}$") | ||
if inner then | if inner then | ||
| Line 65: | Line 68: | ||
local currentPeriod = nil | local currentPeriod = nil | ||
for _, r in ipairs(rows) do | for _, r in ipairs(rows) do | ||
local yr = r.date:match("^(%d%d%d%d)%- | local yr = r.date:match("^(%d%d%d%d)%-") | ||
local period = periodForYear(yr) | local period = periodForYear(yr) | ||
if period and period ~= currentPeriod then | if period and period ~= currentPeriod then | ||
Latest revision as of 00:10, 24 April 2025
Documentation for this module may be created at Module:BCIChapterList/doc
-- Module:BCIChapterList
local p = {}
-- helper: format ISO date YYYY-MM-DD → M/D/YY
local function formatDate(iso)
local y,m,d = iso:match("^(%d%d%d%d)%-(%d%d)%-(%d%d)$")
if not y then return iso end
m = tonumber(m); d = tonumber(d)
return m .. "/" .. d .. "/" .. y:sub(3,4)
end
-- helper: determine period for a given year
local function periodForYear(year)
year = tonumber(year)
if not year then return nil end
if year >= 2010 and year <= 2014 then
return "2010–2014"
elseif year >= 2015 and year <= 2019 then
return "2015–2019"
elseif year >= 2020 and year <= 2025 then
return "2020–2025"
end
return nil
end
function p.renderTable(frame)
-- grab raw wikitext of your template
local tpl = mw.title.new("Template:BCIChapterList")
local raw = tpl and tpl:getContent() or ""
-- split on newlines into lines
local lines = mw.text.split(raw, "\n", true)
local rows = {}
for _, line in ipairs(lines) do
local trimmed = mw.text.trim(line)
if trimmed:find("^{{BCIChapter") then
-- extract the inside of {{BCIChapter ... }}
local inner = trimmed:match("^{{BCIChapter%s*(.+)}}$")
if inner then
if inner:sub(1,1) == "|" then inner = inner:sub(2) end
local params = {}
for part in inner:gmatch("[^|]+") do
local k,v = part:match("^(%w+)%s*=%s*(.+)$")
if k and v then params[k] = v end
end
if params.title and params.date and params.seq then
table.insert(rows, {
title = params.title,
date = params.date,
seq = tonumber(params.seq) or 0
})
end
end
end
end
-- sort by seq ascending
table.sort(rows, function(a,b) return a.seq < b.seq end)
-- build the wikitable with period headers
local out = {
'{| class="wikitable sortable"',
'! Title',
'! Published'
}
local currentPeriod = nil
for _, r in ipairs(rows) do
local yr = r.date:match("^(%d%d%d%d)%-")
local period = periodForYear(yr)
if period and period ~= currentPeriod then
table.insert(out, "|-")
table.insert(out, "! colspan=\"2\" | " .. period)
currentPeriod = period
end
table.insert(out, "|-")
table.insert(out,
"| [[" .. r.title .. "]] || " .. formatDate(r.date)
)
end
table.insert(out, "|}")
return table.concat(out, "\n")
end
return p