Module:BCIChapterNavbox

Revision as of 03:11, 27 May 2024 by NeonWabbit (talk | contribs)

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,
        group = 'group1',
        list = 'list1',
    },
    {
        name = '2015–2019',
        start_year = 2015,
        end_year = 2019,
        group = 'group2',
        list = 'list2',
    },
    {
        name = '2020–2024',
        start_year = 2020,
        end_year = 2024,
        group = 'group3',
        list = 'list3',
    }
}

-- Special cases mapping: Actual title to MediaWiki-safe title
local special_cases = {
    ["Augustus, You Jerk #2"] = "Augustus, You Jerk 2"
}

-- 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 to get the MediaWiki-safe title
function p.getSafeTitle(frame)
    local title = frame.args and frame.args.title or frame
    return special_cases[title] or title
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 Club International]] Chapters",
        titlestyle = '',
        image = '',
        above = '',
        below = ''
    }

    -- Add groups and lists
    for _, group in ipairs(groups) do
        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 safeTitle = p.getSafeTitle(chapter.title)
                local encodedTitle = mw.uri.encode(safeTitle, '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