Módulo:Chamada modelo

Na Galipedia, a Wikipedia en galego.
Indicacións de uso do módulo

Obxectivo[editar a fonte]

Meta-módulo para producir chamadas a modelos MediaWiki.

Uso[editar a fonte]

Carga inicial do módulo para usar as funcións do mesmo:

local mTemplateInvocation = require('Módulo:Chamada modelo')

Funcións[editar a fonte]

Name[editar a fonte]
mTemplateInvocation.name(title)

Produce o nome a usar na chamada de modelo. Para páxinas no espazo de nomes de modelos devolve o nome da páxina sen prefixo de espazo de nomes. Para páxinas no espazo de nomes principal devolve o nome completo xunto co prefixo ":", e para outras páxinas devolve o nome completo da páxina. title pode ser unha cadea ou un obxecto mw.title.

Invocation[editar a fonte]
mTemplateInvocation.invocation(name, args, format)

Crea unha chamada a un modelo MediaWiki.

Parámetros:

  • name - Nome do modelo (requerido).
  • args - Argumentos a usar na chamada (requerido).
  • format - Formato da chamada.

Exemplo[editar a fonte]

O código
mTemplateInvocation.invocation('foo', {'bar', 'baz', abc = 'def'})
produce {{foo|bar|baz|abc=def}}.
Esta documentación está transcluída desde Módulo:Chamada modelo/uso. Os editores poden probar cambios no mesmo en Módulo:Chamada modelo/probas.
Por favor, engade as categorías na subpáxina de documentación e os interwikis no Wikidata. Ver as subpáxinas deste módulo.

-- This module provides functions for making MediaWiki template invocations.

local checkType = require('libraryUtil').checkType

local p = {}

------------------------------------------------------------------------
--         Name:  p.name
--      Purpose:  Find a template invocation name from a page name or a
--                mw.title object.
--  Description:  This function detects whether a string or a mw.title
--                object has been passed in, and uses that to find a
--                template name as it is used in template invocations.
--   Parameters:  title - full page name or mw.title object for the
--                template (string or mw.title object)
--      Returns:  String
------------------------------------------------------------------------

function p.name(title)
	if type(title) == 'string' then
		title = mw.title.new(title)
		if not title then
			error("título non válido no parámetro #1 da función 'name'", 2)
		end
	elseif type(title) ~= 'table' or type(title.getContent) ~= 'function' then
		error("o parámetro #1 da función 'name' debe ser unha cadea ou un obxecto mw.title", 2)
	end
	if title.namespace == 10 then
		return title.text
	elseif title.namespace == 0 then
		return ':' .. title.prefixedText
	else
		return title.prefixedText
	end
end

------------------------------------------------------------------------
--         Name:  p.invocation
--      Purpose:  Construct a MediaWiki template invocation.
--  Description:  This function makes a template invocation from the
--                name and the arguments given. Note that it isn't
--                perfect: we have no way of knowing what whitespace was
--                in the original invocation, the named parameters will be
--                alphabetically sorted, and any parameters with duplicate keys
--                will be removed.
--   Parameters:  name - the template name, formatted as it will appear
--                    in the invocation. (string)
--                args - a table of template arguments. (table)
--                format - formatting options. (string, optional)
--                    Set to "nowiki" to escape, curly braces, pipes and
--                    equals signs with their HTML entities. The default
--                    is unescaped.
--      Returns:  String
------------------------------------------------------------------------

function p.invocation(name, args, format)
	checkType('invocation', 1, name, 'string')
	checkType('invocation', 2, args, 'table')
	checkType('invocation', 3, format, 'string', true)

	-- Validate the args table and make a copy to work from. We need to
	-- make a copy of the table rather than just using the original, as
	-- some of the values may be erased when building the invocation.
	local invArgs = {}
	for k, v in pairs(args) do
		local typek = type(k)
		local typev = type(v)
		if typek ~= 'string' and typek ~= 'number'
			or typev ~= 'string' and typev ~= 'number'
		then
			error("táboa de argumentos non válida no parámetro #2 de " ..
			"'invocation' (as chaves e valores deben ser cadeas ou números)", 2)
		end
		invArgs[k] = v
	end

	-- Get the separators to use.
	local seps = {
		openb = '{{',
		closeb = '}}',
		pipe = '|',
		equals = '='
	}
	if format == 'nowiki' then
		for k, v in pairs(seps) do
			seps[k] = mw.text.nowiki(v)
		end
	end

	-- Build the invocation body with numbered args first, then named.
	local ret = {}
	ret[#ret + 1] = seps.openb
	ret[#ret + 1] = name
	for k, v in ipairs(invArgs) do
		if type(v) == 'string' and v:find('=', 1, true) then
			-- Likely something like 1=foo=bar, we need to do it as a named arg
			break
		end
		ret[#ret + 1] = seps.pipe
		ret[#ret + 1] = v
		invArgs[k] = nil -- Erase the key so that we don't add the value twice
	end
	local invArgs_list = {} -- sort a parameter list; preferable to randomly sorted output
	for k, v in pairs(invArgs) do
		invArgs_list[#invArgs_list + 1] = k
	end
	table.sort(invArgs_list)
	for i, v in ipairs(invArgs_list) do -- Add named args based on sorted parameter list
		ret[#ret + 1] = seps.pipe
		ret[#ret + 1] = v
		ret[#ret + 1] = seps.equals
		ret[#ret + 1] = invArgs[v]
	end
	ret[#ret + 1] = seps.closeb

	return table.concat(ret)
end

return p