Module:BCIChapterNavbox

Revision as of 14:46, 26 May 2024 by NeonWabbit (talk | contribs) (Created page with "local p = {} -- Define the 5-year ranges and styles local groups = { { name = '2010–2014', start_year = 2010, end_year = 2014, style = 'background:#707ebd;color:white;', group = 'group1', list = 'list1', }, { name = '2015–2019', start_year = 2015, end_year = 2019, style = 'background:#912f28;color:white;', group = 'group2', list = 'list2', }, {...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

local p = {}

-- Define the 5-year ranges and styles
local groups = {
    {
        name = '2010–2014',
        start_year = 2010,
        end_year = 2014,
        style = 'background:#707ebd;color:white;',
        group = 'group1',
        list = 'list1',
    },
    {
        name = '2015–2019',
        start_year = 2015,
        end_year = 2019,
        style = 'background:#912f28;color:white;',
        group = 'group2',
        list = 'list2',
    },
    {
        name = '2020–2024',
        start_year = 2020,
        end_year = 2024,
        style = 'background:#2b8842;color:white;',
        group = 'group3',
        list = 'list3',
    }
}

-- Function to check if a chapter is within a year range
local function isChapterInYearRange(chapter_year, start_year, end_year)
    local year = tonumber(chapter_year)
    return year and year >= start_year and year <= end_year
end

function p.generateNavbox(frame)
    local bciChaptersModule = require('Module:BCIChapters')
    local chapters = bciChaptersModule.getChaptersList()

    -- Determine the last publish year dynamically
    local last_publish_year = 0
    for _, chapter in ipairs(chapters) do
        local year = tonumber(string.sub(chapter.date, 1, 4))
        if year and year > last_publish_year then
            last_publish_year = year
        end
    end

    -- Update the last group end year to the last publish year
    if last_publish_year > 0 then
        for _, group in ipairs(groups) do
            if group.start_year == 2020 then
                group.end_year = last_publish_year
                group.name = '2020–' .. last_publish_year
                break
            end
        end
    end

    local navboxParams = {
        bodyclass = 'hlist',
        name = 'BCI chapters',
        title = "''[[Bittersweet Candy Bowl]]'' BCI Chapters",
        titlestyle = '',
        image = '',
        above = '',
        below = ''
    }

    -- Add groups and lists
    for _, group in ipairs(groups) do
        navboxParams[group.group .. 'style'] = group.style
        navboxParams[group.group] = group.name

        local list = {}
        for _, chapter in ipairs(chapters) do
            local year = tonumber(string.sub(chapter.date, 1, 4))
            if isChapterInYearRange(year, group.start_year, group.end_year) then
                local encodedTitle = mw.uri.encode(chapter.title, 'PATH')
                table.insert(list, '* [[' .. encodedTitle .. '|' .. chapter.title .. ']]')
            end
        end
        navboxParams[group.list] = table.concat(list, '\n')
    end

    return frame:expandTemplate{ title = 'Navbox', args = navboxParams }
end

return p