Module:InterviewNavigation

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

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

local p = {}

function compare_dates(a, b)
    local date1 = os.time{year = a.year, month = a.month, day = a.day or 1}
    local date2 = os.time{year = b.year, month = b.month, day = b.day or 1}
    return date1 < date2
end

function parse_date(date_str)
    local month_name, day, year = date_str:match('(%a+)%s*(%d*)%s*,%s*(%d+)')
    if not month_name then
        return nil
    end

    local months = {
        January = 1, February = 2, March = 3, April = 4, May = 5, June = 6,
        July = 7, August = 8, September = 9, October = 10, November = 11, December = 12
    }

    local month = months[month_name]
    day = tonumber(day) or nil
    year = tonumber(year)

    if not month or not year then
        return nil
    end

    return {year = year, month = month, day = day}
end


function p.main(frame)
    local currentPageTitle = frame.args[1]
    local action = frame.args[2] or ""

    local original_interviews = mw.loadJsonData('JoJo_Wiki:Interviews').interviews
    local interviews = {}

    for _, interview in ipairs(original_interviews) do
        local interview_copy = {
            title = interview.title,
            date = interview.date,
            parsed_date = parse_date(interview.date)
        }
        table.insert(interviews, interview_copy)
    end

    table.sort(interviews, function(a, b) return compare_dates(a.parsed_date, b.parsed_date) end)

    local prevInterview, nextInterview

	for _, interview in ipairs(original_interviews) do
	    local parsed_date = parse_date(interview.date)
	    if parsed_date then
	        local interview_copy = {
	            title = interview.title,
	            date = interview.date,
	            parsed_date = parsed_date
	        }
	        table.insert(interviews, interview_copy)
	    end
	end

    if action == "prev" and prevInterview then
        return "[[" .. prevInterview.title .. "|Prev]]"
    elseif action == "next" and nextInterview then
        return "[[" .. nextInterview.title .. "|Next]]"
    end

    return ""
end

return p