Module: GOT episode

From A Wiki of Ice and Fire
Jump to: navigation, search

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

local data = require('Module:GOT episode/data')

local p = {}

function p.title(frame)
    local args = frame.args
    local season = tonumber(args[1])
    local episode = tonumber(args[2])
    
    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 season = tonumber(args[1])
    local episode = tonumber(args[2])
    
    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 season = tonumber(args[1])
    local episode = tonumber(args[2])
    
    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