Module:Chapters: Difference between revisions

From Candypedia
No edit summary
No edit summary
Line 69: Line 69:
     local image = frame.args.image
     local image = frame.args.image
     local caption = frame.args.caption
     local caption = frame.args.caption
    -- Debug: Log the caption value
    mw.log("Caption: " .. (caption or "No caption"))


     local chapter_no = p.getChapterDetails(chapterTitle, "number") or "N/A"
     local chapter_no = p.getChapterDetails(chapterTitle, "number") or "N/A"
Line 80: Line 77:
     local prev_chapter = p.getPreviousChapter(chapterTitle) or "N/A"
     local prev_chapter = p.getPreviousChapter(chapterTitle) or "N/A"


     local infobox = mw.html.create('table')
     local wikitext = ""
    infobox:addClass('infobox')
 
    wikitext = wikitext .. '{| class="infobox"\n'


     -- Add title as heading
     -- Add title as heading
     infobox:tag('tr')
     wikitext = wikitext .. '|-\n! colspan="2" style="text-align: center; font-size: 125%; padding: 5px;" | ' .. chapterTitle .. '\n'
        :tag('th')
            :attr('colspan', '2')
            :css('text-align', 'center')
            :css('font-size', '125%')
            :css('padding', '5px')
            :wikitext(chapterTitle)
            :done()
        :done()
      
      
     -- Optionally add image with standard size and caption
     -- Optionally add image with standard size and caption
     if image and image ~= "" then
     if image and image ~= "" then
         infobox:tag('tr')
         wikitext = wikitext .. '|-\n| colspan="2" style="text-align: center;" | [[File:' .. image .. '|250px]]\n'
            :tag('td')
                :attr('colspan', '2')
                :css('text-align', 'center')
                :wikitext('[[File:' .. image .. '|250px]]')
                :done()
            :done()
 
        -- Add caption below image
         if caption and caption ~= "" then
         if caption and caption ~= "" then
             infobox:tag('tr')
             wikitext = wikitext .. '|-\n| colspan="2" style="text-align: center; font-size: 90%; padding: 5px;" | ' .. caption .. '\n'
                :tag('td')
                    :attr('colspan', '2')
                    :css('text-align', 'center')
                    :css('font-size', '90%')
                    :css('padding', '5px')
                    :wikitext(caption)
                    :done()
                :done()
         end
         end
     end
     end
Line 120: Line 94:
     -- Add chapter number and total chapters on the same line
     -- Add chapter number and total chapters on the same line
     local chapter_info = string.format("%s of %s [[Template:ChapterList|(list of chapters)]]", chapter_no, total_chapters)
     local chapter_info = string.format("%s of %s [[Template:ChapterList|(list of chapters)]]", chapter_no, total_chapters)
     infobox:tag('tr')
     wikitext = wikitext .. '|-\n! Chapter\n| ' .. chapter_info .. '\n'
        :tag('th'):wikitext('Chapter'):done()
        :tag('td'):wikitext(chapter_info):done()
        :done()


     infobox:tag('tr')
     wikitext = wikitext .. '|-\n! Date\n| ' .. p.formatDate(date) .. '\n'
        :tag('th'):wikitext('Date'):done()
        :tag('td'):wikitext(p.formatDate(date)):done()
        :done()


     infobox:tag('tr')
     wikitext = wikitext .. '|-\n! Page count\n| ' .. pagecount .. '\n'
        :tag('th'):wikitext('Page count'):done()
        :tag('td'):wikitext(pagecount):done()
        :done()


     -- Add chapter navigation heading
     -- Add chapter navigation heading
     infobox:tag('tr')
     wikitext = wikitext .. '|-\n! colspan="2" style="text-align: center; font-size: 110%; padding: 5px; background-color: #f0f0f0;" | Chapter navigation\n'
        :tag('th')
            :attr('colspan', '2')
            :css('text-align', 'center')
            :css('font-size', '110%')
            :css('padding', '5px')
            :css('background-color', '#f0f0f0')
            :wikitext('Chapter navigation')
            :done()
        :done()


     -- Add chapter navigation links in separate columns
     -- Add chapter navigation links in separate columns
     local nav_row = infobox:tag('tr')
     local prev_link = prev_chapter ~= "N/A" and string.format("[[%s:%s|← %s]]", mw.title.getCurrentTitle().nsText, prev_chapter, prev_chapter) or "← Previous"
    nav_row:tag('td')
     local next_link = next_chapter ~= "N/A" and string.format("[[%s:%s|%s →]]", mw.title.getCurrentTitle().nsText, next_chapter, next_chapter) or "Next →"
        :css('text-align', 'left')
    wikitext = wikitext .. '|-\n| style="text-align: left;" | ' .. prev_link .. '\n| style="text-align: right;" | ' .. next_link .. '\n'
        :wikitext(prev_chapter ~= "N/A" and string.format("[[%s:%s|← %s]]", mw.title.getCurrentTitle().nsText, prev_chapter, prev_chapter) or "← Previous")
 
        :done()
     wikitext = wikitext .. '|}\n'
     nav_row:tag('td')
        :css('text-align', 'right')
        :wikitext(next_chapter ~= "N/A" and string.format("[[%s:%s|%s →]]", mw.title.getCurrentTitle().nsText, next_chapter, next_chapter) or "Next →")
        :done()
     nav_row:done()


     -- Return the complete wikitext
     -- Return the complete wikitext
     return frame:preprocess(tostring(infobox))
     return frame:preprocess(wikitext)
