Module: TV episode

From A Wiki of Ice and Fire
Revision as of 17:39, 31 August 2022 by Abjiklam (talk | contribs) (Created page with "local p = {} function p.title(frame) local args = frame.args local data = require('Module:TV episode/' .. args[1]) local season = tonumber(args[2]) local epis...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Documentation for this module may be created at Module:TV episode/doc

local p = {}

function p.title(frame)
    local args = frame.args
    local data = require('Module:TV episode/' .. args[1])
    local season = tonumber(args[2])
    local episode = tonumber(args[3])
    
    local title = data[season][episode].title
    if title ~= nil then
        return title
    else
        error("Episode doesn't exist")
    end
end

function p.Prev(frame)
    local args = frame.args
    local data = require('Module:TV episode/' .. args[1])
    local season = tonumber(args[2])
    local episode = tonumber(args[3])
    
    local prev_season = data[season-1]
    local last_episode = prev_season and prev_season[#prev_season]
    
    local prev_episode = data[season][episode-1] or last_episode
    
    if prev_episode then
        local title = prev_episode.title
        local link  = prev_episode.link or prev_episode.title
        return '"[[' .. link .. '|' .. title .. ']]"'
    else
        return nil
    end
end

function p.Next(frame)
    local args = frame.args
    local data = require('Module:TV episode/' .. args[1])
    local season = tonumber(args[2])
    local episode = tonumber(args[3])
    
    local next_season = data[season+1]
    local first_episode = next_season and next_season[1]
    
    local next_episode = data[season][episode+1] or first_episode
    
    if next_episode then
        local title = next_episode.title
        local link  = next_episode.link or next_episode.title
        return '"[[' .. link .. '|' .. title .. ']]"'
    else
        return nil
    end
end

return p