Module:Chapters: Difference between revisions

From Candypedia
No edit summary
No edit summary
Line 3: Line 3:


function p.getChaptersList()
function p.getChaptersList()
     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 60: Line 60:
     end
     end
     return nil
     return nil
end
function p.infoboxChapter(frame)
    local name = frame.args.name
    local chapterTitle = frame.args.chapter_title
    local chapter_no = p.getChapterDetails(chapterTitle, "number") or "N/A"
    local date = p.getChapterDetails(chapterTitle, "date") or "N/A"
    local pagecount = p.getChapterDetails(chapterTitle, "pagecount") or "N/A"
    local total_chapters = p.getTotalChapters() or "N/A"
    local next_chapter = p.getNextChapter(chapterTitle) or "N/A"
    local prev_chapter = p.getPreviousChapter(chapterTitle) or "N/A"
    local infobox = mw.html.create('table')
    infobox:addClass('infobox')
    infobox:tag('tr')
        :tag('th'):wikitext('Name'):done()
        :tag('td'):wikitext(name):done()
        :done()
    infobox:tag('tr')
        :tag('th'):wikitext('Chapter No.'):done()
        :tag('td'):wikitext(chapter_no):done()
        :done()
    infobox:tag('tr')
        :tag('th'):wikitext('Title'):done()
        :tag('td'):wikitext(chapterTitle):done()
        :done()
    infobox:tag('tr')
        :tag('th'):wikitext('Date'):done()
        :tag('td'):wikitext(date):done()
        :done()
    infobox:tag('tr')
        :tag('th'):wikitext('Pagecount'):done()
        :tag('td'):wikitext(pagecount):done()
        :done()
    infobox:tag('tr')
        :tag('th'):wikitext('Total Chapters'):done()
        :tag('td'):wikitext(total_chapters):done()
        :done()
    infobox:tag('tr')
        :tag('th'):wikitext('Next Chapter'):done()
        :tag('td'):wikitext(next_chapter):done()
        :done()
    infobox:tag('tr')
        :tag('th'):wikitext('Previous Chapter'):done()
        :tag('td'):wikitext(prev_chapter):done()
        :done()
    return infobox
end
end


return p
return p

Revision as of 13:49, 19 May 2024

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

-- Module:Chapters
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, 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

    mw.logObject(chapters)  -- Debug: log the parsed chapters

    return chapters
end

function p.getChapterDetails(chapterTitle, detail)
    local chapters = p.getChaptersList()
    for _, chapter in ipairs(chapters) do
        if chapter.title == chapterTitle then
            mw.logObject(chapter)  -- Debug: log the chapter details
            return chapter[detail]
        end
    end
    return nil
end

function p.getTotalChapters()
    local totalChapters = #p.getChaptersList()
    mw.log("Total chapters: " .. totalChapters)  -- Debug: log the total number of chapters
    return totalChapters
end

function p.getNextChapter(chapterTitle)
    local chapters = p.getChaptersList()
    for i, chapter in ipairs(chapters) do
        if chapter.title == chapterTitle and i < #chapters then
            mw.log("Next chapter: " .. chapters[i + 1].title)  -- Debug: log the next chapter
            return chapters[i + 1].title
        end
    end
    return nil
end

function p.getPreviousChapter(chapterTitle)
    local chapters = p.getChaptersList()
    for i, chapter in ipairs(chapters) do
        if chapter.title == chapterTitle and i > 1 then
            mw.log("Previous chapter: " .. chapters[i - 1].title)  -- Debug: log the previous chapter
            return chapters[i - 1].title
        end
    end
    return nil
end

function p.infoboxChapter(frame)
    local name = frame.args.name
    local chapterTitle = frame.args.chapter_title

    local chapter_no = p.getChapterDetails(chapterTitle, "number") or "N/A"
    local date = p.getChapterDetails(chapterTitle, "date") or "N/A"
    local pagecount = p.getChapterDetails(chapterTitle, "pagecount") or "N/A"
    local total_chapters = p.getTotalChapters() or "N/A"
    local next_chapter = p.getNextChapter(chapterTitle) or "N/A"
    local prev_chapter = p.getPreviousChapter(chapterTitle) or "N/A"

    local infobox = mw.html.create('table')
    infobox:addClass('infobox')

    infobox:tag('tr')
        :tag('th'):wikitext('Name'):done()
        :tag('td'):wikitext(name):done()
        :done()

    infobox:tag('tr')
        :tag('th'):wikitext('Chapter No.'):done()
        :tag('td'):wikitext(chapter_no):done()
        :done()

    infobox:tag('tr')
        :tag('th'):wikitext('Title'):done()
        :tag('td'):wikitext(chapterTitle):done()
        :done()

    infobox:tag('tr')
        :tag('th'):wikitext('Date'):done()
        :tag('td'):wikitext(date):done()
        :done()

    infobox:tag('tr')
        :tag('th'):wikitext('Pagecount'):done()
        :tag('td'):wikitext(pagecount):done()
        :done()

    infobox:tag('tr')
        :tag('th'):wikitext('Total Chapters'):done()
        :tag('td'):wikitext(total_chapters):done()
        :done()

    infobox:tag('tr')
        :tag('th'):wikitext('Next Chapter'):done()
        :tag('td'):wikitext(next_chapter):done()
        :done()

    infobox:tag('tr')
        :tag('th'):wikitext('Previous Chapter'):done()
        :tag('td'):wikitext(prev_chapter):done()
        :done()

    return infobox
end

return p