end
end


return p
return p

Revision as of 04:03, 21 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

    return chapters
end

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

function p.getTotalChapters()
    local totalChapters = #p.getChaptersList()
    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
            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
            return chapters[i - 1].title
        end
    end
    return nil
end

function p.formatDate(date)
    local year, month, day = string.match(date, "(%d+)%-(%d+)%-(%d+)")
    if year and month and day then
        return string.format("{{Start date|%s|%s|%s}}", year, month, day)
    else
        return date
    end
end

function p.infoboxChapter(frame)
    local chapterTitle = frame.args.chapter_title or mw.title.getCurrentTitle().text
    local image = frame.args.image
    local caption = frame.args.caption

    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 wikitext = ""

    wikitext = wikitext .. '{| class="infobox"\n'

    -- Add title as heading
    wikitext = wikitext .. '|-\n! colspan="2" style="text-align: center; font-size: 125%; padding: 5px;" | ' .. chapterTitle .. '\n'
    
    -- Optionally add image with standard size and caption
    if image and image ~= "" then
        wikitext = wikitext .. '|-\n| colspan="2" style="text-align: center;" | [[File:' .. image .. '|250px]]\n'
        if caption and caption ~= "" then
            wikitext = wikitext .. '|-\n| colspan="2" style="text-align: center; font-size: 90%; padding: 5px;" | ' .. caption .. '\n'
        end
    end

    -- Add chapter number and total chapters on the same line
    local chapter_info = string.format("%s of %s [[Template:ChapterList|(list of chapters)]]", chapter_no, total_chapters)
    wikitext = wikitext .. '|-\n! Chapter\n| ' .. chapter_info .. '\n'

    wikitext = wikitext .. '|-\n! Date\n| ' .. p.formatDate(date) .. '\n'

    wikitext = wikitext .. '|-\n! Page count\n| ' .. pagecount .. '\n'

    -- Add chapter navigation heading
    wikitext = wikitext .. '|-\n! colspan="2" style="text-align: center; font-size: 110%; padding: 5px; background-color: #f0f0f0;" | Chapter navigation\n'

    -- Add chapter navigation links in separate columns
    local prev_link = prev_chapter ~= "N/A" and string.format("[[%s:%s|← %s]]", mw.title.getCurrentTitle().nsText, prev_chapter, prev_chapter) or "← Previous"
    local next_link = next_chapter ~= "N/A" and string.format("[[%s:%s|%s →]]", mw.title.getCurrentTitle().nsText, next_chapter, next_chapter) or "Next →"
    wikitext = wikitext .. '|-\n| style="text-align: left;" | ' .. prev_link .. '\n| style="text-align: right;" | ' .. next_link .. '\n'

    wikitext = wikitext .. '|}\n'

    -- Return the complete wikitext
    return frame:preprocess(wikitext)
end

return p