Module:Parameter names example: Difference between revisions

From Random Island Wiki
Jump to navigation Jump to search
>Mr. Stradivarius
(use the current page instead of the base page if we are on a /sandbox subpage, and use pcall to expand the template in case there are any errors)
>Andrybak
(add popular variants of demonstration purposes parameters: nocat=yes, categories=no, and demo=yes)
 
(4 intermediate revisions by 3 users not shown)
Line 11: Line 11:
local function italicize(s)
local function italicize(s)
return "''" .. s .. "''"
return "''" .. s .. "''"
end
local function plain(s)
return s
end
end


Line 18: Line 22:
if args._display == 'italics' or args._display == 'italic' then
if args._display == 'italics' or args._display == 'italic' then
formatFunc = italicize
formatFunc = italicize
elseif args._display == 'plain' then
formatFunc = plain
else
else
formatFunc = makeParam
formatFunc = makeParam
Line 31: Line 37:
end
end
end
end
targs['nocat'] = 'yes';
targs['categories'] = 'no';
targs['demo'] = 'yes';


-- Find the template name.
-- Find the template name.
Line 48: Line 57:
frame = frame or mw.getCurrentFrame()
frame = frame or mw.getCurrentFrame()
local success, result = pcall(
local success, result = pcall(
frame.expantTemplate,
frame.expandTemplate,
frame,
frame,
{title = template, args = targs}
{title = template, args = targs}

Latest revision as of 23:07, 25 March 2020

Documentation for this module may be created at Module:Parameter names example/doc

-- 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

local function plain(s)
	return s
end

function p._main(args, frame)
	-- Find how we want to format the arguments to the template.
	local formatFunc
	if args._display == 'italics' or args._display == 'italic' then
		formatFunc = italicize
	elseif args._display == 'plain' then
		formatFunc = plain
	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)
		elseif not k:find('^_') then
			targs[k] = v
		end
	end
	targs['nocat'] = 'yes';
	targs['categories'] = 'no';
	targs['demo'] = 'yes';

	-- Find the template name.
	local template
	if args._template then
		template = args._template
	else
		local currentTitle = mw.title.getCurrentTitle()
		if currentTitle.prefixedText:find('/sandbox$') then
			template = currentTitle.prefixedText
		else
			template = currentTitle.basePageTitle.prefixedText
		end
	end

	-- Call the template with the arguments.
	frame = frame or mw.getCurrentFrame()
	local success, result = pcall(
		frame.expandTemplate,
		frame,
		{title = template, args = targs}
	)
	if success then
		return result
	else
		return ''
	end
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