Jump to content

Module:Category pair/sandbox: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Copied from Module:Category pair and sandboxed a tweak that avoids concatenation-based formatting
(No difference)

Revision as of 17:58, 20 December 2021

require('Module:No globals')
local getArgs = require('Module:Arguments').getArgs
local hatnoteModule = require('Module:Hatnote')
local hatnote = hatnoteModule._hatnote
local formatLink = hatnoteModule._formatLink

local p = {}

-- Lua implementation of [[Template:CategoryPair]]
-- Arguments:
--   prevTitle -- mw.title.Title object for preceding category
--   nextTitle -- mw.title.Title object for succeeding category
-- Returns:
--   hatnote that says "see also" for one or both of prev/next (depending on whether they exist)
function p._pair(prevTitle, nextTitle)
	prevTitle = prevTitle and prevTitle.exists and prevTitle
	nextTitle = nextTitle and nextTitle.exists and nextTitle
	local note = ''
	local bitmap = 0
	if prevTitle then bitmap = bitmap + 1 end
	if nextTitle then bitmap = bitmap + 2 end
	if bitmap == 0 then
		if mw.title.getCurrentTitle().nsText ~= 'Category' then
			return ''
		end
		return '[[Category:Pages using category pair with no output]]'
	elseif bitmap == 1 then 
		note = mw.ustring.format(
			'See also the preceding %s', formatLink{link=prevTitle.fullText}
		)
	elseif bitmap == 2 then
		note = mw.ustring.format(
			'See also the succeeding %s', formatLink{link=nextTitle.fullText}
		)
	elseif bitmap == 3 then
		note = mw.ustring.format(
			'See also the preceding %s and the succeeding %s',
			formatLink{link = prevTitle.fullText},
			formatLink{link = nextTitle.fullText}
		)
	end
	return hatnote(note, {extraclasses = 'seealso'})
end

function p.catPair(frame)
	local args = getArgs(frame, {wrappers={'Template:Category pair'}})
	local prevTitle = args[1] and mw.title.new(args[1],'Category')
	local nextTitle = args[2] and mw.title.new(args[2],'Category')
	return p._pair(prevTitle, nextTitle)
end

function p.prevCat(frame)
	local args = getArgs(frame, {wrappers={'Template:Preceding category'}})
	local prevTitle = args[1] and mw.title.new(args[1],'Category')
	return p._pair(prevTitle, nil)
end

function p.nextCat(frame)
	local args = getArgs(frame, {wrappers={'Template:Succeeding category'}})
	local nextTitle = args[1] and mw.title.new(args[1],'Category')
	return p._pair(nil, nextTitle)
end

return p