(Maybe it's better to make Next Episode N/A as well.) Tag: sourceedit |
(I think this should work as well. Not 100% sure.) Tag: sourceedit |
||
Line 15: | Line 15: | ||
if basetitle.exists then |
if basetitle.exists then |
||
local basecontent = string.lower( basetitle:getContent() ) |
local basecontent = string.lower( basetitle:getContent() ) |
||
− | if |
+ | if basecontent:find( "{{episode" ) or |
− | + | basecontent:find( "{{template:episode" ) then |
|
if title.text == basetitle.text then |
if title.text == basetitle.text then |
||
pageType = "Episode" |
pageType = "Episode" |
||
Line 31: | Line 31: | ||
if pageType then |
if pageType then |
||
− | local endpos = |
+ | local endpos = title.text:find( "(episode)" ) |
if endpos then |
if endpos then |
||
− | episode = |
+ | episode = title.text:sub( 1, endpos - 3 ) |
else |
else |
||
episode = title.baseText |
episode = title.baseText |
Revision as of 16:00, 11 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
local basecontent = string.lower( basetitle:getContent() )
if basecontent:find( "{{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 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( 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( frame, 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( frame, episode, 1 )
if nextEpisode == "N/A" then
return "N/A"
else
return createLink( nextEpisode )
end
else
return ""
end
end
return p