Steven Universe Wiki

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

READ MORE

Steven Universe Wiki
No edit summary
Tag: sourceedit
m (Removing link(s))
Tag: apiedit
Line 1: Line 1:
  +
--<nowiki>
 
local p = {}
 
local p = {}
   

Revision as of 14:28, 4 July 2016

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

--<nowiki>
local p = {}

local relativeEpisode = require('Module:TabSwitch')._relativeEpisode
local title = mw.title.getCurrentTitle()
local basetitle = mw.title.new(title.baseText)
local searchTerm = ' (episode)'
local pageType
local episode

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

local function createLink(episode)
    if pageType == 'Episode' then
        if mw.title.new(episode .. searchTerm).exists then
            return '"[[' .. episode .. searchTerm .. '|' .. episode .. ']]"'
        else
            return '"[[' .. episode .. ']]"'
        end
    else
        if mw.title.new(episode .. searchTerm .. '/' .. pageType).exists then
            return '"[[' .. episode .. searchTerm .. '/' .. pageType .. '|' .. episode .. ']]"'
        else
            return '"[[' .. episode .. '/' .. pageType .. '|' .. episode .. ']]"'
        end
    end
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

local function prevLink()
    if not pageType then return '' end
    local prevEpisode = relativeEpisode(episode, -1)
    if prevEpisode == 'N/A' then
        return 'N/A'
    else
        return createLink(prevEpisode)
    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

local function nextLink()
    if not pageType then return '' end
    local nextEpisode = relativeEpisode(episode, 1)
    if nextEpisode == 'N/A' then
        return 'N/A'
    else
        return createLink(nextEpisode)
    end
end

function p.main()
    -- 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') then
            if basecontent:find('season -= -shorts') then
                searchTerm = ' (short)'
            end
            if title.text == basetitle.text then
                pageType = 'Episode'
            else
                pageType = title.subpageText
            end
        end
    end
    -- Get the current episode name based on the pagename
    -- Returns empty string if wrong pagetype
    if pageType then
        local endpos = title.text:find(searchTerm, 1, true)
        if endpos then
            episode = title.text:sub(1, endpos - 1)
        else
            episode = title.baseText
        end
    else
        episode = ''
    end
    return episode .. ';' .. prevLink() .. ';' .. nextLink()
end
        
return p