Editing Parameters
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.
The edit can be undone.
Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 1: | Line 1: | ||
-- This module implements [[Template:Parameters]]. | -- This module implements [[Template:Parameters]]. | ||
-- [SublimeLinter luacheck-globals:mw] | -- [SublimeLinter luacheck-globals:mw] | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
local p = {} | local p = {} | ||
local removeDuplicates = require('Module:TableTools').removeDuplicates | local removeDuplicates = require('Module:TableTools').removeDuplicates | ||
− | |||
local function makeInvokeFunction(funcName) | local function makeInvokeFunction(funcName) | ||
return function(frame) | return function(frame) | ||
local getArgs = require('Module:Arguments').getArgs | local getArgs = require('Module:Arguments').getArgs | ||
− | return p[funcName](getArgs(frame | + | return p[funcName](getArgs(frame)) |
end | end | ||
end | end | ||
− | local function extractParams( | + | local function extractParams(source) |
− | |||
− | |||
local parameters = {} | local parameters = {} | ||
for parameter in string.gmatch(source, '{{{(.-)%f[}|<>]') do | for parameter in string.gmatch(source, '{{{(.-)%f[}|<>]') do | ||
Line 54: | Line 20: | ||
end | end | ||
− | local function | + | local function strMap(iter, transform) |
local returnTable = {} | local returnTable = {} | ||
− | for | + | for _, v in pairs(iter) do |
− | returnTable | + | table.insert(returnTable, transform(v)) |
end | end | ||
− | |||
− | |||
− | |||
− | |||
− | |||
return table.concat(returnTable) | return table.concat(returnTable) | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
function p._code(args) | function p._code(args) | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
local title = args._base or mw.title.getCurrentTitle().baseText | local title = args._base or mw.title.getCurrentTitle().baseText | ||
return string.format([[{{%s | return string.format([[{{%s | ||
− | %s}}]], title, strMap( | + | %s}}]], title, strMap(extractParams(args.base), |
− | function(s) | + | function(s) return string.format('| %s = \n', s) end)) |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
Line 109: | Line 40: | ||
local function normaliseParams(parameters) | local function normaliseParams(parameters) | ||
local paramsNorm = {} | local paramsNorm = {} | ||
− | |||
− | |||
− | |||
− | |||
local paramIndex = setmetatable({}, {__index = function(t, k) | local paramIndex = setmetatable({}, {__index = function(t, k) | ||
if not rawget(t, k) then | if not rawget(t, k) then | ||
Line 127: | Line 54: | ||
paramsNorm = removeDuplicates(paramsNorm) | paramsNorm = removeDuplicates(paramsNorm) | ||
− | |||
− | |||
− | |||
setmetatable(paramsNorm, {__index = paramIndex}) | setmetatable(paramsNorm, {__index = paramIndex}) | ||
return paramsNorm | return paramsNorm | ||
Line 164: | Line 88: | ||
return string.format('{{Parameter names example%s|%s}}', title, | return string.format('{{Parameter names example%s|%s}}', title, | ||
table.concat(extractParams(args.base), '|')) | table.concat(extractParams(args.base), '|')) | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
Line 184: | Line 95: | ||
end | end | ||
− | |||
p.code = makeInvokeFunction('_code') | p.code = makeInvokeFunction('_code') | ||
p.compare = makeInvokeFunction('_compare') | p.compare = makeInvokeFunction('_compare') | ||
p.demo = makeInvokeFunction('_demo') | p.demo = makeInvokeFunction('_demo') | ||
− | |||
p.list = makeInvokeFunction('_list') | p.list = makeInvokeFunction('_list') | ||
return p | return p |