Harvey Beaks! Wiki
Advertisement

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
Advertisement