Module:ParameterCount and Module:ParameterCount/sandbox: Difference between pages
(Difference between pages)
imported>Mr. Stradivarius m Protected "Module:ParameterCount": High-risk Lua module: 20,000+ transclusions ([Edit=Require template editor access] (indefinite) [Move=Require template editor access] (indefinite)) |
imported>Gonnym No edit summary |
||
| Line 10: | Line 10: | ||
-- Test whether a string is blank | -- Test whether a string is blank | ||
local function isBlank(s) | local function isBlank(s) | ||
--mw.log("here?") | |||
--mw.log(s) | |||
--mw.log(s:find('%S')) | |||
--mw.log(not s:find('%S')) | |||
return not s:find('%S') | return not s:find('%S') | ||
end | end | ||
| Line 27: | Line 31: | ||
for key, val in pairs(args) do | for key, val in pairs(args) do | ||
if testFunc(key, val) then | if testFunc(key, val) then | ||
--mw.log(key) | |||
--mw.log(val) | |||
ret = ret + 1 | ret = ret + 1 | ||
end | end | ||
| Line 36: | Line 42: | ||
local function main(frame, testFunc) | local function main(frame, testFunc) | ||
local blankifiedTestFunc | local blankifiedTestFunc | ||
if yesno(frame.args.checkblanks) | local modified_count = 0 | ||
if yesno(frame.args.checkblanks) then | |||
-- Extend the test function to check for blanks as well. | -- Extend the test function to check for blanks as well. | ||
blankifiedTestFunc = function (key, val) | blankifiedTestFunc = function(key, val) | ||
if not isBlank(val) then | if not isBlank(val) then | ||
return testFunc(key, val) | return testFunc(key, val) | ||
end | end | ||
end | end | ||
modified_count = -1 | |||
else | else | ||
blankifiedTestFunc = testFunc | blankifiedTestFunc = testFunc | ||
end | end | ||
if frame.args.checkblanks then | |||
mw.log("here?") | |||
modified_count = modified_count - 1 | |||
end | |||
local module_count = count(frame.args, blankifiedTestFunc) | |||
local parent_count = count(frame:getParent().args, blankifiedTestFunc) | |||
return module_count + parent_count + modified_count | |||
end | end | ||
| Line 53: | Line 70: | ||
-- All specified parameters are counted, even those not supported by the | -- All specified parameters are counted, even those not supported by the | ||
-- template. | -- template. | ||
all = function (frame) | all = function(frame) | ||
return main(frame, function () return true end) | return main(frame, function() return true end) | ||
end, | end, | ||
| Line 60: | Line 77: | ||
-- Users can specify a list of parameters to check, and a list of Lua | -- Users can specify a list of parameters to check, and a list of Lua | ||
-- Ustring patterns to check each parameter against. | -- Ustring patterns to check each parameter against. | ||
main = function (frame) | main = function(frame) | ||
local args = frame.args | local args = frame.args | ||
local keys, patterns = {}, {} | local keys, patterns = {}, {} | ||
| Line 92: | Line 109: | ||
-- Construct the test function | -- Construct the test function | ||
local testFunc = function (key, val) | local testFunc = function(key, val) | ||
if keys[key] then | if keys[key] then | ||
return true | return true | ||