Módulo:Redirect template

Documentação do módulo[ver] [editar] [histórico] [purgar]


Descrição editar

Este Módulo tem funções auxiliares para criar ligações para modelos.

Uso editar

Outra documentação:

require('strict')

local p = {}

-- key is beginning of arg name. value is table with namespace number and link
-- alternatively, a function taking the namespace number and returning a validity
-- can be used
local namespaceCategories = {
	all = { function() return true end },
	main = { 0, '[[wp:domínio principal|principal]]' },
	help = { 12, '[[wp:domínio de ajuda|ajuda]]' },
	portal = { 100, '[[wp:portal|portal]]' },
	talk = { function(n) return n > 0 and n%2 == 1 end, '[[wp:página de discussão|discussão]]' },
	template = { 10, '[[wp:predefinições|predefinição]]' },
	wikipedia = { 4, '[[wp:domínio do projeto|projeto Wikipédia]]' },
	category = { 14, '[[wp:categorização|categoria]]' },
	user = { 2, '[[wp:página do usuário|usuária(o)]]' },
	tudo = { function() return true end },
	todos = { function() return true end },
	todas = { function() return true end },
	principal = { 0, '[[wp:domínio principal|principal]]' },
	ajuda = { 12, '[[wp:domínio de ajuda|ajuda]]' },
	['discussão'] = { function(n) return n > 0 and n%2 == 1 end, '[[wp:página de discussão|discussão]]' },
	['predefinição'] = { 10, '[[wp:predefinições|predefinição]]' },
	['wikipédia'] = { 4, '[[wp:domínio do projeto|projeto Wikipédia]]' },
	categoria = { 14, '[[wp:categorização|categoria]]' },
	['usuária'] = { 2, '[[wp:página de usuário|usuária]]' },
	['usuário'] = { 2, '[[wp:página de usuário|usuário]]' },
}

-- remove whitespaces from beginning and end of args
local function valueFunc(key, val)
	if type(val) == 'string' then
		val = val:match('^%s*(.-)%s*$')
		if val == '' then
			return nil
		end
	end
	return val
end

local function getPrettyName(args)
	local argsName = args.name or args.nome
	for k in pairs(namespaceCategories) do
		local argsCategoryK = args[k .. ' category'] or args['categoria ' .. k]
		if argsCategoryK then
			return string.format("'''[[:Categoria:%s|%s]]''': ", argsCategoryK, argsName)
		end
	end
	return string.format("'''%s''': ", argsName)
end

function p.main(frame)
	local args = require('Módulo:Arguments').getArgs(frame, {wrappers = 'Predefinição:Predefinição de redirecionamento', valueFunc = valueFunc})
	local namespace = mw.title.getCurrentTitle().namespace

	--- XXX: this is a HORRIBLE HACK. kill it with fire as soon as https://bugzilla.wikimedia.org/show_bug.cgi?id=12974 is fixed
	local beCompatibleWithBug12974 = args.info and (args.info:find('^[:;#*]', 1) == 1 or args.info:find('{|', 1, true) == 1) and '\n' or ' '
	
	local argsClass = args.class or args.class
	local argsName = args.name or args.nome
	local argsFrom = args.from or args.de
	local argsTo = args.to or args.para
	local content = string.format('\n<div class="rcat %s">\n*%sEste é um redirecionamento%s%s.%s%s</div>',
		argsClass or '',
		argsName and getPrettyName(args) or '',
		argsFrom and (' de ' .. args.from) or '',
		argsTo and (' para ' .. args.to) or '',
		args.info and beCompatibleWithBug12974 or '',
		args.info or ''
	)
	
	for k,v in pairs(namespaceCategories) do
		local argsCategoryK = args[k .. ' category'] or args['categoria ' .. k]
		local argsOtherCategory = args['other category'] or args['outra categoria']
		if argsCategoryK then
			if type(v[1]) == 'function' and v[1](namespace) or v[1] == namespace then
				content = content .. string.format('[[Categoria:%s]]', argsCategoryK)
			elseif argsOtherCategory then
				content = content .. string.format('[[Categoria:%s]]', argsOtherCategory)
			else
				content = content .. frame:expandTemplate{title = 'Predefinição de redirecionamento incorreto', args = {v[2]}}
			end
		end
	end

	if namespace == 0 then
		local argsPrintworthy = args.printworthy or args['impressão'] or args['permitir impressão']
		local yesno = require('Módulo:Yesno')
		if yesno(argsPrintworthy) == true then
			return content .. '[[Categoria:!Redirecionamentos permitidos em impressões]]'
		elseif yesno(argsPrintworthy) == false then
			return content .. '[[Categoria:!Redirecionamentos não permitidos em impressões]]'
		end
	end
	return content
end

return p