మాడ్యూల్:Category see also: కూర్పుల మధ్య తేడాలు

వికీపీడియా నుండి
Jump to navigation Jump to search
Content deleted Content added
create replacement for Template:Category see also
(తేడా లేదు)

12:45, 12 మే 2017 నాటి కూర్పు

-- This module implements {{Category see also}}

local mHatnote = require('Module:Hatnote')

local p = {}

local function makeWikitextError(msg)
	return string.format(
		'<strong class="error">Error: %s ([[Template:Category see also]])</strong>',
		msg
	)
end

-- Given a project string, returns a function that formats categories for that
-- project. The project can be nil, in which case it formats the category for
-- the current project.
--
-- This is implemented as a function generator rather than a simple function
-- so that we can just process the project name once, instead of every time
-- we generate a category.
local function generateCategoryLinkMaker(project)
	local formatString
	if project then
		formatString = '[[:' .. project .. ':Category:%s|%s]]'
	else
		formatString = '[[:Category:%s|%s]]'
	end
	return function (category)
		return formatString:format(category, category)
	end
end

function p._main(args)
	if not args[1] then
		return makeWikitextError('at least one parameter required')
	end
	local makeCategoryLink = generateCategoryLinkMaker(args.project)
	local links = {}
	for i, cat in ipairs(args) do
		links[i] = makeCategoryLink(cat)
	end
	local hatnoteText = string.format(
		'%s: %s.',
		args.LABEL or 'See also',
		mw.text.listToText(links, ', ', ', and ')
	)
	return mHatnote._hatnote(hatnoteText, {selfref = true})
end

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		wrappers = 'Template:Category see also',
	})
	return p._main(args)
end

return p