Module:InterviewNavigation
Jump to navigation
Jump to search
Documentation for this module may be created at Module:InterviewNavigation/doc
local p = {}
local JSON_PAGE = 'JoJo Wiki:Interviews'
function compare_dates(a, b)
if not a then return false end
if not b then return true end
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 find_previous_next_interview(interviews, current_date, direction)
local target_interview = nil
local target_date_difference = nil
for _, interview in ipairs(interviews) do
local interview_date = interview.parsed_date
local date_difference = os.difftime(os.time(interview_date), os.time(current_date))
if direction == "prev" then
if date_difference < 0 and (not target_date_difference or date_difference > target_date_difference) then
target_interview = interview
target_date_difference = date_difference
end
elseif direction == "next" then
if date_difference > 0 and (not target_date_difference or date_difference < target_date_difference) then
target_interview = interview
target_date_difference = date_difference
end
end
end
return target_interview
end
function p.main(frame)
local current_title = frame.args[1]
local direction = frame.args[2]
local original_interviews = mw.loadJsonData(JSON_PAGE)
local interviews = {}
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
table.sort(interviews, function(a, b) return compare_dates(a.parsed_date, b.parsed_date) end)
-- Debugging: output sorted interviews
mw.log("Sorted Interviews:")
for _, interview in ipairs(interviews) do
mw.log(interview.title .. " - " .. interview.date)
end
local current_date = parse_date(mw.title.new(current_title).text)
if not current_date then
return nil
end
local target_interview = find_previous_next_interview(interviews, current_date, direction)
if not target_interview then
return nil
end
-- Debugging: output current title, current date, target title, and target date
mw.log("Current title: " .. current_title)
mw.log("Current date: " .. tostring(current_date.year) .. "-" .. tostring(current_date.month) .. "-" .. tostring(current_date.day or "nil"))
mw.log("Target title: " .. target_interview.title)
mw.log("Target date: " .. target_interview.date)
return "[[Interview:" .. target_interview.title .. "|" .. target_interview.date .. "]]"
end
return p