Module:InterviewNavbox

From JoJo's Bizarre Encyclopedia - JoJo Wiki
Revision as of 06:42, 28 February 2024 by Vish (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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

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

-- Initialize caches
local dateRangeCache = {}
local parsedDateCache = {}

local function getDateRange(year)
    -- Check if the year's range is cached
    if not dateRangeCache[year] then
        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
                dateRangeCache[year] = range[3] -- Cache the result
                break
            end
        end
    end
    return dateRangeCache[year] or 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
	-- Use caching for date parsing
	for _, interview in ipairs(interviews_table) do
	    local date = interview.date
	    -- Check if the parsed date is cached
	    if not parsedDateCache[date] then
	        parsedDateCache[date] = dateParser.parse(date) -- Cache the parsed date
	    end
	    local year = parsedDateCache[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