Module:InterviewNavigation

From JoJo's Bizarre Encyclopedia - JoJo Wiki
Revision as of 21:21, 2 May 2023 by Vish (talk | contribs) (Created page with "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+)') local months = { January = 1, February = 2, March = 3, April = 4, May = 5, June = 6, July = 7, August = 8, September = 9,...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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+)')
    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)
    return {year = year, month = month, day = day}
end

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

    local interviews = mw.loadData('JoJo_Wiki:Interviews').interviews

    for _, interview in ipairs(interviews) do
        interview.parsed_date = parse_date(interview.date)
    end

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

    local prevInterview, nextInterview

    for i, interview in ipairs(interviews) do
        if interview.title == currentPageTitle then
            prevInterview = interviews[i - 1]
            nextInterview = interviews[i + 1]
            break
        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