Jump to content

Moduli:Sandbox/pppery/sentence list hatnote

Nga Wikipedia, enciklopedia e lirë
--------------------------------------------------------------------------------
--                               Sentence list                                --
--                                                                            --
-- This module does the core work of creating a hatnote that ends with        --
-- a list of pages . Many hatnote template use this format, and the module    --
-- heavily customizible for this purpose                                      --
--------------------------------------------------------------------------------

local mHatnote = require('Module:Hatnote')
local mHatlist = require('Module:Hatnote list/sandbox')
local yesno = require('Module:Yesno')
local mArguments --initialize lazily
local p = {}

-- Defaults global to this module
local defaults = {
	prefix = '',
	format = '%s %s.',
	template = 'Module:Sentence list hatnote',
	conjunction = 'and'
}

-- Produces a pages-list hatnote.
-- The main frame (template definition) takes 1 or 2 arguments, for a singular
-- and (optionally) plural label, as well as a few named arguments to specify various options
-- * {{#invoke:Sentence list hatnote|list|Singular label|Plural label|allowText=yes/no|conjunction=and/or}}
-- The resulting template takes pagename parameters normally.
function p.list (frame)
	mArguments = require('Module:Arguments')
	local labels = {frame.args[1] or defaults.prefix}
	labels[2] = frame.args[2] or labels[1]
	local template = frame:getParent():getTitle()
	local args = mArguments.getArgs(frame, {parentOnly = true, removeBlanks = false})
	local start = tonumber(frame.args.start) or 0
	local options = {
		extraclasses = frame.args.extraclasses,
		category = args.category,
		selfref = frame.args.selfref or args.selfref,
		template = template,
		conjunction = frame.args.conjunction,
		default_page = frame.args.default_page
	}
	if yesno(frame.args.allowText) then
		options.text = args.text
	end
	local pages = {}
	for k, v in pairs(args) do
		if type(k) == 'number' and k >= start and #v > 0 then
			pages[#pages + 1] = v
		end
	end
	return p._list(pages, labels, options)
end

function p._list (pages, labels, options)
	labels = labels or {}
	if not options.text and #pages == 0 then
		if options.default_page then
			pages[#pages+1] = mHatnote.disambiguate(options.default_page)
		else
			return mHatnote.makeWikitextError(
				'no page names specified',
				(options.template or defaults.template) .. '#Errors',
				options.category
			)
		end
	end
	label = (#pages == 1 and labels[1] or labels[2]) or defaults.label
	local text = string.format(
		options.format or defaults.format,
		label,
		options.text or mHatlist.conjList(options.conjunction or defaults.conjunction, pages, true)
	)
	local hnOptions = {
		extraclasses = options.extraclasses,
		selfref = options.selfref
	}
	return mHatnote._hatnote(text, hnOptions)
end

return p