(Undo revision 1586222 by Andrey Andrey (talk)) |
m (Protected "Module:EpisodeLinks" ([edit=sysop] (indefinite) [move=sysop] (indefinite))) |
||
(8 intermediate revisions by one other user not shown) | |||
Line 1: | Line 1: | ||
− | --<nowiki> |
+ | -- <nowiki> |
+ | |||
local p = {} |
local p = {} |
||
local relativeEpisode = require('Module:TabSwitch')._relativeEpisode |
local relativeEpisode = require('Module:TabSwitch')._relativeEpisode |
||
local title = mw.title.getCurrentTitle() |
local title = mw.title.getCurrentTitle() |
||
− | local basetitle = mw.title.new(title.fullText) |
||
− | local episodeTerm = ' (episode)' |
||
− | local shortTerm = ' (short)' |
||
− | local pageType |
||
⚫ | |||
+ | -- Removes content between parentheses and /Transcript or /Gallery parts in titles |
||
− | |||
⚫ | |||
− | -- Checks if page has the postfix (short) in the title |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
− | return ep:find('%(short%)') ~= nil |
||
end |
end |
||
− | -- |
+ | -- Removes any subpage parts in the title supplied |
− | -- Returns a |
+ | -- Returns a string |
− | local function |
+ | local function baseTitle(_title) |
− | return |
+ | return _title:gsub('/%a+', '') |
end |
end |
||
⚫ | |||
⚫ | |||
⚫ | |||
− | -- Creates |
+ | -- Creates a wikilink with the arguments provided |
+ | -- Returns a string |
||
− | |||
− | local function createLink( |
+ | local function createLink(page, displayText) |
− | if |
+ | if displayText ~= page and displayText then |
⚫ | |||
− | if isEpisode(_episode) then |
||
⚫ | |||
− | elseif isShort(_episode) then |
||
− | return '"[[' .. _episode .. '|' .. readableTitle(_episode) .. ']]"' |
||
⚫ | |||
⚫ | |||
− | end |
||
else |
else |
||
⚫ | |||
− | if isEpisode(_episode) then |
||
⚫ | |||
− | elseif isShort(_episode) then |
||
− | return '"[[' .. _episode .. '/' .. pageType .. '|' .. readableTitle(_episode) .. ']]"' |
||
− | else |
||
− | return '"[[' .. _episode .. '/' .. pageType .. '|' .. readableTitle(_episode) .. ']]"' |
||
− | end |
||
end |
end |
||
end |
end |
||
+ | -- Utility function to provide easier way of calling createLink |
||
− | -- Creates a link to the previous episode/gallery/transcript page |
||
− | -- Returns |
+ | -- Returns a string |
+ | local function createEpisodeLink(episode) |
||
− | -- Returns "N/A" if the previous episode is not known |
||
⚫ | |||
⚫ | |||
+ | -- Restores /Gallery or /Transcript parts in page name depending on second parameter subpage text |
||
− | local function prevLink(episodeTitle) |
||
+ | -- Returns a string |
||
− | if not pageType then return '' end |
||
+ | local function restoreSubpage(episode, original) |
||
⚫ | |||
− | + | local subpage = original:match('/%a+') |
|
− | + | if not subpage then |
|
⚫ | |||
else |
else |
||
− | return |
+ | return episode .. subpage |
end |
end |
||
end |
end |
||
+ | -- Returns relative episode link from offset number |
||
− | -- Creates a link to the next episode/gallery/transcript page |
||
− | -- Returns |
+ | -- Returns a string |
+ | local function relativeLink(episode, offset) |
||
− | -- Returns "N/A" if the next episode is not known |
||
⚫ | |||
− | |||
⚫ | |||
− | local function nextLink(episodeTitle) |
||
− | + | return relEpisode |
|
− | local nextEpisode = relativeEpisode(episodeTitle, 1) |
||
⚫ | |||
⚫ | |||
else |
else |
||
+ | local relTitle = restoreSubpage(relEpisode, episode) |
||
− | return createLink(nextEpisode) |
||
+ | return createEpisodeLink(relTitle) |
||
end |
end |
||
end |
end |
||
+ | -- Utility function for calling relativeLink(episode, -1) |
||
+ | -- Returns a string |
||
⚫ | |||
+ | return relativeLink(episode, -1) |
||
⚫ | |||
+ | |||
+ | -- Utility function for calling relativeLink(episode, 1) |
||
+ | -- Returns a string |
||
⚫ | |||
+ | 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() |
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 |
||
⚫ | |||
− | local basecontent = string.lower(basetitle:getContent()) |
||
− | if basecontent:find('season -= -shorts') then |
||
− | episodeTerm = ' (short)' |
||
− | end |
||
− | if title.text == basetitle.baseText then |
||
− | pageType = 'Episode' |
||
− | else |
||
− | pageType = title.subpageText |
||
− | end |
||
⚫ | |||
− | -- Get the current episode name based on the pagename |
||
− | -- Returns empty string if wrong pagetype |
||
− | if pageType then |
||
⚫ | |||
else |
else |
||
− | + | local name = title.fullText |
|
⚫ | |||
end |
end |
||
− | return episode .. ';' .. prevLink(title.baseText) .. ';' .. nextLink(title.baseText) |
||
end |
end |
||
+ | |||
− | |||
return p |
return p |
Latest revision as of 03:53, 22 March 2020
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