Module:InterviewNavbox

From JoJo's Bizarre Encyclopedia - JoJo Wiki
Revision as of 10:11, 6 May 2023 by Vish (talk | contribs)
Jump to navigation Jump to search

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

local p = {}
local dateParser = require('Module:InterviewUtils')

local function getDateRange(year)
    if year >= 1981 and year <= 1995 then
        return 1
    elseif year >= 1996 and year <= 2005 then
        return 2
    elseif year >= 2006 and year <= 2015 then
        return 3
    elseif year >= 2016 and year <= 2020 then
        return 4
    elseif year >= 2021 and year <= 2025 then
        return 5
    elseif year >= 2026 and year <= 2030 then
        return 6
    end
    
    return nil
end

function p.main(frame)
    local dateRangeInput = tonumber(frame.args[1]) or 1
    local content = mw.loadJsonData('JoJo Wiki:Interviews')
    local interviews_table = content.interviews
    local list = {}
    local titleGroup = {}

    -- Group interviews by title without date
    for _, interview in ipairs(interviews_table) do
        local year = dateParser.parse(interview.date)
        local titleWithoutDate = string.match(interview.title, '^(.-)%s*%(.-%)$') or interview.title

        titleGroup[titleWithoutDate] = titleGroup[titleWithoutDate] or {}
        table.insert(titleGroup[titleWithoutDate], {interview = interview, year = year})
    end

    -- Sort interviews by date within each title group
    for _, interviews in pairs(titleGroup) do
        table.sort(interviews, function(a, b) return a.year < b.year end)
    end

    -- Generate list
    for titleWithoutDate, interviews in pairs(titleGroup) do
        local hasMultipleInterviews = #interviews > 1

        for _, interviewData in ipairs(interviews) do
            local interview = interviewData.interview
            local year = interviewData.year
            local dateRange = getDateRange(year)

            if dateRange == dateRangeInput then
                local itemTitle = hasMultipleInterviews and interview.title or titleWithoutDate
                local item = frame:expandTemplate{title = 'INT', args = {interview.title, itemTitle}}
                table.insert(list, '*' .. item)
            end
        end
    end

    return table.concat(list, '\n')
end

return p