Changes

Jump to navigation Jump to search
231 bytes removed ,  11:38, 4 December 2010
m
<texcode>
\startluacode
-- Initialise Initialize a userdata namespace name space to keep our own functions in.
-- That way, we won't interfere with anything ConTeXt keeps in
-- the global namespacename space.
userdata = userdata or {}
function userdata.dedentedtyping(content)
local newline = lpeg.P("\n\r") + lpeg.P("\r\n") + lpeg.P("\n") + lpeg.P("\r") local splitter = lpeg.Ct(lpeg.splitat(newline)) local lines = lpegstring.matchsplitlines(splitter, content) 
local indent = string.match(lines[1], '^ +') or ''
indent = lpeg.P(indent) local any pattern = lpeg'^' ..Cs(1) local parser = indent * lpeg.C(any^0) 
for i=1,#lines do
lines[i] = lpegstring.matchgsub(parser, lines[i],pattern,"")
end
-- does.
end
\stopluacode</texcode>
The only hard part is capturing the content of the environment and passing it to Lua. As explained in [[Inside_ConTeXt#Passing_verbatim_text_as_macro_parameter|Inside ConText]], the trick to capturing the content of an environment verbatim is to ensure that spaces and newlines have a catcode that makes them significant. This is done using <code>\obeyspaces</code> and <code>\obeylines</code>. Using that trick, we can write this macro as
\def\stopdedentedtyping
{\ctxlua
{userdata.dedentedtyping(buffers.getcontent('dedentedtyping'))}}
</texcode>
Unlike MkII, where the contents of a buffer were written to an external file, in MkIV buffers are stored in memory. Thus, with LuaTeX is really simple to manipulate verbatim text: pass the contents of the environment to Lua; use Lua functions to do the text-manipulation; and in Lua call [[cld|<code>context.something()</code>]] functions to produce the ConTeXt code you want.

Navigation menu