Module:Replace

From Coral Island Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:Replace/doc

--- Module copied from Genshin Impact wiki.

local p = {}
local ie = require('Module:Feature').isEmpty

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		parentFirst = true,
		wrapper = { 'Template:Replace' }
	})
	return p._main(args)
end

function p._main(args)
	local inputString = args[1] or nil
	local search = args[2] or args.s1 or nil
	local replace = args[3] or args.r1 or ''

	-- argument validation
	if ie(inputString) then
		return ''
	end
	if ie(search) then
		return inputstring
	end

	if args[4] or args.s2 then
		local y = 2
		while args[y] or args['s' .. y/2] do
			inputString = p.Replace(inputString, (args[y] or args['s'..y/2]), (args[y+1] or args['s'..(y+1)/2] or ''))
			y = y + 2
		end
	else
		inputString = p.Replace(inputString, search, replace)
	end
	return inputString
end

function p.Replace(str, search, replace)
	-- replace key terms
	search = search:gsub('{space}', ' ')
	replace = replace:gsub('{space}', ' '):gsub('{newline}', '<br />')

	-- result
	return str:gsub(search, replace)
end

return p