(should have seen that) Tag: sourceedit |
No edit summary |
||
(47 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
+ | -- <nowiki> |
||
+ | |||
local p = {} |
local p = {} |
||
+ | local relativeEpisode = require('Module:TabSwitch')._relativeEpisode |
||
local title = mw.title.getCurrentTitle() |
local title = mw.title.getCurrentTitle() |
||
− | local basetitle = mw.title.new( title.baseText ) |
||
− | local pageType |
||
+ | -- Removes content between parentheses and /Transcript or /Gallery parts in titles |
||
− | -- Check if it is an episode (sub)page |
||
⚫ | |||
− | -- by checking if the base page has the Episode template |
||
+ | local function readableTitle(_title) |
||
− | -- |
||
+ | return _title:gsub(' %b()', ''):gsub('/%a+', '') |
||
− | -- pageType = |
||
⚫ | |||
− | -- "Episode" on episode pages, |
||
− | -- "Gallery" on gallery pages, |
||
− | -- "Transcript" on transcript pages |
||
+ | -- Removes any subpage parts in the title supplied |
||
− | if basetitle.exists then |
||
⚫ | |||
− | if string.find( basetitle:getContent(), "{{Episode" ) or |
||
+ | local function baseTitle(_title) |
||
− | string.find( basetitle:getContent(), "{{Template:Episode") then |
||
+ | return _title:gsub('/%a+', '') |
||
⚫ | |||
− | pageType = "Episode"; |
||
− | else |
||
− | pageType = title.subpageText |
||
− | end |
||
− | end |
||
end |
end |
||
⚫ | |||
+ | -- Creates a wikilink with the arguments provided |
||
− | -- Get the current episode name based on the pagename |
||
− | -- Returns |
+ | -- Returns a string |
+ | local function createLink(page, displayText) |
||
− | |||
+ | if displayText ~= page and displayText then |
||
− | if pageType then |
||
⚫ | |||
− | local endpos = string.find( title.text, "(episode)" ) |
||
⚫ | |||
− | episode = string.sub( title.text, 1, endpos ) |
||
else |
else |
||
− | + | return '[[' .. page .. ']]' |
|
end |
end |
||
− | else |
||
− | episode = "" |
||
end |
end |
||
+ | -- Utility function to provide easier way of calling createLink |
||
− | -- Get the relative episode based on the episodeName given |
||
+ | -- Returns a string |
||
− | -- Diff is the amount of episodes between the given episode |
||
⚫ | |||
− | -- and the episode that should be returned |
||
+ | return createLink(episode, readableTitle(episode)) |
||
− | -- |
||
⚫ | |||
− | -- For example, diff = 1 will return the next episode |
||
− | -- and diff = -1 will return the previous episode |
||
+ | -- Restores /Gallery or /Transcript parts in page name depending on second parameter subpage text |
||
⚫ | |||
+ | -- Returns a string |
||
− | local epNum = frame:expandTemplate{ title = "TabSwitch", args = { episode } } |
||
+ | local function restoreSubpage(episode, original) |
||
− | -- check if it is a number |
||
+ | local subpage = original:match('/%a+') |
||
− | if tonumber(epNum) then |
||
⚫ | |||
− | return frame:expandTemplate{ title = "TabSwitch", args = { epNum + diff } } |
||
⚫ | |||
else |
else |
||
− | return |
+ | return episode .. subpage |
end |
end |
||
end |
end |
||
+ | -- Returns relative episode link from offset number |
||
− | -- Creates the actual link for p.nextLink and p.prevLink methods |
||
+ | -- Returns a string |
||
− | |||
− | local function |
+ | local function relativeLink(episode, offset) |
⚫ | |||
− | if pageType == "Episode" then |
||
− | + | if relEpisode == 'N/A' then |
|
− | + | return relEpisode |
|
− | else |
||
− | return "[[" .. episode .. "]]" |
||
− | end |
||
else |
else |
||
⚫ | |||
− | if mw.title.new( episode .. " (episode)/" .. pageType ).exists then |
||
+ | return createEpisodeLink(relTitle) |
||
− | return "[[" .. episode .. " (episode)/" .. pageType .. "|" .. episode .. "]]" |
||
− | else |
||
⚫ | |||
− | end |
||
end |
end |
||
end |
end |
||
+ | -- Utility function for calling relativeLink(episode, -1) |
||
− | -- Get the current episode name based on the pagename |
||
− | -- Returns |
+ | -- Returns a string |
⚫ | |||
+ | return relativeLink(episode, -1) |
||
⚫ | |||
+ | -- Utility function for calling relativeLink(episode, 1) |
||
⚫ | |||
+ | -- Returns a string |
||
⚫ | |||
⚫ | |||
+ | return relativeLink(episode, 1) |
||
end |
end |
||
+ | -- Main function when invoking the module through {{#invoke: EpisodeLinks}} |
||
− | -- Creates a link to the previous episode/gallery/transcript page |
||
+ | -- Takes no arguments |
||
⚫ | |||
− | -- Returns |
+ | -- Returns readable title, previous episode link and next episode link separated with semicolons |
⚫ | |||
− | |||
⚫ | |||
− | function p.prevLink( frame ) |
||
− | + | return ';;' |
|
⚫ | |||
− | if prevEpisode == "N/A" then |
||
− | return "N/A" |
||
− | else |
||
− | createLink( prevEpisode ) |
||
− | end |
||
else |
else |
||
− | + | local name = title.fullText |
|
+ | return readableTitle(name) .. ';"' .. prevLink(name) .. '";"' .. nextLink(name) .. '"' |
||
end |
end |
||
end |
end |
||
− | -- Creates a link to the next episode/gallery/transcript page |
||
⚫ | |||
− | -- Returns "TBA" if the next episode is not known |
||
− | |||
⚫ | |||
− | if pageType then |
||
⚫ | |||
− | if nextEpisode == "N/A" then |
||
− | return "TBA" |
||
− | else |
||
− | createLink( nextEpisode ) |
||
− | end |
||
− | else |
||
− | return "" |
||
⚫ | |||
⚫ | |||
− | |||
return p |
return p |
Revision as of 15:16, 12 November 2017
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()
-- Removes content between parentheses and /Transcript or /Gallery parts in titles
-- Returns a string
local function readableTitle(_title)
return _title:gsub(' %b()', ''):gsub('/%a+', '')
end
-- Removes any subpage parts in the title supplied
-- Returns a string
local function baseTitle(_title)
return _title:gsub('/%a+', '')
end
-- Creates a wikilink with the arguments provided
-- Returns a string
local function createLink(page, displayText)
if displayText ~= page and displayText then
return '[[' .. page .. '|' .. displayText .. ']]'
else
return '[[' .. page .. ']]'
end
end
-- Utility function to provide easier way of calling createLink
-- Returns a string
local function createEpisodeLink(episode)
return createLink(episode, readableTitle(episode))
end
-- Restores /Gallery or /Transcript parts in page name depending on second parameter subpage text
-- Returns a string
local function restoreSubpage(episode, original)
local subpage = original:match('/%a+')
if not subpage then
return episode
else
return episode .. subpage
end
end
-- Returns relative episode link from offset number
-- Returns a string
local function relativeLink(episode, offset)
local relEpisode = relativeEpisode(baseTitle(episode), offset)
if relEpisode == 'N/A' then
return relEpisode
else
local relTitle = restoreSubpage(relEpisode, episode)
return createEpisodeLink(relTitle)
end
end
-- Utility function for calling relativeLink(episode, -1)
-- Returns a string
local function prevLink(episode)
return relativeLink(episode, -1)
end
-- Utility function for calling relativeLink(episode, 1)
-- Returns a string
local function nextLink(episode)
return relativeLink(episode, 1)
end
-- Main function when invoking the module through {{#invoke: EpisodeLinks}}
-- Takes no arguments
-- Returns readable title, previous episode link and next episode link separated with semicolons
function p.main()
if title.namespace ~= 0 then
return ';;'
else
local name = title.fullText
return readableTitle(name) .. ';"' .. prevLink(name) .. '";"' .. nextLink(name) .. '"'
end
end
return p