Jump to content

Module:Category pair/sandbox: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
m Added comments to bitmap lines for clarity
Tweaks
Line 13: Line 13:
-- hatnote that says "see also" for one or both of prev/next (depending on whether they exist)
-- hatnote that says "see also" for one or both of prev/next (depending on whether they exist)
function p._pair(prevTitle, nextTitle)
function p._pair(prevTitle, nextTitle)
prevTitle = prevTitle and prevTitle.exists and prevTitle
prevTitle =
prevTitle and prevTitle.exists and
formatLink{link = prevTitle.fullText}
nextTitle = nextTitle and nextTitle.exists and nextTitle
nextTitle =
nextTitle and nextTitle.exists and
formatLink{link = nextTitle.fullText}
local note = ''
local note = ''
if not prevTitle and not nextTitle then
local bitmap = 0
if mw.title.getCurrentTitle().namespace ~= 14 then -- 14 <-> Category
if prevTitle then bitmap = bitmap + 1 end
if nextTitle then bitmap = bitmap + 2 end
if bitmap == 0 then --neither prev nor next
if mw.title.getCurrentTitle().nsText ~= 'Category' then
return ''
return ''
end
end
return '[[Category:Pages using category pair with no output]]'
return '[[Category:Pages using category pair with no output]]'
elseif bitmap == 1 then --prev and not next
elseif prevTitle and (not nextTitle) then
note = mw.ustring.format(
note = mw.ustring.format('See also the preceding %s', prevTitle)
elseif (not prevTitle) and nextTitle then
'See also the preceding %s', formatLink{link = prevTitle.fullText}
note = mw.ustring.format('See also the succeeding %s', nextTitle)
)
elseif bitmap == 2 then --next and not prev
elseif prevTitle and nextTitle then
note = mw.ustring.format(
'See also the succeeding %s', formatLink{link = nextTitle.fullText}
)
elseif bitmap == 3 then --both prev and next
note = mw.ustring.format(
note = mw.ustring.format(
'See also the preceding %s and the succeeding %s',
'See also the preceding %s and the succeeding %s',
formatLink{link = prevTitle.fullText},
prevTitle, nextTitle
formatLink{link = nextTitle.fullText}
)
)
end
end
Line 51: Line 47:
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],'Category')
local prevTitle = args[1] and mw.title.new(args[1], 14)
return p._pair(prevTitle, nil)
return p._pair(prevTitle, nil)
end
end
Line 57: Line 53:
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],'Category')
local nextTitle = args[1] and mw.title.new(args[1], 14)
return p._pair(nil, nextTitle)
return p._pair(nil, nextTitle)
end
end

Revision as of 04:03, 24 December 2021

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 = {}

-- 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
		if mw.title.getCurrentTitle().namespace ~= 14 then -- 14 <-> Category
			return ''
		end
		return '[[Category:Pages using category pair with no output]]'
	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],'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], 14)
	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], 14)
	return p._pair(nil, nextTitle)
end

return p