Module:InterviewNavbox

From JoJo's Bizarre Encyclopedia - JoJo Wiki
Revision as of 06:34, 28 February 2024 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)
    local ranges = {
        {1981, 1995, 1},
        {1996, 2005, 2},
        {2006, 2015, 3},
        {2016, 2020, 4},
        {2021, 2025, 5},
        {2026, 2030, 6},
    }
    for _, range in ipairs(ranges) do
        if year >= range[1] and year <= range[2] then
            return range[3]
        end
    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

    -- Generate list
    for _, interview in ipairs(interviews_table) do
        local year = dateParser.parse(interview.date)
        local dateRange = getDateRange(year)

        if dateRange == dateRangeInput then
            local titleWithoutDate = string.match(interview.title, '^(.-)%s*%(.-%)$') or interview.title
            local interviews = titleGroup[titleWithoutDate]
            local hasMultipleInterviews = #interviews > 1
            local itemTitle = hasMultipleInterviews and interview.title or titleWithoutDate
            local item = frame:expandTemplate{title = 'INT', args = {interview.title, itemTitle}}
            table.insert(list, '*' .. item)
        end
    end

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


return p