Module:InterviewNavigation

From JoJo's Bizarre Encyclopedia - JoJo Wiki
Revision as of 15:40, 5 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 = {}

local function load_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

    return interviews
end

local function find_prev_and_next_interviews(interviews, current_title)
    local prev, next
    local formatted_title = current_title:gsub("Interview:", "")

    for i, interview in ipairs(interviews) do
        if interview.title == formatted_title then
            prev = i > 1 and interviews[i - 1] or nil
            next = i < #interviews and 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 interviews = load_interviews()
    local prev_interview, next_interview = find_prev_and_next_interviews(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