[luatex] Passing verbatim text to a function

Sietse Brouwer sbbrouwer at gmail.com
Mon Sep 27 00:16:02 CEST 2010

Dear list,

I'm using ConTeXt Mark IV, and trying to write a function that uses
Lua to dedent a block of verbatim text before handing it off to
\starttyping. My current problem: I have not yet succeeded in passing
the block of text to Lua with newlines and spaces preserved.

I started out with the usual pattern for defining a macro that calls Lua:

%%%% ---- begin usual pattern ---- %%%%
    dedent = function(code)
        -- Create a logfile.
        logfile = io.open("debug.txt", "w")
        -- Prove that strings with newlines get written correctly
        logfile:write("--\n" .. "    test\n" .. "--\n")
        -- Now write to the logfile that what ConTeXt gave us.
        -- Actual dedenting code. Not relevant yet.
        lead = string.match(code, '^ +')
        code = string.gsub(code, '^' .. lead, '')
        code = string.gsub(code, '\n' .. lead, '\n')
        -- Placeholder. Messing with context.type()
        -- or context.starttyping() comes later.

\def\Dedent#1{ \directlua{ dedent( [[#1]] ) } }

% Try it out. If all goes well, logfile.txt should contain
%  the below with linebreaks and spaces preserved.
\Dedent{    andra

%%%% ---- end usual pattern ---- %%%%

That pattern, however, got me my spaces and newlines discarded. I next
tried a two-macro structure, as described in
http://tinyurl.com/mbp2ub (which leads to
but I'm not convinced that URL won't get broken).

%%%% ---- start two-macro pattern ---- %%%%
% Lua code remains the same

% new two-macro structure

% test remains the same
%%%% ---- start two-macro pattern ---- %%%%

Alas! This preserves spaces, but not lines. I am now at the end of my
wits. Does anyone on this list know a solution?

Kind regards,

Sietse Brouwer

More information about the luatex mailing list