Module:BCIChapterList: Difference between revisions
m typo! |
m Another bugfix |
||
| (2 intermediate revisions by the same user not shown) | |||
| Line 8: | Line 8: | ||
m = tonumber(m); d = tonumber(d) | m = tonumber(m); d = tonumber(d) | ||
return m .. "/" .. d .. "/" .. y:sub(3,4) | 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 | end | ||
function p.renderTable(frame) | function p.renderTable(frame) | ||
-- | -- grab raw wikitext of your template | ||
local tpl = mw.title.new("Template:BCIChapterList") | 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 | |||
if | local trimmed = mw.text.trim(line) | ||
-- extract | if trimmed:find("^{{BCIChapter") then | ||
local inner = | -- extract the inside of {{BCIChapter ... }} | ||
local inner = trimmed:match("^{{BCIChapter%s*(.+)}}$") | |||
if inner then | if inner then | ||
if inner:sub(1,1) == "|" then inner = inner:sub(2) end | if inner:sub(1,1) == "|" then inner = inner:sub(2) end | ||
local params = {} | local params = {} | ||
for part in inner:gmatch("[^|]+") do | for part in inner:gmatch("[^|]+") do | ||
| Line 31: | Line 45: | ||
if k and v then params[k] = v end | if k and v then params[k] = v end | ||
end | end | ||
if params.title and params.date then | if params.title and params.date and params.seq then | ||
table.insert(rows, { | table.insert(rows, { | ||
title = params.title, | title = params.title, | ||
| Line 42: | Line 56: | ||
end | end | ||
-- | -- sort by seq ascending | ||
table.sort(rows, function(a,b) return a.seq < b.seq end) | table.sort(rows, function(a,b) return a.seq < b.seq end) | ||
-- | -- build the wikitable with period headers | ||
local out = { | local out = { | ||
'{| class="wikitable sortable"', | |||
'! Title', | |||
'! Published' | |||
} | } | ||
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 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, "|-") | ||
table.insert(out, | table.insert(out, | ||
| Line 57: | Line 80: | ||
) | ) | ||
end | end | ||
table.insert(out, "|}") | table.insert(out, "|}") | ||
return table.concat(out, "\n") | return table.concat(out, "\n") | ||
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