Steven Universe Wiki

Spoilers will be present! Please browse at your own risk.

READ MORE

Steven Universe Wiki
Register
(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
 
 
-- Returns a string
-- by checking if the base page has the Episode template
 
  +
local function readableTitle(_title)
--
 
  +
return _title:gsub(' %b()', ''):gsub('/%a+', '')
-- pageType =
 
 
end
-- "Episode" on episode pages,
 
-- "Gallery" on gallery pages,
 
-- "Transcript" on transcript pages
 
   
  +
-- Removes any subpage parts in the title supplied
if basetitle.exists then
 
 
-- Returns a string
if string.find( basetitle:getContent(), "{{Episode" ) or
 
  +
local function baseTitle(_title)
string.find( basetitle:getContent(), "{{Template:Episode") then
 
  +
return _title:gsub('/%a+', '')
if title.text == basetitle.text then
 
pageType = "Episode";
 
else
 
pageType = title.subpageText
 
end
 
end
 
 
end
 
end
   
local episode
 
   
  +
-- Creates a wikilink with the arguments provided
-- Get the current episode name based on the pagename
 
-- Returns empty string if wrong pagetype
+
-- Returns a string
  +
local function createLink(page, displayText)
 
  +
if displayText ~= page and displayText then
if pageType then
 
 
return '[[' .. page .. '|' .. displayText .. ']]'
local endpos = string.find( title.text, "(episode)" )
 
if endpos then
 
episode = string.sub( title.text, 1, endpos )
 
 
else
 
else
episode = title.baseText
+
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
 
 
local function createEpisodeLink(episode)
-- and the episode that should be returned
 
  +
return createLink(episode, readableTitle(episode))
--
 
 
end
-- 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
local function relativeEpisode( frame, episode, diff )
 
  +
-- 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
 
 
if not subpage then
return frame:expandTemplate{ title = "TabSwitch", args = { epNum + diff } }
 
 
return episode
 
else
 
else
return "N/A"
+
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 createLink( episode )
+
local function relativeLink(episode, offset)
 
local relEpisode = relativeEpisode(baseTitle(episode), offset)
if pageType == "Episode" then
 
if mw.title.new( episode .. " (episode)" ).exists then
+
if relEpisode == 'N/A' then
return "[[" .. episode .. " (episode)|" .. episode .. "]]"
+
return relEpisode
else
 
return "[[" .. episode .. "]]"
 
end
 
 
else
 
else
 
local relTitle = restoreSubpage(relEpisode, episode)
if mw.title.new( episode .. " (episode)/" .. pageType ).exists then
 
  +
return createEpisodeLink(relTitle)
return "[[" .. episode .. " (episode)/" .. pageType .. "|" .. episode .. "]]"
 
else
 
return "[[" .. episode .. "/" .. pageType .. "|" .. episode .. "]]"
 
end
 
 
end
 
end
 
end
 
end
   
  +
-- Utility function for calling relativeLink(episode, -1)
-- Get the current episode name based on the pagename
 
-- Returns empty string if wrong pagetype
+
-- Returns a string
 
local function prevLink(episode)
  +
return relativeLink(episode, -1)
 
end
   
  +
-- Utility function for calling relativeLink(episode, 1)
function p.episode( frame )
 
  +
-- Returns a string
return episode
 
 
local function nextLink(episode)
  +
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 empty string if wrong pagetype
 
-- Returns "N/A" if the next episode is not known
+
-- Returns readable title, previous episode link and next episode link separated with semicolons
 
function p.main()
 
 
if title.namespace ~= 0 then
function p.prevLink( frame )
 
if pageType then
+
return ';;'
local prevEpisode = relativeEpisode( frame, episode, -1 )
 
if prevEpisode == "N/A" then
 
return "N/A"
 
else
 
createLink( prevEpisode )
 
end
 
 
else
 
else
return ""
+
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 empty string if wrong pagetype
 
-- Returns "TBA" if the next episode is not known
 
 
function p.nextLink( frame )
 
if pageType then
 
local nextEpisode = relativeEpisode( frame, episode, 1 )
 
if nextEpisode == "N/A" then
 
return "TBA"
 
else
 
createLink( nextEpisode )
 
end
 
else
 
return ""
 
end
 
end
 
 
 
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