Module:ChapterNavbox

From Candypedia
Revision as of 10:28, 26 May 2024 by 165.225.232.184 (talk)

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

local p = {}

-- Define the volume ranges and styles
local volumes = {
    {
        name = 'Volume One',
        start = 1,
        end_ = 46,
        style = 'background:#707ebd;color:white;',
    },
    {
        name = 'Volume Two',
        start = 47,
        end_ = 58,
        style = 'background:#912f28;color:white;',
    },
    {
        name = 'Volume Three',
        start = 59,
        end_ = 69.1,
        style = 'background:#2b8842;color:white;',
    },
    {
        name = 'Volume Four',
        start = 70,
        end_ = 80,
        style = 'background:#4589c8;color:white;',
    },
    {
        name = 'Volume Five',
        start = 81,
        end_ = 88,
        style = 'background:#aa715e;color:white;',
    },
    {
        name = 'Volume Six',
        start = 89,
        end_ = 96.1,
        style = 'background:#f27191;color:white;',
    },
    {
        name = 'Volume Seven',
        start = 97,
        end_ = 110,
        style = 'background:#efb63b;color:white;',
    },
}

-- Function to check if a chapter is within a volume range
local function isChapterInVolume(chapter_no, start, end_)
    local number = tonumber(chapter_no)
    return number and number >= start and number <= end_
end

function p.generateNavbox()
    local chaptersModule = require('Module:Chapters')
    local chapters = chaptersModule.getChaptersList()

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

    -- Add title row
    navbox:tag('tr')
        :tag('th')
            :attr('colspan', '2')
            :css('text-align', 'center')
            :css('background-color', '#707ebd')
            :css('color', 'white')
            :wikitext("''[[Bittersweet Candy Bowl]]'' Chapters")
            :done()
        :done()

    -- Add chapters to the appropriate volume groups
    for _, volume in ipairs(volumes) do
        navbox:tag('tr')
            :tag('th')
                :attr('colspan', '2')
                :css('background-color', volume.style:match('background:[^;]+'))
                :css('color', volume.style:match('color:[^;]+'))
                :wikitext(volume.name)
                :done()
            :done()

        local list = {}
        for _, chapter in ipairs(chapters) do
            if isChapterInVolume(chapter.number, volume.start, volume.end_) then
                table.insert(list, '[[' .. chapter.title .. ']]')
            end
        end
        if #list > 0 then
            navbox:tag('tr')
                :tag('td')
                    :attr('colspan', '2')
                    :css('text-align', 'left')
                    :wikitext('* ' .. table.concat(list, ' · '))
                    :done()
                :done()
        end
    end

    -- Add group for chapters not in print
    navbox:tag('tr')
        :tag('th')
            :attr('colspan', '2')
            :css('background-color', '#707ebd')
            :css('color', 'white')
            :wikitext('Chapters Not Yet in Print')
            :done()
        :done()

    local list_not_in_print = {}
    for _, chapter in ipairs(chapters) do
        local number = tonumber(chapter.number)
        local in_volume = false
        for _, volume in ipairs(volumes) do
            if isChapterInVolume(chapter.number, volume.start, volume.end_) then
                in_volume = true
                break
            end
        end

        if not in_volume then
            table.insert(list_not_in_print, '[[' .. chapter.title .. ']]')
        end
    end
    if #list_not_in_print > 0 then
        navbox:tag('tr')
            :tag('td')
                :attr('colspan', '2')
                :css('text-align', 'left')
                :wikitext('* ' .. table.concat(list_not_in_print, ' · '))
                :done()
            :done()
    end

    return tostring(navbox)
end

return p