Module:MultiReplace: Difference between revisions

Content deleted Content added
No edit summary
Per edit request
 
(12 intermediate revisions by 3 users not shown)
Line 1:
local p = {}
 
local function p.mainMultiReplace(frameargs)
local args = frame:getParent().args
local input = args[1] or "{{{1}}}"
local plain = args.plain == "yes"
Line 11 ⟶ 10:
local change = {pattern = args[i * 2], repl = args[i * 2 + 1]}
if not change.repl then
return framerequire('Module:expandTemplateError').error{
args = { 'MultireplaceMultiReplace: Unpaired argument: <code>' .. (i * 2) .. ' = ' .. mw.text.nowiki(change.pattern) .. '</code>' }
title = 'Error',
args = { 'Multireplace: Unpaired argument: <code>' .. (i * 2) .. ' = ' .. change.pattern .. '</code>' }
}
end
Line 39 ⟶ 37:
if bestChange then
local fragment = mw.ustring.sub(input, bestStart, bestStop)
result = result .. "(" .. bestStart .. "-" ..bestStop .. ":" .. fragment .. ")"
result = result .. (plain and bestChange.repl or
mw.ustring.gsub(fragment, bestChange.pattern, bestChange.repl, 1))
end
pos = bestStop + 1
result = result .. "(pos=" .. pos .. ")"
end
return result .. "x" .. args[10] .. "x"
end
 
function p.main(frame, ...)
local args =
type(frame) ~= 'table' and {frame, ...} or
type(frame.args) ~= 'table' and frame or
frame.args[1] and frame.args or
local args = frame:getParent().args
return MultiReplace(args)
end