(let's see if Doru can write a valid working code from the first try)
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 episodeTerm = ' (episode)'
 
local shortTerm = ' (short)'
 
local pageType
 
local episode
 
 
   
 
-- Checks if page has the postfix (short) in the title
 
-- Checks if page has the postfix (short) in the title
Line 15: Line 11:
 
return ep:find('%(short%)') ~= nil
 
return ep:find('%(short%)') ~= nil
 
end
 
end
  +
 
 
-- Checks if page has a postfix (episode) in the title
 
-- Checks if page has a postfix (episode) in the title
 
-- Returns a boolean
 
-- Returns a boolean
Line 22: Line 18:
 
end
 
end
   
  +
-- Removes content between parentheses in titles
  +
-- Returns a string
 
local function readableTitle(_title)
 
local function readableTitle(_title)
return _title:gsub(' %b()', '')
+
return _title:gsub(' %b()', ''):gsub('/%a+')
 
end
 
end
   
-- Creates the actual link for p.nextLink and p.prevLink methods
+
-- Creates a wikilink with the arguments provided
  +
-- Returns a string
 
local function createLink(_episode)
+
local function createLink(page, displayText)
if pageType == 'Episode' then
+
if displayText ~= page and displayText then
 
return '[[' .. page .. '|' .. displayText .. ']]'
if isEpisode(_episode) then
 
return '"[[' .. _episode .. '|' .. readableTitle(_episode) .. ']]"'
 
elseif isShort(_episode) then
 
return '"[[' .. _episode .. '|' .. readableTitle(_episode) .. ']]"'
 
else
 
return '"[[' .. readableTitle(_episode) .. ']]"'
 
end
 
 
else
 
else
 
return '[[' .. page .. ']]'
if isEpisode(_episode) then
 
return '"[[' .. _episode .. '/' .. pageType .. '|' .. readableTitle(_episode) .. ']]"'
 
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 empty string if wrong pagetype
+
-- Returns a string
  +
local function createEpisodeLink(episode)
-- Returns "N/A" if the previous episode is not known
 
 
return createLink(episode, readableTitle(episode))
 
end
   
  +
-- Returns relative episode link from offset number
local function prevLink(episodeTitle)
 
  +
-- Returns a string
if not pageType then return '' end
 
  +
local function relativeLink(episode, offset)
local prevEpisode = relativeEpisode(episodeTitle, -1)
 
  +
local relEpisode = relativeEpisod(episode, offset)
if prevEpisode == 'N/A' then
 
return 'N/A'
+
if relEpisode == 'N/A' then
 
return relEpisode
 
else
 
else
return createLink(prevEpisode)
+
return createEpisodeLink(relEpisode)
 
end
 
end
 
end
 
end
   
  +
-- Utility function for calling relativeLink(episode, -1)
-- Creates a link to the next episode/gallery/transcript page
 
-- Returns empty string if wrong pagetype
+
-- Returns a string
 
local function prevLink(episode)
-- Returns "N/A" if the next episode is not known
 
  +
return relativeLink(episode, -1)
 
end
   
local function nextLink(episodeTitle)
+
-- Utility function for calling relativeLink(episode, 1)
  +
-- Returns a string
if not pageType then return '' end
 
 
local function nextLink(episode)
local nextEpisode = relativeEpisode(episodeTitle, 1)
 
  +
return relativeLink(episode, 1)
if nextEpisode == 'N/A' then
 
return 'N/A'
 
else
 
return createLink(nextEpisode)
 
end
 
 
end
 
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()
 
if title.namespace ~= 0 then
-- Check if it is an episode (sub)page
 
 
return ';;'
-- by checking if the base page has the Episode template
 
--
 
-- pageType =
 
-- "Episode" on episode pages,
 
-- "Gallery" on gallery pages,
 
-- "Transcript" on transcript pages
 
if title.namespace == 0 then
 
if title.text == title.baseText then
 
pageType = 'Episode'
 
else
 
pageType = title.subpageText
 
end
 
end
 
-- Get the current episode name based on the pagename
 
-- Returns empty string if wrong pagetype
 
if pageType then
 
episode = readableTitle(title.baseText)
 
 
else
 
else
episode = ''
+
local name = title.fullText
 
return readableTitle(name) .. ';' .. prevLink(name) .. ';' .. nextLink(name)
 
end
 
end
return episode .. ';' .. prevLink(title.baseText) .. ';' .. nextLink(title.baseText)
 
 
end
 
end
  +
 
 
return p
 
return p

Revision as of 14:20, 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()

-- Checks if page has the postfix (short) in the title
-- Returns a boolean
local function isShort(ep)
    return ep:find('%(short%)') ~= nil
end
 
-- Checks if page has a postfix (episode) in the title
-- Returns a boolean
local function isEpisode(ep) 
    return ep:find('%(episode%)') ~= nil
end

-- Removes content between parentheses in titles
-- Returns a string
local function readableTitle(_title) 
    return _title:gsub(' %b()', ''):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

-- Returns relative episode link from offset number
-- Returns a string
local function relativeLink(episode, offset)
    local relEpisode = relativeEpisod(episode, offset)
    if relEpisode == 'N/A' then
        return relEpisode
    else
        return createEpisodeLink(relEpisode)
    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
Community content is available under CC-BY-SA unless otherwise noted.