Module:ChapterList: Difference between revisions
Table headers and other stuff loaded out of the other module Tag: Reverted |
Table headers and other stuff loaded out of the other module Tag: Manual revert |
||
| Line 1: | Line 1: | ||
-- Module:ChapterList | -- Module:ChapterList | ||
-- | -- Reads Template:ChapterList, parses {{Chapter|...}}, and renders a sortable table. | ||
local p = {} | local p = {} | ||
---- | -- Helper: format ISO date YYYY-MM-DD → M/D/YY | ||
local function formatDate(iso) | local function formatDate(iso) | ||
local y, m, d = iso:match("^(%d%d%d%d)%-(%d%d)%-(%d%d)$") | local y, m, d = iso:match("^(%d%d%d%d)%-(%d%d)%-(%d%d)$") | ||
if not y then return iso | if not y then return iso end | ||
m = tonumber(m) | |||
d = tonumber(d) | |||
local yy = y:sub(3,4) | |||
return m .. "/" .. d .. "/" .. yy | |||
end | 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 '' | |||
local | |||
local raw = | |||
-- | -- Parse each {{Chapter|...}} line | ||
local chapters = {} | local chapters = {} | ||
for | 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 | if inner:sub(1,1) == '|' then inner = inner:sub(2) end | ||
-- split into key=value params | |||
local params = {} | local params = {} | ||
for part in inner:gmatch('[^|]+') do | for part in inner:gmatch('[^|]+') do | ||
| Line 44: | Line 34: | ||
end | end | ||
if params.number and params.title and params.date then | if params.number and params.title and params.date then | ||
table.insert(chapters, params) | table.insert(chapters, { | ||
number = params.number, | |||
title = params.title, | |||
date = params.date | |||
}) | |||
end | end | ||
end | end | ||
end | end | ||
-- | -- Build wikitable | ||
local | 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 | 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 | end | ||
Revision as of 00:21, 24 April 2025
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