Steven Universe Wiki

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

READ MORE

Steven Universe Wiki
(I think this should work as well. Not 100% sure.)
Tag: sourceedit
(This should work. Will undo if it doesn't.)
Tag: sourceedit
Line 1: Line 1:
 
local p = {}
 
local p = {}
   
  +
local tabSwitch = require( "Module:TabSwitch" ).main
 
local title = mw.title.getCurrentTitle()
 
local title = mw.title.getCurrentTitle()
 
local basetitle = mw.title.new( title.baseText )
 
local basetitle = mw.title.new( title.baseText )
Line 16: Line 17:
 
local basecontent = string.lower( basetitle:getContent() )
 
local basecontent = string.lower( basetitle:getContent() )
 
if basecontent:find( "{{episode" ) or
 
if basecontent:find( "{{episode" ) or
basecontent:find( "{{template:episode" ) then
+
basecontent:find( "{{template:episode" ) or
  +
basecontent:find( "{{template: episode" ) then
 
if title.text == basetitle.text then
 
if title.text == basetitle.text then
 
pageType = "Episode"
 
pageType = "Episode"
Line 48: Line 50:
 
-- and diff = -1 will return the previous episode
 
-- and diff = -1 will return the previous episode
   
function relativeEpisode( frame, episode, diff )
+
function p.relativeEpisode( episode, diff )
local epNum = frame:expandTemplate{ title = "TabSwitch", args = { episode } }
+
local epNum = tabSwitch( episode )
 
-- check if it is a number
 
-- check if it is a number
 
if tonumber(epNum) then
 
if tonumber(epNum) then
return frame:expandTemplate{ title = "TabSwitch", args = { epNum + diff } }
+
return tabSwitch( epNum + diff )
 
else
 
else
 
return "N/A"
 
return "N/A"
Line 89: Line 91:
 
function p.prevLink( frame )
 
function p.prevLink( frame )
 
if pageType then
 
if pageType then
local prevEpisode = relativeEpisode( frame, episode, -1 )
+
local prevEpisode = relativeEpisode( episode, -1 )
 
if prevEpisode == "N/A" then
 
if prevEpisode == "N/A" then
 
return "N/A"
 
return "N/A"
Line 106: Line 108:
 
function p.nextLink( frame )
 
function p.nextLink( frame )
 
if pageType then
 
if pageType then
local nextEpisode = relativeEpisode( frame, episode, 1 )
+
local nextEpisode = relativeEpisode( episode, 1 )
 
if nextEpisode == "N/A" then
 
if nextEpisode == "N/A" then
 
return "N/A"
 
return "N/A"

Revision as of 20:48, 13 July 2015

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

local p = {}

local tabSwitch = require( "Module:TabSwitch" ).main
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
    local basecontent = string.lower( basetitle:getContent() )
    if basecontent:find( "{{episode" ) or
       basecontent:find( "{{template:episode" ) or
       basecontent:find( "{{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 = title.text:find( "(episode)" )
    if endpos then
        episode = title.text:sub( 1, endpos - 3 )
    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

function p.relativeEpisode( episode, diff )
    local epNum = tabSwitch( episode )
    -- check if it is a number
    if tonumber(epNum) then
        return tabSwitch( 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( episode .. " (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 previous episode is not known

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

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

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