Module:NotYetInPrint

Revision as of 08:28, 19 May 2024 by SuitCase (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:NotYetInPrint/doc

-- Module:NotYetInPrint
local p = {}

function p.getChaptersList()
    local listPage = mw.title.new('Template:ChapterList')
    if not listPage then
        return {}
    end

    local content = listPage:getContent()
    if not content then
        return {}
    end

    local chapters = {}
    for number, title in string.gmatch(content, "{{Chapter|number=([%d%.]+)|title=([^|]+)|") do
        local num = tonumber(number)
        if num then
            table.insert(chapters, {number = num, title = title})
        end
    end

    table.sort(chapters, function(a, b) return a.number < b.number end)

    return chapters
end

function p.generateList(frame)
    local startNumber = tonumber(frame.args.start)
    if not startNumber then
        return "Error: Invalid start number."
    end

    local chapters = p.getChaptersList()
    local result = {}
    local start = false

    for _, chapter in ipairs(chapters) do
        if chapter.number >= startNumber then
            start = true
        end
        if start then
            table.insert(result, "* [[" .. chapter.title .. "]]")
        end
    end

    return table.concat(result, "\n")
end

return p