A bit late to the party, but this fix to https://tex.stackexchange.com/users/243/topskip's answer does the job:
in response to question in comments:
In topskip's answer Hello World is still written to the pdf file, which is not typically what one would want. So I've taken his answer and made the change that if the line read does not contain "\end{foobar}" then nothing is returned to the tex interpreter, but if it does contain "\end{foobar}" it gets spit back (via the nil return value) to the tex interpreter which then ends the foobar environment. If the "\end{foobar}" line would not get returned then tex would never end the environment, which is clearly not what you want.
The example is still imperfect in that it doesn't give the desired result if there is text before "\end{foobar}" on the same line.
\documentclass{article}
\usepackage{luacode}
\begin{luacode*}
do
local mybuf = ""
function readbuf( buf )
print("buf=" .. buf)
i,j = string.find(buf,"\\end{foobar}")
print(i,j)
if i==nil then
mybuf = mybuf .. buf .. "\n"
print("[" .. mybuf .. "]")
return ""
else
return nil
end
end
function startrecording()
luatexbase.add_to_callback('process_input_buffer', readbuf, 'readbuf')
end
function stoprecording()
luatexbase.remove_from_callback('process_input_buffer', 'readbuf')
local buf_without_end = mybuf:gsub("\\end{foobar}\n","")
print(string.format("Lua: %s", buf_without_end))
end
end
\end{luacode*}
\begin{document}
\newenvironment{foobar}{\directlua{startrecording()}}{\directlua{stoprecording()}}
\begin{foobar}
Hello {World}
\end{foobar}
\end{document}