وحدة:ChapterList

من قصص عارف

-- Module:ChapterList

local p = {}

function p.list(frame)
    local parentArgs = frame:getParent().args
    local result = ""

    -- Collect numeric keys
    local chapterKeys = {}

    for key in pairs(parentArgs) do
        if tonumber(key) then
            table.insert(chapterKeys, tonumber(key))
        end
    end

    -- Sort the keys numerically
    table.sort(chapterKeys)

    -- Process chapters in order
    for _, key in ipairs(chapterKeys) do
        local chapter = parentArgs[tostring(key)]
        if chapter and chapter ~= '' then
            result = result .. frame:expandTemplate{ title = "ChapterSchema", args = { name = chapter } } .. "\n"
        end
    end

    return result
end

return p