Jump to content

Module:Parameter names example/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 11:35, 29 August 2014 (separate the options and the args early on, so that we can assume that all targs keys correlate with args values if named arguments are not used). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
-- This module implements {{parameter names example}}.

local p = {}

local function makeParam(s)
	local lb = '{'
	local rb = '}'
	return lb:rep(3) .. s .. rb:rep(3)
end

local function italicize(s)
	return "''" .. s .. "''"
end

function p._main(origArgs, frame)
	frame = frame or mw.getCurrentFrame()

	-- Build the table of template arguments, record whether any named
	-- arguments are being used, and separate the options from the rest of the
	-- arguments.
	local hasNamedArgs = false
	local args, targs, options = {}, {}, {}
	for k, v in pairs(origArgs) do
		if type(k) == 'number' then
			args[k] = v
			targs[v] = formatFunc(v)
		elseif k:find('^_') then
			options[k:gsub('^_', '')] = v
		else
			hasNamedArgs = true
			args[k] = v
			targs[k] = v
		end
	end

	-- Find how we want to format the arguments to the template.
	local formatFunc
	if options.display == 'italics' or options.display == 'italic' then
		formatFunc = italicize
	else
		formatFunc = makeParam
	end

	-- Find the template name.
	local templateName
	if options.template then
		templateName = options.template
	else
		-- Find the title object for the template.
		local templateTitle
		local currentTitle = mw.title.getCurrentTitle()
		if currentTitle.prefixedText:find('/sandbox$') then
			templateTitle = currentTitle
		else
			templateTitle = currentTitle.basePageTitle
		end

		-- Format the template name according to the namespace we are in.
		if templateTitle.namespace == 10 then -- NS_TEMPLATE
			templateName = templateTitle.text
		elseif templateTitle.namespace == 0 then -- NS_MAIN
			templateName = ':' .. templateTitle.text
		else
			templateName = templateTitle.prefixedText
		end
	end

	local ret = ''

	-- Build the template invocation.
	if options.code then
		-- Order the arguments. If we have named arguments this is alphabetical
		-- order, otherwise it's the order that they were passed into the
		-- template.
		local order = {}
		for k in pairs(hasNamedArgs and targs or args) do
			order[#order + 1] = k
		end
		table.sort(order, function (a, b)
			local typeA = type(a)
			local typeB = type(b)
			if typeA ~= typeB then
				return typeA == 'number' -- numbers go first
			else
				return a < b
			end
		end)

		-- Assemble the invocation.
		local code = { templateName }
		for _, k in ipairs(order) do
			code[#code + 1] = ' ' .. tostring(k) .. ' = ' .. targs[k]
		end
		code = '{{' .. table.concat(code, '\n|') .. '\n}}'
		code = '<pre style="white-space: pre-wrap;">\n' .. code .. '\n</pre>\n\n'
		ret = ret .. frame:preprocess(code)
	end

	-- Call the template with the arguments.
	local success, result = pcall(
		frame.expandTemplate,
		frame,
		{title = templateName, args = targs}
	)
	if success then
		ret = ret .. result
	end

	return ret
end

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

return p