Module:NotYetInPrint

Revision as of 08:27, 19 May 2024 by SuitCase (talk | contribs) (Created page with "-- 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 table.insert(chapters, {number = tonumber(number), title = title}) end...")
(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
        table.insert(chapters, {number = tonumber(number), title = title})
    end

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

    return chapters
end

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

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

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

return p