Jump to content

Module:Check for unknown parameters and Module:Check for unknown parameters/sandbox: Difference between pages

(Difference between pages)
Page 1
Page 2
Content deleted Content added
per edit request (permalink); + require ('strict');
 
No edit summary
 
Line 2: Line 2:
-- with a list of arguments, returning a specified result if an argument is
-- with a list of arguments, returning a specified result if an argument is
-- not on the list
-- not on the list

require ('strict');

local p = {}
local p = {}


Line 18: Line 15:
-- Return text cleaned for display and truncated if too long.
-- Return text cleaned for display and truncated if too long.
-- Strip markers are replaced with dummy text representing the original wikitext.
-- Strip markers are replaced with dummy text representing the original wikitext.
local pos, truncated
local truncated
local function truncate(text)
local function truncate(text)
if truncated then
if truncated then
Line 30: Line 27:
end
end
local parts = {}
local parts = {}
local pos
for before, tag, remainder in text:gmatch('([^\127]*)\127[^\127]*%-(%l+)%-[^\127]*\127()') do
for before, tag, remainder in text:gmatch('([^\127]*)\127[^\127]*%-(%l+)%-[^\127]*\127()') do
pos = remainder
pos = remainder
Line 38: Line 36:
end
end


function p._check(args, pargs)
local function get_values(args, pargs)
if type(args) ~= "table" or type(pargs) ~= "table" then
-- TODO: error handling
return
end

-- create the list of known args, regular expressions, and the return string
-- create the list of known args, regular expressions, and the return string
local knownargs = {}
local knownargs = {}
Line 55: Line 48:
end
end
end
end

-- loop over the parent args, and make sure they are on the list
-- loop over the parent args, and make sure they are on the list
local ignoreblank = isnotempty(args['ignoreblank'])
local ignoreblank = isnotempty(args['ignoreblank'])
Line 85: Line 77:
end
end
end
end
return values
end

function p._check(args, pargs)
if type(args) ~= "table" or type(pargs) ~= "table" then
-- TODO: error handling
return
end

local values = get_values(args, pargs)


-- add results to the output tables
-- add results to the output tables
local res = {}
local previews = {}
local categories = {}
local unknown_category = args['unknown']
local preview_text = args['preview'] or 'Found _VALUE_, '
if #values > 0 then
if #values > 0 then
preview_text = require('Module:If preview')._warning({preview_text})
local unknown_text = args['unknown'] or 'Found _VALUE_, '


if mw.getCurrentFrame():preprocess( "{{REVISIONID}}" ) == "" then
local preview_text = args['preview']
if isnotempty(preview_text) then
preview_text = require('Module:If preview')._warning({preview_text})
elseif preview_text == nil then
preview_text = unknown_text
end
unknown_text = preview_text
end
for _, v in pairs(values) do
for _, v in pairs(values) do
-- Fix odd bug for | = which gets stripped to the empty string and
-- Fix odd bug for | = which gets stripped to the empty string and
Line 106: Line 102:


-- avoid error with v = 'example%2' ("invalid capture index")
-- avoid error with v = 'example%2' ("invalid capture index")
local r = unknown_text:gsub('_VALUE_', {_VALUE_ = v})
local category = unknown_category:gsub('_VALUE_', {_VALUE_ = v})
local preview = preview_text:gsub('_VALUE_', {_VALUE_ = v})
table.insert(res, r)
table.insert(categories, category)
table.insert(previews, preview)
end
end
end
end


return table.concat(res)
return table.concat(previews) .. "\n" .. table.concat(categories)
end
end