Module:ChapterList
Documentation for this module may be created at Module:ChapterList/doc
-- Module:ChapterList
-- Reads Template:ChapterList, parses {{Chapter|...}}, and renders a sortable table.
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)
local yy = y:sub(3,4)
return m .. "/" .. d .. "/" .. yy
end
function p.renderTable(frame)
-- Load raw wikitext of Template:ChapterList
local tpl = mw.title.new('Template:ChapterList')
local raw = tpl and tpl:getContent() or ''
-- Parse each {{Chapter|...}} line
local chapters = {}
for line in raw:gmatch('[^\r\n]+') do
local trimmed = mw.text.trim(line)
if trimmed:find('^{{Chapter') then
-- strip leading '{{Chapter' and trailing '}}'
local inner = trimmed:match('^{{Chapter%s*(.+)}}$') or ''
-- remove possible leading '|'
if inner:sub(1,1) == '|' then inner = inner:sub(2) end
-- split into key=value params
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.number and params.title and params.date then
table.insert(chapters, {
number = params.number,
title = params.title,
date = params.date
})
end
end
end
-- Build wikitable
local out = {}
table.insert(out, '{| class="wikitable sortable"')
table.insert(out, '! #')
table.insert(out, '! Title')
table.insert(out, '! Published')
for _, ch in ipairs(chapters) do
local isInter = ch.number:find('%.')
local dispNum = isInter and '' or ch.number
local dispTitle = isInter
and ('Intermission: [[' .. ch.title .. ']]')
or ('[[' .. ch.title .. ']]')
local dispDate = formatDate(ch.date)
table.insert(out, '|-')
table.insert(out, '| ' .. dispNum .. ' || ' .. dispTitle .. ' || ' .. dispDate)
end
table.insert(out, '|}')
return table.concat(out, '\n')
end
return p