Module:InterviewNavigation
Jump to navigation
Jump to search
Documentation for this module may be created at Module:InterviewNavigation/doc
local p = {}
local function parse_date(date_string)
local month_name, day, year = date_string:match("(%a+)%s*(%d*),%s*(%d+)")
if not year then
month_name, day, year = date_string:match("(%a+)%s+(%d+)%s+(%d+)")
end
local month_number = mw.language.getContentLanguage():formatDate("n", month_name .. " 1, " .. year)
return {
year = tonumber(year),
month = tonumber(month_number),
day = tonumber(day)
}
end
local function load_and_sort_interviews()
local content = mw.loadJsonData("JoJo Wiki:Interviews")
local interviews_table = content.interviews
-- Convert the interviews table to an array
local interviews = {}
for _, interview in pairs(interviews_table) do
table.insert(interviews, interview)
end
-- Sort interviews by date
table.sort(interviews, function(a, b)
local a_date = parse_date(a.date)
local b_date = parse_date(b.date)
if a_date.year ~= b_date.year then
return a_date.year < b_date.year
elseif a_date.month ~= b_date.month then
return a_date.month < b_date.month
else
return a_date.day and b_date.day and a_date.day < b_date.day or false
end
end)
return interviews
end
local function find_prev_and_next_interviews(sorted_interviews, current_title)
local prev, next
local formatted_title = current_title:gsub("Interview:", "")
for i, interview in ipairs(sorted_interviews) do
if interview.title == formatted_title then
prev = i > 1 and sorted_interviews[i - 1] or nil
next = i < #sorted_interviews and sorted_interviews[i + 1] or nil
break
end
end
return prev, next
end
function p.main(frame)
local current_title = frame.args[1]
local nav_type = frame.args[2]
local sorted_interviews = load_and_sort_interviews()
local prev_interview, next_interview = find_prev_and_next_interviews(sorted_interviews, current_title)
mw.log("Sorted Interviews:")
mw.logObject(sorted_interviews)
if nav_type == "prev" and prev_interview then
return prev_interview.title
elseif nav_type == "next" and next_interview then
return next_interview.title
else
return ""
end
end
return p