Module:ComicsHF/PreviousNext

local PreviousNext = {}

-- Libraries of functions --

-- Parses invocation parameters, trims whitespace, and removes blanks local getArgs = require('Dev:Arguments').getArgs

-- Since this should only be evaluated once per pageview, it's now global _G.vars = { Pagename = mw.title.getCurrentTitle.text }

-- -- Public functions (called from a Template or article) -- -- function PreviousNext.Previous( frame ) local args = getArgs(frame, { trim = true, removeBlanks = true }) return _Previous(args) end

function PreviousNext.Next( frame ) local args = getArgs(frame, { trim = true, removeBlanks = true }) return _Next(args) end

PreviousNext.Comic_Previous = invokeInt('_Comic_Previous') PreviousNext.Comic_Next = invokeInt('_Comic_Next')

-- Local functions (used only in this Module) --

local function _Previous( args ) local ArticleType = args['articletype'] or args[1] or nil end

local function _Next( args ) local ArticleType = args['articletype'] or args[1] or nil end

local function invokeInt(funcName) -- This wrapper allows invocation of an internal (local) function from a frame. return function (frame) local args = getArgs(frame, { trim = true, removeBlanks = true }) return funcName(args, vars) end end

local function Comic_Previous( args ) local Title, Volume, Issue if args.PreviousIssue then Title, Volume, Issue = string.match( PreviousIssue, "(.*)%s*Vol%s*(%d)%s*(%d*)") else Title, Volume, Issue = string.match( vars.Pagename, "(.*)%s*Vol%s*(%d)%s*(%d*)") end Title = args.Title or Title or nil Volume = tonumber(args.Volume) or tonumber(Volume) or nil Issue = tonumber(args.Issue) or tonumber(Issue) or nil if (Volume == 1 and Issue == 1) or (Volume == nil and Issue == nil) then -- new title, no previous return '' elseif Issue > 1 and Volume ~= 1 then -- same volume, new issue Issue = Issue - 1 return string.format('%s Vol %s # %s', Title, Volume, Issue, Title, Volume, Issue ) elseif Issue > 1 and Volume == 1 then -- same first volume, new issue Issue = Issue - 1 return string.format('%s # %s', Title, Volume, Issue, Title, Issue ) elseif Volume > 1 and Issue == 1 then -- first issue of a new volume, refer to last volume Volume = Volume - 1 return string.format('%s Vol %s', Title, Volume) else -- no other use cases return '' end end

local function Comic_Next( args ) local Title, Volume, Issue if args.NextIssue then Title, Volume, Issue = string.match( NextIssue, "(.*)%s*Vol%s*(%d)%s*(%d*)") else Title, Volume, Issue = string.match( vars.Pagename, "(.*)%s*Vol%s*(%d)%s*(%d*)") end Title = args.Title or Title or nil Volume = tonumber(args.Volume) or tonumber(Volume) or nil Issue = tonumber(args.Issue) or tonumber(Issue) or nil if Volume == nil and Issue == nil then -- new title, no previous return '' else NextIssue = string.format('%s Vol %s %s', Title, Volume, Issue + 1 ) NextVolume = string.format('%s Vol %s', Title, Volume + 1 ) end if mw.title.new( NextIssue ).exists == true then if Volume == 1 then return string.format('%s # %s', NextIssue, Title, Issue + 1) else return string.format('%s Vol %s # %s', NextIssue, Title, Volume, Issue + 1) end elseif mw.title.new( NextVolume ).exists == true then return string.format(''..'%s Vol %s', Title, Volume + 1 ) else return '' end end

return PreviousNext