function Util.RunEmbeddedCommands(P,O)O=encodeCommandEscape(O)local Q={}for O in O:gmatch("\036\040\037\098\123\125\041")do local G=true local v=O:sub(2,#O-1)if v:match("\094\123\046\043\125\036")then G=false v=v:sub(2,#v-1)end Q[O]=P:EvaluateAndRun(v)if G and Q[O]:find("\037\115")then Q[O]=string.format("\037\113",Q[O])end end return decodeCommandEscape(O:gsub("\036\040\037\098\123\125\041",Q))end
--- Runs embedded commands and replaces them
function Util.RunEmbeddedCommands(dispatcher, str)
	str = encodeCommandEscape(str)

	local results = {}
	-- We need to do this because you can't yield in the gsub function
	for text in str:gmatch("$(%b{})") do
		local doQuotes = true
		local commandString = text:sub(2, #text - 1)

		if commandString:match("^{.+}$") then -- Allow double curly for literal replacement
			doQuotes = false
			commandString = commandString:sub(2, #commandString - 1)
		end

		results[text] = dispatcher:EvaluateAndRun(commandString)

		if doQuotes and results[text]:find("%s") then
			results[text] = string.format("%q", results[text])
		end
	end

	return decodeCommandEscape(str:gsub("$(%b{})", results))
end
