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 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, transform) |
− | local | + | local removeDuplicates = require('Module:TableTools').removeDuplicates |
+ | transform = transform or function(s) return s end | ||
local parameters = {} | local parameters = {} | ||
for parameter in string.gmatch(source, '{{{(.-)%f[}|<>]') do | for parameter in string.gmatch(source, '{{{(.-)%f[}|<>]') do | ||
− | table.insert(parameters, parameter) | + | table.insert(parameters, transform(parameter)) |
end | end | ||
return removeDuplicates(parameters) | return removeDuplicates(parameters) | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
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, | + | %s}}]], title, table.concat(extractParams(args.base, |
− | function(s) | + | function(s) return string.format('| %s = \n', s) end))) |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
Line 109: | Line 34: | ||
local function normaliseParams(parameters) | local function normaliseParams(parameters) | ||
local paramsNorm = {} | local paramsNorm = {} | ||
− | + | local paramIndex = {} | |
− | |||
− | |||
− | |||
− | local paramIndex = | ||
− | |||
− | |||
− | |||
− | |||
− | |||
for _, parameter in pairs(parameters) do | for _, parameter in pairs(parameters) do | ||
table.insert(paramsNorm, | table.insert(paramsNorm, | ||
string.lower(string.gsub(parameter, '%A', ''))) | string.lower(string.gsub(parameter, '%A', ''))) | ||
− | + | paramIndex[ | |
− | string.lower(string.gsub(parameter, '%A', ''))] | + | string.lower(string.gsub(parameter, '%A', ''))] = parameter |
end | end | ||
− | |||
− | |||
− | |||
− | |||
setmetatable(paramsNorm, {__index = paramIndex}) | setmetatable(paramsNorm, {__index = paramIndex}) | ||
return paramsNorm | return paramsNorm | ||
Line 140: | Line 52: | ||
local otherNormParams = normaliseParams(otherParams) | local otherNormParams = normaliseParams(otherParams) | ||
+ | local similar = {} | ||
+ | for _, v in pairs(Set.valueIntersection(baseNormParams, otherNormParams)) do | ||
+ | table.insert(similar, string.format('%s (%s; %s)', | ||
+ | v, baseNormParams[v], otherNormParams[v])) | ||
+ | end | ||
return string.format([[Identical: | return string.format([[Identical: | ||
− | %s | + | * %s |
+ | |||
Similar: | Similar: | ||
− | %s | + | * %s |
− | + | ||
− | %s]], | + | Not matched: |
− | + | * %s]], | |
− | + | table.concat(Set.valueIntersection(baseParams, otherParams), '\n* '), | |
− | + | table.concat(similar, '\n* '), | |
− | + | table.concat(Set.valueComplement(otherNormParams, | |
− | + | baseNormParams), '\n* ')) | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
Line 164: | Line 75: | ||
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 | ||
function p._list(args) | function p._list(args) | ||
− | return | + | return table.concat(extractParams(args.base, |
− | function(s) return string.format('* %s\n', s) end) | + | function(s) return string.format('* %s\n', s) end)) |
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 |