Actions

Module

Difference between revisions of "Parameters"

From Unofficial Stationeers Wiki

(Wikilist)
(Code output)
Line 11: Line 11:
 
end
 
end
  
local function extractParams(args)
+
local function extractParams(args, transform)
 
local removeDuplicates = require('Module:TableTools').removeDuplicates
 
local removeDuplicates = require('Module:TableTools').removeDuplicates
 +
transform = transform or '%s'
  
 
local parameters = {}
 
local parameters = {}
 
for parameter in string.gmatch(args.base, '{{{(.-)%f[}|<>]') do
 
for parameter in string.gmatch(args.base, '{{{(.-)%f[}|<>]') do
table.insert(parameters, parameter)
+
table.insert(parameters, string.format(transform, parameter))
 
end
 
end
 
return removeDuplicates(parameters)
 
return removeDuplicates(parameters)
 +
end
 +
 +
function p._code(args)
 +
return string.format([[{{%s
 +
%s}}]],
 +
(args._base or mw.title.getCurrentTitle().baseText),
 +
table.concat(extractParams(args, '| %s = \n')))
 
end
 
end
  
Line 27: Line 35:
  
 
function p._list(args)
 
function p._list(args)
local parameters = extractParams(args)
+
return table.concat(extractParams(args, '* %s\n'))
for k, v in pairs(parameters) do
 
parameters[k] = string.format('* %s\n', v)
 
end
 
return table.concat(parameters)
 
 
end
 
end
  
 +
p.code = makeInvokeFunction('_code')
 
p.demo = makeInvokeFunction('_demo')
 
p.demo = makeInvokeFunction('_demo')
 
p.list = makeInvokeFunction('_list')
 
p.list = makeInvokeFunction('_list')
  
 
return p
 
return p

Revision as of 12:48, 5 June 2015


-- This module implements [[Template:Parameters]].
-- [SublimeLinter luacheck-globals:mw]

local p = {}

local function makeInvokeFunction(funcName)
	return function(frame)
		local getArgs = require('Module:Arguments').getArgs
		return p[funcName](getArgs(frame))
	end
end

local function extractParams(args, transform)
	local removeDuplicates = require('Module:TableTools').removeDuplicates
	transform = transform or '%s'

	local parameters = {}
	for parameter in string.gmatch(args.base, '{{{(.-)%f[}|<>]') do
		table.insert(parameters, string.format(transform, parameter))
	end
	return removeDuplicates(parameters)
end

function p._code(args)
	return string.format([[{{%s
%s}}]],
		(args._base or mw.title.getCurrentTitle().baseText),
		table.concat(extractParams(args, '| %s = \n')))
end

function p._demo(args)
	return string.format('{{Parameter names example|_template=%s|%s}}',
		args._base or '', table.concat(extractParams(args), '|'))
end

function p._list(args)
	return table.concat(extractParams(args, '* %s\n'))
end

p.code = makeInvokeFunction('_code')
p.demo = makeInvokeFunction('_demo')
p.list = makeInvokeFunction('_list')

return p