Module:EpisodeLinks

-- local p = {}

local relativeEpisode = require('Module:TabSwitch')._relativeEpisode local title = mw.title.getCurrentTitle local basetitle = mw.title.new(title.baseText) local episodeTerm = ' (episode)' local shortTerm = ' (short)' 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 .. episodeTerm).exists then return '"' .. episode .. '"' else return '"' .. episode .. '"' end if mw.title.new(episode .. shortTerm).exists then return '"' .. episode .. '"' else return '"' .. episode .. '"' end else if mw.title.new(episode .. episodeTerm .. '/' .. pageType).exists then return '"' .. episode .. '"' else return '"' .. episode .. '"' end if mw.title.new(episode .. shortTerm .. '/' .. pageType).exists then return '"' .. episode .. '"' else return '"' .. 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 episodeTerm = ' (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(episodeTerm, 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