Module:Chapters: Difference between revisions

From Candypedia
Created page with "-- Module:Chapters local p = {} function p.getChaptersList() local listPage = mw.title.new('Template:ChaptersList') if not listPage then return {} end local content = listPage:getContent() if not content then return {} end local chapters = {} for number, title, date, pagecount in string.gmatch(content, "{{Chapter|number=(%d+)|title=([^|]+)|date=([^|]+)|pagecount=(%d+)}}") do table.insert(chapters, {number = tonum..."
 
No edit summary
Line 2: Line 2:
local p = {}
local p = {}


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


function p.getChapterDetails(chapterTitle, detail)
function p.getChapterDetails(chapterTitle, detail)
     local chapters = p.getChaptersList()
     local chapters = p.getChapterList()
     for _, chapter in ipairs(chapters) do
     for _, chapter in ipairs(chapters) do
         if chapter.title == chapterTitle then
         if chapter.title == chapterTitle then
Line 32: Line 32:


function p.getTotalChapters()
function p.getTotalChapters()
     return #p.getChaptersList()
     return #p.getChapterList()
end
end


function p.getNextChapter(chapterTitle)
function p.getNextChapter(chapterTitle)
     local chapters = p.getChaptersList()
     local chapters = p.getChapterList()
     for i, chapter in ipairs(chapters) do
     for i, chapter in ipairs(chapters) do
         if chapter.title == chapterTitle and i < #chapters then
         if chapter.title == chapterTitle and i < #chapters then
Line 46: Line 46:


function p.getPreviousChapter(chapterTitle)
function p.getPreviousChapter(chapterTitle)
     local chapters = p.getChaptersList()
     local chapters = p.getChapterList()
     for i, chapter in ipairs(chapters) do
     for i, chapter in ipairs(chapters) do
         if chapter.title == chapterTitle and i > 1 then
         if chapter.title == chapterTitle and i > 1 then

Revision as of 08:17, 19 May 2024

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

-- Module:Chapters
local p = {}

function p.getChapterList()
    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, date, pagecount in string.gmatch(content, "{{Chapter|number=(%d+)|title=([^|]+)|date=([^|]+)|pagecount=(%d+)}}") do
        table.insert(chapters, {number = tonumber(number), title = title, date = date, pagecount = tonumber(pagecount)})
    end

    return chapters
end

function p.getChapterDetails(chapterTitle, detail)
    local chapters = p.getChapterList()
    for _, chapter in ipairs(chapters) do
        if chapter.title == chapterTitle then
            return chapter[detail]
        end
    end
    return nil
end

function p.getTotalChapters()
    return #p.getChapterList()
end

function p.getNextChapter(chapterTitle)
    local chapters = p.getChapterList()
    for i, chapter in ipairs(chapters) do
        if chapter.title == chapterTitle and i < #chapters then
            return chapters[i + 1].title
        end
    end
    return nil
end

function p.getPreviousChapter(chapterTitle)
    local chapters = p.getChapterList()
    for i, chapter in ipairs(chapters) do
        if chapter.title == chapterTitle and i > 1 then
            return chapters[i - 1].title
        end
    end
    return nil
end

return p