Jump to content

Module:Relatit

Frae Wikipedia, the free beuk o knawledge
Reveesion aes o 21:09, 2 October 2020 bi MJL (Collogue | contribs) (try this)

--[[
-- This module produces a "Relatit: a, b an c" airtin. It implements the
-- template {{relatit}}.
--]]

local mHatnote = require('Module:Hatnote')
local mTableTools -- lazily initialise
local mArguments -- lazily initialise

local p = {}

function p.relatit(frame)
	mTableTools = require('Module:TableTools')
	mArguments = require('Module:Arguments')
	local args = mArguments.getArgs(frame, {parentOnly = true})
	local pages = {}
	for k, v in pairs(args) do
		if type(k) == 'number' then
			local numstring = tostring(k)
			local display = args['label ' .. numstring]
				or args['l' .. numstring]
			local page = {v, display}
			pages[k] = page
		end
	end
	pages = mTableTools.compressSparseArray(pages)
	if not pages[1] then
		return mHatnote.makeWikitextError(
			'no page names specified',
			'Template:Relatit#Errors',
			args.category
		)
	end
	local options = {
		altphrase = args.altphrase,
		selfref = args.selfref
	}
	return p._relatit(options, unpack(pages))
end

function p._relatit(options, ...)
	local altphrase = options and options.altphrase or 'Relatit'
	local links = mHatnote.formatPageTables(...)
	links = mw.text.listToText(links)
	local pageType = mHatnote.findNamespaceId(firstPage) == 0 and 'page' or 'page'
	local pageType = mHatnote.findNamespaceId(firstPage) == 10 and 'template' or 'page'
	local isPlural = #args > 1
	local altphrase
	local altphrase = isPlural and 'Relatit %ss: %s' or 'Relatit %s: %s'
	local text = string.format(altphrase, pageType, ': ', links)
	-- Pass options through.
	local hnOptions = {}
	hnOptions.selfref = options.selfref
	hnOptions.extraclasses = 'boilerplate relatit'

	return mHatnote._hatnote(text, hnOptions)
end

return p