Steven Universe Wiki

Spoilers will be present! Please browse at your own risk.

READ MORE

Steven Universe Wiki
No edit summary
Tag: sourceedit
No edit summary
Tag: sourceedit
Line 47: Line 47:
 
-- and diff = -1 will return the previous episode
 
-- and diff = -1 will return the previous episode
   
local function relativeEpisode( episode, diff )
+
local function relativeEpisode( frame, episode, diff )
 
local epNum = frame:expandTemplate{ title = "TabSwitch", args = { episode } }
 
local epNum = frame:expandTemplate{ title = "TabSwitch", args = { episode } }
 
-- check if it is a number
 
-- check if it is a number
Line 88: Line 88:
 
function p.prevLink( frame )
 
function p.prevLink( frame )
 
if pageType then
 
if pageType then
local prevEpisode = relativeEpisode( episode, -1 )
+
local prevEpisode = relativeEpisode( frame, episode, -1 )
 
if prevEpisode == "N/A" then
 
if prevEpisode == "N/A" then
 
return "N/A"
 
return "N/A"
Line 105: Line 105:
 
function p.nextLink( frame )
 
function p.nextLink( frame )
 
if pageType then
 
if pageType then
local nextEpisode = relativeEpisode( episode, 1 )
+
local nextEpisode = relativeEpisode( frame, episode, 1 )
 
if nextEpisode == "N/A" then
 
if nextEpisode == "N/A" then
 
return "TBA"
 
return "TBA"

Revision as of 11:33, 9 July 2015

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

local p = {}

local title = mw.title.getCurrentTitle()
local basetitle = mw.title.new( title.baseText )
local pageType

-- Check if it is an episode (sub)page
-- by checking if the base page has the Episode template
--
-- pageType =
-- "Episode" on episode pages,
-- "Gallery" on gallery pages,
-- "Transcript" on transcript pages

if basetitle.exists then
    if string.find( basetitle:getContent(), "{{Episode" ) or
       string.find( basetitle:getContent(), "{{Template:Episode") then
        if title.text == basetitle.text then
            pageType = "Episode";
        else
            pageType = title.subpageText
        end
    end
end

local episode

-- Get the current episode name based on the pagename
-- Returns empty string if wrong pagetype

if pageType then
    local endpos = string.find( title.text, "(episode)" )
    if endpos then
        episode = string.sub( title.text, 1, endpos )
    else
        episode = title.baseText
    end
else
    episode = ""
end

-- Get the relative episode based on the episodeName given
-- Diff is the amount of episodes between the given episode
-- and the episode that should be returned
--
-- For example, diff = 1 will return the next episode
-- and diff = -1 will return the previous episode

local function relativeEpisode( frame, episode, diff )
    local epNum = frame:expandTemplate{ title = "TabSwitch", args = { episode } }
    -- check if it is a number
    if tonumber(epNum) then
        return frame:expandTemplate{ title = "TabSwitch", args = { epNum + diff } }
    else
        return "N/A"
    end
end

-- Creates the actual link for p.nextLink and p.prevLink methods

local function createLink( episode )
    if pageType == "Episode" then
        if mw.title.new( nextEpisode .. " (episode)" ).exists then
            return "[[" .. episode .. " (episode)|" .. episode .. "]]"
        else
            return "[[" .. episode .. "]]"
        end
    else
        if mw.title.new( episode .. " (episode)/" .. pageType ).exists then
            return "[[" .. episode .. " (episode)/" .. pageType .. "|" .. episode .. "]]"
        else
            return "[[" .. episode .. "/" .. pageType .. "|" .. episode .. "]]"
        end
    end
end

-- Get the current episode name based on the pagename
-- Returns empty string if wrong pagetype

function p.episode( frame )
    return episode
end

-- Creates a link to the previous episode/gallery/transcript page
-- Returns empty string if wrong pagetype
-- Returns "N/A" if the next episode is not known

function p.prevLink( frame )
    if pageType then
        local prevEpisode = relativeEpisode( frame, episode, -1 )
        if prevEpisode == "N/A" then
            return "N/A"
        else
            createLink( prevEpisode )
        end
    else
        return ""
    end
end

-- Creates a link to the next episode/gallery/transcript page
-- Returns empty string if wrong pagetype
-- Returns "TBA" if the next episode is not known

function p.nextLink( frame )
    if pageType then
        local nextEpisode = relativeEpisode( frame, episode, 1 )
        if nextEpisode == "N/A" then
            return "TBA"
        else
            createLink( nextEpisode )
        end
    else
        return ""
    end
end
        
return p