Module:InterviewNavigation: Difference between revisions
Jump to navigation
Jump to search
Content deleted Content added
No edit summary |
Tag: Undo |
||
(14 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
local function parse_date(date_string) |
local function parse_date(date_string) |
||
local month_name, day, year = date_string:match("(%a+)%s*(%d*),%s*(%d+)") |
local month_name, day, year = date_string:match("(%a+)%s*(%d*),%s*(%d+)") |
||
⚫ | |||
⚫ | |||
⚫ | |||
month_name, year = date_string:match("(%a+)%s*(%d+)") |
|||
day = nil |
|||
⚫ | |||
⚫ | |||
if not month_name then |
|||
year = date_string:match("(%d+)") |
|||
month_name = nil |
|||
day = nil |
|||
⚫ | |||
local month_number |
|||
if month_name then |
|||
⚫ | |||
end |
|||
return { |
return { |
||
Line 10: | Line 25: | ||
day = tonumber(day) |
day = tonumber(day) |
||
} |
} |
||
⚫ | |||
local function compare_dates(a, b) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end |
end |
||
local function load_and_sort_interviews() |
local function load_and_sort_interviews() |
||
local content = mw.loadJsonData("JoJo Wiki:Interviews") |
local content = mw.loadJsonData("JoJo Wiki:Interviews") |
||
local |
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) |
|||
⚫ | |||
⚫ | |||
if a_date.year ~= b_date.year then |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
else |
|||
return false |
|||
end |
|||
end) |
|||
return interviews |
return interviews |
||
Line 38: | Line 58: | ||
local function find_prev_and_next_interviews(sorted_interviews, current_title) |
local function find_prev_and_next_interviews(sorted_interviews, current_title) |
||
local prev, next |
local prev, next |
||
local formatted_title = current_title:gsub("Interview:", "") |
|||
for i, interview in ipairs(sorted_interviews) do |
for i, interview in ipairs(sorted_interviews) do |
||
if interview.title == |
if interview.title == formatted_title then |
||
prev = i > 1 and sorted_interviews[i - 1] or nil |
prev = i > 1 and sorted_interviews[i - 1] or nil |
||
next = i < #sorted_interviews and sorted_interviews[i + 1] or nil |
next = i < #sorted_interviews and sorted_interviews[i + 1] or nil |
||
Line 56: | Line 77: | ||
local sorted_interviews = load_and_sort_interviews() |
local sorted_interviews = load_and_sort_interviews() |
||
local prev_interview, next_interview = find_prev_and_next_interviews(sorted_interviews, current_title) |
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 |
if nav_type == "prev" and prev_interview then |
Latest revision as of 15:41, 5 May 2023
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, year = date_string:match("(%a+)%s*(%d+)")
day = nil
end
if not month_name then
year = date_string:match("(%d+)")
month_name = nil
day = nil
end
local month_number
if month_name then
month_number = mw.language.getContentLanguage():formatDate("n", month_name .. " 1, " .. year)
end
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 and b_date.month and a_date.month ~= b_date.month then
return a_date.month < b_date.month
elseif a_date.day and b_date.day and a_date.day ~= b_date.day then
return a_date.day < b_date.day
else
return 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)
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