Documentation for this module may be created at Module:Sandbox/Wnt/FindAndReplace/doc
---- NOTE: while I haven't looked through quite carefully enough, I think that [[Module:String]]
---- can do everything that this can do. I may put it up for deletion soon.
---- Sorry, this doesn't work for many applications because all templates are destroyed. It does work on plain text though.
---- This module is meant to be processed by putting
---- "{{#invoke:FindAndReplace|main|(string to find)|(string to replace)}}"
---- into an existing page. Add "|pattern=yes" to enjoy all the wild and wooly
---- features of Lua pattern strings. This should deliver back all the text,
---- as modified, in wiki source form, for you to copy and paste back into the
---- page while editing. Caveat: I don't presently know if things like comments
---- will be lost. This is intended as a handy tool for editors who don't have
---- find and replace on their browser and don't want to open another program -
---- or to provide better Lua find and replace where their word processors come
---- up short. NOTE that text MUST be saved to be processed with this unless
---- you use an additional parameter |input=(the text to find and replace from)
local p = {}
function p.main(frame)
local args=frame.args
local parent=frame.getParent(frame)
local pargs={}
if parent then pargs=parent.args end
local find=args.find or pargs.find or args[1] or pargs[1]
if not(find) then return "error you need to say what to find! (first unnamed parameter ''or'' find=(text)" end
local replace=args.replace or pargs.replace or args[2] or pargs[2] or ""
local pattern=args.pattern or pargs.pattern or "no"
if pattern=="no" then pattern = nil end
local input=args.input or pargs.input or args[3] or pargs[3] or ""
if input == nil or input == "" then return "failed to get any text" end
input=mw.ustring.gsub(input,"{{#invoke:FindAndReplace.-}}","") or input --we don't want shrapnel from this usage in the new page.
if not(pattern) then find=string.gsub(find,"[%^%$%(%)%%%.%[%]%*%+%-%?%]]","%%1") end -- exterminate all delimiters
input=mw.ustring.gsub(input,find,replace)
---- I have no idea why {{x gets returned as [[:Template:x but I don't like it.
--input=mw.ustring.gsub(input,[=[%[%[:Template:]=].."(.-)"..[=[%]%]]=],"{{%1}}")
-- no good - it still loses all the parameters out of all the templates!
return frame.preprocess(frame, "<pre><nowiki>" .. input .. [[</nowiki></pre>]])
end
return p
-- I wonder if there's a way to put this directly back into an open edit window...