Jump to content

Module:Category pair/sandbox: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Tweaks
sync
Line 5: Line 5:


local p = {}
local p = {}

local catNS = 14 -- category namespace


-- Lua implementation of [[Template:CategoryPair]]
-- Lua implementation of [[Template:CategoryPair]]
Line 21: Line 23:
local note = ''
local note = ''
if not prevTitle and not nextTitle then
if not prevTitle and not nextTitle then
if mw.title.getCurrentTitle().namespace ~= 14 then -- 14 <-> Category
return mw.title.getCurrentTitle().namespace == catNS and '[[Category:Pages using category pair with no output]]' or ''
return ''
end
return '[[Category:Pages using category pair with no output]]'
elseif prevTitle and (not nextTitle) then
elseif prevTitle and (not nextTitle) then
note = mw.ustring.format('See also the preceding %s', prevTitle)
note = mw.ustring.format('See also the preceding %s', prevTitle)
Line 40: Line 39:
function p.catPair(frame)
function p.catPair(frame)
local args = getArgs(frame, {wrappers={'Template:Category pair'}})
local args = getArgs(frame, {wrappers={'Template:Category pair'}})
local prevTitle = args[1] and mw.title.new(args[1],'Category')
local prevTitle = args[1] and mw.title.new(args[1],catNS)
local nextTitle = args[2] and mw.title.new(args[2],'Category')
local nextTitle = args[2] and mw.title.new(args[2],catNS)
return p._pair(prevTitle, nextTitle)
return p._pair(prevTitle, nextTitle)
end
end
Line 47: Line 46:
function p.prevCat(frame)
function p.prevCat(frame)
local args = getArgs(frame, {wrappers={'Template:Preceding category'}})
local args = getArgs(frame, {wrappers={'Template:Preceding category'}})
local prevTitle = args[1] and mw.title.new(args[1], 14)
local prevTitle = args[1] and mw.title.new(args[1], catNS)
return p._pair(prevTitle, nil)
return p._pair(prevTitle, nil)
end
end
Line 53: Line 52:
function p.nextCat(frame)
function p.nextCat(frame)
local args = getArgs(frame, {wrappers={'Template:Succeeding category'}})
local args = getArgs(frame, {wrappers={'Template:Succeeding category'}})
local nextTitle = args[1] and mw.title.new(args[1], 14)
local nextTitle = args[1] and mw.title.new(args[1], catNS)
return p._pair(nil, nextTitle)
return p._pair(nil, nextTitle)
end
end

Revision as of 05:57, 14 January 2022

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

local p = {}

local catNS = 14 -- category namespace

-- 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
		formatLink{link = prevTitle.fullText}
	nextTitle =
		nextTitle and nextTitle.exists and
		formatLink{link = nextTitle.fullText}
	local note = ''
	if not prevTitle and not nextTitle then
		return mw.title.getCurrentTitle().namespace == catNS and '[[Category:Pages using category pair with no output]]' or ''
	elseif prevTitle and (not nextTitle) then
		note = mw.ustring.format('See also the preceding %s', prevTitle)
	elseif (not prevTitle) and nextTitle then
		note = mw.ustring.format('See also the succeeding %s', nextTitle)
	elseif prevTitle and nextTitle then
		note = mw.ustring.format(
			'See also the preceding %s and the succeeding %s',
			prevTitle, nextTitle
		)
	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],catNS)
	local nextTitle = args[2] and mw.title.new(args[2],catNS)
	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], catNS)
	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], catNS)
	return p._pair(nil, nextTitle)
end

return p