Jump to content

Module:Parameter names example/sandbox: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
record the original order the args are fetched from the frame object and try using that
see if the original order is preserved if we don't use Module:Arguments
Line 13: Line 13:
end
end


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

-- Separate the args and the options, and record the order in which we
-- Separate the args and the options, and record the order in which we
-- receive the args from the frame object.
-- receive the args from the frame object.
local args, options, order = {}, {}, {}
local args, options, order = {}, {}, {}
for k, v in pairs(origArgs) do
for k, v in pairs(frame:getParent().args) do
v = v:match('^%s*(.-)%s*$')
if type(k) == 'number' or not k:find('^_') then
args[k] = v
if v ~= '' then
if type(k) == 'number' or not k:find('^_') then
order[#order + 1] = k
args[k] = v
else
options[k:gsub('^_', '')] = v
order[#order + 1] = k
else
options[k:gsub('^_', '')] = v
end
end
end
end
end
Line 100: Line 101:


return ret
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
end



Revision as of 14:41, 29 August 2014

-- 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(frame)
	-- Separate the args and the options, and record the order in which we
	-- receive the args from the frame object.
	local args, options, order = {}, {}, {}
	for k, v in pairs(frame:getParent().args) do
		v = v:match('^%s*(.-)%s*$')
		if v ~= '' then
			if type(k) == 'number' or not k:find('^_') then
				args[k] = v
				order[#order + 1] = k
			else
				options[k:gsub('^_', '')] = v
			end
		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

	-- Build the table of template arguments.
	local targs = {}
	for k, v in pairs(args) do
		if type(k) == 'number' then
			targs[v] = formatFunc(v)
		else
			targs[k] = v
		end
	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
		local code = { templateName }
		for _, k in ipairs(order) do
			local displayKey
			if type(k) == 'number' then
				displayKey = args[k]
			else
				displayKey = k
			end
			code[#code + 1] = ' ' .. displayKey .. ' = ' .. targs[displayKey]
		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

return p