Module:ChapterNavbox

From Candypedia
Revision as of 07:04, 26 May 2024 by 49.180.247.252 (talk) (Created page with "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;', }, {...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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()

        for _, chapter in ipairs(chapters) do
            if isChapterInVolume(chapter.number, volume.start, volume.end_) then
                navbox:tag('tr')
                    :tag('td')
                        :attr('colspan', '2')
                        :css('text-align', 'left')
                        :wikitext('* [[' .. chapter.title .. ']]')
                        :done()
                    :done()
            end
        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()

    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
            navbox:tag('tr')
                :tag('td')
                    :attr('colspan', '2')
                    :css('text-align', 'left')
                    :wikitext('* [[' .. chapter.title .. ']]')
                    :done()
                :done()
        end
    end

    return tostring(navbox)
end

return p