Module:NotYetInPrint: Difference between revisions

From Candypedia
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..."
 
No edit summary
 
Line 15: Line 15:
     local chapters = {}
     local chapters = {}
     for number, title in string.gmatch(content, "{{Chapter|number=([%d%.]+)|title=([^|]+)|") do
     for number, title in string.gmatch(content, "{{Chapter|number=([%d%.]+)|title=([^|]+)|") do
         table.insert(chapters, {number = tonumber(number), title = title})
         local num = tonumber(number)
        if num then
            table.insert(chapters, {number = num, title = title})
        end
     end
     end


Line 23: Line 26:
end
end


function p.generateList(startNumber)
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 chapters = p.getChaptersList()
     local result = {}
     local result = {}
Line 29: Line 37:


     for _, chapter in ipairs(chapters) do
     for _, chapter in ipairs(chapters) do
         if chapter.number >= tonumber(startNumber) then
         if chapter.number >= startNumber then
             start = true
             start = true
         end
         end

Latest revision as of 08:28, 19 May 2024

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