Module:EpisodeLinks

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 .. "" else return "" .. episode .. "" end else if mw.title.new( episode .. " (episode)/" .. pageType ).exists then return "" .. episode .. "" else return "" .. 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