FANDOM


(let's see if Doru can write a valid working code from the first try)
m (Protected "Module:EpisodeLinks" (‎[edit=sysop] (indefinite) ‎[move=sysop] (indefinite)))
 
(6 intermediate revisions by one user not shown)
Line 6: Line 6:
 
local title = mw.title.getCurrentTitle()
 
local title = mw.title.getCurrentTitle()
   
-- Checks if page has the postfix (short) in the title
+
-- Removes content between parentheses and /Transcript or /Gallery parts in titles
-- Returns a boolean
+
-- Returns a string
local function isShort(ep)
+
local function readableTitle(_title)
return ep:find('%(short%)') ~= nil
+
return _title:gsub(' %b()', ''):gsub('/%a+', '')
end
 
 
-- Checks if page has a postfix (episode) in the title
 
-- Returns a boolean
 
local function isEpisode(ep)
 
return ep:find('%(episode%)') ~= nil
 
 
end
 
end
   
-- Removes content between parentheses in titles
+
-- Removes any subpage parts in the title supplied
 
-- Returns a string
 
-- Returns a string
local function readableTitle(_title)
+
local function baseTitle(_title)
return _title:gsub(' %b()', ''):gsub('/%a+')
+
return _title:gsub('/%a+', '')
 
end
 
end
  +
   
 
-- Creates a wikilink with the arguments provided
 
-- Creates a wikilink with the arguments provided
Line 32: Line 33:
 
local function createEpisodeLink(episode)
 
local function createEpisodeLink(episode)
 
return createLink(episode, readableTitle(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
 
end
   
Line 37: Line 49:
 
-- Returns a string
 
-- Returns a string
 
local function relativeLink(episode, offset)
 
local function relativeLink(episode, offset)
local relEpisode = relativeEpisod(episode, offset)
+
local relEpisode = relativeEpisode(baseTitle(episode), offset)
 
if relEpisode == 'N/A' then
 
if relEpisode == 'N/A' then
 
return relEpisode
 
return relEpisode
 
else
 
else
return createEpisodeLink(relEpisode)
+
local relTitle = restoreSubpage(relEpisode, episode)
  +
return createEpisodeLink(relTitle)
 
end
 
end
 
end
 
end
Line 65: Line 77:
 
else
 
else
 
local name = title.fullText
 
local name = title.fullText
return readableTitle(name) .. ';' .. prevLink(name) .. ';' .. nextLink(name)
+
return readableTitle(name) .. ';"' .. prevLink(name) .. '";"' .. nextLink(name) .. '"'
 
end
 
end
 
end
 
end

Latest revision as of 03:53, March 22, 2020

-- <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
Community content is available under CC-BY-SA unless otherwise noted.